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(store, &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 error = wasmtime_instance_new(module, NULL, 0, &instance, &trap); 75 if (error != NULL || trap != NULL) 76 exit_with_error("failed to instantiate", error, trap); 77 wasm_module_delete(module); 78 79 // Extract export. 80 printf("Extracting export...\n"); 81 own wasm_extern_vec_t exports; 82 wasm_instance_exports(instance, &exports); 83 if (exports.size == 0) { 84 printf("> Error accessing exports!\n"); 85 return 1; 86 } 87 // Getting second export (first is memory). 88 wasm_func_t* run_func = wasm_extern_as_func(exports.data[fib_idx]); 89 if (run_func == NULL) { 90 printf("> Error accessing export!\n"); 91 return 1; 92 } 93 94 wasm_instance_delete(instance); 95 96 // Call. 97 printf("Calling fib...\n"); 98 wasm_val_t params[1] = { {.kind = WASM_I32, .of = {.i32 = 6}} }; 99 wasm_val_t results[1]; 100 error = wasmtime_func_call(run_func, params, 1, results, 1, &trap); 101 if (error != NULL || trap != NULL) 102 exit_with_error("failed to call function", error, trap); 103 104 wasm_extern_vec_delete(&exports); 105 106 printf("> fib(6) = %d\n", results[0].of.i32); 107 108 // Shut down. 109 printf("Shutting down...\n"); 110 wasm_store_delete(store); 111 wasm_engine_delete(engine); 112 113 // All done. 114 printf("Done.\n"); 115 return 0; 116 } 117 118 static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) { 119 fprintf(stderr, "error: %s\n", message); 120 wasm_byte_vec_t error_message; 121 if (error != NULL) { 122 wasmtime_error_message(error, &error_message); 123 } else { 124 wasm_trap_message(trap, &error_message); 125 } 126 fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data); 127 wasm_byte_vec_delete(&error_message); 128 exit(1); 129 } 130