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