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