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