Skip to content

Commit d1ff01d

Browse files
committed
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>
1 parent df05cae commit d1ff01d

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

docs/library/micropython.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ Functions
8080
used. The absolute value of this is not particularly useful, rather it
8181
should be used to compute differences in stack usage at different points.
8282

83+
Note: When building with sanitizers (ASan/UBSan), an 8 KiB stack margin is
84+
automatically reserved unless the port overrides MICROPY_STACK_CHECK_MARGIN.
85+
8386
.. function:: heap_lock()
8487
.. function:: heap_unlock()
8588
.. function:: heap_locked()

ports/unix/mpconfigport.h

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

140+
// Reserve extra C-stack headroom for overflow checks.
141+
// Sanitizer builds enlarge call frames; 8 KiB prevents
142+
// false positives without noticeably shrinking usable stack.
143+
#define MICROPY_STACK_CHECK_MARGIN (8192)
144+
140145
// Implementation of the machine module.
141146
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/unix/modmachine.c"
142147

py/mpconfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@
701701
// Additional margin between the places in the runtime where Python stack is
702702
// checked and the actual end of the C stack. Needs to be large enough to avoid
703703
// overflows from function calls made between checks.
704+
#if !defined(MICROPY_STACK_CHECK_MARGIN)
705+
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_UNDEFINED__)
706+
#define MICROPY_STACK_CHECK_MARGIN (8192)
707+
#elif defined(__has_feature)
708+
#if __has_feature(address_sanitizer) || __has_feature(undefined_sanitizer)
709+
#define MICROPY_STACK_CHECK_MARGIN (8192)
710+
#endif
711+
#endif
712+
#endif
713+
704714
#ifndef MICROPY_STACK_CHECK_MARGIN
705715
#define MICROPY_STACK_CHECK_MARGIN (0)
706716
#endif

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy