Add standalone cases (#3536)

This commit is contained in:
Zhang, Yi
2024-06-19 16:40:37 +08:00
committed by GitHub
parent 7f94d183ac
commit 16e70f99aa
129 changed files with 3880 additions and 3 deletions

View File

@ -0,0 +1,12 @@
#!/bin/bash
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
set -e
cd c-embed
rm -fr build && mkdir build
cmake $1 -B build
cmake --build build -j >/dev/null 2>&1

View File

@ -0,0 +1,15 @@
#!/bin/bash
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
set -e
PLATFORM=$(uname -s | tr A-Z a-z)
readonly WAMR_DIR="$PWD/../../.."
rm -fr build && mkdir build && cd build
cmake ${WAMR_DIR}/product-mini/platforms/${PLATFORM} $1
make -j > /dev/null 2>&1
cd ..

View File

@ -0,0 +1,59 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project(c_embed_test)
cmake_minimum_required(VERSION 3.14)
include(CheckPIESupported)
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if (APPLE)
add_definitions(-DBH_PLATFORM_DARWIN)
endif ()
set (CMAKE_C_STANDARD 99)
set (CMAKE_CXX_STANDARD 17)
set(WAMR_BUILD_TARGET "X86_64")
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_FAST_INTERP 0)
set(WAMR_BUILD_AOT 0)
set(WAMR_BUILD_LIBC_BUILTIN 1)
set(WAMR_BUILD_LIBC_WASI 1)
set(WAMR_BUILD_SIMD 1)
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../..)
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_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
# build out vmlib
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
# application related
include(${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
add_executable(c_embed_test src/main.c ${UNCOMMON_SHARED_SOURCE})
target_link_libraries(c_embed_test vmlib m ${LLVM_AVAILABLE_LIBS})
add_custom_command(TARGET c_embed_test POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_LIST_DIR}/../wasm-apps/mytest.wasm ${CMAKE_CURRENT_LIST_DIR}/../wasm-apps/hello.wasm
${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Copy main.wasm and hello.wasm to the build directory"
)

View File

@ -0,0 +1,208 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_read_file.h"
#include "wasm_export.h"
RunningMode
str_to_running_mode(char const *str)
{
RunningMode running_mode = 0;
#if WASM_ENABLE_INTERP != 0
if (!strcmp(str, "interp")) {
running_mode = Mode_Interp;
}
#endif
#if WASM_ENABLE_FAST_JIT != 0
else if (!strcmp(str, "fast-jit")) {
running_mode = Mode_Fast_JIT;
}
#endif
#if WASM_ENABLE_JIT != 0
else if (!strcmp(str, "llvm-jit")) {
running_mode = Mode_LLVM_JIT;
}
#endif
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
&& WASM_ENABLE_LAZY_JIT != 0
else if (!strcmp(str, "multi-tier-jit")) {
running_mode = Mode_Multi_Tier_JIT;
}
#endif
return running_mode;
}
int
one_time_run_wasm(RunningMode default_running_mode,
RunningMode module_running_mode)
{
char *buffer, *another_buffer, error_buf[128];
wasm_module_t module = NULL, another_module = NULL;
wasm_module_inst_t module_inst = NULL, another_module_inst = NULL;
wasm_function_inst_t main_func = NULL, echo_func = NULL;
wasm_exec_env_t exec_env = NULL, another_exec_env = NULL;
uint32_t size, stack_size = 8092, heap_size = 8092;
if (wasm_runtime_is_running_mode_supported(default_running_mode)) {
printf("Support running mode: %d\n", default_running_mode);
}
else {
printf("This runtime Doesn't support running mode: %d\n",
default_running_mode);
goto fail;
}
if (wasm_runtime_set_default_running_mode(default_running_mode)) {
printf("Successfully set default running mode: %d\n",
default_running_mode);
}
else {
printf("Set default running mode: %d failed\n", default_running_mode);
goto fail;
}
/* module 1 */
if (!(buffer = bh_read_file_to_buffer("mytest.wasm", &size))) {
printf("Open wasm app file %s failed.\n", "mytest.wasm");
goto fail;
}
if (!(module = wasm_runtime_load((uint8_t *)buffer, size, error_buf,
sizeof(error_buf)))) {
printf("Load wasm module failed. error: %s\n", error_buf);
goto fail;
}
if (!(module_inst = wasm_runtime_instantiate(
module, stack_size, heap_size, error_buf, sizeof(error_buf)))) {
printf("Instantiate wasm module failed. error: %s\n", error_buf);
goto fail;
}
if (!(exec_env = wasm_runtime_create_exec_env(module_inst, stack_size))) {
printf("Create wasm execution environment failed.\n");
goto fail;
}
/* module 2 */
if (!(another_buffer = bh_read_file_to_buffer("hello.wasm", &size))) {
printf("Open wasm app file %s failed.\n", "hello.wasm");
goto fail;
}
if (!(another_module = wasm_runtime_load((uint8_t *)another_buffer, size,
error_buf, sizeof(error_buf)))) {
printf("Load wasm module failed. error: %s\n", error_buf);
goto fail;
}
if (!(another_module_inst =
wasm_runtime_instantiate(another_module, stack_size, heap_size,
error_buf, sizeof(error_buf)))) {
printf("Instantiate wasm module failed. error: %s\n", error_buf);
goto fail;
}
if (!(another_exec_env =
wasm_runtime_create_exec_env(another_module_inst, stack_size))) {
printf("Create wasm execution environment failed.\n");
goto fail;
}
/* run main function in module 1 */
uint32 wasm_argv[2];
if (!(main_func =
wasm_runtime_lookup_function(module_inst, "__main_argc_argv"))) {
printf("The main wasm function from module 1 is not found.\n");
goto fail;
}
wasm_argv[0] = 3;
if (wasm_runtime_call_wasm(exec_env, main_func, 2, wasm_argv)) {
printf("Run module 1 in running mode: %d\n",
wasm_runtime_get_running_mode(module_inst));
assert(default_running_mode
== wasm_runtime_get_running_mode(module_inst));
printf("Wasm main function return: %d\n", wasm_argv[0]);
}
else {
printf("%s\n", wasm_runtime_get_exception(module_inst));
goto fail;
}
/* run echo function in module 2 */
if (!(wasm_runtime_set_running_mode(another_module_inst,
module_running_mode))) {
printf("Set running mode for module instance failed\n");
goto fail;
}
if (!(echo_func =
wasm_runtime_lookup_function(another_module_inst, "echo"))) {
printf("The echo wasm function from module 2 is not found.\n");
goto fail;
}
wasm_argv[0] = 5;
if (wasm_runtime_call_wasm(another_exec_env, echo_func, 1, wasm_argv)) {
printf("Run module 2 in running mode: %d\n",
wasm_runtime_get_running_mode(another_module_inst));
assert(module_running_mode
== wasm_runtime_get_running_mode(another_module_inst));
printf("Wasm echo function return: %d\n\n", wasm_argv[0]);
}
else {
printf("%s\n", wasm_runtime_get_exception(another_module_inst));
goto fail;
}
fail:
if (exec_env)
wasm_runtime_destroy_exec_env(exec_env);
if (another_exec_env)
wasm_runtime_destroy_exec_env(another_exec_env);
if (module_inst)
wasm_runtime_deinstantiate(module_inst);
if (another_module_inst)
wasm_runtime_deinstantiate(another_module_inst);
if (module)
wasm_runtime_unload(module);
if (another_module)
wasm_runtime_unload(another_module);
return 0;
}
int
main(int argc, char const *argv[])
{
RunningMode default_running_mode = 0, module_running_mode = 0;
for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
if (!strncmp(argv[0], "--default-running-mode=", 23)) {
default_running_mode = str_to_running_mode(argv[0] + 23);
}
else if (!strncmp(argv[0], "--module-running-mode=", 22)) {
module_running_mode = str_to_running_mode(argv[0] + 22);
}
}
/* all the runtime memory allocations are restricted in the global_heap_buf
* array */
static char global_heap_buf[512 * 1024];
RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
/* configure the memory allocator for the runtime */
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 with user configurations*/
if (!wasm_runtime_full_init(&init_args)) {
printf("Init runtime environment failed.\n");
return -1;
}
for (int i = 0; i < 1; ++i) {
one_time_run_wasm(default_running_mode, module_running_mode);
}
wasm_runtime_destroy();
return 0;
}

View File

@ -0,0 +1,23 @@
#!/bin/bash
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
set -e
readonly COMPILE_CMD="/opt/wasi-sdk/bin/clang"
if ! test -x ${COMPILE_CMD}; then
echo "wasi-sdk could not be found"
exit
fi
${COMPILE_CMD} -O3 \
-z stack-size=8192 -nostdlib -Wl,--allow-undefined -Wl,--no-entry -Wl,--initial-memory=65536 \
-o wasm-apps/mytest.wasm wasm-apps/mytest.c \
-Wl,--export=__main_argc_argv
${COMPILE_CMD} -O3 \
-Wl,--no-entry -nostdlib \
-o wasm-apps/hello.wasm wasm-apps/hello.c \
-Wl,--export=echo

View File

@ -0,0 +1,33 @@
#!/bin/bash
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
set -e
if [[ $2 == "--sgx" ]];then
echo "running modes feature on SGX isn't supported yet, ignored."
exit 0
else
readonly IWASM_CMD="$PWD/build/iwasm"
fi
echo "============> test test-running-modes"
./compile_wasm_app.sh
# multi-tier jit
# test iwasm
./build_iwasm.sh "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1"
${IWASM_CMD} --heap-size=16384 wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --interp wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --fast-jit wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --llvm-jit wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --llvm-jit --llvm-jit-size-level=1 wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --llvm-jit --llvm-jit-size-level=2 --llvm-jit-opt-level=1 wasm-apps/mytest.wasm
${IWASM_CMD} --heap-size=16384 --multi-tier-jit wasm-apps/mytest.wasm
# test c embed api
./build_c_embed.sh "-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1"
cd c-embed/build
./c_embed_test --default-running-mode=llvm-jit --module-running-mode=multi-tier-jit

View File

@ -0,0 +1,76 @@
#!python3
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
import os
from collections import OrderedDict
def CLI_ARGS_GENREATOR(running_modes_supported: list[str]) -> list[str]:
res = []
list_2d = [["--default-running-mode={} --module-running-mode={}".format(i, j)
for i in running_modes_supported] for j in running_modes_supported]
for list_1d in list_2d:
res.extend(list_1d)
return res
def main():
RUNNING_MODES: list[str] = [
"interp",
"fast-jit",
"llvm-jit",
"multi-tier-jit",
]
COMPILE_FLAGS: list[str] = [
"-DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=0",
"-DWAMR_BUILD_FAST_JIT=1",
"-DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=1",
"-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1",
"-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=0",
]
# Python 3.7+: Dictionary iteration order is guaranteed to be in order of insertion.
# just to be safe, using orderreddict
# key: value -> compile mode, {"compile_flag": CMake compile flag, "iwasm_cli_args": array of CLI args tested}
test_options = OrderedDict({
"INTERP": {"compile_flag": COMPILE_FLAGS[0], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:1])},
"FAST_JIT": {"compile_flag": COMPILE_FLAGS[1], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:2])},
"LLVM_JIT": {"compile_flag": COMPILE_FLAGS[2],
"cli_args": CLI_ARGS_GENREATOR([RUNNING_MODES[0], RUNNING_MODES[2]])},
"MULTI_TIER_JIT": {"compile_flag": COMPILE_FLAGS[3], "cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES)},
"EAGER_JIT_WITH_BOTH_JIT": {"compile_flag": COMPILE_FLAGS[4],
"cli_args": CLI_ARGS_GENREATOR(RUNNING_MODES[:3])}
})
build_cmd = "./build_c_embed.sh \"{build_flag}\""
run_cmd = "cd c-embed/build && ./c_embed_test {cli_args}"
for compile_mode in test_options.keys():
build_flag: str = test_options[compile_mode]["compile_flag"]
cli_args_li: list = test_options[compile_mode]["cli_args"]
# compile
print("\r\n\r\nCompile C program embed WAMR in {} mode".format(compile_mode))
ret = os.system(build_cmd.format(build_flag=build_flag))
if ret:
print("Compile failed")
# iter over cli args combination
for cli_args in cli_args_li:
print(run_cmd.format(cli_args=cli_args))
ret = os.system(run_cmd.format(cli_args=cli_args))
if ret:
break
else: # if inner for loop finish normally
continue
# if break from inner for loop
print("Run failed")
break
if __name__ == '__main__':
main()

View File

@ -0,0 +1,71 @@
#!python3
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
import os
from collections import OrderedDict
def main():
IWASM_CMD = "../../../wamr/product-mini/platforms/linux/build/iwasm"
IWASM_CLI_ARGS: list[str] = [
"--heap-size=16384 --interp",
"--heap-size=16384 --fast-jit",
"--heap-size=16384 --llvm-jit",
"--heap-size=16384 --multi-tier-jit",
"--heap-size=16384 --llvm-jit --llvm-jit-size-level=1",
"--heap-size=16384 --llvm-jit --llvm-jit-size-level=2 --llvm-jit-opt-level=1"
]
COMPILE_FLAGS: list[str] = [
"-DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=0",
"-DWAMR_BUILD_FAST_JIT=1",
"-DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=1",
"-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1",
"-DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=0",
]
# Python 3.7+: Dictionary iteration order is guaranteed to be in order of insertion.
# just to be safe, using orderreddict
# key: value -> compile mode, {"compile_flag": CMake compile flag, "iwasm_cli_args": array of CLI args tested}
test_options = OrderedDict({
"INTERP": {"compile_flag": COMPILE_FLAGS[0], "iwasm_cli_args": IWASM_CLI_ARGS[:1]},
"FAST_JIT": {"compile_flag": COMPILE_FLAGS[1], "iwasm_cli_args": IWASM_CLI_ARGS[:2]},
"LLVM_JIT": {"compile_flag": COMPILE_FLAGS[2], "iwasm_cli_args": [IWASM_CLI_ARGS[0], IWASM_CLI_ARGS[2]]},
"MULTI_TIER_JIT": {"compile_flag": COMPILE_FLAGS[3], "iwasm_cli_args": IWASM_CLI_ARGS},
"EAGER_JIT_WITH_BOTH_JIT": {"compile_flag": COMPILE_FLAGS[4],
"iwasm_cli_args": IWASM_CLI_ARGS[:3] + IWASM_CLI_ARGS[4:]}
})
build_cmd = "./build_iwasm.sh \"{build_flag}\""
wasm_file = "wasm-apps/mytest.wasm"
run_cmd = "{IWASM_CMD} {cli_args} " + wasm_file
for compile_mode in test_options.keys():
build_flag: str = test_options[compile_mode]["compile_flag"]
cli_args_li: list = test_options[compile_mode]["iwasm_cli_args"]
# compile
print("\r\n\r\nCompile iwasm in {} mode".format(compile_mode))
ret = os.system(build_cmd.format(build_flag=build_flag))
if ret:
print("Compile failed")
# iter over cli args combination
for cli_args in cli_args_li:
print(run_cmd.format(IWASM_CMD=IWASM_CMD, cli_args=cli_args))
ret = os.system(run_cmd.format(
IWASM_CMD=IWASM_CMD, cli_args=cli_args))
if ret:
break
else: # if inner for loop finish normally
continue
# if break from inner for loop
print("Run failed")
break
if __name__ == '__main__':
main()

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
double
foo(double d)
{
return d / 3.0;
}
double
maybe_min(double d, double e)
{
return d < e ? d : e;
}
double
factor(double a, double b, double c)
{
return (a * c) + (b * c);
}
int
echo(int a)
{
double b = foo(14.5);
double c = maybe_min(12.2, 15.4);
double d = factor(a, b, c);
return 2 * a;
}

Binary file not shown.

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <stdio.h>
#include <stdlib.h>
int
recursive(int a)
{
if (a > 0) {
return recursive(a - 1) + 1;
}
else
return 0;
}
int
testFunction(int *input, int length)
{
int sum = 0;
for (int i = 0; i < length; ++i) {
sum += input[i];
}
return sum;
}
int
main(int argc, char **argv)
{
int arr[5] = { 1, 2, 3, 4, 5 };
testFunction(arr, recursive(5));
char *buf;
printf("Hello world!\n");
buf = malloc(1024);
if (!buf) {
printf("malloc buf failed\n");
return -1;
}
printf("buf ptr: %p\n", buf);
snprintf(buf, 1024, "%s", "1234\n");
printf("buf: %s", buf);
free(buf);
return 0;
}

Binary file not shown.