From 5875a37f341df74bcab9317122c6870329d67082 Mon Sep 17 00:00:00 2001 From: wenyongh Date: Wed, 18 Dec 2019 11:53:43 +0800 Subject: [PATCH] Remove iwasm/runtime/platform src files, refine interpreter and fix issue of --repl option (#150) --- .../lib/native/libc/libc_builtin_wrapper.c | 5 +- .../sandboxed-system-primitives/src/locking.h | 2 +- .../sandboxed-system-primitives/src/posix.c | 2 +- .../sandboxed-system-primitives/src/random.c | 2 +- .../src/{config.h => ssp_config.h} | 4 +- .../sandboxed-system-primitives/src/str.c | 2 +- .../sandboxed-system-primitives/src/str.h | 2 +- core/iwasm/products/alios-things/iwasm.mk | 1 - core/iwasm/products/darwin/CMakeLists.txt | 1 - core/iwasm/products/darwin/main.c | 6 +- core/iwasm/products/linux-sgx/CMakeLists.txt | 1 - core/iwasm/products/linux/CMakeLists.txt | 1 - core/iwasm/products/linux/main.c | 6 +- core/iwasm/products/vxworks/CMakeLists.txt | 1 - core/iwasm/products/vxworks/main.c | 6 +- .../products/zephyr/simple/CMakeLists.txt | 3 +- .../runtime/platform/alios/wasm_native.c | 15 - .../runtime/platform/darwin/platform.cmake | 14 - .../runtime/platform/darwin/wasm_native.c | 14 - .../runtime/platform/linux-sgx/platform.cmake | 14 - .../runtime/platform/linux-sgx/wasm_native.c | 15 - .../runtime/platform/linux/platform.cmake | 14 - .../runtime/platform/linux/wasm_native.c | 365 ------------------ .../runtime/platform/vxworks/platform.cmake | 14 - .../runtime/platform/vxworks/wasm_native.c | 15 - .../runtime/platform/zephyr/platform.cmake | 14 - .../runtime/platform/zephyr/wasm_native.c | 15 - core/iwasm/runtime/vmcore-wasm/wasm.h | 2 - .../runtime/vmcore-wasm/wasm_application.c | 21 +- core/iwasm/runtime/vmcore-wasm/wasm_interp.c | 26 +- core/iwasm/runtime/vmcore-wasm/wasm_loader.c | 8 +- core/iwasm/runtime/vmcore-wasm/wasm_native.h | 3 - core/iwasm/runtime/vmcore-wasm/wasm_runtime.c | 80 +--- core/iwasm/runtime/vmcore-wasm/wasm_runtime.h | 13 +- core/shared-lib/include/config.h | 1 + core/shared-lib/platform/alios/bh_platform.h | 1 + core/shared-lib/platform/zephyr/bh_platform.h | 5 + .../platform/zephyr/bh_platform_log.c | 2 +- .../linux-build/CMakeLists.txt | 2 +- .../zephyr-build/CMakeLists.txt | 2 +- .../littlevgl/vgl-wasm-runtime/CMakeLists.txt | 6 +- .../zephyr-build/CMakeLists.txt | 2 +- samples/simple/CMakeLists.txt | 2 +- 43 files changed, 84 insertions(+), 646 deletions(-) rename core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/{config.h => ssp_config.h} (98%) delete mode 100644 core/iwasm/runtime/platform/alios/wasm_native.c delete mode 100644 core/iwasm/runtime/platform/darwin/platform.cmake delete mode 100644 core/iwasm/runtime/platform/darwin/wasm_native.c delete mode 100644 core/iwasm/runtime/platform/linux-sgx/platform.cmake delete mode 100644 core/iwasm/runtime/platform/linux-sgx/wasm_native.c delete mode 100644 core/iwasm/runtime/platform/linux/platform.cmake delete mode 100644 core/iwasm/runtime/platform/linux/wasm_native.c delete mode 100644 core/iwasm/runtime/platform/vxworks/platform.cmake delete mode 100644 core/iwasm/runtime/platform/vxworks/wasm_native.c delete mode 100644 core/iwasm/runtime/platform/zephyr/platform.cmake delete mode 100644 core/iwasm/runtime/platform/zephyr/wasm_native.c diff --git a/core/iwasm/lib/native/libc/libc_builtin_wrapper.c b/core/iwasm/lib/native/libc/libc_builtin_wrapper.c index f099b63c..bd6cd41f 100644 --- a/core/iwasm/lib/native/libc/libc_builtin_wrapper.c +++ b/core/iwasm/lib/native/libc/libc_builtin_wrapper.c @@ -1245,7 +1245,9 @@ wasm_native_func_lookup(const char *module_name, const char *func_name) uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef); WASMNativeFuncDef *func_def = native_func_defs; WASMNativeFuncDef *func_def_end = func_def + size; +#if WASM_ENABLE_WASI != 0 void *ret; +#endif if (!module_name || !func_name) return NULL; @@ -1259,9 +1261,6 @@ wasm_native_func_lookup(const char *module_name, const char *func_name) func_def++; } - if ((ret = wasm_platform_native_func_lookup(module_name, func_name))) - return ret; - #if WASM_ENABLE_WASI != 0 if ((ret = wasi_native_func_lookup(module_name, func_name))) return ret; diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h index 0efb788e..97d11bef 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/locking.h @@ -12,7 +12,7 @@ #ifndef LOCKING_H #define LOCKING_H -#include "config.h" +#include "ssp_config.h" #include #include diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c index 2f2f1066..c698489a 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/posix.c @@ -9,7 +9,7 @@ // // Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/ -#include "config.h" +#include "ssp_config.h" #include diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c index a3f17bbc..d551fd80 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/random.c @@ -9,7 +9,7 @@ // // Copyright (c) 2016 Nuxi, https://nuxi.nl/ -#include "config.h" +#include "ssp_config.h" #include #include diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/ssp_config.h similarity index 98% rename from core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h rename to core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/ssp_config.h index 714e29d2..d4af4077 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/config.h +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/ssp_config.h @@ -9,8 +9,8 @@ // // Copyright (c) 2016 Nuxi, https://nuxi.nl/ -#ifndef CONFIG_H -#define CONFIG_H +#ifndef SSP_CONFIG_H +#define SSP_CONFIG_H #include diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c index 02a9518f..87b9ba75 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.c @@ -9,7 +9,7 @@ // // Copyright (c) 2016 Nuxi, https://nuxi.nl/ -#include "config.h" +#include "ssp_config.h" #include #include diff --git a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h index a9c5f226..8399415e 100644 --- a/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h +++ b/core/iwasm/lib/native/libc/wasmtime-wasi-c/sandboxed-system-primitives/src/str.h @@ -12,7 +12,7 @@ #ifndef STR_H #define STR_H -#include "config.h" +#include "ssp_config.h" char *str_nullterminate(const char *, size_t); diff --git a/core/iwasm/products/alios-things/iwasm.mk b/core/iwasm/products/alios-things/iwasm.mk index c43c5c70..5821b723 100644 --- a/core/iwasm/products/alios-things/iwasm.mk +++ b/core/iwasm/products/alios-things/iwasm.mk @@ -17,7 +17,6 @@ GLOBAL_INCLUDES += ${IWASM_ROOT}/runtime/include \ $(NAME)_SOURCES := ${IWASM_ROOT}/runtime/utils/wasm_hashmap.c \ ${IWASM_ROOT}/runtime/utils/wasm_log.c \ ${IWASM_ROOT}/runtime/utils/wasm_dlfcn.c \ - ${IWASM_ROOT}/runtime/platform/alios/wasm_native.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_application.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_interp.c \ ${IWASM_ROOT}/runtime/vmcore-wasm/wasm_loader.c \ diff --git a/core/iwasm/products/darwin/CMakeLists.txt b/core/iwasm/products/darwin/CMakeLists.txt index 9d5f5f58..36228346 100644 --- a/core/iwasm/products/darwin/CMakeLists.txt +++ b/core/iwasm/products/darwin/CMakeLists.txt @@ -92,7 +92,6 @@ include_directories (. enable_language (ASM) -include (../../runtime/platform/${PLATFORM}/platform.cmake) include (../../runtime/utils/utils.cmake) include (../../runtime/vmcore-wasm/vmcore.cmake) include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake) diff --git a/core/iwasm/products/darwin/main.c b/core/iwasm/products/darwin/main.c index ffda65c9..0b4b42f0 100644 --- a/core/iwasm/products/darwin/main.c +++ b/core/iwasm/products/darwin/main.c @@ -60,12 +60,10 @@ app_instance_main(wasm_module_inst_t module_inst) static void* app_instance_func(wasm_module_inst_t module_inst, char *func_name) { - const char *exception; - wasm_application_execute_func(module_inst, func_name, app_argc - 1, app_argv + 1); - if ((exception = wasm_runtime_get_exception(module_inst))) - wasm_printf("%s\n", exception); + /* The result of wasm function or exception info was output inside + wasm_application_execute_func(), here we don't output them again. */ return NULL; } diff --git a/core/iwasm/products/linux-sgx/CMakeLists.txt b/core/iwasm/products/linux-sgx/CMakeLists.txt index de91f1e6..bcb76fd3 100644 --- a/core/iwasm/products/linux-sgx/CMakeLists.txt +++ b/core/iwasm/products/linux-sgx/CMakeLists.txt @@ -93,7 +93,6 @@ include_directories (. enable_language (ASM) -include (../../runtime/platform/${PLATFORM}/platform.cmake) include (../../runtime/utils/utils.cmake) include (../../runtime/vmcore-wasm/vmcore.cmake) include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake) diff --git a/core/iwasm/products/linux/CMakeLists.txt b/core/iwasm/products/linux/CMakeLists.txt index 142c82a7..66ff8140 100644 --- a/core/iwasm/products/linux/CMakeLists.txt +++ b/core/iwasm/products/linux/CMakeLists.txt @@ -89,7 +89,6 @@ include_directories (. enable_language (ASM) -include (../../runtime/platform/${PLATFORM}/platform.cmake) include (../../runtime/utils/utils.cmake) include (../../runtime/vmcore-wasm/vmcore.cmake) include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake) diff --git a/core/iwasm/products/linux/main.c b/core/iwasm/products/linux/main.c index 0ea3b9fd..9ef04f51 100644 --- a/core/iwasm/products/linux/main.c +++ b/core/iwasm/products/linux/main.c @@ -87,12 +87,10 @@ app_instance_main(wasm_module_inst_t module_inst) static void* app_instance_func(wasm_module_inst_t module_inst, const char *func_name) { - const char *exception; - wasm_application_execute_func(module_inst, func_name, app_argc - 1, app_argv + 1); - if ((exception = wasm_runtime_get_exception(module_inst))) - wasm_printf("%s\n", exception); + /* The result of wasm function or exception info was output inside + wasm_application_execute_func(), here we don't output them again. */ return NULL; } diff --git a/core/iwasm/products/vxworks/CMakeLists.txt b/core/iwasm/products/vxworks/CMakeLists.txt index 64045a3d..aa00d4c6 100644 --- a/core/iwasm/products/vxworks/CMakeLists.txt +++ b/core/iwasm/products/vxworks/CMakeLists.txt @@ -107,7 +107,6 @@ include_directories (. enable_language (ASM) -include (../../runtime/platform/${PLATFORM}/platform.cmake) include (../../runtime/utils/utils.cmake) include (../../runtime/vmcore-wasm/vmcore.cmake) include (../../lib/native/libc/wasmtime-wasi-c/wasi.cmake) diff --git a/core/iwasm/products/vxworks/main.c b/core/iwasm/products/vxworks/main.c index 6cbe2438..0d92ec61 100644 --- a/core/iwasm/products/vxworks/main.c +++ b/core/iwasm/products/vxworks/main.c @@ -87,12 +87,10 @@ app_instance_main(wasm_module_inst_t module_inst) static void* app_instance_func(wasm_module_inst_t module_inst, const char *func_name) { - const char *exception; - wasm_application_execute_func(module_inst, func_name, app_argc - 1, app_argv + 1); - if ((exception = wasm_runtime_get_exception(module_inst))) - wasm_printf("%s\n", exception); + /* The result of wasm function or exception info was output inside + wasm_application_execute_func(), here we don't output them again. */ return NULL; } diff --git a/core/iwasm/products/zephyr/simple/CMakeLists.txt b/core/iwasm/products/zephyr/simple/CMakeLists.txt index efa74f8a..be1e848d 100644 --- a/core/iwasm/products/zephyr/simple/CMakeLists.txt +++ b/core/iwasm/products/zephyr/simple/CMakeLists.txt @@ -37,7 +37,6 @@ message ("-- Build as target ${BUILD_TARGET}") set (IWASM_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/iwasm) set (SHARED_LIB_ROOT ${IWASM_ROOT}/../shared-lib) -include (${IWASM_ROOT}/runtime/platform/${PLATFORM}/platform.cmake) include (${IWASM_ROOT}/runtime/utils/utils.cmake) include (${IWASM_ROOT}/runtime/vmcore-wasm/vmcore.cmake) include (${IWASM_ROOT}/lib/native/libc/wasmtime-wasi-c/wasi.cmake) @@ -47,6 +46,8 @@ include (${SHARED_LIB_ROOT}/platform/${PLATFORM}/shared_platform.cmake) include (${SHARED_LIB_ROOT}/mem-alloc/mem_alloc.cmake) include (${SHARED_LIB_ROOT}/utils/shared_utils.cmake) +include_directories (${IWASM_ROOT}/runtime/platform/include) + set (VM_LIB_SRCS ${WASM_PLATFORM_LIB_SOURCE} ${WASM_UTILS_LIB_SOURCE} diff --git a/core/iwasm/runtime/platform/alios/wasm_native.c b/core/iwasm/runtime/platform/alios/wasm_native.c deleted file mode 100644 index 70f87d38..00000000 --- a/core/iwasm/runtime/platform/alios/wasm_native.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "wasm_native.h" - - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - diff --git a/core/iwasm/runtime/platform/darwin/platform.cmake b/core/iwasm/runtime/platform/darwin/platform.cmake deleted file mode 100644 index 887a4fc5..00000000 --- a/core/iwasm/runtime/platform/darwin/platform.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L) - -set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${PLATFORM_LIB_DIR}) -include_directories(${PLATFORM_LIB_DIR}/../include) - -file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c) - -set (WASM_PLATFORM_LIB_SOURCE ${source_all}) - diff --git a/core/iwasm/runtime/platform/darwin/wasm_native.c b/core/iwasm/runtime/platform/darwin/wasm_native.c deleted file mode 100644 index fc3303ea..00000000 --- a/core/iwasm/runtime/platform/darwin/wasm_native.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "wasm_native.h" - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - diff --git a/core/iwasm/runtime/platform/linux-sgx/platform.cmake b/core/iwasm/runtime/platform/linux-sgx/platform.cmake deleted file mode 100644 index e687f830..00000000 --- a/core/iwasm/runtime/platform/linux-sgx/platform.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L) - -set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${PLATFORM_LIB_DIR}) -include_directories(${PLATFORM_LIB_DIR}/../include) - -file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c) - -set (WASM_PLATFORM_LIB_SOURCE ${source_all}) - diff --git a/core/iwasm/runtime/platform/linux-sgx/wasm_native.c b/core/iwasm/runtime/platform/linux-sgx/wasm_native.c deleted file mode 100644 index 70f87d38..00000000 --- a/core/iwasm/runtime/platform/linux-sgx/wasm_native.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "wasm_native.h" - - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - diff --git a/core/iwasm/runtime/platform/linux/platform.cmake b/core/iwasm/runtime/platform/linux/platform.cmake deleted file mode 100644 index 4924a3fb..00000000 --- a/core/iwasm/runtime/platform/linux/platform.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE) - -set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${PLATFORM_LIB_DIR}) -include_directories(${PLATFORM_LIB_DIR}/../include) - -file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c) - -set (WASM_PLATFORM_LIB_SOURCE ${source_all}) - diff --git a/core/iwasm/runtime/platform/linux/wasm_native.c b/core/iwasm/runtime/platform/linux/wasm_native.c deleted file mode 100644 index 37d9066e..00000000 --- a/core/iwasm/runtime/platform/linux/wasm_native.c +++ /dev/null @@ -1,365 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -/** - * Currently WAMR supports libc for WASM applications in two modes: - * (1) the built-in libc subset for embedded environment and - * (2) WASI for standard libc - * so here the libc syscall mode is disabled by default, if developer - * wants to enable the libc syscall mode, please set the following - * macro to 1, and implements the related syscall wrappers. Here some - * syscall wrapper examples are given. - */ -#define WASM_TEST_SYSCALL_WRAPPER 0 - -#if WASM_TEST_SYSCALL_WRAPPER == 0 - -#include "wasm_native.h" - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - -#else - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE /* for O_DIRECT */ -#endif - -#include "wasm_native.h" -#include "wasm_runtime.h" -#include "wasm_log.h" -#include "wasm_memory.h" -#include "wasm_platform_log.h" -#include "bh_common.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define validate_app_addr(offset, size) \ - wasm_runtime_validate_app_addr(module_inst, offset, size) - -#define addr_app_to_native(offset) \ - wasm_runtime_addr_app_to_native(module_inst, offset) - -#define addr_native_to_app(ptr) \ - wasm_runtime_addr_native_to_app(module_inst, ptr) - -#define module_malloc(size) \ - wasm_runtime_module_malloc(module_inst, size) - -#define module_free(offset) \ - wasm_runtime_module_free(module_inst, offset) - - -static int32 -__syscall0_wrapper(WASMModuleInstance *module_inst, int32 arg0) -{ - switch (arg0) { - case 199: /* getuid */ - /* TODO */ - default: - bh_printf("##_syscall0 called, syscall id: %d\n", arg0); - } - return 0; -} - -static int32 -__syscall1_wrapper(WASMModuleInstance *module_inst, int32 arg0, int32 arg1) -{ - switch (arg0) { - case 6: /* close */ - /* TODO */ - default: - bh_printf("##_syscall1 called, syscall id: %d\n", arg0); - } - return 0; -} - -static int32 -__syscall2_wrapper(WASMModuleInstance *module_inst, - int32 arg0, int32 arg1, int32 arg2) -{ - switch (arg0) { - case 183: /* getcwd */ - /* TODO */ - default: - bh_printf("##_syscall2 called, syscall id: %d\n", arg0); - } - return 0; -} - -static int32 -__syscall3_wrapper(WASMModuleInstance *module_inst, - int32 arg0, int32 arg1, int32 arg2, int32 arg3) -{ - switch (arg0) { - case 54: /* ioctl */ - { - /* Implement syscall 54 and syscall 146 to support printf() - for non SIDE_MODULE=1 mode */ - struct winsize *wsz; - - if (!validate_app_addr(arg3, sizeof(struct winsize))) - return 0; - - wsz = (struct winsize*)addr_app_to_native(arg3); - return (int32)syscall(54, arg1, arg2, wsz); - } - - case 146: /* writev */ - { - /* Implement syscall 54 and syscall 146 to support printf() - for non SIDE_MODULE=1 mode */ - struct iovec_app { - int32 iov_base_offset; - uint32 iov_len; - } *vec; - int32 vec_offset = arg2, str_offset; - uint32 iov_count = (uint32)arg3, i; - int32 count = 0; - char *iov_base, *str; - - if (!validate_app_addr(vec_offset, sizeof(struct iovec_app))) - return 0; - - vec = (struct iovec_app *)addr_app_to_native(vec_offset); - for (i = 0; i < iov_count; i++, vec++) { - if (vec->iov_len > 0) { - if (!validate_app_addr(vec->iov_base_offset, 1)) - return 0; - iov_base = (char*)addr_app_to_native(vec->iov_base_offset); - - if (!(str_offset = module_malloc(vec->iov_len + 1))) - return 0; - - str = addr_app_to_native(str_offset); - - bh_memcpy_s(str, vec->iov_len + 1, iov_base, vec->iov_len); - str[vec->iov_len] = '\0'; - count += wasm_printf("%s", str); - - module_free(str_offset); - } - } - return count; - } - case 145: /* readv */ - case 3: /* read*/ - case 5: /* open */ - case 221: /* fcntl */ - /* TODO */ - default: - bh_printf("##_syscall3 called, syscall id: %d\n", arg0); - } - return 0; -} - -static int32 -__syscall4_wrapper(WASMModuleInstance *module_inst, - int32 arg0, int32 arg1, int32 arg2, - int32 arg3, int32 arg4) -{ - bh_printf("##_syscall4 called, syscall id: %d\n", arg0); - return 0; -} - -static int32 -__syscall5_wrapper(WASMModuleInstance *module_inst, - int32 arg0, int32 arg1, int32 arg2, - int32 arg3, int32 arg4, int32 arg5) -{ - switch (arg0) { - case 140: /* llseek */ - /* TODO */ - default: - bh_printf("##_syscall5 called, args[0]: %d\n", arg0); - } - return 0; -} - -#define GET_EMCC_SYSCALL_ARGS() \ - int32 *args; \ - if (!validate_app_addr(args_off, 1)) \ - return 0; \ - args = addr_app_to_native(args_off) \ - -#define EMCC_SYSCALL_WRAPPER0(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id) { \ - return __syscall0_wrapper(module_inst, id); \ - } - -#define EMCC_SYSCALL_WRAPPER1(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id, int32 args_off) { \ - GET_EMCC_SYSCALL_ARGS(); \ - return __syscall1_wrapper(module_inst, id, args[0]); \ - } - -#define EMCC_SYSCALL_WRAPPER2(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id, int32 args_off){ \ - GET_EMCC_SYSCALL_ARGS(); \ - return __syscall2_wrapper(module_inst, id, args[0], args[1]); \ - } - -#define EMCC_SYSCALL_WRAPPER3(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id, int32 args_off) { \ - GET_EMCC_SYSCALL_ARGS(); \ - return __syscall3_wrapper(module_inst, id, \ - args[0], args[1], args[2]); \ - } - -#define EMCC_SYSCALL_WRAPPER4(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id, int32 args_off) { \ - GET_EMCC_SYSCALL_ARGS(); \ - return __syscall4_wrapper(module_inst, id, \ - args[0], args[1], args[2], args[3]); \ - } - -#define EMCC_SYSCALL_WRAPPER5(id) \ - static int32 ___syscall##id##_wrapper(WASMModuleInstance *module_inst,\ - int32 _id, int32 args_off) { \ - GET_EMCC_SYSCALL_ARGS(); \ - return __syscall5_wrapper(module_inst, id, \ - args[0], args[1], args[2], \ - args[3], args[4]); \ - } - -EMCC_SYSCALL_WRAPPER0(199) - -EMCC_SYSCALL_WRAPPER1(6) - -EMCC_SYSCALL_WRAPPER2(183) - -EMCC_SYSCALL_WRAPPER3(3) -EMCC_SYSCALL_WRAPPER3(5) -EMCC_SYSCALL_WRAPPER3(54) -EMCC_SYSCALL_WRAPPER3(145) -EMCC_SYSCALL_WRAPPER3(146) -EMCC_SYSCALL_WRAPPER3(221) - -EMCC_SYSCALL_WRAPPER5(140) - -static uint32 -getTotalMemory_wrapper(WASMModuleInstance *module_inst) -{ - WASMMemoryInstance *memory = module_inst->default_memory; - return NumBytesPerPage * memory->cur_page_count; -} - -static int32 -enlargeMemory_wrapper(WASMModuleInstance *module_inst) -{ - bool ret; - WASMMemoryInstance *memory = module_inst->default_memory; - uint32 DYNAMICTOP_PTR_offset = module_inst->DYNAMICTOP_PTR_offset; - uint32 addr_data_offset = *(uint32*)(memory->global_data + DYNAMICTOP_PTR_offset); - uint32 *DYNAMICTOP_PTR = (uint32*)(memory->memory_data + addr_data_offset); - uint32 memory_size_expected = *DYNAMICTOP_PTR; - uint32 total_page_count = (memory_size_expected + NumBytesPerPage - 1) / NumBytesPerPage; - - if (total_page_count < memory->cur_page_count) { - return 1; - } - else { - ret = wasm_runtime_enlarge_memory(module_inst, total_page_count - - memory->cur_page_count); - return ret ? 1 : 0; - } -} - -static void -_abort_wrapper(WASMModuleInstance *module_inst, int32 code) -{ - char buf[32]; - - snprintf(buf, sizeof(buf), "env.abort(%i)", code); - wasm_runtime_set_exception(module_inst, buf); -} - -static void -abortOnCannotGrowMemory_wrapper(WASMModuleInstance *module_inst) -{ - wasm_runtime_set_exception(module_inst, "abort on cannot grow memory"); -} - -static void -___setErrNo_wrapper(WASMModuleInstance *module_inst, int32 error_no) -{ - errno = error_no; -} - -/* TODO: add function parameter/result types check */ -#define REG_NATIVE_FUNC(module_name, func_name) \ - {#module_name, #func_name, func_name##_wrapper} - -typedef struct WASMNativeFuncDef { - const char *module_name; - const char *func_name; - void *func_ptr; -} WASMNativeFuncDef; - -static WASMNativeFuncDef native_func_defs[] = { - REG_NATIVE_FUNC(env, __syscall0), - REG_NATIVE_FUNC(env, __syscall1), - REG_NATIVE_FUNC(env, __syscall2), - REG_NATIVE_FUNC(env, __syscall3), - REG_NATIVE_FUNC(env, __syscall4), - REG_NATIVE_FUNC(env, __syscall5), - REG_NATIVE_FUNC(env, ___syscall3), - REG_NATIVE_FUNC(env, ___syscall5), - REG_NATIVE_FUNC(env, ___syscall6), - REG_NATIVE_FUNC(env, ___syscall54), - REG_NATIVE_FUNC(env, ___syscall140), - REG_NATIVE_FUNC(env, ___syscall145), - REG_NATIVE_FUNC(env, ___syscall146), - REG_NATIVE_FUNC(env, ___syscall183), - REG_NATIVE_FUNC(env, ___syscall199), - REG_NATIVE_FUNC(env, ___syscall221), - REG_NATIVE_FUNC(env, _abort), - REG_NATIVE_FUNC(env, abortOnCannotGrowMemory), - REG_NATIVE_FUNC(env, enlargeMemory), - REG_NATIVE_FUNC(env, getTotalMemory), - REG_NATIVE_FUNC(env, ___setErrNo), -}; - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef); - WASMNativeFuncDef *func_def = native_func_defs; - WASMNativeFuncDef *func_def_end = func_def + size; - - if (!module_name || !func_name) - return NULL; - - while (func_def < func_def_end) { - if (!strcmp(func_def->module_name, module_name) - && !strcmp(func_def->func_name, func_name)) - return (void*)(uintptr_t)func_def->func_ptr; - func_def++; - } - - return NULL; -} - -#endif /* end of WASM_TEST_SYSCALL_WRAPPER */ - diff --git a/core/iwasm/runtime/platform/vxworks/platform.cmake b/core/iwasm/runtime/platform/vxworks/platform.cmake deleted file mode 100644 index 4924a3fb..00000000 --- a/core/iwasm/runtime/platform/vxworks/platform.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE) - -set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${PLATFORM_LIB_DIR}) -include_directories(${PLATFORM_LIB_DIR}/../include) - -file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c) - -set (WASM_PLATFORM_LIB_SOURCE ${source_all}) - diff --git a/core/iwasm/runtime/platform/vxworks/wasm_native.c b/core/iwasm/runtime/platform/vxworks/wasm_native.c deleted file mode 100644 index 70f87d38..00000000 --- a/core/iwasm/runtime/platform/vxworks/wasm_native.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "wasm_native.h" - - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - diff --git a/core/iwasm/runtime/platform/zephyr/platform.cmake b/core/iwasm/runtime/platform/zephyr/platform.cmake deleted file mode 100644 index 4924a3fb..00000000 --- a/core/iwasm/runtime/platform/zephyr/platform.cmake +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2019 Intel Corporation. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE) - -set (PLATFORM_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}) - -include_directories(${PLATFORM_LIB_DIR}) -include_directories(${PLATFORM_LIB_DIR}/../include) - -file (GLOB_RECURSE source_all ${PLATFORM_LIB_DIR}/*.c) - -set (WASM_PLATFORM_LIB_SOURCE ${source_all}) - diff --git a/core/iwasm/runtime/platform/zephyr/wasm_native.c b/core/iwasm/runtime/platform/zephyr/wasm_native.c deleted file mode 100644 index 70f87d38..00000000 --- a/core/iwasm/runtime/platform/zephyr/wasm_native.c +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (C) 2019 Intel Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - */ - -#include "wasm_native.h" - - -void* -wasm_platform_native_func_lookup(const char *module_name, - const char *func_name) -{ - return NULL; -} - diff --git a/core/iwasm/runtime/vmcore-wasm/wasm.h b/core/iwasm/runtime/vmcore-wasm/wasm.h index 94cd093f..697276bf 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm.h +++ b/core/iwasm/runtime/vmcore-wasm/wasm.h @@ -146,7 +146,6 @@ typedef struct WASMGlobalImport { char *field_name; uint8 type; bool is_mutable; - bool is_addr; /* global data after linked */ WASMValue global_data_linked; } WASMGlobalImport; @@ -179,7 +178,6 @@ typedef struct WASMFunction { typedef struct WASMGlobal { uint8 type; bool is_mutable; - bool is_addr; InitializerExpression init_expr; } WASMGlobal; diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_application.c b/core/iwasm/runtime/vmcore-wasm/wasm_application.c index 98e7a08f..2dad4973 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_application.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_application.c @@ -214,7 +214,7 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, { WASMFunctionInstance *func; WASMType *type; - uint32 argc1, *argv1; + uint32 argc1, *argv1 = NULL; int32 i, p; uint64 total_size; const char *exception; @@ -225,14 +225,14 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, if (!func || func->is_import_func) { snprintf(buf, sizeof(buf), "lookup function %s failed.", name); wasm_runtime_set_exception(module_inst, buf); - return false; + goto fail; } type = func->u.func->func_type; if (type->param_count != (uint32)argc) { wasm_runtime_set_exception(module_inst, "invalid input argument count."); - return false; + goto fail; } argc1 = func->param_cell_num; @@ -240,9 +240,12 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, if (total_size >= UINT32_MAX || (!(argv1 = wasm_malloc((uint32)total_size)))) { wasm_runtime_set_exception(module_inst, "allocate memory failed."); - return false; + goto fail; } + /* Clear errno before parsing arguments */ + errno = 0; + /* Parse arguments */ for (i = 0, p = 0; i < argc; i++) { char *endptr = NULL; @@ -332,9 +335,6 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, wasm_runtime_set_exception(module_inst, NULL); if (!wasm_runtime_call_wasm(module_inst, NULL, func, argc1, argv1)) { - exception = wasm_runtime_get_exception(module_inst); - wasm_assert(exception); - wasm_printf("%s\n", exception); goto fail; } @@ -374,7 +374,12 @@ wasm_application_execute_func(WASMModuleInstance *module_inst, return true; fail: - wasm_free(argv1); + if (argv1) + wasm_free(argv1); + + exception = wasm_runtime_get_exception(module_inst); + wasm_assert(exception); + wasm_printf("%s\n", exception); return false; } diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_interp.c b/core/iwasm/runtime/vmcore-wasm/wasm_interp.c index b4b4e635..8c5d041d 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_interp.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_interp.c @@ -84,15 +84,15 @@ GET_F64_FROM_ADDR (uint32 *addr) uintptr_t addr1 = (uintptr_t)(addr); \ union { int64 val; uint32 u32[2]; \ uint16 u16[4]; uint8 u8[8]; } u; \ - if (addr1 % 8 == 0) \ + if ((addr1 & (uintptr_t)7) == 0) \ *(int64*)(addr) = (int64)(value); \ else { \ u.val = (int64)(value); \ - if (addr1 % 4 == 0) { \ + if ((addr1 & (uintptr_t)3) == 0) { \ ((uint32*)(addr))[0] = u.u32[0]; \ ((uint32*)(addr))[1] = u.u32[1]; \ } \ - else if (addr1 % 2 == 0) { \ + else if ((addr1 & (uintptr_t)1) == 0) { \ ((uint16*)(addr))[0] = u.u16[0]; \ ((uint16*)(addr))[1] = u.u16[1]; \ ((uint16*)(addr))[2] = u.u16[2]; \ @@ -110,11 +110,11 @@ GET_F64_FROM_ADDR (uint32 *addr) uintptr_t addr1 = (uintptr_t)(addr); \ union { uint32 val; \ uint16 u16[2]; uint8 u8[4]; } u; \ - if (addr1 % 4 == 0) \ + if ((addr1 & (uintptr_t)3) == 0) \ *(uint32*)(addr) = (uint32)(value); \ else { \ u.val = (uint32)(value); \ - if (addr1 % 2 == 0) { \ + if ((addr1 & (uintptr_t)1) == 0) { \ ((uint16*)(addr))[0] = u.u16[0]; \ ((uint16*)(addr))[1] = u.u16[1]; \ } \ @@ -141,14 +141,14 @@ LOAD_I64(void *addr) uintptr_t addr1 = (uintptr_t)addr; union { int64 val; uint32 u32[2]; uint16 u16[4]; uint8 u8[8]; } u; - if (addr1 % 8 == 0) + if ((addr1 & (uintptr_t)7) == 0) return *(int64*)addr; - if (addr1 % 4 == 0) { + if ((addr1 & (uintptr_t)3) == 0) { u.u32[0] = ((uint32*)addr)[0]; u.u32[1] = ((uint32*)addr)[1]; } - else if (addr1 % 2 == 0) { + else if ((addr1 & (uintptr_t)1) == 0) { u.u16[0] = ((uint16*)addr)[0]; u.u16[1] = ((uint16*)addr)[1]; u.u16[2] = ((uint16*)addr)[2]; @@ -168,14 +168,14 @@ LOAD_F64(void *addr) uintptr_t addr1 = (uintptr_t)addr; union { float64 val; uint32 u32[2]; uint16 u16[4]; uint8 u8[8]; } u; - if (addr1 % 8 == 0) + if ((addr1 & (uintptr_t)7) == 0) return *(float64*)addr; - if (addr1 % 4 == 0) { + if ((addr1 & (uintptr_t)3) == 0) { u.u32[0] = ((uint32*)addr)[0]; u.u32[1] = ((uint32*)addr)[1]; } - else if (addr1 % 2 == 0) { + else if ((addr1 & (uintptr_t)1) == 0) { u.u16[0] = ((uint16*)addr)[0]; u.u16[1] = ((uint16*)addr)[1]; u.u16[2] = ((uint16*)addr)[2]; @@ -194,10 +194,10 @@ LOAD_I32(void *addr) { uintptr_t addr1 = (uintptr_t)addr; union { int32 val; uint16 u16[2]; uint8 u8[4]; } u; - if (addr1 % 4 == 0) + if ((addr1 & (uintptr_t)3) == 0) return *(int32*)addr; - if (addr1 % 2 == 0) { + if ((addr1 & (uintptr_t)1) == 0) { u.u16[0] = ((uint16*)addr)[0]; u.u16[1] = ((uint16*)addr)[1]; } diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_loader.c b/core/iwasm/runtime/vmcore-wasm/wasm_loader.c index 51deaf2a..cf9a5bf4 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_loader.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_loader.c @@ -256,25 +256,25 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, case INIT_EXPR_TYPE_I32_CONST: read_leb_int32(p, p_end, init_expr->u.i32); break; - /* i64.const */ + /* i64.const */ case INIT_EXPR_TYPE_I64_CONST: read_leb_int64(p, p_end, init_expr->u.i64); break; - /* f32.const */ + /* f32.const */ case INIT_EXPR_TYPE_F32_CONST: CHECK_BUF(p, p_end, 4); p_float = (uint8*)&init_expr->u.f32; for (i = 0; i < sizeof(float32); i++) *p_float++ = *p++; break; - /* f64.const */ + /* f64.const */ case INIT_EXPR_TYPE_F64_CONST: CHECK_BUF(p, p_end, 8); p_float = (uint8*)&init_expr->u.f64; for (i = 0; i < sizeof(float64); i++) *p_float++ = *p++; break; - /* get_global */ + /* get_global */ case INIT_EXPR_TYPE_GET_GLOBAL: read_leb_uint32(p, p_end, init_expr->u.global_index); break; diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_native.h b/core/iwasm/runtime/vmcore-wasm/wasm_native.h index 8efd0efe..eca44c6c 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_native.h +++ b/core/iwasm/runtime/vmcore-wasm/wasm_native.h @@ -45,9 +45,6 @@ bool wasm_native_global_lookup(const char *module_name, const char *global_name, WASMGlobalImport *global); -void* wasm_platform_native_func_lookup(const char *module_name, - const char *func_name); - #ifdef __cplusplus } #endif diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c index 79c786c6..1d0e72e2 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c +++ b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.c @@ -167,14 +167,14 @@ memories_deinstantiate(WASMMemoryInstance **memories, uint32 count) static WASMMemoryInstance* memory_instantiate(uint32 init_page_count, uint32 max_page_count, - uint32 addr_data_size, uint32 global_data_size, + uint32 global_data_size, uint32 heap_size, char *error_buf, uint32 error_buf_size) { WASMMemoryInstance *memory; uint64 total_size = offsetof(WASMMemoryInstance, base_addr) + NumBytesPerPage * (uint64)init_page_count + - addr_data_size + global_data_size; + global_data_size; /* Allocate memory space, addr data and global data */ if (total_size >= UINT32_MAX @@ -188,10 +188,7 @@ memory_instantiate(uint32 init_page_count, uint32 max_page_count, memory->cur_page_count = init_page_count; memory->max_page_count = max_page_count; - memory->addr_data = memory->base_addr; - memory->addr_data_size = addr_data_size; - - memory->memory_data = memory->addr_data + addr_data_size; + memory->memory_data = memory->base_addr; memory->global_data = memory->memory_data + NumBytesPerPage * memory->cur_page_count;; @@ -233,7 +230,7 @@ fail1: * Instantiate memories in a module. */ static WASMMemoryInstance** -memories_instantiate(const WASMModule *module, uint32 addr_data_size, +memories_instantiate(const WASMModule *module, uint32 global_data_size, uint32 heap_size, char *error_buf, uint32 error_buf_size) { @@ -264,7 +261,7 @@ memories_instantiate(const WASMModule *module, uint32 addr_data_size, if (!(memory = memories[mem_index++] = memory_instantiate(import->u.memory.init_page_count, import->u.memory. max_page_count, - addr_data_size, global_data_size, + global_data_size, heap_size, error_buf, error_buf_size))) { set_error_buf(error_buf, error_buf_size, "Instantiate memory failed: " @@ -279,7 +276,7 @@ memories_instantiate(const WASMModule *module, uint32 addr_data_size, if (!(memory = memories[mem_index++] = memory_instantiate(module->memories[i].init_page_count, module->memories[i].max_page_count, - addr_data_size, global_data_size, + global_data_size, heap_size, error_buf, error_buf_size))) { set_error_buf(error_buf, error_buf_size, "Instantiate memory failed: " @@ -292,7 +289,7 @@ memories_instantiate(const WASMModule *module, uint32 addr_data_size, if (mem_index == 0) { /* no import memory and define memory, but has global variables */ if (!(memory = memories[mem_index++] = - memory_instantiate(0, 0, addr_data_size, global_data_size, + memory_instantiate(0, 0, global_data_size, heap_size, error_buf, error_buf_size))) { set_error_buf(error_buf, error_buf_size, "Instantiate memory failed: " @@ -525,12 +522,11 @@ globals_deinstantiate(WASMGlobalInstance *globals) */ static WASMGlobalInstance* globals_instantiate(const WASMModule *module, - uint32 *p_addr_data_size, uint32 *p_global_data_size, char *error_buf, uint32 error_buf_size) { WASMImport *import; - uint32 addr_data_offset = 0, global_data_offset = 0; + uint32 global_data_offset = 0; uint32 i, global_count = module->import_global_count + module->global_count; uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count; @@ -553,14 +549,10 @@ globals_instantiate(const WASMModule *module, WASMGlobalImport *global_import = &import->u.global; global->type = global_import->type; global->is_mutable = global_import->is_mutable; - global->is_addr = global_import->is_addr; global->initial_value = global_import->global_data_linked; global->data_offset = global_data_offset; global_data_offset += wasm_value_type_size(global->type); - if (global->is_addr) - addr_data_offset += (uint32)sizeof(uint32); - global++; } @@ -568,19 +560,14 @@ globals_instantiate(const WASMModule *module, for (i = 0; i < module->global_count; i++) { global->type = module->globals[i].type; global->is_mutable = module->globals[i].is_mutable; - global->is_addr = module->globals[i].is_addr; global->data_offset = global_data_offset; global_data_offset += wasm_value_type_size(global->type); - if (global->is_addr) - addr_data_offset += (uint32)sizeof(uint32); - global++; } wasm_assert((uint32)(global - globals) == global_count); - *p_addr_data_size = addr_data_offset; *p_global_data_size = global_data_offset; return globals; } @@ -949,9 +936,9 @@ wasm_runtime_instantiate(WASMModule *module, WASMTableSeg *table_seg; WASMDataSeg *data_seg; WASMGlobalInstance *globals = NULL, *global; - uint32 global_count, addr_data_size = 0, global_data_size = 0, i, j; + uint32 global_count, global_data_size = 0, i, j; uint32 base_offset, length, memory_size; - uint8 *global_data, *global_data_end, *addr_data, *addr_data_end; + uint8 *global_data, *global_data_end; uint8 *memory_data; uint32 *table_data; @@ -970,7 +957,7 @@ wasm_runtime_instantiate(WASMModule *module, /* Instantiate global firstly to get the mutable data size */ global_count = module->import_global_count + module->global_count; if (global_count && - !(globals = globals_instantiate(module, &addr_data_size, + !(globals = globals_instantiate(module, &global_data_size, error_buf, error_buf_size))) return NULL; @@ -998,7 +985,7 @@ wasm_runtime_instantiate(WASMModule *module, /* Instantiate memories/tables/functions */ if (((module_inst->memory_count > 0 || global_count > 0) && !(module_inst->memories = - memories_instantiate(module, addr_data_size, global_data_size, + memories_instantiate(module, global_data_size, heap_size, error_buf, error_buf_size))) || (module_inst->table_count > 0 && !(module_inst->tables = tables_instantiate(module, @@ -1026,8 +1013,6 @@ wasm_runtime_instantiate(WASMModule *module, globals_instantiate_fix(globals, module, module_inst); /* Initialize the global data */ - addr_data = memory->addr_data; - addr_data_end = addr_data + addr_data_size; global_data = memory->global_data; global_data_end = global_data + global_data_size; global = globals; @@ -1035,19 +1020,11 @@ wasm_runtime_instantiate(WASMModule *module, switch (global->type) { case VALUE_TYPE_I32: case VALUE_TYPE_F32: - if (!global->is_addr) - *(int32*)global_data = global->initial_value.i32; - else { - *(int32*)addr_data = global->initial_value.i32; - /* Store the offset to memory data for global of addr */ - *(int32*)global_data = (int32)(addr_data - memory_data); - addr_data += sizeof(int32); - } + *(int32*)global_data = global->initial_value.i32; global_data += sizeof(int32); break; case VALUE_TYPE_I64: case VALUE_TYPE_F64: - wasm_assert(!global->is_addr); bh_memcpy_s(global_data, (uint32)(global_data_end - global_data), &global->initial_value.i64, sizeof(int64)); global_data += sizeof(int64); @@ -1056,22 +1033,8 @@ wasm_runtime_instantiate(WASMModule *module, wasm_assert(0); } } - wasm_assert(addr_data == addr_data_end); wasm_assert(global_data == global_data_end); - global = globals + module->import_global_count; - for (i = 0; i < module->global_count; i++, global++) { - InitializerExpression *init_expr = &module->globals[i].init_expr; - - if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL - && globals[init_expr->u.global_index].is_addr) { - uint8 *global_data_dst = memory->global_data + global->data_offset; - uint8 *global_data_src = - memory->global_data + globals[init_expr->u.global_index].data_offset; - *(uintptr_t*)global_data_dst = *(uintptr_t*)global_data_src; - } - } - /* Initialize the memory data with data segment section */ if (module_inst->default_memory->cur_page_count > 0) { for (i = 0; i < module->data_seg_count; i++) { @@ -1200,7 +1163,6 @@ wasm_runtime_instantiate(WASMModule *module, return NULL; } - (void)addr_data_end; (void)global_data_end; return module_inst; } @@ -1272,7 +1234,6 @@ wasm_runtime_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) WASMMemoryInstance *new_memory; uint32 total_page_count = inc_page_count + memory->cur_page_count; uint64 total_size = offsetof(WASMMemoryInstance, base_addr) + - memory->addr_data_size + NumBytesPerPage * (uint64)total_page_count + memory->global_data_size; @@ -1295,10 +1256,7 @@ wasm_runtime_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) new_memory->cur_page_count = total_page_count; new_memory->max_page_count = memory->max_page_count; - new_memory->addr_data = new_memory->base_addr; - new_memory->addr_data_size = memory->addr_data_size; - - new_memory->memory_data = new_memory->addr_data + new_memory->addr_data_size; + new_memory->memory_data = new_memory->base_addr; new_memory->global_data = new_memory->memory_data + NumBytesPerPage * total_page_count; @@ -1306,11 +1264,11 @@ wasm_runtime_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count) new_memory->end_addr = new_memory->global_data + memory->global_data_size; - /* Copy addr data and memory data */ - bh_memcpy_s(new_memory->addr_data, - (uint32)(memory->global_data - memory->addr_data), - memory->addr_data, - (uint32)(memory->global_data - memory->addr_data)); + /* Copy memory data */ + bh_memcpy_s(new_memory->memory_data, + (uint32)(memory->global_data - memory->memory_data), + memory->memory_data, + (uint32)(memory->global_data - memory->memory_data)); /* Copy global data */ bh_memcpy_s(new_memory->global_data, new_memory->global_data_size, memory->global_data, memory->global_data_size); diff --git a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.h b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.h index 7e990cd2..189683a8 100644 --- a/core/iwasm/runtime/vmcore-wasm/wasm_runtime.h +++ b/core/iwasm/runtime/vmcore-wasm/wasm_runtime.h @@ -23,12 +23,6 @@ typedef struct WASMMemoryInstance { uint32 cur_page_count; /* Maximum page count */ uint32 max_page_count; - /* Data of import globals with address info, like _stdin/_stdout/_stderr, - stdin/stdout/stderr is stored here, but the actual addr info, or offset - to memory_data is stored in global_data section */ - uint8 *addr_data; - /* Size of addr_data */ - uint32 addr_data_size; /* Heap data base address */ uint8 *heap_data; @@ -49,11 +43,11 @@ typedef struct WASMMemoryInstance { uint8 *end_addr; /* Base address, the layout is: - addr_data + thunk_argv data + thunk arg offsets + + thunk_argv data + thunk arg offsets + memory data + global data memory data init size is: NumBytesPerPage * cur_page_count - addr data size and global data size is calculated in module instantiating - Note: when memory is re-allocated, the addr data, thunk argv data, thunk + global data size is calculated in module instantiating + Note: when memory is re-allocated, the thunk argv data, thunk argv offsets and memory data must be copied to new memory also. */ uint8 base_addr[1]; @@ -75,7 +69,6 @@ typedef struct WASMGlobalInstance { uint8 type; /* mutable or constant */ bool is_mutable; - bool is_addr; /* data offset to base_addr of WASMMemoryInstance */ uint32 data_offset; /* initial value */ diff --git a/core/shared-lib/include/config.h b/core/shared-lib/include/config.h index 262b8df9..4835e0c4 100644 --- a/core/shared-lib/include/config.h +++ b/core/shared-lib/include/config.h @@ -4,6 +4,7 @@ */ #ifndef _CONFIG_H_ +#define _CONFIG_H_ #if !defined(BUILD_TARGET_X86_64) \ && !defined(BUILD_TARGET_AMD_64) \ diff --git a/core/shared-lib/platform/alios/bh_platform.h b/core/shared-lib/platform/alios/bh_platform.h index d15eacd5..c51161fa 100644 --- a/core/shared-lib/platform/alios/bh_platform.h +++ b/core/shared-lib/platform/alios/bh_platform.h @@ -17,6 +17,7 @@ #include #include #include +#include #include /* Platform name */ diff --git a/core/shared-lib/platform/zephyr/bh_platform.h b/core/shared-lib/platform/zephyr/bh_platform.h index 94bca17f..fd8a9a61 100644 --- a/core/shared-lib/platform/zephyr/bh_platform.h +++ b/core/shared-lib/platform/zephyr/bh_platform.h @@ -115,6 +115,11 @@ double trunc(double x); int signbit(double x); int isnan(double x); +unsigned long long int strtoull(const char *nptr, char **endptr, + int base); +double strtod(const char *nptr, char **endptr); +float strtof(const char *nptr, char **endptr); + int bh_platform_init(); #endif diff --git a/core/shared-lib/platform/zephyr/bh_platform_log.c b/core/shared-lib/platform/zephyr/bh_platform_log.c index aab7a795..76e37769 100644 --- a/core/shared-lib/platform/zephyr/bh_platform_log.c +++ b/core/shared-lib/platform/zephyr/bh_platform_log.c @@ -27,7 +27,7 @@ static int char_out(int c, struct out_context *ctx) static int bh_vprintk(const char *fmt, va_list ap) { struct out_context ctx = { 0 }; - _vprintk((out_func_t) char_out, &ctx, fmt, ap); + z_vprintk((out_func_t) char_out, &ctx, fmt, ap); return ctx.count; } diff --git a/samples/gui/wasm-runtime-wgl/linux-build/CMakeLists.txt b/samples/gui/wasm-runtime-wgl/linux-build/CMakeLists.txt index 5a66b169..80c0bbe5 100644 --- a/samples/gui/wasm-runtime-wgl/linux-build/CMakeLists.txt +++ b/samples/gui/wasm-runtime-wgl/linux-build/CMakeLists.txt @@ -83,7 +83,6 @@ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -Wall - enable_language (ASM) -include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake) include (${WASM_DIR}/runtime/utils/utils.cmake) include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake) include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake) @@ -103,6 +102,7 @@ include (${SHARED_DIR}/coap/lib_coap.cmake) set (PROJECT_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/../src/platform/${TARGET_PLATFORM}) include_directories(${SHARED_DIR}/include) +include_directories(${WASM_DIR}/runtime/platform/include) include_directories(${PROJECT_SRC_DIR}) add_definitions (-DWASM_ENABLE_BASE_LIB) diff --git a/samples/gui/wasm-runtime-wgl/zephyr-build/CMakeLists.txt b/samples/gui/wasm-runtime-wgl/zephyr-build/CMakeLists.txt index d219b6f6..8fbbea30 100644 --- a/samples/gui/wasm-runtime-wgl/zephyr-build/CMakeLists.txt +++ b/samples/gui/wasm-runtime-wgl/zephyr-build/CMakeLists.txt @@ -51,7 +51,6 @@ set (SHARED_DIR ${WASM_DIR}/../shared-lib) set (TARGET_PLATFORM "zephyr") -include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake) include (${WASM_DIR}/runtime/utils/utils.cmake) include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake) include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake) @@ -68,6 +67,7 @@ include (${SHARED_DIR}/utils/shared_utils.cmake) include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake) include (${SHARED_DIR}/coap/lib_coap.cmake) +include_directories(${WASM_DIR}/runtime/platform/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/platform/zephyr) diff --git a/samples/littlevgl/vgl-wasm-runtime/CMakeLists.txt b/samples/littlevgl/vgl-wasm-runtime/CMakeLists.txt index a853e7a4..10344698 100644 --- a/samples/littlevgl/vgl-wasm-runtime/CMakeLists.txt +++ b/samples/littlevgl/vgl-wasm-runtime/CMakeLists.txt @@ -78,7 +78,6 @@ set(SHARED_DIR ${REPO_ROOT_DIR}/core/shared-lib) enable_language (ASM) -include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake) include (${WASM_DIR}/runtime/utils/utils.cmake) include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake) include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake) @@ -94,8 +93,9 @@ include (${SHARED_DIR}/utils/shared_utils.cmake) include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake) include (${SHARED_DIR}/coap/lib_coap.cmake) - -include_directories(${SHARED_DIR}/include ${CMAKE_CURRENT_LIST_DIR}/src) +include_directories(${SHARED_DIR}/include) +include_directories(${WASM_DIR}/runtime/platform/include) +include_directories(${CMAKE_CURRENT_LIST_DIR}/src) add_definitions (-DWASM_ENABLE_BASE_LIB) add_definitions (-Dattr_container_malloc=bh_malloc) diff --git a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt index 400cce6b..c5f91668 100644 --- a/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt +++ b/samples/littlevgl/vgl-wasm-runtime/zephyr-build/CMakeLists.txt @@ -49,7 +49,6 @@ set (SHARED_DIR ${WASM_DIR}/../shared-lib) set (TARGET_PLATFORM "zephyr") -include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake) include (${WASM_DIR}/runtime/utils/utils.cmake) include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake) include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake) @@ -65,6 +64,7 @@ include (${SHARED_DIR}/utils/shared_utils.cmake) include (${SHARED_DIR}/mem-alloc/mem_alloc.cmake) include (${SHARED_DIR}/coap/lib_coap.cmake) +include_directories(${WASM_DIR}/runtime/platform/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/platform/zephyr) diff --git a/samples/simple/CMakeLists.txt b/samples/simple/CMakeLists.txt index 07be7bd6..24a57250 100644 --- a/samples/simple/CMakeLists.txt +++ b/samples/simple/CMakeLists.txt @@ -89,7 +89,6 @@ endif() enable_language (ASM) -include (${WASM_DIR}/runtime/platform/${TARGET_PLATFORM}/platform.cmake) include (${WASM_DIR}/runtime/utils/utils.cmake) include (${WASM_DIR}/runtime/vmcore-wasm/vmcore.cmake) include (${WASM_DIR}/lib/native/base/wasm_lib_base.cmake) @@ -110,6 +109,7 @@ include (${SHARED_DIR}/coap/lib_coap.cmake) include_directories(${SHARED_DIR}/include) +include_directories(${WASM_DIR}/runtime/platform/include) include_directories(${CMAKE_CURRENT_LIST_DIR}/src) #Note: uncomment below line to use UART mode