aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/wamr/Makefile7
-rw-r--r--lib/wamr/custom/platform.c115
-rw-r--r--lib/wamr/custom/runtime.c73
-rw-r--r--lib/wamr/test/fibonacci.c27
-rw-r--r--lib/wamr/test/main.c47
-rw-r--r--lib/wamr/wamr.h9
6 files changed, 168 insertions, 110 deletions
diff --git a/lib/wamr/Makefile b/lib/wamr/Makefile
index ef26bb6..e65a812 100644
--- a/lib/wamr/Makefile
+++ b/lib/wamr/Makefile
@@ -25,7 +25,8 @@ SRC = core/iwasm/common/wasm_application.c \
core/shared/utils/bh_queue.c \
core/shared/utils/bh_vector.c \
core/shared/utils/runtime_timer.c \
- custom/platform.c
+ custom/platform.c \
+ custom/runtime.c
CFLAGS = -DBH_FREE=wasm_runtime_free \
-DBH_MALLOC=wasm_runtime_malloc \
@@ -42,6 +43,8 @@ CFLAGS = -DBH_FREE=wasm_runtime_free \
LIB = libwamr.a
OBJ = $(patsubst %.c,%.o,$(SRC))
+CLANG = clang
+
.PHONY: all
all: $(LIB) ;
@@ -63,4 +66,4 @@ test/test.exe: test/main.c $(LIB)
$(CC) -o $@ $< -I. -L. -lwamr
test/fibonacci.wasm: test/fibonacci.c
- clang -target wasm32 -nostdlib -Wl,--no-entry -Wl,--export=fibonacci -Wl,--allow-undefined -o $@ $<
+ $(CLANG) -target wasm32 -nostdlib -Wl,--no-entry -o $@ $<
diff --git a/lib/wamr/custom/platform.c b/lib/wamr/custom/platform.c
index 64250d0..1355f75 100644
--- a/lib/wamr/custom/platform.c
+++ b/lib/wamr/custom/platform.c
@@ -9,41 +9,27 @@
#endif
#ifdef BH_DEBUG
-#define NOT_IMPLEMENTED() \
- do { \
- 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(); \
+#define NOT_IMPLEMENTED() \
+ do { \
+ 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)
#else
#define NOT_IMPLEMENTED() abort()
#endif
-int bh_platform_init(void) {
- return 0;
-}
-
-void bh_platform_destroy(void) {
- return;
-}
-
-void *os_malloc(unsigned int size) {
- NOT_IMPLEMENTED();
-}
-
-void *os_realloc(void *ptr, unsigned int size) {
- NOT_IMPLEMENTED();
-}
-
-void os_free(void *ptr) {
- NOT_IMPLEMENTED();
-}
+int bh_platform_init(void) { return 0; }
+void bh_platform_destroy(void) { return; }
+void *os_malloc(unsigned int size) { NOT_IMPLEMENTED(); }
+void *os_realloc(void *ptr, unsigned int size) { NOT_IMPLEMENTED(); }
+void os_free(void *ptr) { NOT_IMPLEMENTED(); }
int os_printf(const char *format, ...) {
int ret;
@@ -68,7 +54,7 @@ uint64_t os_time_get_boot_us(void) {
#ifdef BH_DEBUG
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
- return 0;
+ return 0;
}
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
#else
@@ -76,31 +62,15 @@ uint64_t os_time_get_boot_us(void) {
#endif
}
-korp_tid os_self_thread(void) {
- return 0;
-}
-
-uint8_t *os_thread_get_stack_boundary(void) {
- return NULL;
-}
-
-int os_mutex_init(korp_mutex *mutex) {
- return 0;
-}
-
-int os_mutex_destroy(korp_mutex *mutex) {
- return 0;
-}
+korp_tid os_self_thread(void) { return 0; }
+uint8_t *os_thread_get_stack_boundary(void) { return NULL; }
+int os_mutex_init(korp_mutex *mutex) { return 0; }
+int os_mutex_destroy(korp_mutex *mutex) { return 0; }
+int os_mutex_lock(korp_mutex *mutex) { return 0; }
+int os_mutex_unlock(korp_mutex *mutex) { return 0; }
-int os_mutex_lock(korp_mutex *mutex) {
- return 0;
-}
-
-int os_mutex_unlock(korp_mutex *mutex) {
- return 0;
-}
-
-void *os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) {
+void *os_mmap(void *hint, size_t size, int prot, int flags,
+ os_file_handle file) {
bh_assert(file == os_get_invalid_handle());
if ((uint64_t)size > UINT32_MAX) {
@@ -114,38 +84,19 @@ void *os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
return buf;
}
-void os_munmap(void *addr, size_t size) {
- wasm_runtime_free(addr);
-}
-
-int os_mprotect(void *addr, size_t size, int prot) {
- return 0;
-}
+void os_munmap(void *addr, size_t size) { wasm_runtime_free(addr); }
+int os_mprotect(void *addr, size_t size, int prot) { return 0; }
void *os_mremap(void *old_addr, size_t old_size, size_t new_size) {
return os_mremap_slow(old_addr, old_size, new_size);
}
-int os_cond_init(korp_cond *cond) {
- NOT_IMPLEMENTED();
-}
-
-int os_cond_destroy(korp_cond *cond) {
- NOT_IMPLEMENTED();
-}
-
-int os_dumps_proc_mem_info(char *out, unsigned int size) {
- NOT_IMPLEMENTED();
-}
-
-os_file_handle os_get_invalid_handle(void) {
- return -1;
-}
-
-uint32_t os_getpagesize(void) {
- return 4096;
-}
+int os_cond_init(korp_cond *cond) { NOT_IMPLEMENTED(); }
+int os_cond_destroy(korp_cond *cond) { NOT_IMPLEMENTED(); }
+int os_dumps_proc_mem_info(char *out, unsigned int size) { NOT_IMPLEMENTED(); }
+os_file_handle os_get_invalid_handle(void) { return -1; }
+uint32_t os_getpagesize(void) { return 4096; }
-void invokeNative(void (*native_code)(), uint32_t argv[], uint32_t argc) {
+void invokeNative(void (*f)(), uint32_t argv[], uint32_t argc) {
NOT_IMPLEMENTED();
}
diff --git a/lib/wamr/custom/runtime.c b/lib/wamr/custom/runtime.c
new file mode 100644
index 0000000..84667e8
--- /dev/null
+++ b/lib/wamr/custom/runtime.c
@@ -0,0 +1,73 @@
+#include "../core/iwasm/common/wasm_runtime_common.h"
+#include "../core/iwasm/interpreter/wasm.h"
+#include "../core/shared/utils/bh_log.h"
+#include "../wamr.h"
+
+bool wasm_runtime_link_globals(wasm_module_t module, const char *module_name,
+ const NativeGlobal *globals,
+ uint32_t n_globals) {
+#if WASM_ENABLE_INTERP != 0
+ if (module->module_type == Wasm_Module_Bytecode) {
+ WASMModule *m = (WASMModule *)module;
+
+ struct {
+ WASMGlobalImport *g;
+ WASMValue v;
+ } linked[n_globals];
+ memset(linked, 0, sizeof linked);
+
+ for (uint32_t i = 0; i < m->import_global_count; i++) {
+ WASMGlobalImport *g = &m->import_globals[i].u.global;
+ if (strcmp(g->module_name, module_name) != 0) {
+ continue;
+ }
+
+ for (uint32_t j = 0; j < n_globals; j++) {
+ const NativeGlobal *n = &globals[j];
+ if (strcmp(g->field_name, n->symbol) != 0) {
+ continue;
+ }
+
+ uint8_t val_type;
+
+ linked[j].g = g;
+ switch (n->val.kind) {
+ case WASM_I32:
+ val_type = VALUE_TYPE_I32;
+ linked[j].v.i32 = n->val.of.i32;
+ break;
+ case WASM_I64:
+ val_type = VALUE_TYPE_I64;
+ linked[j].v.i64 = n->val.of.i64;
+ break;
+ case WASM_F32:
+ val_type = VALUE_TYPE_F32;
+ linked[j].v.f32 = n->val.of.f32;
+ break;
+ case WASM_F64:
+ val_type = VALUE_TYPE_F64;
+ linked[j].v.f64 = n->val.of.f64;
+ break;
+ default:
+ LOG_WARNING("unsupported native global kind %x", n->val.kind);
+ return false;
+ }
+
+ if (g->type.val_type != val_type) {
+ LOG_WARNING("global import type %x does not match native type %x",
+ g->type.val_type, val_type);
+ return false;
+ }
+ }
+ }
+
+ for (size_t i = 0; i < n_globals; i++) {
+ if (linked[i].g) {
+ linked[i].g->is_linked = true;
+ linked[i].g->global_data_linked = linked[i].v;
+ }
+ }
+ }
+#endif
+ return true;
+}
diff --git a/lib/wamr/test/fibonacci.c b/lib/wamr/test/fibonacci.c
index c511883..ddb5f9c 100644
--- a/lib/wamr/test/fibonacci.c
+++ b/lib/wamr/test/fibonacci.c
@@ -1,9 +1,28 @@
#include <stdint.h>
-extern void lognumber(int32_t n);
-extern int maxint;
+#define WASM_IMPORT(module, name) \
+ __attribute__((import_module(module), import_name(name)))
-extern int32_t fibonacci(int32_t i) {
+#define WASM_EXPORT(name) \
+ __attribute__((visibility("default"), export_name(name)))
+
+__asm__(".global maxint\n"
+ ".import_module maxint, env\n"
+ ".import_name maxint, maxint\n"
+ ".globaltype maxint, i32, immutable\n");
+static inline int getmaxint() {
+ int val;
+ __asm__("global.get maxint\n"
+ "local.set %0"
+ : "=r"(val));
+ return val;
+}
+
+WASM_IMPORT("env", "lognumber")
+void lognumber(int32_t n);
+
+WASM_EXPORT("fibonacci")
+int32_t fibonacci(int32_t i) {
if (i < 0) {
return -1;
}
@@ -17,7 +36,7 @@ extern int32_t fibonacci(int32_t i) {
result += last;
last = current;
- if (result > maxint) {
+ if (result > getmaxint()) {
// overflow
return -1;
}
diff --git a/lib/wamr/test/main.c b/lib/wamr/test/main.c
index 0172b14..5e27005 100644
--- a/lib/wamr/test/main.c
+++ b/lib/wamr/test/main.c
@@ -4,20 +4,19 @@
#include "wamr.h"
-#define P_CHECK(x, name) \
- if (!(x)) { \
- perror(name); \
- return EXIT_FAILURE; \
+#define P_CHECK(x, name) \
+ if (!(x)) { \
+ perror(name); \
+ return EXIT_FAILURE; \
}
-#define WAMR_CHECK(x, err) \
- if (!(x)) { \
- fprintf(stderr, "ERROR: %s\n", err); \
- return EXIT_FAILURE; \
+#define WAMR_CHECK(x, err) \
+ if (!(x)) { \
+ fprintf(stderr, "ERROR: %s\n", err); \
+ return EXIT_FAILURE; \
}
-#define LEN_OF(x) \
- ((sizeof (x)) / (sizeof (x)[0]))
+#define LEN_OF(x) ((sizeof(x)) / (sizeof(x)[0]))
static void lognumber(wasm_exec_env_t exec, uint64_t *args) {
native_raw_get_arg(int32_t, n, args);
@@ -40,29 +39,33 @@ int main(int argc, char *argv[argc + 1]) {
uint8_t heap[16 * 1024 * 1024];
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,
+ .mem_alloc_type = Alloc_With_Pool,
+ .mem_alloc_option.pool.heap_buf = heap,
+ .mem_alloc_option.pool.heap_size = sizeof heap,
};
wasm_runtime_full_init(&init);
struct NativeSymbol env[] = {
- {"lognumber", lognumber, "(i)"},
+ {"lognumber", lognumber, "(i)"},
};
- WAMR_CHECK(wasm_runtime_register_natives_raw("env", env, LEN_OF(env)),
- "failed to register natives");
+ ok = wasm_runtime_register_natives_raw("env", env, LEN_OF(env));
+ WAMR_CHECK(ok, "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, 0, err, sizeof err);
+ struct NativeGlobal genv[] = {
+ {"maxint", {WASM_I32, .of = {.i32 = INT32_MAX}}},
+ };
+ ok = wasm_runtime_link_globals(module, "env", genv, LEN_OF(genv));
+ WAMR_CHECK(ok, "failed to link globals");
+
+ 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");
+ 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);
diff --git a/lib/wamr/wamr.h b/lib/wamr/wamr.h
index 6c0236e..2ecc388 100644
--- a/lib/wamr/wamr.h
+++ b/lib/wamr/wamr.h
@@ -3,4 +3,13 @@
#include "./core/iwasm/include/wasm_export.h"
+typedef struct NativeGlobal {
+ char *symbol;
+ wasm_val_t val;
+} NativeGlobal;
+
+WASM_RUNTIME_API_EXTERN bool
+wasm_runtime_link_globals(const wasm_module_t module, const char *module_name,
+ const NativeGlobal *globals, uint32_t n_globals);
+
#endif