xref: /wasmtime-44.0.1/examples/interrupt.c (revision ef70686b)
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 You can also build using cmake:
21 
22 mkdir build && cd build && cmake .. && \
23   cmake --build . --target wasmtime-interrupt
24 */
25 
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <wasm.h>
30 #include <wasmtime.h>
31 
32 #ifdef _WIN32
33 static void spawn_interrupt(wasm_engine_t *engine) {
34   wasmtime_engine_increment_epoch(engine);
35 }
36 #else
37 #include <pthread.h>
38 #include <time.h>
39 
40 static void *helper(void *_engine) {
41   wasm_engine_t *engine = _engine;
42   struct timespec sleep_dur;
43   sleep_dur.tv_sec = 1;
44   sleep_dur.tv_nsec = 0;
45   nanosleep(&sleep_dur, NULL);
46   printf("Sending an interrupt\n");
47   wasmtime_engine_increment_epoch(engine);
48   return 0;
49 }
50 
51 static void spawn_interrupt(wasm_engine_t *engine) {
52   pthread_t child;
53   int rc = pthread_create(&child, NULL, helper, engine);
54   assert(rc == 0);
55 }
56 #endif
57 
58 static void exit_with_error(const char *message, wasmtime_error_t *error,
59                             wasm_trap_t *trap);
60 
61 int main() {
62   // Create a `wasm_store_t` with interrupts enabled
63   wasm_config_t *config = wasm_config_new();
64   assert(config != NULL);
65   wasmtime_config_epoch_interruption_set(config, true);
66   wasm_engine_t *engine = wasm_engine_new_with_config(config);
67   assert(engine != NULL);
68   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
69   assert(store != NULL);
70   wasmtime_context_t *context = wasmtime_store_context(store);
71 
72   // Configure the epoch deadline after which WebAssembly code will trap.
73   wasmtime_context_set_epoch_deadline(context, 1);
74 
75   // Read our input file, which in this case is a wasm text file.
76   FILE *file = fopen("examples/interrupt.wat", "r");
77   assert(file != NULL);
78   fseek(file, 0L, SEEK_END);
79   size_t file_size = ftell(file);
80   fseek(file, 0L, SEEK_SET);
81   wasm_byte_vec_t wat;
82   wasm_byte_vec_new_uninitialized(&wat, file_size);
83   assert(fread(wat.data, file_size, 1, file) == 1);
84   fclose(file);
85 
86   // Parse the wat into the binary wasm format
87   wasm_byte_vec_t wasm;
88   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
89   if (error != NULL)
90     exit_with_error("failed to parse wat", error, NULL);
91   wasm_byte_vec_delete(&wat);
92 
93   // Now that we've got our binary webassembly we can compile our module.
94   wasmtime_module_t *module = NULL;
95   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
96   wasm_byte_vec_delete(&wasm);
97   if (error != NULL)
98     exit_with_error("failed to compile module", error, NULL);
99 
100   wasm_trap_t *trap = NULL;
101   wasmtime_instance_t instance;
102   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
103   if (error != NULL || trap != NULL)
104     exit_with_error("failed to instantiate", error, trap);
105   wasmtime_module_delete(module);
106 
107   // Lookup our `run` export function
108   wasmtime_extern_t run;
109   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
110   assert(ok);
111   assert(run.kind == WASMTIME_EXTERN_FUNC);
112 
113   // Spawn a thread to send us an interrupt after a period of time.
114   spawn_interrupt(engine);
115 
116   // And call it!
117   printf("Entering infinite loop...\n");
118   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
119   assert(error == NULL);
120   assert(trap != NULL);
121   printf("Got a trap!...\n");
122 
123   wasmtime_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,
129                             wasm_trap_t *trap) {
130   fprintf(stderr, "error: %s\n", message);
131   wasm_byte_vec_t error_message;
132   if (error != NULL) {
133     wasmtime_error_message(error, &error_message);
134     wasmtime_error_delete(error);
135   } else {
136     wasm_trap_message(trap, &error_message);
137     wasm_trap_delete(trap);
138   }
139   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
140   wasm_byte_vec_delete(&error_message);
141   exit(1);
142 }
143