xref: /wasmtime-44.0.1/examples/interrupt.c (revision 9ce3ffe1)
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   if (fread(wat.data, file_size, 1, file) != 1) {
84     printf("> Error loading module!\n");
85     return 1;
86   }
87   fclose(file);
88 
89   // Parse the wat into the binary wasm format
90   wasm_byte_vec_t wasm;
91   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
92   if (error != NULL)
93     exit_with_error("failed to parse wat", error, NULL);
94   wasm_byte_vec_delete(&wat);
95 
96   // Now that we've got our binary webassembly we can compile our module.
97   wasmtime_module_t *module = NULL;
98   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
99   wasm_byte_vec_delete(&wasm);
100   if (error != NULL)
101     exit_with_error("failed to compile module", error, NULL);
102 
103   wasm_trap_t *trap = NULL;
104   wasmtime_instance_t instance;
105   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
106   if (error != NULL || trap != NULL)
107     exit_with_error("failed to instantiate", error, trap);
108   wasmtime_module_delete(module);
109 
110   // Lookup our `run` export function
111   wasmtime_extern_t run;
112   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
113   assert(ok);
114   assert(run.kind == WASMTIME_EXTERN_FUNC);
115 
116   // Spawn a thread to send us an interrupt after a period of time.
117   spawn_interrupt(engine);
118 
119   // And call it!
120   printf("Entering infinite loop...\n");
121   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
122   assert(error == NULL);
123   assert(trap != NULL);
124   printf("Got a trap!...\n");
125 
126   wasmtime_store_delete(store);
127   wasm_engine_delete(engine);
128   return 0;
129 }
130 
131 static void exit_with_error(const char *message, wasmtime_error_t *error,
132                             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