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