xref: /wasmtime-44.0.1/examples/interrupt.c (revision 770c5d0f)
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 .. && 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, wasm_trap_t *trap);
58 
59 int main() {
60   // Create a `wasm_store_t` with interrupts enabled
61   wasm_config_t *config = wasm_config_new();
62   assert(config != NULL);
63   wasmtime_config_epoch_interruption_set(config, true);
64   wasm_engine_t *engine = wasm_engine_new_with_config(config);
65   assert(engine != NULL);
66   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
67   assert(store != NULL);
68   wasmtime_context_t *context = wasmtime_store_context(store);
69 
70   // Configure the epoch deadline after which WebAssembly code will trap.
71   wasmtime_context_set_epoch_deadline(context, 1);
72 
73   // Read our input file, which in this case is a wasm text file.
74   FILE* file = fopen("examples/interrupt.wat", "r");
75   assert(file != NULL);
76   fseek(file, 0L, SEEK_END);
77   size_t file_size = ftell(file);
78   fseek(file, 0L, SEEK_SET);
79   wasm_byte_vec_t wat;
80   wasm_byte_vec_new_uninitialized(&wat, file_size);
81   assert(fread(wat.data, file_size, 1, file) == 1);
82   fclose(file);
83 
84   // Parse the wat into the binary wasm format
85   wasm_byte_vec_t wasm;
86   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
87   if (error != NULL)
88     exit_with_error("failed to parse wat", error, NULL);
89   wasm_byte_vec_delete(&wat);
90 
91   // Now that we've got our binary webassembly we can compile our module.
92   wasmtime_module_t *module = NULL;
93   error = wasmtime_module_new(engine, (uint8_t*) wasm.data, wasm.size, &module);
94   wasm_byte_vec_delete(&wasm);
95   if (error != NULL)
96     exit_with_error("failed to compile module", error, NULL);
97 
98   wasm_trap_t *trap = NULL;
99   wasmtime_instance_t instance;
100   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
101   if (error != NULL || trap != NULL)
102     exit_with_error("failed to instantiate", error, trap);
103   wasmtime_module_delete(module);
104 
105   // Lookup our `run` export function
106   wasmtime_extern_t run;
107   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
108   assert(ok);
109   assert(run.kind == WASMTIME_EXTERN_FUNC);
110 
111   // Spawn a thread to send us an interrupt after a period of time.
112   spawn_interrupt(engine);
113 
114   // And call it!
115   printf("Entering infinite loop...\n");
116   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
117   assert(error == NULL);
118   assert(trap != NULL);
119   printf("Got a trap!...\n");
120 
121   wasmtime_store_delete(store);
122   wasm_engine_delete(engine);
123   return 0;
124 }
125 
126 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
127   fprintf(stderr, "error: %s\n", message);
128   wasm_byte_vec_t error_message;
129   if (error != NULL) {
130     wasmtime_error_message(error, &error_message);
131     wasmtime_error_delete(error);
132   } else {
133     wasm_trap_message(trap, &error_message);
134     wasm_trap_delete(trap);
135   }
136   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
137   wasm_byte_vec_delete(&error_message);
138   exit(1);
139 }
140