xref: /wasmtime-44.0.1/examples/interrupt.c (revision 3f694ae3)
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   wasm_store_t *store = wasm_store_new(engine);
65   assert(store != NULL);
66 
67   // Create our interrupt handle we'll use later
68   wasmtime_interrupt_handle_t *handle = wasmtime_interrupt_handle_new(store);
69   assert(handle != NULL);
70 
71   // Read our input file, which in this case is a wasm text file.
72   FILE* file = fopen("examples/interrupt.wat", "r");
73   assert(file != NULL);
74   fseek(file, 0L, SEEK_END);
75   size_t file_size = ftell(file);
76   fseek(file, 0L, SEEK_SET);
77   wasm_byte_vec_t wat;
78   wasm_byte_vec_new_uninitialized(&wat, file_size);
79   assert(fread(wat.data, file_size, 1, file) == 1);
80   fclose(file);
81 
82   // Parse the wat into the binary wasm format
83   wasm_byte_vec_t wasm;
84   wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
85   if (error != NULL)
86     exit_with_error("failed to parse wat", error, NULL);
87   wasm_byte_vec_delete(&wat);
88 
89   // Now that we've got our binary webassembly we can compile our module.
90   wasm_module_t *module = NULL;
91   wasm_trap_t *trap = NULL;
92   wasm_instance_t *instance = NULL;
93   wasm_extern_vec_t imports = WASM_EMPTY_VEC;
94   error = wasmtime_module_new(engine, &wasm, &module);
95   wasm_byte_vec_delete(&wasm);
96   if (error != NULL)
97     exit_with_error("failed to compile module", error, NULL);
98   error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
99   if (instance == NULL)
100     exit_with_error("failed to instantiate", error, trap);
101 
102   // Lookup our `run` export function
103   wasm_extern_vec_t externs;
104   wasm_instance_exports(instance, &externs);
105   assert(externs.size == 1);
106   wasm_func_t *run = wasm_extern_as_func(externs.data[0]);
107   assert(run != NULL);
108 
109   // Spawn a thread to send us an interrupt after a period of time.
110   spawn_interrupt(handle);
111 
112   // And call it!
113   printf("Entering infinite loop...\n");
114   wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
115   wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
116   error = wasmtime_func_call(run, &args_vec, &results_vec, &trap);
117   assert(error == NULL);
118   assert(trap != NULL);
119   printf("Got a trap!...\n");
120 
121   // `trap` can be inspected here to see the trap message has an interrupt in it
122 
123   wasm_trap_delete(trap);
124   wasm_extern_vec_delete(&externs);
125   wasm_instance_delete(instance);
126   wasm_module_delete(module);
127   wasm_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