diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 7041fd89c..834311f7e 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -593,8 +593,8 @@ wasm_interp_get_frame_ref(WASMInterpFrame *frame) #endif #if WASM_ENABLE_MEMORY64 != 0 -#define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : POP_I32()) -#define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : POP_I32()) +#define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : (uint32)POP_I32()) +#define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : (uint32)POP_I32()) #else #define POP_MEM_OFFSET() POP_I32() #define POP_TBL_ELEM_IDX() POP_I32() diff --git a/tests/unit/shared-heap/CMakeLists.txt b/tests/unit/shared-heap/CMakeLists.txt index 6baf420f8..2b06c537f 100644 --- a/tests/unit/shared-heap/CMakeLists.txt +++ b/tests/unit/shared-heap/CMakeLists.txt @@ -8,7 +8,7 @@ project(test-shared-heap) add_definitions(-DRUN_ON_LINUX) set(WAMR_BUILD_APP_FRAMEWORK 0) -set(WAMR_BUILD_AOT 0) +set(WAMR_BUILD_AOT 1) set(WAMR_BUILD_INTERP 1) set(WAMR_BUILD_FAST_INTERP 1) set(WAMR_BUILD_JIT 0) diff --git a/tests/unit/shared-heap/shared_heap_test.cc b/tests/unit/shared-heap/shared_heap_test.cc index 5e45d3111..deb4bbb38 100644 --- a/tests/unit/shared-heap/shared_heap_test.cc +++ b/tests/unit/shared-heap/shared_heap_test.cc @@ -92,37 +92,28 @@ destroy_module_env(struct ret_env module_env) } } -TEST_F(shared_heap_test, test_shared_heap) +static void test_shared_heap(WASMSharedHeap *shared_heap, const char *file, const char *func_name, uint32 argc, uint32 argv[]) { struct ret_env tmp_module_env; WASMFunctionInstanceCommon *func_test = nullptr; bool ret = false; - uint32 argv[1] = { 65535 }; const char *exception = nullptr; - SharedHeapInitArgs args; - WASMSharedHeap *shared_heap = nullptr; - args.size = 1024; - shared_heap = wasm_runtime_create_shared_heap(&args); - tmp_module_env = load_wasm((char *)"test.wasm", 0); + tmp_module_env = load_wasm((char *)file, 0); - if (!shared_heap) { - printf("Failed to create shared heap\n"); - goto test_failed; - } if (!wasm_runtime_attach_shared_heap(tmp_module_env.wasm_module_inst, shared_heap)) { printf("Failed to attach shared heap\n"); goto test_failed; } - func_test = wasm_runtime_lookup_function( - tmp_module_env.wasm_module_inst, "test"); + func_test = wasm_runtime_lookup_function(tmp_module_env.wasm_module_inst, + func_name); if (!func_test) { printf("\nFailed to wasm_runtime_lookup_function!\n"); goto test_failed; } ret = - wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, 1, argv); + wasm_runtime_call_wasm(tmp_module_env.exec_env, func_test, argc, argv); if (!ret) { printf("\nFailed to wasm_runtime_call_wasm!\n"); const char *s = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst); @@ -131,12 +122,119 @@ TEST_F(shared_heap_test, test_shared_heap) } wasm_runtime_detach_shared_heap(tmp_module_env.wasm_module_inst); - - EXPECT_EQ(10, argv[0]); - destroy_module_env(tmp_module_env); return; test_failed: destroy_module_env(tmp_module_env); EXPECT_EQ(1, 0); } + +TEST_F(shared_heap_test, test_shared_heap_basic) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test.wasm", "test", 1, argv); + EXPECT_EQ(10, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test.aot", "test", 1, argv); + EXPECT_EQ(10, argv[0]); + +} + +TEST_F(shared_heap_test, test_shared_heap_malloc_fail) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test.wasm", "test_malloc_fail", 1, argv); + EXPECT_EQ(1, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test.aot", "test_malloc_fail", 1, argv); + EXPECT_EQ(1, argv[0]); +} + +#ifndef native_function +#define native_function(func_name, signature) \ + { #func_name, (void *)glue_##func_name, signature, NULL } + +#endif +#ifndef nitems +#define nitems(_a) (sizeof(_a) / sizeof(0 [(_a)])) +#endif /* nitems */ +uintptr_t glue_test_addr_conv(wasm_exec_env_t env, uintptr_t addr) +{ + wasm_module_inst_t module_inst = get_module_inst(env); + uintptr_t ret; + void *native_addr = (void *)addr; + uintptr_t app_addr = addr_native_to_app(native_addr); + + native_addr = addr_app_to_native(app_addr); + if (native_addr != (void *)addr) + { + EXPECT_EQ(1, 0); + } + return app_addr; +} + +static NativeSymbol g_test_native_symbols[] = +{ + native_function(test_addr_conv,"(*)i"), +}; + +TEST_F(shared_heap_test, test_addr_conv) +{ + SharedHeapInitArgs args; + WASMSharedHeap *shared_heap = nullptr; + uint32 argv[1] = { 0 }; + struct ret_env tmp_module_env; + WASMFunctionInstanceCommon *func_test = nullptr; + bool ret = false; + const char *exception = nullptr; + wasm_module_inst_t module_inst = tmp_module_env.wasm_module_inst; + + ret = wasm_native_register_natives("env", g_test_native_symbols, + nitems(g_test_native_symbols)); + if (!ret) + { + EXPECT_EQ(1, 0); + return; + } + + args.size = 1024; + shared_heap = wasm_runtime_create_shared_heap(&args); + if (!shared_heap) { + printf("Failed to create shared heap\n"); + EXPECT_EQ(1, 0); + } + + // test wasm + test_shared_heap(shared_heap, "test_addr_conv.wasm", "test", 1, argv); + EXPECT_EQ(1, argv[0]); + + // test aot + test_shared_heap(shared_heap, "test_addr_conv.aot", "test", 1, argv); + EXPECT_EQ(1, argv[0]); +} diff --git a/tests/unit/shared-heap/wasm-apps/CMakeLists.txt b/tests/unit/shared-heap/wasm-apps/CMakeLists.txt index 3627a2c14..097f66ae5 100644 --- a/tests/unit/shared-heap/wasm-apps/CMakeLists.txt +++ b/tests/unit/shared-heap/wasm-apps/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.14) project(wasm-apps) set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) +set(WAMRC_ROOT_DIR ${WAMR_ROOT_DIR}/wamr-compiler/build) set(CMAKE_SYSTEM_PROCESSOR wasm32) set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot) @@ -36,4 +37,36 @@ add_custom_command(TARGET test.wasm POST_BUILD ${CMAKE_CURRENT_BINARY_DIR}/test.wasm ${CMAKE_CURRENT_BINARY_DIR}/../ COMMENT "Copy test.wasm to the same directory of google test" - ) \ No newline at end of file + ) + +add_custom_command(TARGET test.wasm POST_BUILD + COMMAND ${WAMRC_ROOT_DIR}/wamrc --opt-level=0 --enable-shared-heap --bounds-checks=1 + -o + test.aot + test.wasm + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test.aot + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test.aot to the same directory of google test" + ) + +add_executable(test_addr_conv.wasm test_addr_conv.c) +target_link_libraries(test.wasm) + +add_custom_command(TARGET test_addr_conv.wasm POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.wasm + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test_addr_conv.wasm to the same directory of google test" + ) + +add_custom_command(TARGET test_addr_conv.wasm POST_BUILD + COMMAND ${WAMRC_ROOT_DIR}/wamrc --opt-level=0 --enable-shared-heap --bounds-checks=1 + -o + test_addr_conv.aot + test_addr_conv.wasm + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/test_addr_conv.aot + ${CMAKE_CURRENT_BINARY_DIR}/../ + COMMENT "Copy test_addr_conv.aot to the same directory of google test" + ) diff --git a/tests/unit/shared-heap/wasm-apps/test.c b/tests/unit/shared-heap/wasm-apps/test.c index b83ee64ff..bd0df19c2 100644 --- a/tests/unit/shared-heap/wasm-apps/test.c +++ b/tests/unit/shared-heap/wasm-apps/test.c @@ -13,10 +13,22 @@ shared_heap_free(void *offset); int test() { - int *ptr = (int *)shared_heap_malloc(10); + int *ptr = (int *)shared_heap_malloc(4); *ptr = 10; int a = *ptr; shared_heap_free(ptr); return a; } + +int +test_malloc_fail() +{ + int *ptr = (int *)shared_heap_malloc(8192); + + if (ptr == NULL) { + return 1; + } + shared_heap_free(ptr); + return 0; +} diff --git a/tests/unit/shared-heap/wasm-apps/test_addr_conv.c b/tests/unit/shared-heap/wasm-apps/test_addr_conv.c new file mode 100644 index 000000000..f91764c84 --- /dev/null +++ b/tests/unit/shared-heap/wasm-apps/test_addr_conv.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2024 Xiaomi Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include + +extern void * +shared_heap_malloc(int size); +extern void +shared_heap_free(void *offset); +extern void * +test_addr_conv(void *ptr); + +int +test() +{ + int *ptr = NULL; + int *ptr2 = NULL; + + ptr = (int *)shared_heap_malloc(4); + + if (ptr == NULL) { + return 0; + } + ptr2 = test_addr_conv(ptr); + if (ptr2 != ptr) { + return 0; + } + shared_heap_free(ptr); + return 1; +}