Add standalone cases (#3536)
This commit is contained in:
127
tests/standalone/test-invoke-native/CMakeLists.txt
Normal file
127
tests/standalone/test-invoke-native/CMakeLists.txt
Normal file
@ -0,0 +1,127 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required (VERSION 3.14)
|
||||
|
||||
project (iwasm)
|
||||
# set (CMAKE_VERBOSE_MAKEFILE 1)
|
||||
|
||||
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
||||
if (APPLE)
|
||||
add_definitions(-DBH_PLATFORM_DARWIN)
|
||||
endif ()
|
||||
|
||||
# Reset default linker flags
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
||||
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
||||
|
||||
set (CMAKE_C_STANDARD 99)
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
|
||||
# Set WAMR_BUILD_TARGET, currently values supported:
|
||||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", "MIPS", "XTENSA"
|
||||
if (NOT DEFINED WAMR_BUILD_TARGET)
|
||||
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
|
||||
set (WAMR_BUILD_TARGET "AARCH64")
|
||||
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
|
||||
set (WAMR_BUILD_TARGET "RISCV64")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
# Build as X86_64 by default in 64-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_64")
|
||||
else ()
|
||||
# Build as X86_32 by default in 32-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_32")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_INTERP)
|
||||
# Enable Interpreter by default
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_AOT)
|
||||
# Enable AOT by default.
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_JIT)
|
||||
# Disable JIT by default.
|
||||
set (WAMR_BUILD_JIT 0)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
|
||||
# Enable libc builtin support by default
|
||||
set (WAMR_BUILD_LIBC_BUILTIN 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
|
||||
# Enable libc wasi support by default
|
||||
set (WAMR_BUILD_LIBC_WASI 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
|
||||
# Enable fast interpreter
|
||||
set (WAMR_BUILD_FAST_INTERP 1)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
|
||||
# Enable multiple modules
|
||||
set (WAMR_BUILD_MULTI_MODULE 0)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
|
||||
# Disable pthread library by default
|
||||
set (WAMR_BUILD_LIB_PTHREAD 0)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
|
||||
# Disable wasm mini loader by default
|
||||
set (WAMR_BUILD_MINI_LOADER 0)
|
||||
endif ()
|
||||
|
||||
if (NOT DEFINED WAMR_BUILD_SIMD)
|
||||
# Disable SIMD by default
|
||||
set (WAMR_BUILD_SIMD 0)
|
||||
endif ()
|
||||
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
|
||||
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
|
||||
if (NOT WAMR_BUILD_PLATFORM STREQUAL "darwin")
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE")
|
||||
endif ()
|
||||
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow")
|
||||
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
|
||||
|
||||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
|
||||
|
||||
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
# The following flags are to enhance security, but it may impact performance,
|
||||
# we disable them by default.
|
||||
#if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
|
||||
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2")
|
||||
#endif ()
|
||||
#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
|
||||
#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
|
||||
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
|
||||
add_executable (test_invoke_native main.c test_invoke_native.c ${UNCOMMON_SHARED_SOURCE})
|
||||
|
||||
install (TARGETS test_invoke_native DESTINATION bin)
|
||||
|
||||
target_link_libraries (test_invoke_native vmlib ${LLVM_AVAILABLE_LIBS} -lm -ldl -lpthread)
|
||||
|
||||
39
tests/standalone/test-invoke-native/main.c
Normal file
39
tests/standalone/test-invoke-native/main.c
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
static char global_heap_buf[10 * 1024 * 1024] = { 0 };
|
||||
|
||||
void
|
||||
test_invoke_native();
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
RuntimeInitArgs init_args;
|
||||
|
||||
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);
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
test_invoke_native();
|
||||
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
}
|
||||
33
tests/standalone/test-invoke-native/run.sh
Executable file
33
tests/standalone/test-invoke-native/run.sh
Executable file
@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
if [[ $1 == "--classic-interp" ]]; then
|
||||
CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0"
|
||||
elif [[ $1 == "--fast-interp" ]]; then
|
||||
CMAKE_FLAGS="-DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=1"
|
||||
elif [[ $1 == "--fast-jit" ]]; then
|
||||
CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1"
|
||||
elif [[ $1 == "--jit" ]]; then
|
||||
CMAKE_FLAGS="-DWAMR_BUILD_JIT=1"
|
||||
elif [[ $1 == "--multi-tier-jit" ]]; then
|
||||
CMAKE_FLAGS="-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1"
|
||||
fi
|
||||
|
||||
TARGET="X86_64"
|
||||
if [[ $3 = "X86_32" ]]; then
|
||||
TARGET="X86_32"
|
||||
fi
|
||||
|
||||
echo "============> test dump-invoke-native"
|
||||
|
||||
rm -fr build
|
||||
mkdir build && cd build
|
||||
cmake .. ${CMAKE_FLAGS} -DWAMR_BUILD_TARGET=${TARGET}
|
||||
make -j ${nproc} > /dev/null 2>&1
|
||||
cd ..
|
||||
|
||||
echo "============> run test-invoke-native"
|
||||
./build/test_invoke_native
|
||||
206
tests/standalone/test-invoke-native/test_invoke_native.c
Normal file
206
tests/standalone/test-invoke-native/test_invoke_native.c
Normal file
@ -0,0 +1,206 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasm_runtime.h"
|
||||
|
||||
static void
|
||||
test_native_args1(WASMModuleInstance *module_inst, int arg0, uint64_t arg1,
|
||||
float arg2, double arg3, int arg4, int64_t arg5, int64_t arg6,
|
||||
int arg7, double arg8, float arg9, int arg10, double arg11,
|
||||
float arg12, int64_t arg13, uint64_t arg14, float arg15,
|
||||
double arg16, int64_t arg17, uint64_t arg18, float arg19)
|
||||
{
|
||||
printf("##test_native_args1 result:\n");
|
||||
printf("arg0: 0x%X, arg1: 0x%X%08X, arg2: %f, arg3: %f\n", arg0,
|
||||
(int32)(arg1 >> 32), (int32)arg1, arg2, arg3);
|
||||
printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4,
|
||||
(int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6,
|
||||
arg7);
|
||||
printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10,
|
||||
arg11);
|
||||
printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12,
|
||||
(int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32),
|
||||
(int32)arg14, arg15);
|
||||
printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16,
|
||||
(int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32),
|
||||
(int32)arg18, arg19);
|
||||
}
|
||||
|
||||
static void
|
||||
test_native_args2(WASMModuleInstance *module_inst, uint64_t arg1, float arg2,
|
||||
double arg3, int arg4, int64_t arg5, int64_t arg6, int arg7,
|
||||
double arg8, float arg9, int arg10, double arg11, float arg12,
|
||||
int64_t arg13, uint64_t arg14, float arg15, double arg16,
|
||||
int64_t arg17, uint64_t arg18, float arg19)
|
||||
{
|
||||
printf("##test_native_args2 result:\n");
|
||||
printf("arg1: 0x%X%08X, arg2: %f, arg3: %f\n", (int32)(arg1 >> 32),
|
||||
(int32)arg1, arg2, arg3);
|
||||
printf("arg4: 0x%X, arg5: 0x%X%08X, arg6: 0x%X%08X, arg7: 0x%X\n", arg4,
|
||||
(int32)(arg5 >> 32), (int32)arg5, (int32)(arg6 >> 32), (int32)arg6,
|
||||
arg7);
|
||||
printf("arg8: %f, arg9: %f, arg10: 0x%X, arg11: %f\n", arg8, arg9, arg10,
|
||||
arg11);
|
||||
printf("arg12: %f, arg13: 0x%X%08X, arg14: 0x%X%08X, arg15: %f\n", arg12,
|
||||
(int32)(arg13 >> 32), (int32)arg13, (int32)(arg14 >> 32),
|
||||
(int32)arg14, arg15);
|
||||
printf("arg16: %f, arg17: 0x%X%08X, arg18: 0x%X%08X, arg19: %f\n", arg16,
|
||||
(int32)(arg17 >> 32), (int32)arg17, (int32)(arg18 >> 32),
|
||||
(int32)arg18, arg19);
|
||||
}
|
||||
|
||||
static int32
|
||||
test_return_i32(WASMModuleInstance *module_inst)
|
||||
{
|
||||
return 0x12345678;
|
||||
}
|
||||
|
||||
static int64
|
||||
test_return_i64(WASMModuleInstance *module_inst)
|
||||
{
|
||||
return 0x12345678ABCDEFFFll;
|
||||
}
|
||||
|
||||
static float32
|
||||
test_return_f32(WASMModuleInstance *module_inst)
|
||||
{
|
||||
return 1234.5678f;
|
||||
}
|
||||
|
||||
static float64
|
||||
test_return_f64(WASMModuleInstance *module_inst)
|
||||
{
|
||||
return 87654321.12345678;
|
||||
}
|
||||
|
||||
#define STORE_I64(addr, value) \
|
||||
do { \
|
||||
union { \
|
||||
int64 val; \
|
||||
uint32 parts[2]; \
|
||||
} u; \
|
||||
u.val = (int64)(value); \
|
||||
(addr)[0] = u.parts[0]; \
|
||||
(addr)[1] = u.parts[1]; \
|
||||
} while (0)
|
||||
|
||||
#define STORE_F64(addr, value) \
|
||||
do { \
|
||||
union { \
|
||||
float64 val; \
|
||||
uint32 parts[2]; \
|
||||
} u; \
|
||||
u.val = (value); \
|
||||
(addr)[0] = u.parts[0]; \
|
||||
(addr)[1] = u.parts[1]; \
|
||||
} while (0)
|
||||
|
||||
#define I32 VALUE_TYPE_I32
|
||||
#define I64 VALUE_TYPE_I64
|
||||
#define F32 VALUE_TYPE_F32
|
||||
#define F64 VALUE_TYPE_F64
|
||||
|
||||
typedef struct WASMTypeTest {
|
||||
uint16 param_count;
|
||||
/* only one result is supported currently */
|
||||
uint16 result_count;
|
||||
uint16 param_cell_num;
|
||||
uint16 ret_cell_num;
|
||||
/* types of params and results */
|
||||
uint8 types[128];
|
||||
} WASMTypeTest;
|
||||
|
||||
void
|
||||
test_invoke_native()
|
||||
{
|
||||
uint32 argv[128], *p = argv;
|
||||
WASMTypeTest func_type1 = { 20, 0, 0, 0, { I32, I64, F32, F64, I32,
|
||||
I64, I64, I32, F64, F32,
|
||||
I32, F64, F32, I64, I64,
|
||||
F32, F64, I64, I64, F32 } };
|
||||
WASMTypeTest func_type2 = { 19,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
{ I64, F32, F64, I32, I64, I64, I32, F64, F32,
|
||||
I32, F64, F32, I64, I64, F32, F64, I64, I64,
|
||||
F32 } };
|
||||
WASMTypeTest func_type_i32 = { 0, 1, 0, 0, { I32 } };
|
||||
WASMTypeTest func_type_i64 = { 0, 1, 0, 0, { I64 } };
|
||||
WASMTypeTest func_type_f32 = { 0, 1, 0, 0, { F32 } };
|
||||
WASMTypeTest func_type_f64 = { 0, 1, 0, 0, { F64 } };
|
||||
WASMModuleInstance module_inst = { 0 };
|
||||
WASMExecEnv exec_env = { 0 };
|
||||
|
||||
module_inst.module_type = Wasm_Module_Bytecode;
|
||||
exec_env.module_inst = (WASMModuleInstanceCommon *)&module_inst;
|
||||
|
||||
*p++ = 0x12345678;
|
||||
STORE_I64(p, 0xFFFFFFFF87654321ll);
|
||||
p += 2;
|
||||
*(float32 *)p++ = 1234.5678f;
|
||||
STORE_F64(p, 567890.1234);
|
||||
p += 2;
|
||||
|
||||
*p++ = 0x11111111;
|
||||
STORE_I64(p, 0xAAAAAAAABBBBBBBBll);
|
||||
p += 2;
|
||||
STORE_I64(p, 0x7788888899ll);
|
||||
p += 2;
|
||||
*p++ = 0x3456;
|
||||
|
||||
STORE_F64(p, 8888.7777);
|
||||
p += 2;
|
||||
*(float32 *)p++ = 7777.8888f;
|
||||
*p++ = 0x66666;
|
||||
STORE_F64(p, 999999.88888);
|
||||
p += 2;
|
||||
|
||||
*(float32 *)p++ = 555555.22f;
|
||||
STORE_I64(p, 0xBBBBBAAAAAAAAll);
|
||||
p += 2;
|
||||
STORE_I64(p, 0x3333AAAABBBBll);
|
||||
p += 2;
|
||||
*(float32 *)p++ = 88.77f;
|
||||
|
||||
STORE_F64(p, 9999.01234);
|
||||
p += 2;
|
||||
STORE_I64(p, 0x1111122222222ll);
|
||||
p += 2;
|
||||
STORE_I64(p, 0x444455555555ll);
|
||||
p += 2;
|
||||
*(float32 *)p++ = 77.88f;
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_native_args1,
|
||||
(WASMType *)&func_type1, NULL, NULL, argv,
|
||||
p - argv, argv);
|
||||
printf("\n");
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_native_args2,
|
||||
(WASMType *)&func_type2, NULL, NULL, argv + 1,
|
||||
p - argv - 1, argv);
|
||||
printf("\n");
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_return_i32,
|
||||
(WASMType *)&func_type_i32, NULL, NULL, NULL, 0,
|
||||
argv);
|
||||
printf("test_return_i32: 0x%X\n\n", argv[0]);
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_return_i64,
|
||||
(WASMType *)&func_type_i64, NULL, NULL, NULL, 0,
|
||||
argv);
|
||||
printf("test_return_i64: 0x%X%08X\n\n", (int32)((*(int64 *)argv) >> 32),
|
||||
(int32)(*(int64 *)argv));
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_return_f32,
|
||||
(WASMType *)&func_type_f32, NULL, NULL, NULL, 0,
|
||||
argv);
|
||||
printf("test_return_f32: %f\n\n", *(float32 *)argv);
|
||||
|
||||
wasm_runtime_invoke_native(&exec_env, test_return_f64,
|
||||
(WASMType *)&func_type_f64, NULL, NULL, NULL, 0,
|
||||
argv);
|
||||
printf("test_return_f64: %f\n\n", *(float64 *)argv);
|
||||
}
|
||||
Reference in New Issue
Block a user