Skip to content

Commit b02eaa0

Browse files
committed
Add native-stack-overflow sample
This is a test code to examine native stack overflow detection logic. The current output on my environment (macOS amd64): ```shell ====== Interpreter stack size | fail? | leak? | exception --------------------------------------------------------------------------- 0 - 14704 | failed | leaked | Exception: native stack overflow 14704 - 17904 | failed | ok | Exception: native stack overflow 17904 - 24576 | ok | ok | ====== AOT stack size | fail? | leak? | exception --------------------------------------------------------------------------- 0 - 18176 | failed | leaked | Exception: native stack overflow 18176 - 24576 | ok | ok | ====== AOT WAMR_DISABLE_HW_BOUND_CHECK=1 stack size | fail? | leak? | exception --------------------------------------------------------------------------- 0 - 1968 | failed | ok | Exception: native stack overflow 1968 - 24576 | ok | ok | ``` This is a preparation to work on relevant issues, including: #3325 #3320 #3314 #3297
1 parent f6481ce commit b02eaa0

File tree

9 files changed

+483
-0
lines changed

9 files changed

+483
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/out/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required (VERSION 3.14)
5+
6+
include(CheckPIESupported)
7+
8+
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
9+
project (native-stack-overflow)
10+
else()
11+
project (native-stack-overflow C ASM)
12+
endif()
13+
14+
################ runtime settings ################
15+
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
16+
if (APPLE)
17+
add_definitions(-DBH_PLATFORM_DARWIN)
18+
endif ()
19+
20+
# Reset default linker flags
21+
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
22+
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
23+
24+
# WAMR features switch
25+
26+
# Set WAMR_BUILD_TARGET, currently values supported:
27+
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
28+
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
29+
if (NOT DEFINED WAMR_BUILD_TARGET)
30+
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
31+
set (WAMR_BUILD_TARGET "AARCH64")
32+
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
33+
set (WAMR_BUILD_TARGET "RISCV64")
34+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
35+
# Build as X86_64 by default in 64-bit platform
36+
set (WAMR_BUILD_TARGET "X86_64")
37+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
38+
# Build as X86_32 by default in 32-bit platform
39+
set (WAMR_BUILD_TARGET "X86_32")
40+
else ()
41+
message(SEND_ERROR "Unsupported build target platform!")
42+
endif ()
43+
endif ()
44+
45+
if (NOT CMAKE_BUILD_TYPE)
46+
set (CMAKE_BUILD_TYPE Debug)
47+
endif ()
48+
49+
set (WAMR_BUILD_INTERP 1)
50+
set (WAMR_BUILD_AOT 1)
51+
set (WAMR_BUILD_JIT 0)
52+
set (WAMR_BUILD_LIBC_BUILTIN 0)
53+
set (WAMR_BUILD_LIBC_WASI 1)
54+
55+
if (NOT MSVC)
56+
# linker flags
57+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
58+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
59+
endif ()
60+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
61+
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
62+
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
63+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
64+
endif ()
65+
endif ()
66+
endif ()
67+
68+
# build out vmlib
69+
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
70+
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
71+
72+
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
73+
74+
################ application related ################
75+
include_directories(${CMAKE_CURRENT_LIST_DIR}/src)
76+
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
77+
78+
add_executable (native-stack-overflow src/main.c src/native_impl.c ${UNCOMMON_SHARED_SOURCE})
79+
80+
check_pie_supported()
81+
set_target_properties (native-stack-overflow PROPERTIES POSITION_INDEPENDENT_CODE ON)
82+
83+
if (APPLE)
84+
target_link_libraries (native-stack-overflow vmlib -lm -ldl -lpthread)
85+
else ()
86+
target_link_libraries (native-stack-overflow vmlib -lm -ldl -lpthread -lrt)
87+
endif ()
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The "native-stack-overflow" sample project
2+
==========================================
3+
4+
This sample examines native stack overflow detection mechanisms.
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
#
5+
6+
#!/bin/bash
7+
8+
CURR_DIR=$PWD
9+
WAMR_DIR=${PWD}/../..
10+
OUT_DIR=${PWD}/out
11+
12+
WASM_APPS=${PWD}/wasm-apps
13+
14+
15+
rm -rf ${OUT_DIR}
16+
mkdir ${OUT_DIR}
17+
mkdir ${OUT_DIR}/wasm-apps
18+
19+
20+
echo "##################### build (default)"
21+
cd ${CURR_DIR}
22+
mkdir -p cmake_build
23+
cd cmake_build
24+
cmake ..
25+
make -j 4
26+
if [ $? != 0 ];then
27+
echo "BUILD_FAIL native-stack-overflow exit as $?\n"
28+
exit 2
29+
fi
30+
cp -a native-stack-overflow ${OUT_DIR}
31+
32+
echo "##################### build (WAMR_DISABLE_HW_BOUND_CHECK=1)"
33+
cd ${CURR_DIR}
34+
mkdir -p cmake_build_disable_hw_bound
35+
cd cmake_build_disable_hw_bound
36+
cmake -D WAMR_DISABLE_HW_BOUND_CHECK=1 ..
37+
make -j 4
38+
if [ $? != 0 ];then
39+
echo "BUILD_FAIL native-stack-overflow exit as $?\n"
40+
exit 2
41+
fi
42+
cp -a native-stack-overflow ${OUT_DIR}/native-stack-overflow.WAMR_DISABLE_HW_BOUND_CHECK
43+
44+
echo
45+
46+
echo "##################### build wasm apps"
47+
48+
cd ${WASM_APPS}
49+
50+
for i in `ls *.c`
51+
do
52+
APP_SRC="$i"
53+
OUT_FILE=${i%.*}.wasm
54+
55+
# use WAMR SDK to build out the .wasm binary
56+
/opt/wasi-sdk/bin/clang \
57+
-mexec-model=reactor \
58+
-Os -z stack-size=4096 -Wl,--initial-memory=65536 \
59+
-Wl,--allow-undefined \
60+
-o ${OUT_DIR}/wasm-apps/${OUT_FILE} ${APP_SRC}
61+
62+
if [ -f ${OUT_DIR}/wasm-apps/${OUT_FILE} ]; then
63+
echo "build ${OUT_FILE} success"
64+
else
65+
echo "build ${OUT_FILE} fail"
66+
fi
67+
done
68+
echo "#################### build wasm apps done"
69+
70+
echo "#################### aot-compile"
71+
WAMRC=${WAMR_DIR}/wamr-compiler/build/wamrc
72+
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
73+
74+
echo "#################### aot-compile (--bounds-checks=1)"
75+
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot.bounds-checks --bounds-checks=1 ${OUT_DIR}/wasm-apps/${OUT_FILE}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rm -r cmake_build cmake_build_disable_hw_bound out

samples/native-stack-overflow/run.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
echo "====== Interpreter"
4+
out/native-stack-overflow out/wasm-apps/testapp.wasm
5+
6+
echo
7+
echo "====== AOT"
8+
out/native-stack-overflow out/wasm-apps/testapp.wasm.aot
9+
10+
echo
11+
echo "====== AOT WAMR_DISABLE_HW_BOUND_CHECK=1"
12+
out/native-stack-overflow.WAMR_DISABLE_HW_BOUND_CHECK out/wasm-apps/testapp.wasm.aot.bounds-checks
+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (C) 2024 Midokura Japan KK. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include "wasm_export.h"
7+
#include "bh_read_file.h"
8+
9+
uint32_t
10+
host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx,
11+
uint32_t x, uint32_t stack);
12+
uint32_t
13+
host_consume_stack(wasm_exec_env_t exec_env, uint32_t stack);
14+
15+
extern unsigned int nest;
16+
17+
static NativeSymbol native_symbols[] = {
18+
{ "host_consume_stack_and_call_indirect",
19+
host_consume_stack_and_call_indirect, "(iii)i", NULL },
20+
{ "host_consume_stack", host_consume_stack, "(i)i", NULL },
21+
};
22+
23+
struct record {
24+
bool failed;
25+
bool leaked;
26+
char exception[128]; /* EXCEPTION_BUF_LEN */
27+
};
28+
29+
void
30+
print_record(unsigned int start, unsigned int end, const struct record *rec)
31+
{
32+
printf("%5u - %5u | %6s | %6s | %s\n", start, end,
33+
rec->failed ? "failed" : "ok", rec->leaked ? "leaked" : "ok",
34+
rec->exception);
35+
}
36+
37+
int
38+
main(int argc, char **argv)
39+
{
40+
char *buffer;
41+
char error_buf[128];
42+
43+
if (argc != 2) {
44+
return 2;
45+
}
46+
char *module_path = argv[1];
47+
48+
wasm_module_t module = NULL;
49+
uint32 buf_size;
50+
uint32 stack_size = 4096;
51+
/*
52+
* disable app heap.
53+
* - we use wasi
54+
* - https://github.com/bytecodealliance/wasm-micro-runtime/issues/2275
55+
*/
56+
uint32 heap_size = 0;
57+
58+
RuntimeInitArgs init_args;
59+
memset(&init_args, 0, sizeof(RuntimeInitArgs));
60+
init_args.mem_alloc_type = Alloc_With_System_Allocator;
61+
init_args.n_native_symbols = sizeof(native_symbols) / sizeof(NativeSymbol);
62+
init_args.native_module_name = "env";
63+
init_args.native_symbols = native_symbols;
64+
if (!wasm_runtime_full_init(&init_args)) {
65+
printf("wasm_runtime_full_init failed.\n");
66+
return -1;
67+
}
68+
69+
buffer = bh_read_file_to_buffer(module_path, &buf_size);
70+
if (!buffer) {
71+
printf("bh_read_file_to_buffer failed\n");
72+
goto fail;
73+
}
74+
75+
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
76+
sizeof(error_buf));
77+
if (!module) {
78+
printf("wasm_runtime_load failed: %s\n", error_buf);
79+
goto fail;
80+
}
81+
82+
/* header */
83+
printf(" stack size | fail? | leak? | exception\n");
84+
printf("-------------------------------------------------------------------"
85+
"--------\n");
86+
87+
unsigned int stack;
88+
unsigned int prevstack;
89+
unsigned int stack_range_start = 0;
90+
unsigned int stack_range_end = 4096 * 6;
91+
unsigned int step = 16;
92+
struct record rec0;
93+
struct record rec1;
94+
struct record *rec = &rec0;
95+
struct record *prevrec = &rec1;
96+
bool have_prevrec = false;
97+
for (stack = stack_range_start; stack < stack_range_end; stack += step) {
98+
wasm_module_inst_t module_inst = NULL;
99+
wasm_exec_env_t exec_env = NULL;
100+
bool failed = true;
101+
const char *exception = NULL;
102+
nest = 0;
103+
104+
module_inst = wasm_runtime_instantiate(module, stack_size, heap_size,
105+
error_buf, sizeof(error_buf));
106+
if (!module_inst) {
107+
printf("wasm_runtime_instantiate failed: %s\n", error_buf);
108+
goto fail2;
109+
}
110+
111+
exec_env = wasm_runtime_create_exec_env(module_inst, stack_size);
112+
if (!exec_env) {
113+
printf("wasm_runtime_create_exec_env failed\n");
114+
goto fail2;
115+
}
116+
117+
const char *funcname = "test";
118+
wasm_function_inst_t func =
119+
wasm_runtime_lookup_function(module_inst, funcname);
120+
if (!func) {
121+
printf("wasm_runtime_lookup_function failed for %s\n", funcname);
122+
goto fail2;
123+
}
124+
125+
/* note: the function type is (ii)i */
126+
uint32_t wasm_argv[] = {
127+
stack,
128+
30,
129+
};
130+
uint32_t wasm_argc = 2;
131+
if (!wasm_runtime_call_wasm(exec_env, func, wasm_argc, wasm_argv)) {
132+
exception = wasm_runtime_get_exception(module_inst);
133+
goto fail2;
134+
}
135+
failed = false;
136+
fail2:
137+
/*
138+
* note: non-zero "nest" here demonstrates resource leak on longjmp
139+
* from signal handler.
140+
* cf.
141+
* https://github.com/bytecodealliance/wasm-micro-runtime/issues/3320
142+
*/
143+
memset(rec, 0, sizeof(*rec));
144+
rec->failed = failed;
145+
rec->leaked = nest != 0;
146+
strncpy(rec->exception, exception ? exception : "",
147+
sizeof(rec->exception));
148+
if (have_prevrec && memcmp(prevrec, rec, sizeof(*rec))) {
149+
print_record(prevstack, stack, prevrec);
150+
have_prevrec = false;
151+
}
152+
if (!have_prevrec) {
153+
prevstack = stack;
154+
struct record *tmp = prevrec;
155+
prevrec = rec;
156+
rec = tmp;
157+
have_prevrec = true;
158+
}
159+
if (exec_env) {
160+
wasm_runtime_destroy_exec_env(exec_env);
161+
}
162+
if (module_inst) {
163+
wasm_runtime_deinstantiate(module_inst);
164+
}
165+
}
166+
if (have_prevrec) {
167+
print_record(prevstack, stack, prevrec);
168+
}
169+
170+
fail:
171+
if (module) {
172+
wasm_runtime_unload(module);
173+
}
174+
if (buffer) {
175+
BH_FREE(buffer);
176+
}
177+
wasm_runtime_destroy();
178+
}

0 commit comments

Comments
 (0)