xref: /wasmtime-44.0.1/examples/interrupt.c (revision adff9d9d)
1c9a0ba81SAlex Crichton /*
2c9a0ba81SAlex Crichton Example of instantiating of the WebAssembly module and invoking its exported
3c9a0ba81SAlex Crichton function.
4c9a0ba81SAlex Crichton 
557ba95e9SMasashi Yoshimura You can build using cmake:
62ba3025eSTheGreatRambler 
7f8fee938STyler Rockwood mkdir build && cd build && cmake .. && \
8f8fee938STyler Rockwood   cmake --build . --target wasmtime-interrupt
9c9a0ba81SAlex Crichton */
10c9a0ba81SAlex Crichton 
11c9a0ba81SAlex Crichton #include <assert.h>
12c9a0ba81SAlex Crichton #include <stdio.h>
13c9a0ba81SAlex Crichton #include <stdlib.h>
14c9a0ba81SAlex Crichton #include <wasm.h>
15c9a0ba81SAlex Crichton #include <wasmtime.h>
16c9a0ba81SAlex Crichton 
17c9a0ba81SAlex Crichton #ifdef _WIN32
spawn_interrupt(wasm_engine_t * engine)18c22033bfSAlex Crichton static void spawn_interrupt(wasm_engine_t *engine) {
19c22033bfSAlex Crichton   wasmtime_engine_increment_epoch(engine);
20c9a0ba81SAlex Crichton }
21c9a0ba81SAlex Crichton #else
22c9a0ba81SAlex Crichton #include <pthread.h>
23c9a0ba81SAlex Crichton #include <time.h>
24c9a0ba81SAlex Crichton 
helper(void * _engine)25c22033bfSAlex Crichton static void *helper(void *_engine) {
26c22033bfSAlex Crichton   wasm_engine_t *engine = _engine;
27c9a0ba81SAlex Crichton   struct timespec sleep_dur;
28c9a0ba81SAlex Crichton   sleep_dur.tv_sec = 1;
29c9a0ba81SAlex Crichton   sleep_dur.tv_nsec = 0;
30c9a0ba81SAlex Crichton   nanosleep(&sleep_dur, NULL);
31c9a0ba81SAlex Crichton   printf("Sending an interrupt\n");
32c22033bfSAlex Crichton   wasmtime_engine_increment_epoch(engine);
33f94db655SPeter Huene   return 0;
34c9a0ba81SAlex Crichton }
35c9a0ba81SAlex Crichton 
spawn_interrupt(wasm_engine_t * engine)36c22033bfSAlex Crichton static void spawn_interrupt(wasm_engine_t *engine) {
37c9a0ba81SAlex Crichton   pthread_t child;
38c22033bfSAlex Crichton   int rc = pthread_create(&child, NULL, helper, engine);
39c9a0ba81SAlex Crichton   assert(rc == 0);
40c9a0ba81SAlex Crichton }
41c9a0ba81SAlex Crichton #endif
42c9a0ba81SAlex Crichton 
43f8fee938STyler Rockwood static void exit_with_error(const char *message, wasmtime_error_t *error,
44f8fee938STyler Rockwood                             wasm_trap_t *trap);
45c9a0ba81SAlex Crichton 
main()46c9a0ba81SAlex Crichton int main() {
47c9a0ba81SAlex Crichton   // Create a `wasm_store_t` with interrupts enabled
48c9a0ba81SAlex Crichton   wasm_config_t *config = wasm_config_new();
49c9a0ba81SAlex Crichton   assert(config != NULL);
50c22033bfSAlex Crichton   wasmtime_config_epoch_interruption_set(config, true);
51c9a0ba81SAlex Crichton   wasm_engine_t *engine = wasm_engine_new_with_config(config);
52c9a0ba81SAlex Crichton   assert(engine != NULL);
537a1b7cdfSAlex Crichton   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
54c9a0ba81SAlex Crichton   assert(store != NULL);
557a1b7cdfSAlex Crichton   wasmtime_context_t *context = wasmtime_store_context(store);
56c9a0ba81SAlex Crichton 
57c22033bfSAlex Crichton   // Configure the epoch deadline after which WebAssembly code will trap.
58c22033bfSAlex Crichton   wasmtime_context_set_epoch_deadline(context, 1);
59c9a0ba81SAlex Crichton 
60c9a0ba81SAlex Crichton   // Read our input file, which in this case is a wasm text file.
61c9a0ba81SAlex Crichton   FILE *file = fopen("examples/interrupt.wat", "r");
62c9a0ba81SAlex Crichton   assert(file != NULL);
63c9a0ba81SAlex Crichton   fseek(file, 0L, SEEK_END);
64c9a0ba81SAlex Crichton   size_t file_size = ftell(file);
65c9a0ba81SAlex Crichton   fseek(file, 0L, SEEK_SET);
66c9a0ba81SAlex Crichton   wasm_byte_vec_t wat;
67c9a0ba81SAlex Crichton   wasm_byte_vec_new_uninitialized(&wat, file_size);
6893580f24SBruce Mitchener   if (fread(wat.data, file_size, 1, file) != 1) {
6993580f24SBruce Mitchener     printf("> Error loading module!\n");
7093580f24SBruce Mitchener     return 1;
7193580f24SBruce Mitchener   }
72c9a0ba81SAlex Crichton   fclose(file);
73c9a0ba81SAlex Crichton 
74c9a0ba81SAlex Crichton   // Parse the wat into the binary wasm format
75c9a0ba81SAlex Crichton   wasm_byte_vec_t wasm;
767a1b7cdfSAlex Crichton   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
77c9a0ba81SAlex Crichton   if (error != NULL)
78c9a0ba81SAlex Crichton     exit_with_error("failed to parse wat", error, NULL);
79c9a0ba81SAlex Crichton   wasm_byte_vec_delete(&wat);
80c9a0ba81SAlex Crichton 
81c9a0ba81SAlex Crichton   // Now that we've got our binary webassembly we can compile our module.
827a1b7cdfSAlex Crichton   wasmtime_module_t *module = NULL;
837a1b7cdfSAlex Crichton   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
84c9a0ba81SAlex Crichton   wasm_byte_vec_delete(&wasm);
85c9a0ba81SAlex Crichton   if (error != NULL)
86c9a0ba81SAlex Crichton     exit_with_error("failed to compile module", error, NULL);
877a1b7cdfSAlex Crichton 
887a1b7cdfSAlex Crichton   wasm_trap_t *trap = NULL;
897a1b7cdfSAlex Crichton   wasmtime_instance_t instance;
907a1b7cdfSAlex Crichton   error = wasmtime_instance_new(context, module, NULL, 0, &instance, &trap);
917a1b7cdfSAlex Crichton   if (error != NULL || trap != NULL)
92c9a0ba81SAlex Crichton     exit_with_error("failed to instantiate", error, trap);
937a1b7cdfSAlex Crichton   wasmtime_module_delete(module);
94c9a0ba81SAlex Crichton 
95c9a0ba81SAlex Crichton   // Lookup our `run` export function
967a1b7cdfSAlex Crichton   wasmtime_extern_t run;
977a1b7cdfSAlex Crichton   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
987a1b7cdfSAlex Crichton   assert(ok);
997a1b7cdfSAlex Crichton   assert(run.kind == WASMTIME_EXTERN_FUNC);
100c9a0ba81SAlex Crichton 
101c9a0ba81SAlex Crichton   // Spawn a thread to send us an interrupt after a period of time.
102c22033bfSAlex Crichton   spawn_interrupt(engine);
103c9a0ba81SAlex Crichton 
104c9a0ba81SAlex Crichton   // And call it!
105c9a0ba81SAlex Crichton   printf("Entering infinite loop...\n");
1067a1b7cdfSAlex Crichton   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
107c9a0ba81SAlex Crichton   assert(error == NULL);
108c9a0ba81SAlex Crichton   assert(trap != NULL);
109c9a0ba81SAlex Crichton   printf("Got a trap!...\n");
110*adff9d9dSAlex Crichton   wasm_trap_delete(trap);
111c9a0ba81SAlex Crichton 
1127a1b7cdfSAlex Crichton   wasmtime_store_delete(store);
113c9a0ba81SAlex Crichton   wasm_engine_delete(engine);
114c9a0ba81SAlex Crichton   return 0;
115c9a0ba81SAlex Crichton }
116c9a0ba81SAlex Crichton 
exit_with_error(const char * message,wasmtime_error_t * error,wasm_trap_t * trap)117f8fee938STyler Rockwood static void exit_with_error(const char *message, wasmtime_error_t *error,
118f8fee938STyler Rockwood                             wasm_trap_t *trap) {
119c9a0ba81SAlex Crichton   fprintf(stderr, "error: %s\n", message);
120c9a0ba81SAlex Crichton   wasm_byte_vec_t error_message;
121c9a0ba81SAlex Crichton   if (error != NULL) {
122c9a0ba81SAlex Crichton     wasmtime_error_message(error, &error_message);
123c9a0ba81SAlex Crichton     wasmtime_error_delete(error);
124c9a0ba81SAlex Crichton   } else {
125c9a0ba81SAlex Crichton     wasm_trap_message(trap, &error_message);
126c9a0ba81SAlex Crichton     wasm_trap_delete(trap);
127c9a0ba81SAlex Crichton   }
128c9a0ba81SAlex Crichton   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
129c9a0ba81SAlex Crichton   wasm_byte_vec_delete(&error_message);
130c9a0ba81SAlex Crichton   exit(1);
131c9a0ba81SAlex Crichton }
132