diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-20 06:22:23 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-04-20 06:22:23 +0200 |
| commit | 4a23ae3522503fe9497ea8736bf5e8832b959951 (patch) | |
| tree | 01587ec40cb5507a8f3c56c709ba37939bb05485 /lib | |
| parent | 9c6c09994bb8605e23782f4267f6e187a492e028 (diff) | |
Fibonacci Executes!
However, it looks like the native function we install isn't getting
called correctly for whatever reason. I wonder if it is related to the
fact that we are using the `invokeNative_general` implementation
instead of the dedicated `aarch64` one?
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/wamr/core/shared/platform/stub/stubs.c | 83 | ||||
| -rw-r--r-- | lib/wamr/test/main.c | 54 |
2 files changed, 100 insertions, 37 deletions
diff --git a/lib/wamr/core/shared/platform/stub/stubs.c b/lib/wamr/core/shared/platform/stub/stubs.c index 59e560b..f1ea4f2 100644 --- a/lib/wamr/core/shared/platform/stub/stubs.c +++ b/lib/wamr/core/shared/platform/stub/stubs.c @@ -1,7 +1,10 @@ +#include "bh_assert.h" #include "platform_api_extension.h" #include "platform_api_vmcore.h" #include "platform_internal.h" +#include <execinfo.h> + int bh_platform_init(void) { return 0; } @@ -10,96 +13,130 @@ void bh_platform_destroy(void) { return; } -#define NOT_IMPLEMENTED(n) \ +#define NOT_IMPLEMENTED() \ do { \ - fprintf(stderr, "'%s' is not implemented\n", n); \ + fprintf(stderr, "'%s' is not implemented\n", __FUNCTION__); \ + void *callstack[128]; \ + int frames = backtrace(callstack, 128); \ + char **strs = backtrace_symbols(callstack, frames); \ + for (int i = 0; i < frames; ++i) { \ + fprintf(stderr, " %s\n", strs[i]); \ + } \ + free(strs); \ abort(); \ } while (0) void *os_malloc(unsigned int size) { - NOT_IMPLEMENTED("os_malloc"); + NOT_IMPLEMENTED(); } void *os_realloc(void *ptr, unsigned int size) { - NOT_IMPLEMENTED("os_realloc"); + NOT_IMPLEMENTED(); } void os_free(void *ptr) { - NOT_IMPLEMENTED("os_free"); + NOT_IMPLEMENTED(); } int os_printf(const char *format, ...) { - NOT_IMPLEMENTED("os_printf"); + int ret; + va_list ap; + + va_start(ap, format); + ret = os_vprintf(format, ap); + va_end(ap); + + return ret; } int os_vprintf(const char *format, va_list ap) { - NOT_IMPLEMENTED("os_vprintf"); + return vprintf(format, ap); } uint64_t os_time_get_boot_us(void) { - NOT_IMPLEMENTED("os_time_get_boot_us"); + return 0; + // TODO: NOT_IMPLEMENTED(); } korp_tid os_self_thread(void) { - NOT_IMPLEMENTED("os_self_thread"); + return 0; + // TODO: NOT_IMPLEMENTED(); } uint8_t *os_thread_get_stack_boundary(void) { - NOT_IMPLEMENTED("os_thread_get_stack_boundary"); + return NULL; + // TODO: NOT_IMPLEMENTED(); } int os_mutex_init(korp_mutex *mutex) { return 0; - // TODO: NOT_IMPLEMENTED("os_mutex_init"); + // TODO: NOT_IMPLEMENTED(); } int os_mutex_destroy(korp_mutex *mutex) { return 0; - // TODO: NOT_IMPLEMENTED("os_mutex_destroy"); + // TODO: NOT_IMPLEMENTED(); } int os_mutex_lock(korp_mutex *mutex) { return 0; - // TODO: NOT_IMPLEMENTED("os_mutex_lock"); + // TODO: NOT_IMPLEMENTED(); } int os_mutex_unlock(korp_mutex *mutex) { return 0; - // TODO: NOT_IMPLEMENTED("os_mutex_unlock"); + // TODO: NOT_IMPLEMENTED(); } void *os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) { - NOT_IMPLEMENTED("os_mmap"); + if (prot != 0 || flags != 0 || file != os_get_invalid_handle()) { + NOT_IMPLEMENTED(); + } + + if ((uint64_t)size > UINT32_MAX) { + return NULL; + } + + void *buf = wasm_runtime_malloc((unsigned int)size); + if (buf) { + memset(buf, 0, size); + } + return buf; } void os_munmap(void *addr, size_t size) { - NOT_IMPLEMENTED("os_munmap"); + NOT_IMPLEMENTED(); } int os_mprotect(void *addr, size_t size, int prot) { - NOT_IMPLEMENTED("os_mprotect"); + if (prot != (MMAP_PROT_READ | MMAP_PROT_WRITE)) { + NOT_IMPLEMENTED(); + } + return 0; } void *os_mremap(void *old_addr, size_t old_size, size_t new_size) { - NOT_IMPLEMENTED("os_mremap"); + NOT_IMPLEMENTED(); } int os_cond_init(korp_cond *cond) { - NOT_IMPLEMENTED("os_cond_init"); + NOT_IMPLEMENTED(); } int os_cond_destroy(korp_cond *cond) { - NOT_IMPLEMENTED("os_cond_destroy"); + NOT_IMPLEMENTED(); } int os_dumps_proc_mem_info(char *out, unsigned int size) { - NOT_IMPLEMENTED("os_dumps_proc_mem_info"); + NOT_IMPLEMENTED(); } os_file_handle os_get_invalid_handle(void) { - NOT_IMPLEMENTED("os_get_invalid_handle"); + return -1; + // TODO: NOT_IMPLEMENTED(); } uint32_t os_getpagesize(void) { - NOT_IMPLEMENTED("os_getpagesize"); + return 4096; + // TODO: NOT_IMPLEMENTED(); } diff --git a/lib/wamr/test/main.c b/lib/wamr/test/main.c index bfec8a0..1fe6686 100644 --- a/lib/wamr/test/main.c +++ b/lib/wamr/test/main.c @@ -16,6 +16,13 @@ return EXIT_FAILURE; \ } +#define LEN_OF(x) \ + ((sizeof (x)) / (sizeof (x)[0])) + +static void lognumber(wasm_exec_env_t exec, int32_t n) { + printf("-- %d\n", n); +} + int main(int argc, char *argv[argc + 1]) { uint8_t wasm[1024]; size_t wasmsz; @@ -27,28 +34,47 @@ int main(int argc, char *argv[argc + 1]) { P_CHECK(fclose(f) == 0, "fclose"); } + bool ok; char err[1024]; uint8_t heap[16 * 1024 * 1024]; - struct RuntimeInitArgs init = {0}; - { - init.mem_alloc_type = Alloc_With_Pool; - init.mem_alloc_option.pool.heap_buf = heap; - init.mem_alloc_option.pool.heap_size = sizeof heap; - init.native_module_name = "fibonacci.wasm"; - init.native_symbols = NULL; - init.n_native_symbols = 0; - init.running_mode = Mode_Interp; - } - + struct RuntimeInitArgs init = { + .mem_alloc_type = Alloc_With_Pool, + .mem_alloc_option.pool.heap_buf = heap, + .mem_alloc_option.pool.heap_size = sizeof heap, + .native_module_name = "fibonacci.wasm", + .native_symbols = NULL, + .n_native_symbols = 0, + .running_mode = Mode_Interp, + }; wasm_runtime_full_init(&init); - wasm_module_t module = wasm_runtime_load(wasm, sizeof wasm, err, sizeof err); + struct NativeSymbol env[] = { + {"lognumber", lognumber, "(i)"}, + }; + WAMR_CHECK(wasm_runtime_register_natives("env", env, LEN_OF(env)), + "failed to register natives"); + + wasm_module_t module = wasm_runtime_load(wasm, wasmsz, err, sizeof err); WAMR_CHECK(module, err); - wasm_module_inst_t instance = - wasm_runtime_instantiate(module, 8092, 8092, err, sizeof err); + wasm_module_inst_t instance = wasm_runtime_instantiate(module, 8092, 0, err, sizeof err); WAMR_CHECK(instance, err); + wasm_function_inst_t function = wasm_runtime_lookup_function(instance, "fibonacci"); + WAMR_CHECK(function, "could not find function"); + + wasm_exec_env_t exec = wasm_runtime_create_exec_env(instance, 1024); + WAMR_CHECK(exec, "failed to create execution environment"); + + int32_t result; + { + uint32_t args[] = {10}; + WAMR_CHECK(wasm_runtime_call_wasm(exec, function, LEN_OF(args), args), + "failed to call 'fibonacci' function"); + result = args[0]; + } + printf("=> %d\n", result); + return EXIT_SUCCESS; } |
