Skip to content

Commit

Permalink
add testcases for shared heap and fix POP_MEM_OFFSET of memory64 (#3916)
Browse files Browse the repository at this point in the history
- add testcases for shared_heap
- fix POP_MEM_OFFSET and POP_TBL_ELEM_IDX of memory64

Signed-off-by: wenlingyun1 <[email protected]>
  • Loading branch information
WenLY1 authored Nov 24, 2024
1 parent dbdf3df commit b0c6d5c
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 22 deletions.
4 changes: 2 additions & 2 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/shared-heap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
132 changes: 115 additions & 17 deletions tests/unit/shared-heap/shared_heap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
}
35 changes: 34 additions & 1 deletion tests/unit/shared-heap/wasm-apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
)
)

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"
)
14 changes: 13 additions & 1 deletion tests/unit/shared-heap/wasm-apps/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 32 additions & 0 deletions tests/unit/shared-heap/wasm-apps/test_addr_conv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <stdio.h>

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;
}

0 comments on commit b0c6d5c

Please sign in to comment.