xref: /wasmtime-44.0.1/examples/fib-debug/main.c (revision 3f694ae3)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5 
6 #include <wasm.h>
7 #include "wasmtime.h"
8 
9 #define own
10 
11 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
12 
13 int main(int argc, const char* argv[]) {
14   // Configuring engine to support generating of DWARF info.
15   // lldb can be used to attach to the program and observe
16   // original fib-wasm.c source code and variables.
17   wasm_config_t* config = wasm_config_new();
18   wasmtime_config_debug_info_set(config, true);
19 
20   // Initialize.
21   printf("Initializing...\n");
22   wasm_engine_t* engine = wasm_engine_new_with_config(config);
23   wasm_store_t* store = wasm_store_new(engine);
24 
25   // Load binary.
26   printf("Loading binary...\n");
27   FILE* file = fopen("target/wasm32-unknown-unknown/debug/fib.wasm", "rb");
28   if (!file) {
29     printf("> Error opening module!\n");
30     return 1;
31   }
32   fseek(file, 0L, SEEK_END);
33   size_t file_size = ftell(file);
34   fseek(file, 0L, SEEK_SET);
35   wasm_byte_vec_t binary;
36   wasm_byte_vec_new_uninitialized(&binary, file_size);
37   if (fread(binary.data, file_size, 1, file) != 1) {
38     printf("> Error reading module!\n");
39     return 1;
40   }
41   fclose(file);
42 
43   // Compile.
44   printf("Compiling module...\n");
45   wasm_module_t *module = NULL;
46   wasmtime_error_t* error = wasmtime_module_new(engine, &binary, &module);
47   if (!module)
48     exit_with_error("failed to compile module", error, NULL);
49   wasm_byte_vec_delete(&binary);
50 
51   // Figure out which export is the `fib` export
52   wasm_exporttype_vec_t module_exports;
53   wasm_module_exports(module, &module_exports);
54   int fib_idx = -1;
55   for (int i = 0; i < module_exports.size; i++) {
56     const wasm_name_t *name = wasm_exporttype_name(module_exports.data[i]);
57     if (name->size != 3)
58       continue;
59     if (strncmp("fib", name->data, 3) != 0)
60       continue;
61     fib_idx = i;
62     break;
63   }
64   wasm_exporttype_vec_delete(&module_exports);
65   if (fib_idx == -1) {
66     printf("> Error finding `fib` export!\n");
67     return 1;
68   }
69 
70   // Instantiate.
71   printf("Instantiating module...\n");
72   wasm_instance_t* instance = NULL;
73   wasm_trap_t *trap = NULL;
74   wasm_extern_vec_t imports = WASM_EMPTY_VEC;
75   error = wasmtime_instance_new(store, module, &imports, &instance, &trap);
76   if (error != NULL || trap != NULL)
77     exit_with_error("failed to instantiate", error, trap);
78   wasm_module_delete(module);
79 
80   // Extract export.
81   printf("Extracting export...\n");
82   own wasm_extern_vec_t exports;
83   wasm_instance_exports(instance, &exports);
84   if (exports.size == 0) {
85     printf("> Error accessing exports!\n");
86     return 1;
87   }
88   // Getting second export (first is memory).
89   wasm_func_t* run_func = wasm_extern_as_func(exports.data[fib_idx]);
90   if (run_func == NULL) {
91     printf("> Error accessing export!\n");
92     return 1;
93   }
94 
95   wasm_instance_delete(instance);
96 
97   // Call.
98   printf("Calling fib...\n");
99   wasm_val_t params[1] = { WASM_I32_VAL(6) };
100   wasm_val_t results[1];
101   wasm_val_vec_t params_vec = WASM_ARRAY_VEC(params);
102   wasm_val_vec_t results_vec = WASM_ARRAY_VEC(results);
103   error = wasmtime_func_call(run_func, &params_vec, &results_vec, &trap);
104   if (error != NULL || trap != NULL)
105     exit_with_error("failed to call function", error, trap);
106 
107   wasm_extern_vec_delete(&exports);
108 
109   printf("> fib(6) = %d\n", results[0].of.i32);
110 
111   // Shut down.
112   printf("Shutting down...\n");
113   wasm_store_delete(store);
114   wasm_engine_delete(engine);
115 
116   // All done.
117   printf("Done.\n");
118   return 0;
119 }
120 
121 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
122   fprintf(stderr, "error: %s\n", message);
123   wasm_byte_vec_t error_message;
124   if (error != NULL) {
125     wasmtime_error_message(error, &error_message);
126   } else {
127     wasm_trap_message(trap, &error_message);
128   }
129   fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
130   wasm_byte_vec_delete(&error_message);
131   exit(1);
132 }
133