shared heap: Fix some issues and add basic unit test case (#3801)

Fix some issues and add basic unit test case for shared heap feature.

Signed-off-by: wenlingyun1 <wenlingyun1@xiaomi.com>
This commit is contained in:
WenLY1
2024-09-20 14:24:38 +08:00
committed by GitHub
parent 5e20cf383e
commit 4dacef2d60
11 changed files with 332 additions and 56 deletions

View File

@ -50,3 +50,4 @@ add_subdirectory(linux-perf)
add_subdirectory(gc)
add_subdirectory(memory64)
add_subdirectory(tid-allocator)
add_subdirectory(shared-heap)

View File

@ -0,0 +1,59 @@
# Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.14)
project(test-shared-heap)
add_definitions(-DRUN_ON_LINUX)
set(WAMR_BUILD_APP_FRAMEWORK 0)
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_FAST_INTERP 1)
set(WAMR_BUILD_JIT 0)
set(WAMR_BUILD_MEMORY64 1)
set(WAMR_BUILD_SHARED_HEAP 1)
# Compile wasm modules
add_subdirectory(wasm-apps)
# if only load this CMake other than load it as subdirectory
include(../unit_common.cmake)
set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm")
if (NOT EXISTS "${LLVM_SRC_ROOT}/build")
message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build")
endif ()
set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include(${IWASM_DIR}/compilation/iwasm_compl.cmake)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
set(UNIT_SOURCE ${source_all})
aux_source_directory(. SRC_LIST)
set(unit_test_sources
${UNIT_SOURCE}
${WAMR_RUNTIME_LIB_SOURCE}
${UNCOMMON_SHARED_SOURCE}
${SRC_LIST}
)
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(shared_heap_test ${unit_test_sources})
target_link_libraries(shared_heap_test ${LLVM_AVAILABLE_LIBS} gtest_main)
gtest_discover_tests(shared_heap_test)

View File

@ -0,0 +1,142 @@
/*
* Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "test_helper.h"
#include "gtest/gtest.h"
#include "bh_read_file.h"
#include "wasm_runtime_common.h"
class shared_heap_test : public testing::Test
{
protected:
virtual void SetUp() {}
static void SetUpTestCase() {}
virtual void TearDown() {}
WAMRRuntimeRAII<512 * 1024> runtime;
};
struct ret_env {
wasm_exec_env_t exec_env;
wasm_module_t wasm_module;
wasm_module_inst_t wasm_module_inst;
unsigned char *wasm_file_buf;
char error_buf[128];
};
struct ret_env
load_wasm(char *wasm_file_tested, unsigned int app_heap_size)
{
std::string wasm_mem_page = wasm_file_tested;
const char *wasm_file = strdup(wasm_mem_page.c_str());
wasm_module_inst_t wasm_module_inst = nullptr;
wasm_module_t wasm_module = nullptr;
wasm_exec_env_t exec_env = nullptr;
unsigned char *wasm_file_buf = nullptr;
unsigned int wasm_file_size = 0;
unsigned int stack_size = 16 * 1024, heap_size = app_heap_size;
char error_buf[128] = { 0 };
struct ret_env ret_module_env;
memset(ret_module_env.error_buf, 0, 128);
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
if (!wasm_file_buf) {
goto fail;
}
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
if (!wasm_module) {
memcpy(ret_module_env.error_buf, error_buf, 128);
goto fail;
}
wasm_module_inst = wasm_runtime_instantiate(
wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
if (!wasm_module_inst) {
memcpy(ret_module_env.error_buf, error_buf, 128);
goto fail;
}
exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
fail:
ret_module_env.exec_env = exec_env;
ret_module_env.wasm_module = wasm_module;
ret_module_env.wasm_module_inst = wasm_module_inst;
ret_module_env.wasm_file_buf = wasm_file_buf;
return ret_module_env;
}
void
destroy_module_env(struct ret_env module_env)
{
if (module_env.exec_env) {
wasm_runtime_destroy_exec_env(module_env.exec_env);
}
if (module_env.wasm_module_inst) {
wasm_runtime_deinstantiate(module_env.wasm_module_inst);
}
if (module_env.wasm_module) {
wasm_runtime_unload(module_env.wasm_module);
}
if (module_env.wasm_file_buf) {
wasm_runtime_free(module_env.wasm_file_buf);
}
}
TEST_F(shared_heap_test, test_shared_heap)
{
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);
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");
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);
if (!ret) {
printf("\nFailed to wasm_runtime_call_wasm!\n");
const char *s = wasm_runtime_get_exception(tmp_module_env.wasm_module_inst);
printf("exception: %s\n", s);
goto test_failed;
}
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);
}

View File

@ -0,0 +1,39 @@
# Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.14)
project(wasm-apps)
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
set(CMAKE_SYSTEM_PROCESSOR wasm32)
set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
if (NOT DEFINED WASI_SDK_DIR)
set(WASI_SDK_DIR "/opt/wasi-sdk")
endif ()
set(CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=8192 -nostdlib -O0")
set(CMAKE_C_COMPILER_TARGET "wasm32")
set(CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
set(DEFINED_SYMBOLS
"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
set(CMAKE_EXE_LINKER_FLAGS
"-Wl,--no-entry \
-Wl,--initial-memory=65536 \
-Wl,--export-all \
-Wl,--allow-undefined"
)
add_executable(test.wasm test.c)
target_link_libraries(test.wasm)
add_custom_command(TARGET test.wasm POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/test.wasm
${CMAKE_CURRENT_BINARY_DIR}/../
COMMENT "Copy test.wasm to the same directory of google test"
)

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2024 Xiaomi Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
extern void *
shared_malloc(int size);
extern void
shared_free(void *offset);
int
test()
{
int *ptr = (int *)shared_malloc(10);
*ptr = 10;
int a = *ptr;
shared_free(ptr);
return a;
}