xref: /wasmtime-44.0.1/examples/interrupt.c (revision f3b80ece)
1 /*
2 Example of instantiating of the WebAssembly module and invoking its exported
3 function.
4 
5 You can compile and run this example on Linux with:
6 
7    cargo build --release -p wasmtime-c-api
8    cc examples/interrupt.c \
9        -I crates/c-api/include \
10        -I crates/c-api/wasm-c-api/include \
11        target/release/libwasmtime.a \
12        -lpthread -ldl -lm \
13        -o interrupt
14    ./interrupt
15 
16 Note that on Windows and macOS the command will be similar, but you'll need
17 to tweak the `-lpthread` and such annotations as well as the name of the
18 `libwasmtime.a` file on Windows.
19 */
20 
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <wasm.h>
25 #include <wasmtime.h>
26 
27 #ifdef _WIN32
28 static void spawn_interrupt(wasmtime_interrupt_handle_t *handle) {
29   wasmtime_interrupt_handle_interrupt(handle);
30   wasmtime_interrupt_handle_delete(handle);
31 }
32 #else
33 #include <pthread.h>
34 #include <time.h>
35 
36 static void* helper(void *_handle) {
37   wasmtime_interrupt_handle_t *handle = _handle;
38   struct timespec sleep_dur;
39   sleep_dur.tv_sec = 1;
40   sleep_dur.tv_nsec = 0;
41   nanosleep(&sleep_dur, NULL);
42   printf("Sending an interrupt\n");
43   wasmtime_interrupt_handle_interrupt(handle);
44   wasmtime_interrupt_handle_delete(handle);
45   return 0;
46 }
47 
48 static void spawn_interrupt(wasmtime_interrupt_handle_t *handle) {
49   pthread_t child;
50   int rc = pthread_create(&child, NULL, helper, handle);
51   assert(rc == 0);
52 }
53 #endif
54 
55 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
56 
57 int main() {
58   // Create a `wasm_store_t` with interrupts enabled
59   wasm_config_t *config = wasm_config_new();
60   assert(config != NULL);
61   wasmtime_config_interruptable_set(config, true);
62   wasm_engine_t *engine = wasm_engine_new_with_config(config);
63   assert(engine != NULL);
64   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
65   assert(store != NULL);
66   wasmtime_context_t *context = wasmtime_store_context(store);
67 
68   // Create our interrupt handle we'll use later
69   wasmtime_interrupt_handle_t *handle = wasmtime_interrupt_handle_new(context);
70   assert(handle != NULL);
71 
72   // Read our input file, which in this case is a wasm text file.
73   FILE* file = fopen("examples/interrupt.wat", "r");
74   assert(file != NULL);
75   fseek(file, 0L, SEEK_END);
76   size_t file_size = ftell(file);
77   fseek(file, 0L, SEEK_SET);
78   wasm_byte_vec_t wat;
79   wasm_byte_vec_new_uninitialized(&wat, file_size);
80   assert(fread(wat.data, file_size, 1, file) == 1);
81   fclose(file);
82 
83   // Parse the wat into the binary wasm format
84   wasm_byte_vec_t wasm;
85   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
86   if (error != NULL)
87     exit_with_error("failed to parse wat", error, NULL);
88   wasm_byte_vec_delete(&wat);
89 
90   // Now that we've got our binary webassembly we can compile our module.
91   wasmtime_module_t *module = NULL;
92   error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
93   wasm_byte_vec_delete(&wasm);
94   if (error != NULL)
95     exit_with_error("failed to compile module", error, NULL);
96 
97   wasm_trap_t *trap = NULL;
98   wasmtime_instance_t instance;
99   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
100   if (error != NULL || trap != NULL)
101     exit_with_error("failed to instantiate", error, trap);
102   wasmtime_module_delete(module);
103 
104   // Lookup our `run` export function
105   wasmtime_extern_t run;
106   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
107   assert(ok);
108   assert(run.kind == WASMTIME_EXTERN_FUNC);
109 
110   // Spawn a thread to send us an interrupt after a period of time.
111   spawn_interrupt(handle);
112 
113   // And call it!
114   printf("Entering infinite loop...\n");
115   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
116   assert(error == NULL);
117   assert(trap != NULL);
118   printf("Got a trap!...\n");
119 
120   // `trap` can be inspected here to see the trap message has an interrupt in it
121   wasmtime_trap_code_t code;
122   ok = wasmtime_trap_code(trap, &code);
123   assert(ok);
124   assert(code == WASMTIME_TRAP_CODE_INTERRUPT);
125   wasm_trap_delete(trap);
126 
127   wasmtime_store_delete(store);
128   wasm_engine_delete(engine);
129   return 0;
130 }
131 
132 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
133   fprintf(stderr, "error: %s\n", message);
134   wasm_byte_vec_t error_message;
135   if (error != NULL) {
136     wasmtime_error_message(error, &error_message);
137     wasmtime_error_delete(error);
138   } else {
139     wasm_trap_message(trap, &error_message);
140     wasm_trap_delete(trap);
141   }
142   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
143   wasm_byte_vec_delete(&error_message);
144   exit(1);
145 }
146