xref: /wasmtime-44.0.1/examples/interrupt.c (revision eb650f6f)
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 }
46 
47 static void spawn_interrupt(wasmtime_interrupt_handle_t *handle) {
48   pthread_t child;
49   int rc = pthread_create(&child, NULL, helper, handle);
50   assert(rc == 0);
51 }
52 #endif
53 
54 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
55 
56 int main() {
57   // Create a `wasm_store_t` with interrupts enabled
58   wasm_config_t *config = wasm_config_new();
59   assert(config != NULL);
60   wasmtime_config_interruptable_set(config, true);
61   wasm_engine_t *engine = wasm_engine_new_with_config(config);
62   assert(engine != NULL);
63   wasm_store_t *store = wasm_store_new(engine);
64   assert(store != NULL);
65 
66   // Create our interrupt handle we'll use later
67   wasmtime_interrupt_handle_t *handle = wasmtime_interrupt_handle_new(store);
68   assert(handle != NULL);
69 
70   // Read our input file, which in this case is a wasm text file.
71   FILE* file = fopen("examples/interrupt.wat", "r");
72   assert(file != NULL);
73   fseek(file, 0L, SEEK_END);
74   size_t file_size = ftell(file);
75   fseek(file, 0L, SEEK_SET);
76   wasm_byte_vec_t wat;
77   wasm_byte_vec_new_uninitialized(&wat, file_size);
78   assert(fread(wat.data, file_size, 1, file) == 1);
79   fclose(file);
80 
81   // Parse the wat into the binary wasm format
82   wasm_byte_vec_t wasm;
83   wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
84   if (error != NULL)
85     exit_with_error("failed to parse wat", error, NULL);
86   wasm_byte_vec_delete(&wat);
87 
88   // Now that we've got our binary webassembly we can compile our module.
89   wasm_module_t *module = NULL;
90   wasm_trap_t *trap = NULL;
91   wasm_instance_t *instance = NULL;
92   error = wasmtime_module_new(engine, &wasm, &module);
93   wasm_byte_vec_delete(&wasm);
94   if (error != NULL)
95     exit_with_error("failed to compile module", error, NULL);
96   error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
97   if (instance == NULL)
98     exit_with_error("failed to instantiate", error, trap);
99 
100   // Lookup our `run` export function
101   wasm_extern_vec_t externs;
102   wasm_instance_exports(instance, &externs);
103   assert(externs.size == 1);
104   wasm_func_t *run = wasm_extern_as_func(externs.data[0]);
105   assert(run != NULL);
106 
107   // Spawn a thread to send us an interrupt after a period of time.
108   spawn_interrupt(handle);
109 
110   // And call it!
111   printf("Entering infinite loop...\n");
112   error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
113   assert(error == NULL);
114   assert(trap != NULL);
115   printf("Got a trap!...\n");
116 
117   // `trap` can be inspected here to see the trap message has an interrupt in it
118 
119   wasm_trap_delete(trap);
120   wasm_extern_vec_delete(&externs);
121   wasm_instance_delete(instance);
122   wasm_module_delete(module);
123   wasm_store_delete(store);
124   wasm_engine_delete(engine);
125   return 0;
126 }
127 
128 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
129   fprintf(stderr, "error: %s\n", message);
130   wasm_byte_vec_t error_message;
131   if (error != NULL) {
132     wasmtime_error_message(error, &error_message);
133     wasmtime_error_delete(error);
134   } else {
135     wasm_trap_message(trap, &error_message);
136     wasm_trap_delete(trap);
137   }
138   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
139   wasm_byte_vec_delete(&error_message);
140   exit(1);
141 }
142