1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5 
6 #include "wasm.h"
7 
8 #define own
9 
10 // A function to be called from Wasm code.
fail_callback(void * env,const wasm_val_t args[],wasm_val_t results[])11 own wasm_trap_t* fail_callback(
12   void* env, const wasm_val_t args[], wasm_val_t results[]
13 ) {
14   printf("Calling back...\n");
15   own wasm_name_t message;
16   wasm_name_new_from_string(&message, "callback abort");
17   own wasm_trap_t* trap = wasm_trap_new((wasm_store_t*)env, &message);
18   wasm_name_delete(&message);
19   return trap;
20 }
21 
22 
print_frame(wasm_frame_t * frame)23 void print_frame(wasm_frame_t* frame) {
24   printf("> %p @ 0x%zx = %"PRIu32".0x%zx\n",
25     wasm_frame_instance(frame),
26     wasm_frame_module_offset(frame),
27     wasm_frame_func_index(frame),
28     wasm_frame_func_offset(frame)
29   );
30 }
31 
32 
main(int argc,const char * argv[])33 int main(int argc, const char* argv[]) {
34   // Initialize.
35   printf("Initializing...\n");
36   wasm_engine_t* engine = wasm_engine_new();
37   wasm_store_t* store = wasm_store_new(engine);
38 
39   // Load binary.
40   printf("Loading binary...\n");
41   FILE* file = fopen("trap.wasm", "r");
42   if (!file) {
43     printf("> Error loading module!\n");
44     return 1;
45   }
46   fseek(file, 0L, SEEK_END);
47   size_t file_size = ftell(file);
48   fseek(file, 0L, SEEK_SET);
49   wasm_byte_vec_t binary;
50   wasm_byte_vec_new_uninitialized(&binary, file_size);
51   if (fread(binary.data, file_size, 1, file) != 1) {
52     printf("> Error loading module!\n");
53     return 1;
54   }
55   fclose(file);
56 
57   // Compile.
58   printf("Compiling module...\n");
59   own wasm_module_t* module = wasm_module_new(store, &binary);
60   if (!module) {
61     printf("> Error compiling module!\n");
62     return 1;
63   }
64 
65   wasm_byte_vec_delete(&binary);
66 
67   // Create external print functions.
68   printf("Creating callback...\n");
69   own wasm_functype_t* fail_type =
70     wasm_functype_new_0_1(wasm_valtype_new_i32());
71   own wasm_func_t* fail_func =
72     wasm_func_new_with_env(store, fail_type, fail_callback, store, NULL);
73 
74   wasm_functype_delete(fail_type);
75 
76   // Instantiate.
77   printf("Instantiating module...\n");
78   const wasm_extern_t* imports[] = { wasm_func_as_extern(fail_func) };
79   own wasm_instance_t* instance =
80     wasm_instance_new(store, module, imports, NULL);
81   if (!instance) {
82     printf("> Error instantiating module!\n");
83     return 1;
84   }
85 
86   wasm_func_delete(fail_func);
87 
88   // Extract export.
89   printf("Extracting exports...\n");
90   own wasm_extern_vec_t exports;
91   wasm_instance_exports(instance, &exports);
92   if (exports.size < 2) {
93     printf("> Error accessing exports!\n");
94     return 1;
95   }
96 
97   wasm_module_delete(module);
98   wasm_instance_delete(instance);
99 
100   // Call.
101   for (int i = 0; i < 2; ++i) {
102     const wasm_func_t* func = wasm_extern_as_func(exports.data[i]);
103     if (func == NULL) {
104       printf("> Error accessing export!\n");
105       return 1;
106     }
107 
108     printf("Calling export %d...\n", i);
109     own wasm_trap_t* trap = wasm_func_call(func, NULL, NULL);
110     if (!trap) {
111       printf("> Error calling function, expected trap!\n");
112       return 1;
113     }
114 
115     printf("Printing message...\n");
116     own wasm_name_t message;
117     wasm_trap_message(trap, &message);
118     printf("> %s\n", message.data);
119 
120     printf("Printing origin...\n");
121     own wasm_frame_t* frame = wasm_trap_origin(trap);
122     if (frame) {
123       print_frame(frame);
124       wasm_frame_delete(frame);
125     } else {
126       printf("> Empty origin.\n");
127     }
128 
129     printf("Printing trace...\n");
130     own wasm_frame_vec_t trace;
131     wasm_trap_trace(trap, &trace);
132     if (trace.size > 0) {
133       for (size_t i = 0; i < trace.size; ++i) {
134         print_frame(trace.data[i]);
135       }
136     } else {
137       printf("> Empty trace.\n");
138     }
139 
140     wasm_frame_vec_delete(&trace);
141     wasm_trap_delete(trap);
142     wasm_name_delete(&message);
143   }
144 
145   wasm_extern_vec_delete(&exports);
146 
147   // Shut down.
148   printf("Shutting down...\n");
149   wasm_store_delete(store);
150   wasm_engine_delete(engine);
151 
152   // All done.
153   printf("Done.\n");
154   return 0;
155 }
156