Add unit test suites (#3490)

This commit is contained in:
Zhang, Yi
2024-06-04 11:24:27 +08:00
committed by GitHub
parent 0a80cc4e94
commit 380cd7b0e7
194 changed files with 14104 additions and 34 deletions

View File

@ -0,0 +1,73 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 2.9)
project(test-runtime-common)
add_definitions(-DRUN_ON_LINUX)
set(WAMR_BUILD_LIBC_WASI 0)
set(WAMR_BUILD_APP_FRAMEWORK 0)
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_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}
)
add_executable(runtime_common_test ${unit_test_sources})
target_link_libraries(runtime_common_test ${LLVM_AVAILABLE_LIBS} gtest_main)
# Ensure that aot compiled is completed before linear_memory_test_aot is built
set(dummy_output "${CMAKE_CURRENT_BINARY_DIR}/dummy_output")
add_custom_command(OUTPUT ${dummy_output}
COMMAND ./build_aot.sh
COMMAND ${CMAKE_COMMAND} -E touch ${dummy_output}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/build_aot.sh
COMMENT "Executing script to compile aot files"
VERBATIM
)
add_custom_target(
BuildAot ALL
DEPENDS ${dummy_output}
)
add_dependencies(runtime_common_test BuildAot)
add_custom_command(TARGET runtime_common_test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.wasm ${CMAKE_CURRENT_LIST_DIR}/wasm-apps/main.aot
${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Copy main.wasm and main.aot to the directory: build/runtime-common."
)
gtest_discover_tests(runtime_common_test)

View File

@ -0,0 +1,31 @@
#!/bin/bash
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# Define a list of .wasm files
file_names=("main")
WORKDIR="$PWD"
WAMRC_ROOT_DIR="${WORKDIR}/../../../wamr-compiler"
WAMRC="${WAMRC_ROOT_DIR}/build/wamrc"
WAST2WASM="/opt/wabt/bin/wat2wasm"
# build wamrc if not exist
if [ ! -s "$WAMRC" ]; then
cd $WAMRC_ROOT_DIR
if [ -d "$WAMRC/build" ]; then
rm -r build
fi
cmake -B build && cmake --build build -j $(nproc)
cd $WORKDIR
fi
# Iterate over the files array
for file_name in "${file_names[@]}"; do
# compile wasm to aot
$WAMRC -o "wasm-apps/${file_name}.aot" "wasm-apps/${file_name}.wasm"
done

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "test_helper.h"
#include "gtest/gtest.h"
#include "wasm_exec_env.h"
class wasm_exec_env_test_suite : public testing::Test
{
protected:
// You should make the members protected s.t. they can be
// accessed from sub-classes.
// virtual void SetUp() will be called before each test is run. You
// should define it if you need to initialize the varaibles.
// Otherwise, this can be skipped.
virtual void SetUp() {}
// virtual void TearDown() will be called after each test is run.
// You should define it if there is cleanup work to do. Otherwise,
// you don't have to provide it.
//
virtual void TearDown() {}
};
TEST_F(wasm_exec_env_test_suite, wasm_exec_env_create)
{
EXPECT_EQ(nullptr, wasm_exec_env_create(nullptr, 0));
}
TEST_F(wasm_exec_env_test_suite, wasm_exec_env_create_internal)
{
EXPECT_EQ(nullptr, wasm_exec_env_create_internal(nullptr, UINT32_MAX));
}
TEST_F(wasm_exec_env_test_suite, wasm_exec_env_pop_jmpbuf)
{
WASMExecEnv exec_env;
exec_env.jmpbuf_stack_top = nullptr;
EXPECT_EQ(nullptr, wasm_exec_env_pop_jmpbuf(&exec_env));
}

View File

@ -0,0 +1,682 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "test_helper.h"
#include "gtest/gtest.h"
#include "platform_common.h"
#include "wasm_runtime_common.h"
#include "bh_read_file.h"
#include "wasm_runtime.h"
#include "bh_platform.h"
#include "wasm_export.h"
#include "aot_runtime.h"
using namespace std;
extern "C" {
uint32
wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
uint32 size, void **p_native_addr);
bool
wasm_runtime_create_exec_env_and_call_wasm(
WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
uint32 argc, uint32 argv[]);
}
static bh_list loading_module_list_head;
static bh_list *const loading_module_list = &loading_module_list_head;
static korp_mutex loading_module_list_lock;
static std::string CWD;
static std::string MAIN_WASM = "/main.wasm";
static std::string MAIN_AOT = "/main.aot";
static char *WASM_FILE_1;
static char *AOT_FILE_1;
static std::string
get_binary_path()
{
char cwd[1024];
memset(cwd, 0, 1024);
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
}
char *path_end = strrchr(cwd, '/');
if (path_end != NULL) {
*path_end = '\0';
}
return std::string(cwd);
}
class wasm_runtime_common_test_suite : public testing::Test
{
protected:
// You should make the members protected s.t. they can be
// accessed from sub-classes.
// virtual void SetUp() will be called before each test is run. You
// should define it if you need to initialize the varaibles.
// Otherwise, this can be skipped.
virtual void SetUp() {}
static void SetUpTestCase()
{
CWD = get_binary_path();
WASM_FILE_1 = strdup((CWD + MAIN_WASM).c_str());
AOT_FILE_1 = strdup((CWD + MAIN_AOT).c_str());
}
// virtual void TearDown() will be called after each test is run.
// You should define it if there is cleanup work to do. Otherwise,
// you don't have to provide it.
//
virtual void TearDown() {}
static void TearDownTestCase()
{
free(WASM_FILE_1);
free(AOT_FILE_1);
}
WAMRRuntimeRAII<512 * 1024> runtime;
};
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_destroy)
{
wasm_runtime_init();
wasm_runtime_destroy();
}
static bool
reader_test(package_type_t module_type, const char *module_name,
uint8 **p_buffer, uint32 *p_size)
{
return true;
}
static void
destroyer_test(uint8 *buffer, uint32 size)
{}
TEST_F(wasm_runtime_common_test_suite,
set_module_reader_get_module_reader_get_module_destroyer)
{
wasm_runtime_set_module_reader(reader_test, destroyer_test);
EXPECT_EQ((module_reader)reader_test, wasm_runtime_get_module_reader());
EXPECT_EQ((module_destroyer)destroyer_test,
wasm_runtime_get_module_destroyer());
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_register_module)
{
const char *wasm_file = WASM_FILE_1;
wasm_module_t wasm_module = nullptr;
unsigned char *wasm_file_buf = nullptr;
unsigned int wasm_file_size = 0;
char error_buf[128] = { 0 };
char module_name[] = "module_test";
char module_name_1[] = "module_test_1";
// Normal situation.
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
EXPECT_NE(wasm_module, nullptr);
EXPECT_NE(false,
wasm_runtime_register_module("module_test", wasm_module,
error_buf, sizeof(error_buf)));
// Abnormal situation.
EXPECT_EQ(false,
wasm_runtime_register_module(nullptr, nullptr, nullptr, 0));
EXPECT_EQ(false, wasm_runtime_register_module(
"module_test", nullptr, error_buf, sizeof(error_buf)));
EXPECT_EQ(false, wasm_runtime_register_module("module_test", wasm_module,
nullptr, sizeof(error_buf)));
EXPECT_EQ(false, wasm_runtime_register_module("module_test", wasm_module,
error_buf, 0));
EXPECT_EQ(false, wasm_runtime_register_module(
nullptr, wasm_module, error_buf, sizeof(error_buf)));
EXPECT_EQ(false, wasm_runtime_register_module(nullptr, nullptr, error_buf,
sizeof(error_buf)));
EXPECT_EQ(true, wasm_runtime_register_module(module_name, wasm_module,
error_buf, sizeof(error_buf)));
EXPECT_EQ(false, wasm_runtime_register_module_internal(nullptr, wasm_module,
NULL, 0, error_buf,
sizeof(error_buf)));
EXPECT_EQ(false, wasm_runtime_register_module_internal(
module_name_1, wasm_module, NULL, 0, error_buf,
sizeof(error_buf)));
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_unregister_module)
{
wasm_runtime_unregister_module(nullptr);
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_find_module_registered)
{
EXPECT_EQ(nullptr, wasm_runtime_find_module_registered("module_test"));
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_is_module_registered)
{
EXPECT_EQ(nullptr, wasm_runtime_find_module_registered(""));
}
/* TODO: add thread safety test. */
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_add_loading_module)
{
EXPECT_EQ(true, wasm_runtime_add_loading_module(nullptr, nullptr, 0));
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_destroy_loading_module_list)
{
os_mutex_init(&loading_module_list_lock);
wasm_runtime_destroy_loading_module_list();
os_mutex_destroy(&loading_module_list_lock);
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_is_built_in_module)
{
EXPECT_EQ(true, wasm_runtime_is_built_in_module("env"));
EXPECT_EQ(true, wasm_runtime_is_built_in_module("wasi_unstable"));
EXPECT_EQ(true, wasm_runtime_is_built_in_module("wasi_snapshot_preview1"));
EXPECT_EQ(true, wasm_runtime_is_built_in_module(""));
EXPECT_EQ(false, wasm_runtime_is_built_in_module("test"));
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_read_v128)
{
unsigned char buf[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
unsigned char ret1[8] = { 0 };
unsigned char ret2[8] = { 0 };
wasm_runtime_read_v128((const uint8 *)buf, (uint64 *)ret1, (uint64 *)ret2);
EXPECT_EQ(0, strncmp("01234567", (const char *)ret1, 8));
EXPECT_EQ(0, strncmp("89ABCDEF", (const char *)ret2, 8));
}
TEST_F(wasm_runtime_common_test_suite,
wasm_runtime_show_app_heap_corrupted_prompt)
{
wasm_runtime_show_app_heap_corrupted_prompt();
}
TEST_F(wasm_runtime_common_test_suite, wasm_runtime_is_xip_file)
{
// WASM file.
const char *wasm_file = WASM_FILE_1;
unsigned int wasm_file_size = 0;
unsigned char *wasm_file_buf = nullptr;
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
EXPECT_EQ(false, wasm_runtime_is_xip_file(wasm_file_buf, wasm_file_size));
// AoT file.
const char *aot_file = AOT_FILE_1;
unsigned int aot_file_size = 0;
unsigned char *aot_file_buf = nullptr;
aot_file_buf =
(unsigned char *)bh_read_file_to_buffer(aot_file, &aot_file_size);
EXPECT_NE(aot_file_buf, nullptr);
EXPECT_EQ(false, wasm_runtime_is_xip_file(aot_file_buf, aot_file_size));
}
TEST_F(wasm_runtime_common_test_suite, get_package_type)
{
const char *wasm_file = WASM_FILE_1;
unsigned int wasm_file_size = 0;
unsigned char *wasm_file_buf = nullptr;
// WASM file.
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
EXPECT_EQ(Wasm_Module_Bytecode,
get_package_type(wasm_file_buf, wasm_file_size));
// WASM file. Abnormally.
wasm_file_buf[3] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[2] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[1] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[0] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
EXPECT_EQ(Package_Type_Unknown, get_package_type(wasm_file_buf, 0));
EXPECT_EQ(Package_Type_Unknown, get_package_type(nullptr, 0));
// AoT file.
const char *wasm_file_aot = AOT_FILE_1;
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file_aot, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
EXPECT_EQ(Wasm_Module_AoT, get_package_type(wasm_file_buf, wasm_file_size));
// AoT file. Abnormally.
wasm_file_buf[3] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[2] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[1] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
wasm_file_buf[0] = -1;
EXPECT_EQ(Package_Type_Unknown,
get_package_type(wasm_file_buf, wasm_file_size));
EXPECT_EQ(Package_Type_Unknown, get_package_type(wasm_file_buf, 0));
EXPECT_EQ(Package_Type_Unknown, get_package_type(nullptr, 0));
}
TEST_F(wasm_runtime_common_test_suite, functions_on_wasm_module)
{
const char *wasm_file = WASM_FILE_1;
wasm_module_inst_t wasm_module_inst = nullptr;
wasm_module_t wasm_module = nullptr;
wasm_exec_env_t exec_env = nullptr;
wasm_exec_env_t exec_env_1 = nullptr;
unsigned char *wasm_file_buf = nullptr;
WASMFunctionInstanceCommon *func = nullptr;
const char *user_data = "test";
unsigned int wasm_file_size = 0;
unsigned int stack_size = 16 * 1024, heap_size = 16 * 1024;
char error_buf[128] = { 0 };
unsigned int argv[2] = { 0 };
WASMType *func_type = nullptr;
wasm_val_t arguments[1];
char str_test[] = "This is a test.";
char str_exception[] = "Exception: ";
char str_tmp[60] = { 0 };
void *ptr_tmp = nullptr;
unsigned int offset_tmp = 0;
unsigned int tmp = 0;
unsigned char *p_native_start_addr = nullptr;
unsigned char *p_native_end_addr = nullptr;
NativeSymbol *native_symbols;
uint32 n_native_symbols;
const char *exception_test = nullptr;
arguments[0].kind = WASM_I32;
arguments[0].of.i32 = 0;
// Create exec_env.
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
EXPECT_NE(wasm_module, nullptr);
wasm_module_inst = wasm_runtime_instantiate(
wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
EXPECT_NE(wasm_module_inst, nullptr);
exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
EXPECT_NE(exec_env, nullptr);
// Operations on exec_env.
EXPECT_EQ(true, wasm_runtime_register_module_internal("test", wasm_module,
nullptr, 0, error_buf,
sizeof(error_buf)));
EXPECT_NE(nullptr, wasm_runtime_find_module_registered("test"));
EXPECT_EQ(wasm_module_inst, wasm_runtime_get_module_inst(exec_env));
EXPECT_EQ(exec_env->attachment,
wasm_runtime_get_function_attachment(exec_env));
EXPECT_EQ(wasm_module, wasm_exec_env_get_module(exec_env));
wasm_runtime_set_user_data(exec_env, (void *)user_data);
EXPECT_EQ((void *)user_data, wasm_runtime_get_user_data(exec_env));
func = wasm_runtime_lookup_function(wasm_module_inst, "on_timer_event");
func_type =
wasm_runtime_get_function_type(func, wasm_module_inst->module_type);
EXPECT_NE(func_type, nullptr);
EXPECT_EQ(false, wasm_runtime_call_wasm(exec_env, func, 0, argv));
exception_test = wasm_runtime_get_exception(wasm_module_inst);
EXPECT_NE(nullptr, exception_test);
EXPECT_EQ(false, wasm_runtime_call_wasm_a(exec_env, func, 0, nullptr, 1,
arguments));
exception_test = wasm_runtime_get_exception(wasm_module_inst);
EXPECT_NE(nullptr, exception_test);
WASMFunctionInstance func_test_1;
WASMFunction wasm_func_test;
WASMType wasm_type_test;
wasm_func_test.func_type = &wasm_type_test;
func_test_1.u.func = &wasm_func_test;
func_test_1.u.func->func_type->param_count = 1;
func_test_1.u.func->func_type->param_cell_num = 2;
func_test_1.u.func->func_type->types[0] = VALUE_TYPE_I64;
func_test_1.u.func->max_stack_cell_num = 10;
EXPECT_EQ(false, wasm_runtime_call_wasm_v(
exec_env, (WASMFunctionInstanceCommon *)(&func_test_1),
0, nullptr, 1, arguments));
func_test_1.u.func->func_type->types[0] = VALUE_TYPE_F32;
EXPECT_EQ(false, wasm_runtime_call_wasm_v(
exec_env, (WASMFunctionInstanceCommon *)(&func_test_1),
0, nullptr, 1, arguments));
func_test_1.u.func->func_type->types[0] = VALUE_TYPE_F64;
EXPECT_EQ(false, wasm_runtime_call_wasm_v(
exec_env, (WASMFunctionInstanceCommon *)(&func_test_1),
0, nullptr, 1, arguments));
#if 0
WASMFunctionInstance func_test;
WASMFunctionImport func_import_test;
WASMType *func_type_1 = nullptr;
func_import_test.func_type = func_type;
func_test.u.func_import = &func_import_test;
func_test.is_import_func = true;
func_type_1 = wasm_runtime_get_function_type(&func_test,
wasm_module_inst->module_type);
EXPECT_NE(func_type_1, nullptr);
#endif
EXPECT_EQ(true, wasm_runtime_create_exec_env_singleton(wasm_module_inst));
EXPECT_NE(nullptr, wasm_runtime_get_exec_env_singleton(wasm_module_inst));
wasm_runtime_set_exception(wasm_module_inst, str_test);
sprintf(str_tmp, "%s%s", str_exception, str_test);
EXPECT_EQ(0, strcmp(str_tmp, wasm_runtime_get_exception(wasm_module_inst)));
wasm_runtime_clear_exception(wasm_module_inst);
EXPECT_EQ(nullptr, wasm_runtime_get_exception(wasm_module_inst));
wasm_runtime_set_custom_data(wasm_module_inst, (void *)user_data);
EXPECT_EQ((void *)user_data,
wasm_runtime_get_custom_data(wasm_module_inst));
offset_tmp = wasm_runtime_module_malloc(wasm_module_inst, 10, &ptr_tmp);
EXPECT_NE(0, offset_tmp);
EXPECT_EQ(true,
wasm_runtime_validate_app_addr(wasm_module_inst, offset_tmp, 10));
EXPECT_EQ(ptr_tmp,
wasm_runtime_addr_app_to_native(wasm_module_inst, offset_tmp));
EXPECT_EQ(true,
wasm_runtime_validate_native_addr(wasm_module_inst, ptr_tmp, 10));
EXPECT_EQ(offset_tmp,
wasm_runtime_addr_native_to_app(wasm_module_inst, ptr_tmp));
EXPECT_EQ(true, wasm_runtime_get_native_addr_range(
wasm_module_inst, (unsigned char *)ptr_tmp,
&p_native_start_addr, &p_native_end_addr));
EXPECT_NE(0, wasm_runtime_module_realloc(wasm_module_inst, offset_tmp, 100,
&ptr_tmp));
/* can't test like that since shrink size optimization will be applied */
/* EXPECT_EQ(false,
wasm_enlarge_memory((WASMModuleInstance *)wasm_module_inst, 1));
*/
EXPECT_EQ(offset_tmp,
wasm_runtime_addr_native_to_app(wasm_module_inst, ptr_tmp));
EXPECT_EQ(true, wasm_runtime_get_native_addr_range(
wasm_module_inst, (unsigned char *)ptr_tmp,
&p_native_start_addr, &p_native_end_addr));
offset_tmp = wasm_runtime_module_dup_data(wasm_module_inst, str_test,
sizeof(str_test));
EXPECT_EQ(0, strcmp(str_test, (char *)wasm_runtime_addr_app_to_native(
wasm_module_inst, offset_tmp)));
EXPECT_EQ(true,
wasm_runtime_validate_app_str_addr(wasm_module_inst, offset_tmp));
((WASMModuleInstance *)wasm_module_inst)->exec_env_singleton = nullptr;
EXPECT_NE(nullptr, wasm_runtime_get_exec_env_singleton(wasm_module_inst));
EXPECT_EQ(false, wasm_runtime_call_wasm(nullptr, func, 0, argv));
wasm_runtime_set_exception(wasm_module_inst, str_test);
EXPECT_EQ(false, wasm_runtime_call_wasm(exec_env, func, 0, argv));
wasm_runtime_clear_exception(wasm_module_inst);
EXPECT_EQ(false, wasm_runtime_call_wasm_a(exec_env, func, 0, nullptr, 2,
arguments));
WASMFunctionInstance *func_test_call_wasm_a_ptr =
(WASMFunctionInstance *)func;
func_test_call_wasm_a_ptr->u.func->func_type->ret_cell_num = 10;
EXPECT_EQ(true, wasm_runtime_call_wasm_a(exec_env, func, 0, nullptr, 1,
arguments));
// Destroy.
wasm_runtime_module_free(wasm_module_inst, offset_tmp);
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(wasm_module_inst);
wasm_runtime_unload(wasm_module);
if (wasm_file_buf) {
wasm_runtime_free(wasm_file_buf);
}
}
TEST_F(wasm_runtime_common_test_suite, functions_on_aot_module)
{
const char *wasm_file = AOT_FILE_1;
wasm_module_inst_t wasm_module_inst = nullptr;
wasm_module_t wasm_module = nullptr;
wasm_exec_env_t exec_env = nullptr;
wasm_exec_env_t exec_env_1 = nullptr;
unsigned char *wasm_file_buf = nullptr;
WASMFunctionInstanceCommon *func = nullptr;
const char *user_data = "test";
unsigned int wasm_file_size = 0;
unsigned int stack_size = 16 * 1024, heap_size = 16 * 1024;
char error_buf[128] = { 0 };
unsigned int argv[2] = { 0 };
WASMType *func_type = nullptr;
wasm_val_t arguments[1];
char str_test[] = "This is a test.";
char str_exception[] = "Exception: ";
char str_tmp[60] = { 0 };
void *ptr_tmp = nullptr;
unsigned int offset_tmp = 0;
unsigned int tmp = 0;
unsigned char *p_native_start_addr = nullptr;
unsigned char *p_native_end_addr = nullptr;
NativeSymbol *native_symbols;
uint32 n_native_symbols;
const char *exception_test = nullptr;
arguments[0].kind = WASM_I32;
arguments[0].of.i32 = 0;
// Create exec_env.
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
EXPECT_NE(wasm_module, nullptr);
wasm_module_inst = wasm_runtime_instantiate(
wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
EXPECT_NE(wasm_module_inst, nullptr);
exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
EXPECT_NE(exec_env, nullptr);
// Operations on exec_env.
EXPECT_EQ(true, wasm_runtime_register_module_internal("test", wasm_module,
nullptr, 0, error_buf,
sizeof(error_buf)));
EXPECT_NE(nullptr, wasm_runtime_find_module_registered("test"));
EXPECT_EQ(wasm_module_inst, wasm_runtime_get_module_inst(exec_env));
EXPECT_EQ(exec_env->attachment,
wasm_runtime_get_function_attachment(exec_env));
EXPECT_EQ(wasm_module, wasm_exec_env_get_module(exec_env));
wasm_runtime_set_user_data(exec_env, (void *)user_data);
EXPECT_EQ((void *)user_data, wasm_runtime_get_user_data(exec_env));
func = wasm_runtime_lookup_function(wasm_module_inst, "on_timer_event");
func_type =
wasm_runtime_get_function_type(func, wasm_module_inst->module_type);
EXPECT_NE(func_type, nullptr);
EXPECT_EQ(false, wasm_runtime_call_wasm(exec_env, func, 0, argv));
exception_test = wasm_runtime_get_exception(wasm_module_inst);
EXPECT_NE(nullptr, exception_test);
EXPECT_EQ(false, wasm_runtime_call_wasm_a(exec_env, func, 0, nullptr, 1,
arguments));
exception_test = wasm_runtime_get_exception(wasm_module_inst);
EXPECT_NE(nullptr, exception_test);
EXPECT_EQ(false, wasm_runtime_call_wasm_v(exec_env, func, 0, nullptr, 1,
arguments));
exception_test = wasm_runtime_get_exception(wasm_module_inst);
EXPECT_NE(nullptr, exception_test);
AOTFunctionInstance func_test;
AOTImportFunc func_import_test;
func_test.u.func_import = &func_import_test;
func_import_test.func_type = (AOTFuncType *)func_type;
func_test.is_import_func = true;
EXPECT_NE(nullptr, wasm_runtime_get_function_type(
&func_test, wasm_module_inst->module_type));
EXPECT_EQ(true, wasm_runtime_create_exec_env_singleton(wasm_module_inst));
EXPECT_NE(nullptr, wasm_runtime_get_exec_env_singleton(wasm_module_inst));
wasm_runtime_set_exception(wasm_module_inst, str_test);
sprintf(str_tmp, "%s%s", str_exception, str_test);
EXPECT_EQ(0, strcmp(str_tmp, wasm_runtime_get_exception(wasm_module_inst)));
wasm_runtime_clear_exception(wasm_module_inst);
EXPECT_EQ(nullptr, wasm_runtime_get_exception(wasm_module_inst));
wasm_runtime_set_custom_data(wasm_module_inst, (void *)user_data);
EXPECT_EQ((void *)user_data,
wasm_runtime_get_custom_data(wasm_module_inst));
offset_tmp = wasm_runtime_module_malloc(wasm_module_inst, 10, &ptr_tmp);
EXPECT_NE(0, offset_tmp);
EXPECT_EQ(true,
wasm_runtime_validate_app_addr(wasm_module_inst, offset_tmp, 10));
EXPECT_EQ(ptr_tmp,
wasm_runtime_addr_app_to_native(wasm_module_inst, offset_tmp));
EXPECT_EQ(true,
wasm_runtime_validate_native_addr(wasm_module_inst, ptr_tmp, 10));
EXPECT_EQ(offset_tmp,
wasm_runtime_addr_native_to_app(wasm_module_inst, ptr_tmp));
EXPECT_EQ(true, wasm_runtime_get_native_addr_range(
wasm_module_inst, (unsigned char *)ptr_tmp,
&p_native_start_addr, &p_native_end_addr));
EXPECT_NE(0, wasm_runtime_module_realloc(wasm_module_inst, offset_tmp, 100,
&ptr_tmp));
/* can't test like that since shrink size optimization will be applied */
/* EXPECT_EQ(false,
wasm_enlarge_memory((WASMModuleInstance *)wasm_module_inst, 1));
*/
offset_tmp = wasm_runtime_module_dup_data(wasm_module_inst, str_test,
sizeof(str_test));
EXPECT_EQ(0, strcmp(str_test, (char *)wasm_runtime_addr_app_to_native(
wasm_module_inst, offset_tmp)));
EXPECT_EQ(true,
wasm_runtime_validate_app_str_addr(wasm_module_inst, offset_tmp));
((WASMModuleInstance *)wasm_module_inst)->exec_env_singleton = nullptr;
EXPECT_NE(nullptr, wasm_runtime_get_exec_env_singleton(wasm_module_inst));
// Destroy.
wasm_runtime_module_free(wasm_module_inst, offset_tmp);
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(wasm_module_inst);
wasm_runtime_unload(wasm_module);
if (wasm_file_buf) {
wasm_runtime_free(wasm_file_buf);
}
}
TEST_F(wasm_runtime_common_test_suite, functions_on_module_type_unknown)
{
const char *wasm_file = AOT_FILE_1;
wasm_module_inst_t wasm_module_inst = nullptr;
wasm_module_t wasm_module = nullptr;
wasm_exec_env_t exec_env = nullptr;
wasm_exec_env_t exec_env_1 = nullptr;
unsigned char *wasm_file_buf = nullptr;
WASMFunctionInstanceCommon *func = nullptr;
const char *user_data = "test";
unsigned int wasm_file_size = 0;
unsigned int stack_size = 16 * 1024, heap_size = 16 * 1024;
char error_buf[128] = { 0 };
unsigned int argv[2] = { 0 };
WASMType *func_type = nullptr;
wasm_val_t arguments[1];
char str_test[] = "This is a test.";
char str_exception[] = "Exception: ";
char str_tmp[60] = { 0 };
void *ptr_tmp = nullptr;
unsigned int offset_tmp = 0;
unsigned int tmp = 0;
unsigned char *p_native_start_addr = nullptr;
unsigned char *p_native_end_addr = nullptr;
const char *exception_test = nullptr;
arguments[0].kind = WASM_I32;
arguments[0].of.i32 = 0;
// Create exec_env.
wasm_runtime_unregister_module(wasm_module);
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(wasm_file, &wasm_file_size);
EXPECT_NE(wasm_file_buf, nullptr);
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
EXPECT_NE(wasm_module, nullptr);
wasm_module_inst = wasm_runtime_instantiate(
wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
EXPECT_NE(wasm_module_inst, nullptr);
exec_env = wasm_runtime_create_exec_env(wasm_module_inst, stack_size);
EXPECT_NE(exec_env, nullptr);
// wasm_module_inst->module_type = Package_Type_Unknown.
wasm_module_inst->module_type = Package_Type_Unknown;
EXPECT_DEATH(wasm_exec_env_get_module(exec_env), "");
EXPECT_DEATH(
wasm_runtime_validate_app_str_addr(wasm_module_inst, offset_tmp), "");
// wasm_module->module_type = Package_Type_Unknown.
wasm_module->module_type = Package_Type_Unknown;
EXPECT_EQ(nullptr,
wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
error_buf, sizeof(error_buf)));
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
/* Reload unmodified buffer should be valid now */
EXPECT_NE(wasm_module, nullptr);
wasm_file_buf[3] = -1;
wasm_file_buf[2] = -1;
wasm_file_buf[1] = -1;
wasm_file_buf[0] = -1;
wasm_module =
wasm_runtime_load(wasm_file_buf, 0, error_buf, sizeof(error_buf));
EXPECT_EQ(wasm_module, nullptr);
wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
sizeof(error_buf));
EXPECT_EQ(wasm_module, nullptr);
// Destroy.
wasm_runtime_module_free(wasm_module_inst, offset_tmp);
wasm_runtime_destroy_exec_env(exec_env);
wasm_runtime_deinstantiate(wasm_module_inst);
wasm_runtime_unload(wasm_module);
if (wasm_file_buf) {
wasm_runtime_free(wasm_file_buf);
}
}

View File

@ -0,0 +1,162 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "test_helper.h"
#include "gtest/gtest.h"
#include "platform_common.h"
#include "wasm_runtime_common.h"
#include "bh_read_file.h"
#include "wasm_runtime.h"
#include "bh_platform.h"
#include "wasm_export.h"
using namespace std;
extern "C" {
uint32
wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
uint32 size, void **p_native_addr);
bool
wasm_runtime_create_exec_env_and_call_wasm(
WASMModuleInstanceCommon *module_inst, WASMFunctionInstanceCommon *function,
uint32 argc, uint32 argv[]);
}
static char global_heap_buf[100 * 1024 * 1024] = { 0 };
static std::string CWD;
static std::string MAIN_WASM = "/main.wasm";
static std::string MAIN_AOT = "/main.aot";
static char *WASM_FILE_1;
static char *AOT_FILE_1;
static int
foo(int a, int b);
static int
foo_native(wasm_exec_env_t exec_env, int a, int b)
{
return a + b;
}
static NativeSymbol native_symbols[] = { {
"foo", // the name of WASM function name
(void *)foo_native, // the native function pointer
"(ii)i" // the function prototype signature
} };
static std::string
get_binary_path()
{
char cwd[1024];
memset(cwd, 0, 1024);
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
}
char *path_end = strrchr(cwd, '/');
if (path_end != NULL) {
*path_end = '\0';
}
return std::string(cwd);
}
class wasm_runtime_init_test_suite : public testing::Test
{
protected:
// You should make the members protected s.t. they can be
// accessed from sub-classes.
// virtual void SetUp() will be called before each test is run. You
// should define it if you need to initialize the varaibles.
// Otherwise, this can be skipped.
virtual void SetUp() {}
static void SetUpTestCase()
{
CWD = get_binary_path();
WASM_FILE_1 = strdup((CWD + MAIN_WASM).c_str());
AOT_FILE_1 = strdup((CWD + MAIN_AOT).c_str());
}
// virtual void TearDown() will be called after each test is run.
// You should define it if there is cleanup work to do. Otherwise,
// you don't have to provide it.
//
virtual void TearDown() {}
static void TearDownTestCase()
{
free(WASM_FILE_1);
free(AOT_FILE_1);
}
};
TEST_F(wasm_runtime_init_test_suite, init_and_register_natives)
{
EXPECT_EQ(true, wasm_runtime_init());
int n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
EXPECT_EQ(true, wasm_runtime_register_natives("env", native_symbols,
n_native_symbols));
EXPECT_EQ(true, wasm_runtime_register_natives_raw("env", native_symbols,
n_native_symbols));
wasm_runtime_destroy();
}
TEST_F(wasm_runtime_init_test_suite, init_thread_env_destroy_thread_env)
{
EXPECT_EQ(true, wasm_runtime_init_thread_env());
wasm_runtime_destroy_thread_env();
}
TEST_F(wasm_runtime_init_test_suite, wasm_runtime_full_init)
{
RuntimeInitArgs init_args;
unsigned char *wasm_file_buf;
uint32 wasm_file_size;
wasm_module_t module = nullptr;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
wasm_runtime_destroy();
init_args.n_native_symbols = 1;
EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
wasm_runtime_destroy();
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)free;
EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
wasm_runtime_destroy();
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)os_free;
EXPECT_EQ(true, wasm_runtime_full_init(&init_args));
/* Use valid module, and runtime need to be proper inited */
wasm_file_buf =
(unsigned char *)bh_read_file_to_buffer(WASM_FILE_1, &wasm_file_size);
EXPECT_NE(nullptr, wasm_file_buf);
module = wasm_runtime_load(wasm_file_buf, wasm_file_size, nullptr, 0);
EXPECT_NE(nullptr, module);
EXPECT_EQ(true, wasm_runtime_register_module_internal(
"module", module, wasm_file_buf, wasm_file_size, nullptr, 0));
wasm_runtime_destroy();
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = NULL;
init_args.mem_alloc_option.pool.heap_size = 0;
EXPECT_EQ(false, wasm_runtime_full_init(&init_args));
}