Add Wasmtime-specific C API functions to return errors (#1467)

* Add Wasmtime-specific C API functions to return errors

This commit adds new `wasmtime_*` symbols to the C API, many of which
mirror the existing counterparts in the `wasm.h` header. These APIs are
enhanced in a number of respects:

* Detailed error information is now available through a
  `wasmtime_error_t`. Currently this only exposes one function which is
  to extract a string version of the error.

* There is a distinction now between traps and errors during
  instantiation and function calling. Traps only happen if wasm traps,
  and errors can happen for things like runtime type errors when
  interacting with the API.

* APIs have improved safety with respect to embedders where the lengths
  of arrays are now taken as explicit parameters rather than assumed
  from other parameters.

* Handle trap updates

* Update C examples

* Fix memory.c compile on MSVC

* Update test assertions

* Refactor C slightly

* Bare-bones .NET update

* Remove bogus nul handling
This commit is contained in:
Alex Crichton
2020-04-06 15:13:06 -05:00
committed by GitHub
parent 78c548dc8f
commit bd374fd6fc
30 changed files with 817 additions and 403 deletions

View File

@@ -8,6 +8,8 @@
#define own
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
int main(int argc, const char* argv[]) {
// Configuring engine to support generating of DWARF info.
// lldb can be used to attach to the program and observe
@@ -40,11 +42,10 @@ int main(int argc, const char* argv[]) {
// Compile.
printf("Compiling module...\n");
own wasm_module_t* module = wasm_module_new(store, &binary);
if (!module) {
printf("> Error compiling module!\n");
return 1;
}
wasm_module_t *module = NULL;
wasmtime_error_t* error = wasmtime_module_new(store, &binary, &module);
if (!module)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&binary);
// Figure out which export is the `fib` export
@@ -68,12 +69,12 @@ int main(int argc, const char* argv[]) {
// Instantiate.
printf("Instantiating module...\n");
own wasm_instance_t* instance =
wasm_instance_new(store, module, NULL, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
}
wasm_instance_t* instance = NULL;
wasm_trap_t *trap = NULL;
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate", error, trap);
wasm_module_delete(module);
// Extract export.
printf("Extracting export...\n");
@@ -84,23 +85,21 @@ int main(int argc, const char* argv[]) {
return 1;
}
// Getting second export (first is memory).
const wasm_func_t* run_func = wasm_extern_as_func(exports.data[fib_idx]);
wasm_func_t* run_func = wasm_extern_as_func(exports.data[fib_idx]);
if (run_func == NULL) {
printf("> Error accessing export!\n");
return 1;
}
wasm_module_delete(module);
wasm_instance_delete(instance);
// Call.
printf("Calling fib...\n");
wasm_val_t params[1] = { {.kind = WASM_I32, .of = {.i32 = 6}} };
wasm_val_t results[1];
if (wasm_func_call(run_func, params, results)) {
printf("> Error calling function!\n");
return 1;
}
error = wasmtime_func_call(run_func, params, 1, results, 1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
wasm_extern_vec_delete(&exports);
@@ -116,3 +115,15 @@ int main(int argc, const char* argv[]) {
return 0;
}
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
} else {
wasm_trap_message(trap, &error_message);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -23,7 +23,7 @@ to tweak the `-lpthread` and such annotations.
#include <wasm.h>
#include <wasmtime.h>
static void print_trap(wasm_trap_t *trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
int main() {
int ret = 0;
@@ -51,23 +51,23 @@ int main() {
fclose(file);
// Parse the wat into the binary wasm format
wasm_byte_vec_t wasm, error;
if (wasmtime_wat2wasm(&wat, &wasm, &error) == 0) {
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
goto free_store;
}
wasm_byte_vec_t wasm;
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
// Compile and instantiate our module
wasm_module_t *module = wasm_module_new(store, &wasm);
wasm_module_t *module = NULL;
error = wasmtime_module_new(store, &wasm, &module);
if (module == NULL)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&wasm);
assert(module != NULL);
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = wasm_instance_new(store, module, NULL, &trap);
if (instance == NULL) {
print_trap(trap);
goto free_module;
}
wasm_instance_t *instance = NULL;
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
if (instance == NULL)
exit_with_error("failed to instantiate", error, trap);
// Lookup our `gcd` export function
wasm_extern_vec_t externs;
@@ -85,11 +85,9 @@ int main() {
params[0].of.i32 = a;
params[1].kind = WASM_I32;
params[1].of.i32 = b;
trap = wasm_func_call(gcd, params, results);
if (trap != NULL) {
print_trap(trap);
goto free_instance;
}
error = wasmtime_func_call(gcd, params, 2, results, 1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call gcd", error, trap);
assert(results[0].kind == WASM_I32);
printf("gcd(%d, %d) = %d\n", a, b, results[0].of.i32);
@@ -97,22 +95,23 @@ int main() {
// Clean up after ourselves at this point
ret = 0;
free_instance:
wasm_extern_vec_delete(&externs);
wasm_instance_delete(instance);
free_module:
wasm_module_delete(module);
free_store:
wasm_store_delete(store);
wasm_engine_delete(engine);
return ret;
}
static void print_trap(wasm_trap_t *trap) {
assert(trap != NULL);
wasm_message_t message;
wasm_trap_message(trap, &message);
fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data);
wasm_byte_vec_delete(&message);
wasm_trap_delete(trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
} else {
wasm_trap_message(trap, &error_message);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -24,7 +24,7 @@ to tweak the `-lpthread` and such annotations as well as the name of the
#include <wasm.h>
#include <wasmtime.h>
static void print_trap(wasm_trap_t *trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
static wasm_trap_t* hello_callback(const wasm_val_t args[], wasm_val_t results[]) {
printf("Calling back...\n");
@@ -58,18 +58,19 @@ int main() {
fclose(file);
// Parse the wat into the binary wasm format
wasm_byte_vec_t wasm, error;
if (wasmtime_wat2wasm(&wat, &wasm, &error) == 0) {
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
goto free_store;
}
wasm_byte_vec_t wasm;
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &wasm);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
// Now that we've got our binary webassembly we can compile our module.
printf("Compiling module...\n");
wasm_module_t *module = wasm_module_new(store, &wasm);
wasm_module_t *module = NULL;
error = wasmtime_module_new(store, &wasm, &module);
wasm_byte_vec_delete(&wasm);
assert(module != NULL);
if (error != NULL)
exit_with_error("failed to compile module", error, NULL);
// Next up we need to create the function that the wasm module imports. Here
// we'll be hooking up a thunk function to the `hello_callback` native
@@ -84,12 +85,11 @@ int main() {
// to handle that here too.
printf("Instantiating module...\n");
wasm_trap_t *trap = NULL;
wasm_instance_t *instance = NULL;
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
wasm_instance_t *instance = wasm_instance_new(store, module, imports, &trap);
if (instance == NULL) {
print_trap(trap);
goto free_module;
}
error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
if (instance == NULL)
exit_with_error("failed to instantiate", error, trap);
// Lookup our `run` export function
printf("Extracting export...\n");
@@ -101,33 +101,33 @@ int main() {
// And call it!
printf("Calling export...\n");
trap = wasm_func_call(run, NULL, NULL);
if (trap != NULL) {
print_trap(trap);
goto free_instance;
}
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
// Clean up after ourselves at this point
printf("All finished!\n");
ret = 0;
free_instance:
wasm_extern_vec_delete(&externs);
wasm_instance_delete(instance);
free_module:
wasm_module_delete(module);
free_store:
wasm_store_delete(store);
wasm_engine_delete(engine);
return ret;
}
static void print_trap(wasm_trap_t *trap) {
assert(trap != NULL);
wasm_message_t message;
wasm_trap_message(trap, &message);
fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data);
wasm_byte_vec_delete(&message);
wasm_trap_delete(trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -26,7 +26,7 @@ to tweak the `-lpthread` and such annotations.
#define MIN(a, b) ((a) < (b) ? (a) : (b))
static void print_trap(wasm_trap_t *trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes, const char *file);
int main() {
@@ -42,10 +42,15 @@ int main() {
read_wat_file(engine, &linking2_wasm, "examples/linking2.wat");
// Compile our two modules
wasm_module_t *linking1_module = wasm_module_new(store, &linking1_wasm);
assert(linking1_module != NULL);
wasm_module_t *linking2_module = wasm_module_new(store, &linking2_wasm);
assert(linking2_module != NULL);
wasmtime_error_t *error;
wasm_module_t *linking1_module = NULL;
wasm_module_t *linking2_module = NULL;
error = wasmtime_module_new(store, &linking1_wasm, &linking1_module);
if (error != NULL)
exit_with_error("failed to compile linking1", error, NULL);
error = wasmtime_module_new(store, &linking2_wasm, &linking2_module);
if (error != NULL)
exit_with_error("failed to compile linking2", error, NULL);
wasm_byte_vec_delete(&linking1_wasm);
wasm_byte_vec_delete(&linking2_wasm);
@@ -59,45 +64,35 @@ int main() {
wasi_config_inherit_stderr(wasi_config);
wasm_trap_t *trap = NULL;
wasi_instance_t *wasi = wasi_instance_new(store, "wasi_snapshot_preview1", wasi_config, &trap);
if (wasi == NULL) {
print_trap(trap);
exit(1);
}
if (wasi == NULL)
exit_with_error("failed to instantiate wasi", NULL, trap);
// Create our linker which will be linking our modules together, and then add
// our WASI instance to it.
wasmtime_linker_t *linker = wasmtime_linker_new(store);
bool ok = wasmtime_linker_define_wasi(linker, wasi);
assert(ok);
error = wasmtime_linker_define_wasi(linker, wasi);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
// Instantiate `linking2` with our linker.
wasm_instance_t *linking2 = wasmtime_linker_instantiate(linker, linking2_module, &trap);
if (linking2 == NULL) {
if (trap == NULL) {
printf("> failed to link!\n");
} else {
print_trap(trap);
}
exit(1);
}
wasm_instance_t *linking2;
error = wasmtime_linker_instantiate(linker, linking2_module, &linking2, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate linking2", error, trap);
// Register our new `linking2` instance with the linker
wasm_name_t linking2_name;
linking2_name.data = "linking2";
linking2_name.size = strlen(linking2_name.data);
ok = wasmtime_linker_define_instance(linker, &linking2_name, linking2);
assert(ok);
error = wasmtime_linker_define_instance(linker, &linking2_name, linking2);
if (error != NULL)
exit_with_error("failed to link linking2", error, NULL);
// Instantiate `linking1` with the linker now that `linking2` is defined
wasm_instance_t *linking1 = wasmtime_linker_instantiate(linker, linking1_module, &trap);
if (linking1 == NULL) {
if (trap == NULL) {
printf("> failed to link!\n");
} else {
print_trap(trap);
}
exit(1);
}
wasm_instance_t *linking1;
error = wasmtime_linker_instantiate(linker, linking1_module, &linking1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to instantiate linking1", error, trap);
// Lookup our `run` export function
wasm_extern_vec_t linking1_externs;
@@ -105,11 +100,9 @@ int main() {
assert(linking1_externs.size == 1);
wasm_func_t *run = wasm_extern_as_func(linking1_externs.data[0]);
assert(run != NULL);
trap = wasm_func_call(run, NULL, NULL);
if (trap != NULL) {
print_trap(trap);
exit(1);
}
error = wasmtime_func_call(run, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call run", error, trap);
// Clean up after ourselves at this point
wasm_extern_vec_delete(&linking1_externs);
@@ -146,19 +139,23 @@ static void read_wat_file(
fclose(file);
// Parse the wat into the binary wasm format
wasm_byte_vec_t error;
if (wasmtime_wat2wasm(&wat, bytes, &error) == 0) {
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
exit(1);
}
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, bytes);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
}
static void print_trap(wasm_trap_t *trap) {
assert(trap != NULL);
wasm_message_t message;
wasm_trap_message(trap, &message);
fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data);
wasm_byte_vec_delete(&message);
wasm_trap_delete(trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -28,6 +28,7 @@ originally
#include <wasm.h>
#include <wasmtime.h>
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
wasm_memory_t* get_export_memory(const wasm_extern_vec_t* exports, size_t i) {
if (exports->size <= i || !wasm_extern_as_memory(exports->data[i])) {
@@ -53,21 +54,25 @@ void check(bool success) {
}
}
void check_call(wasm_func_t* func, wasm_val_t args[], int32_t expected) {
void check_call(wasm_func_t* func, wasm_val_t args[], size_t num_args, int32_t expected) {
wasm_val_t results[1];
if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) {
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, 1, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
if (results[0].of.i32 != expected) {
printf("> Error on result\n");
exit(1);
}
}
void check_call0(wasm_func_t* func, int32_t expected) {
check_call(func, NULL, expected);
check_call(func, NULL, 0, expected);
}
void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
wasm_val_t args[] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
check_call(func, args, expected);
check_call(func, args, 1, expected);
}
void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
@@ -75,14 +80,14 @@ void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_call(func, args, expected);
check_call(func, args, 2, expected);
}
void check_ok(wasm_func_t* func, wasm_val_t args[]) {
if (wasm_func_call(func, args, NULL)) {
printf("> Error on result, expected empty\n");
exit(1);
}
void check_ok(wasm_func_t* func, wasm_val_t args[], size_t num_args) {
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call function", error, trap);
}
void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
@@ -90,13 +95,17 @@ void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_ok(func, args);
check_ok(func, args, 2);
}
void check_trap(wasm_func_t* func, wasm_val_t args[]) {
void check_trap(wasm_func_t* func, wasm_val_t args[], size_t num_args, size_t num_results) {
wasm_val_t results[1];
wasm_trap_t* trap = wasm_func_call(func, args, results);
if (! trap) {
assert(num_results <= 1);
wasm_trap_t *trap = NULL;
wasmtime_error_t *error = wasmtime_func_call(func, args, num_args, results, num_results, &trap);
if (error != NULL)
exit_with_error("failed to call function", error, NULL);
if (trap == NULL) {
printf("> Error on result, expected trap\n");
exit(1);
}
@@ -105,7 +114,7 @@ void check_trap(wasm_func_t* func, wasm_val_t args[]) {
void check_trap1(wasm_func_t* func, int32_t arg) {
wasm_val_t args[1] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
check_trap(func, args);
check_trap(func, args, 1, 1);
}
void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
@@ -113,7 +122,7 @@ void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
{.kind = WASM_I32, .of = {.i32 = arg1}},
{.kind = WASM_I32, .of = {.i32 = arg2}}
};
check_trap(func, args);
check_trap(func, args, 2, 0);
}
int main(int argc, const char* argv[]) {
@@ -140,30 +149,27 @@ int main(int argc, const char* argv[]) {
fclose(file);
// Parse the wat into the binary wasm format
wasm_byte_vec_t binary, error;
if (wasmtime_wat2wasm(&wat, &binary, &error) == 0) {
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
return 1;
}
wasm_byte_vec_t binary;
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
// Compile.
printf("Compiling module...\n");
wasm_module_t* module = wasm_module_new(store, &binary);
if (!module) {
printf("> Error compiling module!\n");
return 1;
}
wasm_module_t* module = NULL;
error = wasmtime_module_new(store, &binary, &module);
if (error)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&binary);
// Instantiate.
printf("Instantiating module...\n");
wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
}
wasm_instance_t* instance = NULL;
wasm_trap_t *trap = NULL;
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
if (!instance)
exit_with_error("failed to instantiate", error, trap);
// Extract export.
printf("Extracting exports...\n");
@@ -246,3 +252,18 @@ int main(int argc, const char* argv[]) {
printf("Done.\n");
return 0;
}
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -28,6 +28,8 @@ originally
#include <wasm.h>
#include <wasmtime.h>
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
// A function to be called from Wasm code.
wasm_trap_t* callback(
const wasm_val_t args[], wasm_val_t results[]
@@ -83,20 +85,18 @@ int main(int argc, const char* argv[]) {
fclose(file);
// Parse the wat into the binary wasm format
wasm_byte_vec_t binary, error;
if (wasmtime_wat2wasm(&wat, &binary, &error) == 0) {
fprintf(stderr, "failed to parse wat %.*s\n", (int) error.size, error.data);
return 1;
}
wasm_byte_vec_t binary;
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
if (error != NULL)
exit_with_error("failed to parse wat", error, NULL);
wasm_byte_vec_delete(&wat);
// Compile.
printf("Compiling module...\n");
wasm_module_t* module = wasm_module_new(store, &binary);
if (!module) {
printf("> Error compiling module!\n");
return 1;
}
wasm_module_t* module = NULL;
error = wasmtime_module_new(store, &binary, &module);
if (error)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&binary);
@@ -116,12 +116,11 @@ int main(int argc, const char* argv[]) {
// Instantiate.
printf("Instantiating module...\n");
const wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
wasm_instance_t* instance =
wasm_instance_new(store, module, imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
}
wasm_instance_t* instance = NULL;
wasm_trap_t* trap = NULL;
error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
if (!instance)
exit_with_error("failed to instantiate", error, trap);
wasm_func_delete(callback_func);
@@ -133,7 +132,7 @@ int main(int argc, const char* argv[]) {
printf("> Error accessing exports!\n");
return 1;
}
const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
if (run_func == NULL) {
printf("> Error accessing export!\n");
return 1;
@@ -144,16 +143,15 @@ int main(int argc, const char* argv[]) {
// Call.
printf("Calling export...\n");
wasm_val_t args[4];
wasm_val_t args[2];
args[0].kind = WASM_I32;
args[0].of.i32 = 1;
args[1].kind = WASM_I64;
args[1].of.i64 = 2;
wasm_val_t results[2];
if (wasm_func_call(run_func, args, results)) {
printf("> Error calling function!\n");
return 1;
}
error = wasmtime_func_call(run_func, args, 2, results, 2, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call run", error, trap);
wasm_extern_vec_delete(&exports);
@@ -177,3 +175,17 @@ int main(int argc, const char* argv[]) {
return 0;
}
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

View File

@@ -21,11 +21,11 @@ to tweak the `-lpthread` and such annotations.
#include <stdlib.h>
#include <wasm.h>
#include <wasi.h>
#include <wasmtime.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
static void print_trap(wasm_trap_t *trap);
static void read_wat_file(wasm_engine_t *engine, wasm_byte_vec_t *bytes, const char *file);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
int main() {
int ret = 0;
@@ -35,7 +35,6 @@ int main() {
wasm_store_t *store = wasm_store_new(engine);
assert(store != NULL);
wasm_byte_vec_t wasm;
// Load our input file to parse it next
FILE* file = fopen("target/wasm32-wasi/debug/wasi.wasm", "rb");
@@ -54,8 +53,10 @@ int main() {
fclose(file);
// Compile our modules
wasm_module_t *module = wasm_module_new(store, &wasm);
assert(module != NULL);
wasm_module_t *module = NULL;
wasmtime_error_t *error = wasmtime_module_new(store, &wasm, &module);
if (!module)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&wasm);
// Instantiate wasi
@@ -68,10 +69,8 @@ int main() {
wasi_config_inherit_stderr(wasi_config);
wasm_trap_t *trap = NULL;
wasi_instance_t *wasi = wasi_instance_new(store, "wasi_snapshot_preview1", wasi_config, &trap);
if (wasi == NULL) {
print_trap(trap);
exit(1);
}
if (wasi == NULL)
exit_with_error("failed to instantiate WASI", NULL, trap);
// Create import list for our module using wasi
wasm_importtype_vec_t import_types;
@@ -87,15 +86,14 @@ int main() {
exit(1);
}
}
wasm_importtype_vec_delete(&import_types);
// Instantiate the module
wasm_instance_t *instance = wasm_instance_new(store, module, imports, &trap);
if (instance == NULL) {
print_trap(trap);
exit(1);
}
wasm_instance_t *instance = NULL;
error = wasmtime_instance_new(module, imports, import_types.size, &instance, &trap);
if (instance == NULL)
exit_with_error("failed to instantiate", error, trap);
free(imports);
wasm_importtype_vec_delete(&import_types);
// Lookup our `_start` export function
wasm_extern_vec_t externs;
@@ -111,11 +109,9 @@ int main() {
assert(start_extern);
wasm_func_t *start = wasm_extern_as_func(start_extern);
assert(start != NULL);
trap = wasm_func_call(start, NULL, NULL);
if (trap != NULL) {
print_trap(trap);
exit(1);
}
error = wasmtime_func_call(start, NULL, 0, NULL, 0, &trap);
if (error != NULL || trap != NULL)
exit_with_error("failed to call `_start`", error, trap);
// Clean up after ourselves at this point
wasm_exporttype_vec_delete(&exports);
@@ -127,12 +123,17 @@ int main() {
return 0;
}
static void print_trap(wasm_trap_t *trap) {
assert(trap != NULL);
wasm_message_t message;
wasm_trap_message(trap, &message);
fprintf(stderr, "failed to instantiate module %.*s\n", (int) message.size, message.data);
wasm_byte_vec_delete(&message);
wasm_trap_delete(trap);
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}