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