Add unit test suites (#3490)
This commit is contained in:
65
tests/unit/custom-section/CMakeLists.txt
Normal file
65
tests/unit/custom-section/CMakeLists.txt
Normal file
@ -0,0 +1,65 @@
|
||||
# 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-custom-section)
|
||||
|
||||
add_definitions (-DRUN_ON_LINUX)
|
||||
|
||||
set (WAMR_BUILD_LIBC_WASI 0)
|
||||
set (WAMR_BUILD_LIBC_BUILTIN 0)
|
||||
set (WAMR_BUILD_JIT 0)
|
||||
set (WAMR_BUILD_LAZY_JIT 0)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
|
||||
add_definitions(-DWASM_ENABLE_WAMR_COMPILER=1)
|
||||
add_definitions (-DWASM_ENABLE_DUMP_CALL_STACK=1)
|
||||
add_definitions (-DWASM_ENABLE_AOT_STACK_FRAME=1)
|
||||
|
||||
# Feature to test
|
||||
set (WAMR_BUILD_LOAD_CUSTOM_SECTION 1)
|
||||
|
||||
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})
|
||||
|
||||
set (unit_test_sources
|
||||
${UNIT_SOURCE}
|
||||
${PLATFORM_SHARED_SOURCE}
|
||||
${UTILS_SHARED_SOURCE}
|
||||
${MEM_ALLOC_SHARED_SOURCE}
|
||||
${NATIVE_INTERFACE_SOURCE}
|
||||
${IWASM_COMMON_SOURCE}
|
||||
${IWASM_INTERP_SOURCE}
|
||||
${IWASM_AOT_SOURCE}
|
||||
${IWASM_COMPL_SOURCE}
|
||||
${WASM_APP_LIB_SOURCE_ALL}
|
||||
)
|
||||
|
||||
# Automatically build wasm-apps for this test
|
||||
add_subdirectory(wasm-apps)
|
||||
|
||||
# Now simply link against gtest or gtest_main as needed. Eg
|
||||
add_executable (custom_section_test ${unit_test_sources})
|
||||
|
||||
target_link_libraries (custom_section_test ${LLVM_AVAILABLE_LIBS} gtest_main )
|
||||
|
||||
gtest_discover_tests(custom_section_test)
|
||||
161
tests/unit/custom-section/custom_section_test.cc
Normal file
161
tests/unit/custom-section/custom_section_test.cc
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "bh_platform.h"
|
||||
#include <fstream>
|
||||
#include "test_helper.h"
|
||||
#include "aot_export.h"
|
||||
|
||||
class CustomSectionTest : 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<1 * 1024 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(CustomSectionTest, get_custom_section_from_wasm_module_t)
|
||||
{
|
||||
uint32_t length, len_from_aot;
|
||||
const uint8_t *content, *content_from_aot;
|
||||
std::ifstream wasm_file("wasm-apps/app.wasm", std::ios::binary);
|
||||
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(wasm_file),
|
||||
{});
|
||||
{
|
||||
WAMRModule module(buffer.data(), buffer.size());
|
||||
aot_comp_data_t comp_data = NULL;
|
||||
aot_comp_context_t comp_ctx = NULL;
|
||||
std::vector<const char *> sections_to_emit{
|
||||
"name",
|
||||
".debug_info",
|
||||
".debug_abbrev",
|
||||
/* skip ".debug_line" section in AoT module */
|
||||
".debug_str",
|
||||
"producers",
|
||||
};
|
||||
|
||||
AOTCompOption option = { 0 };
|
||||
option.custom_sections = (char **)sections_to_emit.data();
|
||||
option.custom_sections_count = 5;
|
||||
|
||||
{
|
||||
/* Compile an AoT module */
|
||||
comp_data = aot_create_comp_data(module.get(), NULL, false);
|
||||
EXPECT_NE(comp_data, nullptr);
|
||||
|
||||
comp_ctx = aot_create_comp_context(comp_data, &option);
|
||||
EXPECT_NE(comp_ctx, nullptr);
|
||||
|
||||
EXPECT_TRUE(aot_compile_wasm(comp_ctx));
|
||||
|
||||
EXPECT_TRUE(aot_emit_aot_file(comp_ctx, comp_data, "temp.aot"));
|
||||
}
|
||||
|
||||
std::ifstream aot_file("temp.aot", std::ios::binary);
|
||||
std::vector<unsigned char> aot_buffer(
|
||||
std::istreambuf_iterator<char>(aot_file), {});
|
||||
WAMRModule aot_module(aot_buffer.data(), aot_buffer.size());
|
||||
|
||||
/* name */
|
||||
content =
|
||||
wasm_runtime_get_custom_section(module.get(), "name", &length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
/* TODO: aot_emit_name_section don't
|
||||
EMIT_U32(AOT_CUSTOM_SECTION_RAW);*
|
||||
EMIT_STR("name");
|
||||
but instead
|
||||
EMIT_U32(AOT_CUSTOM_SECTION_NAME);
|
||||
can't use get_custom_section to get it
|
||||
*/
|
||||
// content_from_aot = wasm_runtime_get_custom_section(
|
||||
// aot_module.get(), "name", &len_from_aot);
|
||||
// EXPECT_NE(content_from_aot, nullptr);
|
||||
// EXPECT_EQ(len_from_aot, length);
|
||||
// EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
|
||||
|
||||
/* .debug_info */
|
||||
content = wasm_runtime_get_custom_section(module.get(), ".debug_info",
|
||||
&length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), ".debug_info", &len_from_aot);
|
||||
EXPECT_NE(content_from_aot, nullptr);
|
||||
EXPECT_EQ(len_from_aot, length);
|
||||
EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
|
||||
|
||||
/* .debug_abbrev */
|
||||
content = wasm_runtime_get_custom_section(module.get(), ".debug_abbrev",
|
||||
&length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), ".debug_abbrev", &len_from_aot);
|
||||
EXPECT_NE(content_from_aot, nullptr);
|
||||
EXPECT_EQ(len_from_aot, length);
|
||||
EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
|
||||
|
||||
/* .debug_line */
|
||||
content = wasm_runtime_get_custom_section(module.get(), ".debug_line",
|
||||
&length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), ".debug_line", &len_from_aot);
|
||||
EXPECT_EQ(content_from_aot, nullptr);
|
||||
|
||||
/* .debug_str */
|
||||
content = wasm_runtime_get_custom_section(module.get(), ".debug_str",
|
||||
&length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), ".debug_str", &len_from_aot);
|
||||
EXPECT_NE(content_from_aot, nullptr);
|
||||
EXPECT_EQ(len_from_aot, length);
|
||||
EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
|
||||
|
||||
/* producers */
|
||||
content =
|
||||
wasm_runtime_get_custom_section(module.get(), "producers", &length);
|
||||
EXPECT_NE(content, nullptr);
|
||||
EXPECT_GT(length, 0);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), "producers", &len_from_aot);
|
||||
EXPECT_NE(content_from_aot, nullptr);
|
||||
EXPECT_EQ(len_from_aot, length);
|
||||
EXPECT_EQ(memcmp(content_from_aot, content, length), 0);
|
||||
|
||||
/* Not exist */
|
||||
content = wasm_runtime_get_custom_section(module.get(), "producers1",
|
||||
&length);
|
||||
EXPECT_EQ(content, nullptr);
|
||||
|
||||
content_from_aot = wasm_runtime_get_custom_section(
|
||||
aot_module.get(), "producers1", &len_from_aot);
|
||||
EXPECT_EQ(content_from_aot, nullptr);
|
||||
}
|
||||
}
|
||||
14
tests/unit/custom-section/wasm-apps/CMakeLists.txt
Normal file
14
tests/unit/custom-section/wasm-apps/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 2.9)
|
||||
|
||||
project(wasm-apps-custom-section)
|
||||
|
||||
# Add -g option so there will be debugger related custom sections
|
||||
add_custom_target(app.wasm ALL
|
||||
COMMAND /opt/wasi-sdk/bin/clang -g -nostdlib
|
||||
-Wl,--no-entry,--export-all
|
||||
-o ${CMAKE_CURRENT_BINARY_DIR}/app.wasm
|
||||
${CMAKE_CURRENT_LIST_DIR}/app.c
|
||||
)
|
||||
10
tests/unit/custom-section/wasm-apps/app.c
Normal file
10
tests/unit/custom-section/wasm-apps/app.c
Normal file
@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
int
|
||||
main(int argc, char const *argv[])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user