Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/library/micropython.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Functions
used. The absolute value of this is not particularly useful, rather it
should be used to compute differences in stack usage at different points.

Note: When building with sanitizers (ASan/UBSan), an 8 KiB stack margin is
automatically reserved unless the port overrides MICROPY_STACK_CHECK_MARGIN.

.. function:: heap_lock()
.. function:: heap_unlock()
.. function:: heap_locked()
Expand Down
5 changes: 5 additions & 0 deletions ports/unix/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ typedef long mp_off_t;
#define MICROPY_STACKLESS_STRICT (0)
#endif

// Reserve extra C-stack headroom for overflow checks.
// Sanitizer builds enlarge call frames; 8 KiB prevents
// false positives without noticeably shrinking usable stack.
#define MICROPY_STACK_CHECK_MARGIN (8192)

// Implementation of the machine module.
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/unix/modmachine.c"

Expand Down
10 changes: 10 additions & 0 deletions py/mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,16 @@
// Additional margin between the places in the runtime where Python stack is
// checked and the actual end of the C stack. Needs to be large enough to avoid
// overflows from function calls made between checks.
#if !defined(MICROPY_STACK_CHECK_MARGIN)
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_UNDEFINED__)
#define MICROPY_STACK_CHECK_MARGIN (8192)
#elif defined(__has_feature)
#if __has_feature(address_sanitizer) || __has_feature(undefined_sanitizer)
#define MICROPY_STACK_CHECK_MARGIN (8192)
#endif
#endif
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we only support sanitizers on the unix port does it makes sense to move this block into ports/unix/mpconfigport.h?

(It's also redundant in this version of the PR, where the margin is already added unconditionally on the unix port.)


#ifndef MICROPY_STACK_CHECK_MARGIN
#define MICROPY_STACK_CHECK_MARGIN (0)
#endif
Expand Down
6 changes: 5 additions & 1 deletion tests/thread/thread_stacksize1.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test change makes sense though --- if the sanitizer is padding out every stack frame with guard pages, then in a thread context where the standard library can't just freely syscall brk to expand, it makes sense that the test might need to explicitly request larger stacks that can hold all the extra padding.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

# different implementations have different minimum sizes
if sys.implementation.name == "micropython":
sz = 2 * 1024
# Use larger stack size for desktop platforms to accommodate sanitizers
if sys.platform in ("linux", "darwin", "emscripten", "win32"):
sz = 32 * 1024
else:
sz = 2 * 1024
else:
sz = 512 * 1024

Expand Down
Loading