xref: /wasmtime-44.0.1/examples/memory.c (revision eb650f6f)
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/memory.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 memory
14    ./memory
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 Also note that this example was taken from
20 https://github.com/WebAssembly/wasm-c-api/blob/master/example/memory.c
21 originally
22 */
23 
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wasm.h>
29 #include <wasmtime.h>
30 
31 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
32 
33 wasm_memory_t* get_export_memory(const wasm_extern_vec_t* exports, size_t i) {
34   if (exports->size <= i || !wasm_extern_as_memory(exports->data[i])) {
35     printf("> Error accessing memory export %zu!\n", i);
36     exit(1);
37   }
38   return wasm_extern_as_memory(exports->data[i]);
39 }
40 
41 wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
42   if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
43     printf("> Error accessing function export %zu!\n", i);
44     exit(1);
45   }
46   return wasm_extern_as_func(exports->data[i]);
47 }
48 
49 
50 void check(bool success) {
51   if (!success) {
52     printf("> Error, expected success\n");
53     exit(1);
54   }
55 }
56 
57 void check_call(wasm_func_t* func, wasm_val_t args[], size_t num_args, int32_t expected) {
58   wasm_val_t results[1];
59   wasm_trap_t *trap = NULL;
60   wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, 1, &trap);
61   if (error != NULL || trap != NULL)
62     exit_with_error("failed to call function", error, trap);
63   if (results[0].of.i32 != expected) {
64     printf("> Error on result\n");
65     exit(1);
66   }
67 }
68 
69 void check_call0(wasm_func_t* func, int32_t expected) {
70   check_call(func, NULL, 0, expected);
71 }
72 
73 void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
74   wasm_val_t args[] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
75   check_call(func, args, 1, expected);
76 }
77 
78 void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
79   wasm_val_t args[2] = {
80     {.kind = WASM_I32, .of = {.i32 = arg1}},
81     {.kind = WASM_I32, .of = {.i32 = arg2}}
82   };
83   check_call(func, args, 2, expected);
84 }
85 
86 void check_ok(wasm_func_t* func, wasm_val_t args[], size_t num_args) {
87   wasm_trap_t *trap = NULL;
88   wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, NULL, 0, &trap);
89   if (error != NULL || trap != NULL)
90     exit_with_error("failed to call function", error, trap);
91 }
92 
93 void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
94   wasm_val_t args[2] = {
95     {.kind = WASM_I32, .of = {.i32 = arg1}},
96     {.kind = WASM_I32, .of = {.i32 = arg2}}
97   };
98   check_ok(func, args, 2);
99 }
100 
101 void check_trap(wasm_func_t* func, wasm_val_t args[], size_t num_args, size_t num_results) {
102   wasm_val_t results[1];
103   assert(num_results <= 1);
104   wasm_trap_t *trap = NULL;
105   wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, num_results, &trap);
106   if (error != NULL)
107     exit_with_error("failed to call function", error, NULL);
108   if (trap == NULL) {
109     printf("> Error on result, expected trap\n");
110     exit(1);
111   }
112   wasm_trap_delete(trap);
113 }
114 
115 void check_trap1(wasm_func_t* func, int32_t arg) {
116   wasm_val_t args[1] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
117   check_trap(func, args, 1, 1);
118 }
119 
120 void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
121   wasm_val_t args[2] = {
122     {.kind = WASM_I32, .of = {.i32 = arg1}},
123     {.kind = WASM_I32, .of = {.i32 = arg2}}
124   };
125   check_trap(func, args, 2, 0);
126 }
127 
128 int main(int argc, const char* argv[]) {
129   // Initialize.
130   printf("Initializing...\n");
131   wasm_engine_t* engine = wasm_engine_new();
132   wasm_store_t* store = wasm_store_new(engine);
133 
134   // Load our input file to parse it next
135   FILE* file = fopen("examples/memory.wat", "r");
136   if (!file) {
137     printf("> Error loading file!\n");
138     return 1;
139   }
140   fseek(file, 0L, SEEK_END);
141   size_t file_size = ftell(file);
142   fseek(file, 0L, SEEK_SET);
143   wasm_byte_vec_t wat;
144   wasm_byte_vec_new_uninitialized(&wat, file_size);
145   if (fread(wat.data, file_size, 1, file) != 1) {
146     printf("> Error loading module!\n");
147     return 1;
148   }
149   fclose(file);
150 
151   // Parse the wat into the binary wasm format
152   wasm_byte_vec_t binary;
153   wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
154   if (error != NULL)
155     exit_with_error("failed to parse wat", error, NULL);
156   wasm_byte_vec_delete(&wat);
157 
158   // Compile.
159   printf("Compiling module...\n");
160   wasm_module_t* module = NULL;
161   error = wasmtime_module_new(engine, &binary, &module);
162   if (error)
163     exit_with_error("failed to compile module", error, NULL);
164   wasm_byte_vec_delete(&binary);
165 
166   // Instantiate.
167   printf("Instantiating module...\n");
168   wasm_instance_t* instance = NULL;
169   wasm_trap_t *trap = NULL;
170   error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
171   if (!instance)
172     exit_with_error("failed to instantiate", error, trap);
173 
174   // Extract export.
175   printf("Extracting exports...\n");
176   wasm_extern_vec_t exports;
177   wasm_instance_exports(instance, &exports);
178   size_t i = 0;
179   wasm_memory_t* memory = get_export_memory(&exports, i++);
180   wasm_func_t* size_func = get_export_func(&exports, i++);
181   wasm_func_t* load_func = get_export_func(&exports, i++);
182   wasm_func_t* store_func = get_export_func(&exports, i++);
183 
184   wasm_module_delete(module);
185 
186   // Try cloning.
187   wasm_memory_t* copy = wasm_memory_copy(memory);
188   wasm_memory_delete(copy);
189 
190   // Check initial memory.
191   printf("Checking memory...\n");
192   check(wasm_memory_size(memory) == 2);
193   check(wasm_memory_data_size(memory) == 0x20000);
194   check(wasm_memory_data(memory)[0] == 0);
195   check(wasm_memory_data(memory)[0x1000] == 1);
196   check(wasm_memory_data(memory)[0x1003] == 4);
197 
198   check_call0(size_func, 2);
199   check_call1(load_func, 0, 0);
200   check_call1(load_func, 0x1000, 1);
201   check_call1(load_func, 0x1003, 4);
202   check_call1(load_func, 0x1ffff, 0);
203   check_trap1(load_func, 0x20000);
204 
205   // Mutate memory.
206   printf("Mutating memory...\n");
207   wasm_memory_data(memory)[0x1003] = 5;
208   check_ok2(store_func, 0x1002, 6);
209   check_trap2(store_func, 0x20000, 0);
210 
211   check(wasm_memory_data(memory)[0x1002] == 6);
212   check(wasm_memory_data(memory)[0x1003] == 5);
213   check_call1(load_func, 0x1002, 6);
214   check_call1(load_func, 0x1003, 5);
215 
216   // Grow memory.
217   printf("Growing memory...\n");
218   check(wasm_memory_grow(memory, 1));
219   check(wasm_memory_size(memory) == 3);
220   check(wasm_memory_data_size(memory) == 0x30000);
221 
222   check_call1(load_func, 0x20000, 0);
223   check_ok2(store_func, 0x20000, 0);
224   check_trap1(load_func, 0x30000);
225   check_trap2(store_func, 0x30000, 0);
226 
227   check(! wasm_memory_grow(memory, 1));
228   check(wasm_memory_grow(memory, 0));
229 
230   wasm_extern_vec_delete(&exports);
231   wasm_instance_delete(instance);
232 
233   // Create stand-alone memory.
234   printf("Creating stand-alone memory...\n");
235   wasm_limits_t limits = {5, 5};
236   wasm_memorytype_t* memorytype = wasm_memorytype_new(&limits);
237   wasm_memory_t* memory2 = wasm_memory_new(store, memorytype);
238   check(wasm_memory_size(memory2) == 5);
239   check(! wasm_memory_grow(memory2, 1));
240   check(wasm_memory_grow(memory2, 0));
241 
242   wasm_memorytype_delete(memorytype);
243   wasm_memory_delete(memory2);
244 
245   // Shut down.
246   printf("Shutting down...\n");
247   wasm_store_delete(store);
248   wasm_engine_delete(engine);
249 
250   // All done.
251   printf("Done.\n");
252   return 0;
253 }
254 
255 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
256   fprintf(stderr, "error: %s\n", message);
257   wasm_byte_vec_t error_message;
258   if (error != NULL) {
259     wasmtime_error_message(error, &error_message);
260     wasmtime_error_delete(error);
261   } else {
262     wasm_trap_message(trap, &error_message);
263     wasm_trap_delete(trap);
264   }
265   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
266   wasm_byte_vec_delete(&error_message);
267   exit(1);
268 }
269