xref: /wasmtime-44.0.1/examples/serialize.c (revision 4c8edb95)
1 /*
2 Example of instantiating of the WebAssembly module and invoking its exported
3 function.
4 
5 You can compile and run this example on Linux with:
6 
7    cargo build --release -p wasmtime-c-api
8    cc examples/hello.c \
9        -I crates/c-api/include \
10        target/release/libwasmtime.a \
11        -lpthread -ldl -lm \
12        -o hello
13    ./hello
14 
15 Note that on Windows and macOS the command will be similar, but you'll need
16 to tweak the `-lpthread` and such annotations as well as the name of the
17 `libwasmtime.a` file on Windows.
18 
19 You can also build using cmake:
20 
21 mkdir build && cd build && cmake .. && \
22   cmake --build . --target wasmtime-serialize
23 */
24 
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <wasm.h>
29 #include <wasmtime.h>
30 
31 static void exit_with_error(const char *message, wasmtime_error_t *error,
32                             wasm_trap_t *trap);
33 
34 static wasm_trap_t *hello_callback(void *env, wasmtime_caller_t *caller,
35                                    const wasmtime_val_t *args, size_t nargs,
36                                    wasmtime_val_t *results, size_t nresults) {
37   printf("Calling back...\n");
38   printf("> Hello World!\n");
39   return NULL;
40 }
41 
42 int serialize(wasm_byte_vec_t *buffer) {
43   // Set up our compilation context. Note that we could also work with a
44   // `wasm_config_t` here to configure what feature are enabled and various
45   // compilation settings.
46   printf("Initializing...\n");
47   wasm_engine_t *engine = wasm_engine_new();
48   assert(engine != NULL);
49 
50   // Read our input file, which in this case is a wasm text file.
51   FILE *file = fopen("examples/hello.wat", "r");
52   assert(file != NULL);
53   fseek(file, 0L, SEEK_END);
54   size_t file_size = ftell(file);
55   fseek(file, 0L, SEEK_SET);
56   wasm_byte_vec_t wat;
57   wasm_byte_vec_new_uninitialized(&wat, file_size);
58   if (fread(wat.data, file_size, 1, file) != 1) {
59     printf("> Error loading module!\n");
60     return 1;
61   }
62   fclose(file);
63 
64   // Parse the wat into the binary wasm format
65   wasm_byte_vec_t wasm;
66   wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
67   if (error != NULL)
68     exit_with_error("failed to parse wat", error, NULL);
69   wasm_byte_vec_delete(&wat);
70 
71   // Now that we've got our binary webassembly we can compile our module
72   // and serialize into buffer.
73   printf("Compiling and serializing module...\n");
74   wasmtime_module_t *module = NULL;
75   error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
76   wasm_byte_vec_delete(&wasm);
77   if (error != NULL)
78     exit_with_error("failed to compile module", error, NULL);
79   error = wasmtime_module_serialize(module, buffer);
80   wasmtime_module_delete(module);
81   if (error != NULL)
82     exit_with_error("failed to serialize module", error, NULL);
83 
84   printf("Serialized.\n");
85 
86   wasm_engine_delete(engine);
87   return 0;
88 }
89 
90 int deserialize(wasm_byte_vec_t *buffer) {
91   // Set up our compilation context. Note that we could also work with a
92   // `wasm_config_t` here to configure what feature are enabled and various
93   // compilation settings.
94   printf("Initializing...\n");
95   wasm_engine_t *engine = wasm_engine_new();
96   assert(engine != NULL);
97 
98   // With an engine we can create a *store* which is a long-lived group of wasm
99   // modules.
100   wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
101   assert(store != NULL);
102   wasmtime_context_t *context = wasmtime_store_context(store);
103 
104   // Deserialize compiled module.
105   printf("Deserialize module...\n");
106   wasmtime_module_t *module = NULL;
107   wasmtime_error_t *error = wasmtime_module_deserialize(
108       engine, (uint8_t *)buffer->data, buffer->size, &module);
109   if (error != NULL)
110     exit_with_error("failed to compile module", error, NULL);
111 
112   // Next up we need to create the function that the wasm module imports. Here
113   // we'll be hooking up a thunk function to the `hello_callback` native
114   // function above.
115   printf("Creating callback...\n");
116   wasm_functype_t *hello_ty = wasm_functype_new_0_0();
117   wasmtime_func_t hello;
118   wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
119 
120   // With our callback function we can now instantiate the compiled module,
121   // giving us an instance we can then execute exports from. Note that
122   // instantiation can trap due to execution of the `start` function, so we need
123   // to handle that here too.
124   printf("Instantiating module...\n");
125   wasm_trap_t *trap = NULL;
126   wasmtime_instance_t instance;
127   wasmtime_extern_t imports[1];
128   imports[0].kind = WASMTIME_EXTERN_FUNC;
129   imports[0].of.func = hello;
130   error = wasmtime_instance_new(context, module, imports, 1, &instance, &trap);
131   if (error != NULL || trap != NULL)
132     exit_with_error("failed to instantiate", error, trap);
133   wasmtime_module_delete(module);
134 
135   // Lookup our `run` export function
136   wasmtime_extern_t run;
137   bool ok = wasmtime_instance_export_get(context, &instance, "run", 3, &run);
138   assert(ok);
139   assert(run.kind == WASMTIME_EXTERN_FUNC);
140 
141   // And call it!
142   printf("Calling export...\n");
143   error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap);
144   if (error != NULL || trap != NULL)
145     exit_with_error("failed to call function", error, trap);
146 
147   // Clean up after ourselves at this point
148   printf("All finished!\n");
149 
150   wasmtime_store_delete(store);
151   wasm_engine_delete(engine);
152   return 0;
153 }
154 
155 int main() {
156   wasm_byte_vec_t buffer;
157   if (serialize(&buffer)) {
158     return 1;
159   }
160   if (deserialize(&buffer)) {
161     return 1;
162   }
163   wasm_byte_vec_delete(&buffer);
164   return 0;
165 }
166 
167 static void exit_with_error(const char *message, wasmtime_error_t *error,
168                             wasm_trap_t *trap) {
169   fprintf(stderr, "error: %s\n", message);
170   wasm_byte_vec_t error_message;
171   if (error != NULL) {
172     wasmtime_error_message(error, &error_message);
173     wasmtime_error_delete(error);
174   } else {
175     wasm_trap_message(trap, &error_message);
176     wasm_trap_delete(trap);
177   }
178   fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
179   wasm_byte_vec_delete(&error_message);
180   exit(1);
181 }
182