mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-02-06 06:55:07 +00:00
samples/terminate: Add a sample to demonstrate wasm_runtime_terminate (#3043)
This is basically a modified copy of the "shared-module" example.
This commit is contained in:
parent
d13a54f860
commit
b97370e3a8
|
@ -451,6 +451,12 @@ jobs:
|
|||
./build.sh
|
||||
./run.sh
|
||||
|
||||
- name: Build Sample [terminate]
|
||||
run: |
|
||||
cd samples/terminate
|
||||
./build.sh
|
||||
./run.sh
|
||||
|
||||
test:
|
||||
needs:
|
||||
[
|
||||
|
|
6
.github/workflows/compilation_on_macos.yml
vendored
6
.github/workflows/compilation_on_macos.yml
vendored
|
@ -333,3 +333,9 @@ jobs:
|
|||
cd samples/shared-module
|
||||
./build.sh
|
||||
./run.sh
|
||||
|
||||
- name: Build Sample [terminate]
|
||||
run: |
|
||||
cd samples/terminate
|
||||
./build.sh
|
||||
./run.sh
|
||||
|
|
6
.github/workflows/nightly_run.yml
vendored
6
.github/workflows/nightly_run.yml
vendored
|
@ -509,6 +509,12 @@ jobs:
|
|||
cd samples/shared-module
|
||||
./build.sh
|
||||
./run.sh
|
||||
|
||||
- name: Build Sample [terminate]
|
||||
run: |
|
||||
cd samples/terminate
|
||||
./build.sh
|
||||
./run.sh
|
||||
test:
|
||||
needs:
|
||||
[
|
||||
|
|
1
samples/terminate/.gitignore
vendored
Normal file
1
samples/terminate/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/out/
|
97
samples/terminate/CMakeLists.txt
Normal file
97
samples/terminate/CMakeLists.txt
Normal file
|
@ -0,0 +1,97 @@
|
|||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required (VERSION 3.14)
|
||||
|
||||
include(CheckPIESupported)
|
||||
|
||||
project (terminate)
|
||||
|
||||
set (CMAKE_CXX_STANDARD 17)
|
||||
|
||||
################ runtime settings ################
|
||||
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 "")
|
||||
|
||||
# WAMR features switch
|
||||
|
||||
# Set WAMR_BUILD_TARGET, currently values supported:
|
||||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
|
||||
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
|
||||
if (NOT DEFINED WAMR_BUILD_TARGET)
|
||||
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
|
||||
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")
|
||||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
# Build as X86_32 by default in 32-bit platform
|
||||
set (WAMR_BUILD_TARGET "X86_32")
|
||||
else ()
|
||||
message(SEND_ERROR "Unsupported build target platform!")
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (NOT CMAKE_BUILD_TYPE)
|
||||
set (CMAKE_BUILD_TYPE Debug)
|
||||
endif ()
|
||||
|
||||
set (WAMR_BUILD_LIBC_WASI 1)
|
||||
set (WAMR_BUILD_THREAD_MGR 1)
|
||||
set (WAMR_BUILD_INTERP 1)
|
||||
set (WAMR_BUILD_AOT 1)
|
||||
set (WAMR_BUILD_JIT 0)
|
||||
|
||||
# fast interpreter
|
||||
# set (WAMR_BUILD_FAST_INTERP 1)
|
||||
|
||||
# fast-jit
|
||||
# set (WAMR_BUILD_FAST_JIT 1)
|
||||
|
||||
# llvm jit
|
||||
# set (WAMR_BUILD_JIT 1)
|
||||
# set (LLVM_DIR /usr/local/opt/llvm@14/lib/cmake/llvm)
|
||||
|
||||
set (WAMR_BUILD_REF_TYPES 1)
|
||||
|
||||
if (NOT MSVC)
|
||||
# linker flags
|
||||
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
||||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
||||
endif ()
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
|
||||
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 ()
|
||||
endif ()
|
||||
|
||||
# build out vmlib
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
|
||||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
|
||||
|
||||
################ application related ################
|
||||
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
|
||||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
||||
|
||||
add_executable (terminate src/main.c ${UNCOMMON_SHARED_SOURCE})
|
||||
|
||||
check_pie_supported()
|
||||
set_target_properties (terminate PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
if (APPLE)
|
||||
target_link_libraries (terminate vmlib -lm -ldl -lpthread ${LLVM_AVAILABLE_LIBS})
|
||||
else ()
|
||||
target_link_libraries (terminate vmlib -lm -ldl -lpthread -lrt ${LLVM_AVAILABLE_LIBS})
|
||||
endif ()
|
4
samples/terminate/README.md
Normal file
4
samples/terminate/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
The "terminate" sample project
|
||||
==============================
|
||||
|
||||
This sample demonstrates wasm_runtime_terminate API.
|
63
samples/terminate/build.sh
Executable file
63
samples/terminate/build.sh
Executable file
|
@ -0,0 +1,63 @@
|
|||
#
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
#
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
CURR_DIR=$PWD
|
||||
WAMR_DIR=${PWD}/../..
|
||||
OUT_DIR=${PWD}/out
|
||||
|
||||
WASM_APPS=${PWD}/wasm-apps
|
||||
|
||||
|
||||
rm -rf ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}
|
||||
mkdir ${OUT_DIR}/wasm-apps
|
||||
|
||||
|
||||
echo "##################### build terminate project"
|
||||
cd ${CURR_DIR}
|
||||
mkdir -p cmake_build
|
||||
cd cmake_build
|
||||
cmake .. -DCMAKE_BUILD_TYPE=Debug
|
||||
make -j ${nproc}
|
||||
if [ $? != 0 ];then
|
||||
echo "BUILD_FAIL terminate exit as $?\n"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
cp -a terminate ${OUT_DIR}
|
||||
|
||||
printf "\n"
|
||||
|
||||
echo "##################### build wasm apps"
|
||||
|
||||
cd ${WASM_APPS}
|
||||
|
||||
for i in `ls *.wat`
|
||||
do
|
||||
APP_SRC="$i"
|
||||
OUT_FILE=${i%.*}.wasm
|
||||
|
||||
# Note: the CI installs wabt in /opt/wabt
|
||||
if type wat2wasm; then
|
||||
WAT2WASM=${WAT2WASM:-wat2wasm}
|
||||
elif [ -x /opt/wabt/bin/wat2wasm ]; then
|
||||
WAT2WASM=${WAT2WASM:-/opt/wabt/bin/wat2wasm}
|
||||
fi
|
||||
|
||||
${WAT2WASM} -o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
|
||||
|
||||
# aot
|
||||
# wamrc -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
# mv ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
|
||||
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
|
||||
echo "build ${OUT_FILE} success"
|
||||
else
|
||||
echo "build ${OUT_FILE} fail"
|
||||
fi
|
||||
done
|
||||
echo "##################### build wasm apps done"
|
3
samples/terminate/run.sh
Executable file
3
samples/terminate/run.sh
Executable file
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
out/terminate -f out/wasm-apps/testapp.wasm
|
183
samples/terminate/src/main.c
Normal file
183
samples/terminate/src/main.c
Normal file
|
@ -0,0 +1,183 @@
|
|||
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "bh_read_file.h"
|
||||
#include "bh_getopt.h"
|
||||
|
||||
void
|
||||
print_usage(void)
|
||||
{
|
||||
fprintf(stdout, "Options:\r\n");
|
||||
fprintf(stdout, " -f [path of wasm file] \n");
|
||||
}
|
||||
|
||||
static void *
|
||||
runner(void *vp)
|
||||
{
|
||||
wasm_module_inst_t inst = vp;
|
||||
bool ok = wasm_runtime_init_thread_env();
|
||||
assert(ok);
|
||||
wasm_application_execute_main(inst, 0, NULL);
|
||||
wasm_runtime_destroy_thread_env();
|
||||
return inst;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv_main[])
|
||||
{
|
||||
int exit_code = 1;
|
||||
static char global_heap_buf[512 * 1024];
|
||||
char *buffer;
|
||||
char error_buf[128];
|
||||
int opt;
|
||||
char *wasm_path = NULL;
|
||||
int ret;
|
||||
int pipe_fds[2];
|
||||
|
||||
const unsigned int N = 4;
|
||||
wasm_module_t module = NULL;
|
||||
wasm_module_inst_t module_inst[N];
|
||||
pthread_t th[N];
|
||||
unsigned int i;
|
||||
uint32 buf_size, stack_size = 8092, heap_size = 8092;
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
module_inst[i] = NULL;
|
||||
}
|
||||
|
||||
RuntimeInitArgs init_args;
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
while ((opt = getopt(argc, argv_main, "hf:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
wasm_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_usage();
|
||||
return 0;
|
||||
case '?':
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (optind == 1) {
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&init_args, 0, sizeof(init_args));
|
||||
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);
|
||||
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
|
||||
|
||||
if (!buffer) {
|
||||
printf("Open wasm app file [%s] failed.\n", wasm_path);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
|
||||
sizeof(error_buf));
|
||||
if (!module) {
|
||||
printf("Load wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Ensure that fd_read on FD 0 blocks. */
|
||||
ret = pipe(pipe_fds);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
wasm_runtime_set_wasi_args_ex(module, NULL, 0, NULL, 0, NULL, 0, NULL, 0,
|
||||
pipe_fds[0], -1, -1);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
module_inst[i] = wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf));
|
||||
|
||||
if (!module_inst[i]) {
|
||||
printf("Instantiate wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Note: ensure that module inst has an exec env so that
|
||||
* it can receive the termination request.
|
||||
*/
|
||||
wasm_runtime_get_exec_env_singleton(module_inst[i]);
|
||||
|
||||
if ((i % 2) == 0) {
|
||||
printf("terminating thread %u before starting\n", i);
|
||||
wasm_runtime_terminate(module_inst[i]);
|
||||
}
|
||||
|
||||
printf("starting thread %u\n", i);
|
||||
ret = pthread_create(&th[i], NULL, runner, module_inst[i]);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
printf("sleeping a bit to ensure that the threads actually started\n");
|
||||
sleep(1);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
if ((i % 2) != 0) {
|
||||
printf("terminating thread %u\n", i);
|
||||
wasm_runtime_terminate(module_inst[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
printf("joining thread %u\n", i);
|
||||
void *status;
|
||||
ret = pthread_join(th[i], &status);
|
||||
if (ret != 0) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
const char *exception = wasm_runtime_get_exception(module_inst[i]);
|
||||
if (exception != NULL) {
|
||||
if (!strstr(exception, "terminated by user")) {
|
||||
printf("thread %u got an exception: %s (unexpected)\n", i,
|
||||
exception);
|
||||
goto fail;
|
||||
}
|
||||
printf("thread %u got an exception: %s (expected)\n", i, exception);
|
||||
}
|
||||
else {
|
||||
printf("thread %u got no exception (unexpected)\n", i);
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
exit_code = 0;
|
||||
fail:
|
||||
for (i = 0; i < N; i++) {
|
||||
if (module_inst[i])
|
||||
wasm_runtime_deinstantiate(module_inst[i]);
|
||||
}
|
||||
if (module)
|
||||
wasm_runtime_unload(module);
|
||||
if (buffer)
|
||||
BH_FREE(buffer);
|
||||
wasm_runtime_destroy();
|
||||
return exit_code;
|
||||
}
|
22
samples/terminate/wasm-apps/testapp.wat
Normal file
22
samples/terminate/wasm-apps/testapp.wat
Normal file
|
@ -0,0 +1,22 @@
|
|||
;; Copyright (C) 2024 YAMAMOTO Takashi
|
||||
;; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
(module
|
||||
(func $fd_read (import "wasi_snapshot_preview1" "fd_read") (param i32 i32 i32 i32) (result i32))
|
||||
(func (export "_start")
|
||||
;; read from FD 0
|
||||
i32.const 100 ;; iov_base
|
||||
i32.const 200 ;; buffer
|
||||
i32.store
|
||||
i32.const 104 ;; iov_len
|
||||
i32.const 1
|
||||
i32.store
|
||||
i32.const 0 ;; fd 0
|
||||
i32.const 100 ;; iov_base
|
||||
i32.const 1 ;; iov count
|
||||
i32.const 300 ;; retp (out)
|
||||
call $fd_read
|
||||
unreachable
|
||||
)
|
||||
(memory (export "memory") 1)
|
||||
)
|
Loading…
Reference in New Issue
Block a user