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,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;
}