Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65b531b

Browse files
committedDec 15, 2023
wasm2c: Add macro and tests to allow disabling stack exhaustion checks
1 parent 80b9752 commit 65b531b

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed
 

‎.github/workflows/build.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ jobs:
172172
runs-on: ubuntu-latest
173173
env:
174174
USE_NINJA: "1"
175-
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING"
175+
WASM2C_CFLAGS: "-DWASM_RT_USE_MMAP=1 -DWASM_RT_SKIP_SIGNAL_RECOVERY=1 -DWASM_RT_NONCONFORMING_STACK_UNCHECKED=1 -DWASM2C_TEST_EMBEDDER_SIGNAL_HANDLING"
176176
steps:
177177
- uses: actions/setup-python@v1
178178
with:
@@ -182,5 +182,5 @@ jobs:
182182
submodules: true
183183
- run: sudo apt-get install ninja-build
184184
- run: make clang-debug
185-
- name: tests (excluding memory64)
186-
run: ./test/run-tests.py --exclude-dir memory64
185+
- name: tests (wasm2c tests excluding memory64 and stack exhaustion)
186+
run: ./test/run-tests.py *wasm2c* --exclude memory64 --exclude call.txt --exclude call_indirect.txt --exclude fac.txt --exclude skip-stack-guard-page.txt

‎test/run-tests.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -684,17 +684,18 @@ def _Clear(self):
684684
sys.stderr.write('\r%s\r' % (' ' * self.last_length))
685685

686686

687-
def FindTestFiles(ext, filter_pattern_re, exclude_dirs):
687+
def FindTestFiles(ext, filter_pattern_re, excludes):
688688
tests = []
689689
for root, dirs, files in os.walk(TEST_DIR):
690-
for ex in exclude_dirs:
690+
for ex in excludes:
691691
if ex in dirs:
692692
# Filtering out dirs here causes os.walk not to descend into them
693693
dirs.remove(ex)
694694
for f in files:
695-
path = os.path.join(root, f)
696-
if os.path.splitext(f)[1] == ext:
697-
tests.append(os.path.relpath(path, REPO_ROOT_DIR))
695+
if f not in excludes:
696+
path = os.path.join(root, f)
697+
if os.path.splitext(f)[1] == ext:
698+
tests.append(os.path.relpath(path, REPO_ROOT_DIR))
698699
tests.sort()
699700
return [test for test in tests if re.match(filter_pattern_re, test)]
700701

@@ -928,10 +929,10 @@ def main(args):
928929
action='store_true')
929930
parser.add_argument('patterns', metavar='pattern', nargs='*',
930931
help='test patterns.')
931-
parser.add_argument('--exclude-dir', action='append', default=[],
932-
help='directory to exclude.')
932+
parser.add_argument('--exclude', action='append', default=[],
933+
help='file or directory names to exclude.')
933934
options = parser.parse_args(args)
934-
exclude_dirs = options.exclude_dir
935+
excludes = options.exclude
935936

936937
if options.jobs != 1:
937938
if options.fail_fast:
@@ -947,9 +948,9 @@ def main(args):
947948
# By default, exclude wasi tests because WASI support is not include
948949
# by int the build by default.
949950
# TODO(sbc): Find some way to detect the WASI support.
950-
exclude_dirs += ['wasi']
951+
excludes += ['wasi']
951952

952-
test_names = FindTestFiles('.txt', pattern_re, exclude_dirs)
953+
test_names = FindTestFiles('.txt', pattern_re, excludes)
953954

954955
if options.list:
955956
for test_name in test_names:

‎test/spec-wasm2c-prefix.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ static void posix_signal_handler(int sig, siginfo_t* si, void* unused) {
373373
}
374374

375375
static void posix_install_signal_handler(void) {
376+
#if !WASM_RT_NONCONFORMING_STACK_UNCHECKED
376377
/* install altstack */
377378
stack_t ss;
378379
ss.ss_sp = malloc(SIGSTKSZ);
@@ -382,11 +383,15 @@ static void posix_install_signal_handler(void) {
382383
perror("sigaltstack failed");
383384
abort();
384385
}
386+
#endif
385387

386388
/* install signal handler */
387389
struct sigaction sa;
388390
memset(&sa, '\0', sizeof(sa));
389-
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
391+
sa.sa_flags = SA_SIGINFO;
392+
#if !WASM_RT_NONCONFORMING_STACK_UNCHECKED
393+
sa.sa_flags |= SA_ONSTACK;
394+
#endif
390395
sigemptyset(&sa.sa_mask);
391396
sa.sa_sigaction = posix_signal_handler;
392397
if (sigaction(SIGSEGV, &sa, NULL) != 0 || sigaction(SIGBUS, &sa, NULL) != 0) {
@@ -405,6 +410,7 @@ static void posix_cleanup_signal_handler(void) {
405410
abort();
406411
}
407412

413+
#if !WASM_RT_NONCONFORMING_STACK_UNCHECKED
408414
/* disable and free altstack */
409415
stack_t ss;
410416
ss.ss_flags = SS_DISABLE;
@@ -413,6 +419,7 @@ static void posix_cleanup_signal_handler(void) {
413419
abort();
414420
}
415421
free(ss.ss_sp);
422+
#endif
416423
}
417424
#endif
418425

‎wasm2c/wasm-rt.h

+22-2
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,20 @@ extern "C" {
166166
#define WASM_RT_INSTALL_SIGNAL_HANDLER 0
167167
#endif
168168

169+
/* This macro, if defined, allows the embedder to disable all stack exhaustion
170+
* checks. This a non conformant configuration, i.e., this does not respect
171+
* Wasm's specification, and may compromise security. Use with caution.
172+
*/
173+
#ifndef WASM_RT_NONCONFORMING_STACK_UNCHECKED
174+
#define WASM_RT_NONCONFORMING_STACK_UNCHECKED 0
175+
#endif
176+
169177
/* We need to detect and trap stack overflows. If we use a signal handler on
170178
* POSIX systems, this can detect call stack overflows. On windows, or platforms
171179
* without a signal handler, we use stack depth counting. */
172-
#if !defined(WASM_RT_STACK_DEPTH_COUNT) && \
173-
!defined(WASM_RT_STACK_EXHAUSTION_HANDLER)
180+
#if !defined(WASM_RT_STACK_DEPTH_COUNT) && \
181+
!defined(WASM_RT_STACK_EXHAUSTION_HANDLER) && \
182+
!WASM_RT_NONCONFORMING_STACK_UNCHECKED
174183

175184
#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32)
176185
#define WASM_RT_STACK_EXHAUSTION_HANDLER 1
@@ -188,6 +197,15 @@ extern "C" {
188197
#define WASM_RT_STACK_EXHAUSTION_HANDLER 0
189198
#endif
190199

200+
#if WASM_RT_NONCONFORMING_STACK_UNCHECKED
201+
202+
#if (WASM_RT_STACK_EXHAUSTION_HANDLER + WASM_RT_STACK_DEPTH_COUNT) != 0
203+
#error \
204+
"Cannot specify WASM_RT_NONCONFORMING_STACK_UNCHECKED along with WASM_RT_STACK_EXHAUSTION_HANDLER or WASM_RT_STACK_DEPTH_COUNT"
205+
#endif
206+
207+
#else
208+
191209
#if (WASM_RT_STACK_EXHAUSTION_HANDLER + WASM_RT_STACK_DEPTH_COUNT) > 1
192210
#error \
193211
"Cannot specify multiple options from WASM_RT_STACK_EXHAUSTION_HANDLER , WASM_RT_STACK_DEPTH_COUNT"
@@ -196,6 +214,8 @@ extern "C" {
196214
"Must specify one of WASM_RT_STACK_EXHAUSTION_HANDLER , WASM_RT_STACK_DEPTH_COUNT"
197215
#endif
198216

217+
#endif
218+
199219
#if WASM_RT_STACK_EXHAUSTION_HANDLER && !WASM_RT_INSTALL_SIGNAL_HANDLER
200220
#error \
201221
"WASM_RT_STACK_EXHAUSTION_HANDLER can only be used if WASM_RT_INSTALL_SIGNAL_HANDLER is enabled"

0 commit comments

Comments
 (0)
Please sign in to comment.