Skip to content

Commit 008b8c5

Browse files
koxudaxiprojectgus
authored andcommitted
build/sanitizer: Reserve 8 KiB C-stack margin for sanitizer builds.
Compiler sanitizers such as AddressSanitizer and UndefinedSanitizer inflate call frames and can trip MicroPython's C-stack overflow check. Define MICROPY_STACK_CHECK_MARGIN = 8192 when the relevant sanitizer macros are present, update the unix port config, docs, and the thread_stacksize test. Non-sanitizer builds are unchanged. Signed-off-by: Koudai Aono <koxudaxi@gmail.com> Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 4efe627 commit 008b8c5

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

ports/unix/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@ Several classes of checks are disabled via compiler flags:
173173
check is intended to make sure locals in a "returned from" stack frame are not
174174
used. However, this mode interferes with various assumptions that
175175
MicroPython's stack checking, NLR, and GC rely on.
176+
177+
Note: When building with sanitizers (ASan/UBSan), an 8 KiB stack check margin is
178+
automatically reserved unless the build overrides MICROPY_STACK_CHECK_MARGIN.
179+

ports/unix/mpconfigport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ typedef long mp_off_t;
137137
#define MICROPY_STACKLESS_STRICT (0)
138138
#endif
139139

140+
// Reserve extra C-stack headroom for overflow checks if sanitizers
141+
// are enabled (sanitizer builds enlarge call frames). 8 KiB prevents
142+
// false positives without noticeably shrinking usable stack.
143+
#if !defined(MICROPY_STACK_CHECK_MARGIN)
144+
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_UNDEFINED__)
145+
#define MICROPY_STACK_CHECK_MARGIN (8192)
146+
#elif defined(__has_feature)
147+
#if __has_feature(address_sanitizer) || __has_feature(undefined_sanitizer)
148+
#define MICROPY_STACK_CHECK_MARGIN (8192)
149+
#endif
150+
#endif
151+
#endif // !defined(MICROPY_STACK_CHECK_MARGIN)
152+
140153
// Implementation of the machine module.
141154
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/unix/modmachine.c"
142155

tests/thread/thread_stacksize1.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
# different implementations have different minimum sizes
99
if sys.implementation.name == "micropython":
10-
sz = 2 * 1024
10+
# Use larger stack size for desktop platforms to accommodate sanitizers
11+
if sys.platform in ("linux", "darwin", "emscripten", "win32"):
12+
sz = 32 * 1024
13+
else:
14+
sz = 2 * 1024
1115
else:
1216
sz = 512 * 1024
1317

0 commit comments

Comments
 (0)