xref: /wasmtime-44.0.1/examples/linking.c (revision 56dcd4ed)
1 /*
2 Example of compiling, instantiating, and linking two WebAssembly modules
3 together.
4 
5 You can build using cmake:
6 
7 mkdir build && cd build && cmake .. && cmake --build . --target wasmtime-linking
8 */
9 
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <wasi.h>
14 #include <wasm.h>
15 #include <wasmtime.h>
16 
17 #define MIN(a, b) ((a) < (b) ? (a) : (b))
18 
19 static void exit_with_error(const char *message, wasmtime_error_t *error,
20                             wasm_trap_t *trap);
21 static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes,
22                           const char *file);
23 
24 int main() {
25   // Set up our context
26   wasm_engine_t *engine = wasm_engine_new();
27   assert(engine != NULL);
28   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
29   assert(store != NULL);
30   wasmtime_context_t *context = wasmtime_store_context(store);
31 
32   wasm_byte_vec_t linking1_wasm, linking2_wasm;
33   read_wat_file(engine, &linking1_wasm, "examples/linking1.wat");
34   read_wat_file(engine, &linking2_wasm, "examples/linking2.wat");
35 
36   // Compile our two modules
37   wasmtime_error_t *error;
38   wasmtime_module_t *linking1_module = NULL;
39   wasmtime_module_t *linking2_module = NULL;
40   error = wasmtime_module_new(engine, (uint8_t *)linking1_wasm.data,
41                               linking1_wasm.size, &linking1_module);
42   if (error != NULL)
43     exit_with_error("failed to compile linking1", error, NULL);
44   error = wasmtime_module_new(engine, (uint8_t *)linking2_wasm.data,
45                               linking2_wasm.size, &linking2_module);
46   if (error != NULL)
47     exit_with_error("failed to compile linking2", error, NULL);
48   wasm_byte_vec_delete(&linking1_wasm);
49   wasm_byte_vec_delete(&linking2_wasm);
50 
51   // Configure WASI and store it within our `wasmtime_store_t`
52   wasi_config_t *wasi_config = wasi_config_new();
53   assert(wasi_config);
54   wasi_config_inherit_argv(wasi_config);
55   wasi_config_inherit_env(wasi_config);
56   wasi_config_inherit_stdin(wasi_config);
57   wasi_config_inherit_stdout(wasi_config);
58   wasi_config_inherit_stderr(wasi_config);
59   wasm_trap_t *trap = NULL;
60   error = wasmtime_context_set_wasi(context, wasi_config);
61   if (error != NULL)
62     exit_with_error("failed to instantiate wasi", NULL, trap);
63 
64   // Create our linker which will be linking our modules together, and then add
65   // our WASI instance to it.
66   wasmtime_linker_t *linker = wasmtime_linker_new(engine);
67   error = wasmtime_linker_define_wasi(linker);
68   if (error != NULL)
69     exit_with_error("failed to link wasi", error, NULL);
70 
71   // Instantiate `linking2` with our linker.
72   wasmtime_instance_t linking2;
73   error = wasmtime_linker_instantiate(linker, context, linking2_module,
74                                       &linking2, &trap);
75   if (error != NULL || trap != NULL)
76     exit_with_error("failed to instantiate linking2", error, trap);
77 
78   // Register our new `linking2` instance with the linker
79   error = wasmtime_linker_define_instance(linker, context, "linking2",
80                                           strlen("linking2"), &linking2);
81   if (error != NULL)
82     exit_with_error("failed to link linking2", error, NULL);
83 
84   // Instantiate `linking1` with the linker now that `linking2` is defined
85   wasmtime_instance_t linking1;
86   error = wasmtime_linker_instantiate(linker, context, linking1_module,
87                                       &linking1, &trap);
88   if (error != NULL || trap != NULL)
89     exit_with_error("failed to instantiate linking1", error, trap);
90 
91   // Lookup our `run` export function
92   wasmtime_extern_t run;
93   bool ok = wasmtime_instance_export_get(context, &linking1, "run", 3, &run);
94   assert(ok);
95   assert(run.kind == WASMTIME_EXTERN_FUNC);
96   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
97   if (error != NULL || trap != NULL)
98     exit_with_error("failed to call run", error, trap);
99 
100   // Clean up after ourselves at this point
101   wasmtime_linker_delete(linker);
102   wasmtime_module_delete(linking1_module);
103   wasmtime_module_delete(linking2_module);
104   wasmtime_store_delete(store);
105   wasm_engine_delete(engine);
106   return 0;
107 }
108 
109 static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes,
110                           const char *filename) {
111   wasm_byte_vec_t wat;
112   // Load our input file to parse it next
113   FILE *file = fopen(filename, "r");
114   if (!file) {
115     printf("> Error loading file!\n");
116     exit(1);
117   }
118   fseek(file, 0L, SEEK_END);
119   size_t file_size = ftell(file);
120   wasm_byte_vec_new_uninitialized(&wat, file_size);
121   fseek(file, 0L, SEEK_SET);
122   if (fread(wat.data, file_size, 1, file) != 1) {
123     printf("> Error loading module!\n");
124     exit(1);
125   }
126   fclose(file);
127 
128   // Parse the wat into the binary wasm format
129   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, bytes);
130   if (error != NULL)
131     exit_with_error("failed to parse wat", error, NULL);
132   wasm_byte_vec_delete(&wat);
133 }
134 
135 static void exit_with_error(const char *message, wasmtime_error_t *error,
136                             wasm_trap_t *trap) {
137   fprintf(stderr, "error: %s\n", message);
138   wasm_byte_vec_t error_message;
139   if (error != NULL) {
140     wasmtime_error_message(error, &error_message);
141     wasmtime_error_delete(error);
142   } else {
143     wasm_trap_message(trap, &error_message);
144     wasm_trap_delete(trap);
145   }
146   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
147   wasm_byte_vec_delete(&error_message);
148   exit(1);
149 }
150