Skip to content

ci: Enable UBSan for 'longlong' builds in CI, add stack size for sanitizer builds. #17735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

projectgus
Copy link
Contributor

Summary

Testing

Trade-offs and Alternatives

  • Could leave UBSan disabled in CI for the 'longlong' variant, but (as demonstrated) it is useful to find bugs.

@dpgeorge
Copy link
Member

I'm confused why the stack changes are needed here:

  1. For the main unix thread, its allocated stack is probably megabytes long (and might even automatically grow), so MICROPY_STACK_CHECK_MARGIN shouldn't make any difference for the main thread.
  2. ports/unix/mpthreadport.c has:
    // ensure there is enough stack to include a stack-overflow margin
    if (*stack_size < 2 * THREAD_STACK_OVERFLOW_MARGIN) {
        *stack_size = 2 * THREAD_STACK_OVERFLOW_MARGIN;
    }                                                        

which is 16k, so I don't think the change to tests/thread/thread_stacksize1.py is needed (is 16k really not enough?).
3. ports/unix/mpthreadport.c has:

    // adjust stack_size to provide room to recover from hitting the limit
    *stack_size -= THREAD_STACK_OVERFLOW_MARGIN;

and that's already 8k, so I don't think MICROPY_STACK_CHECK_MARGIN is needed.

It's a bit hard to follow the logic of the stack limit for threads. So maybe we should either get rid of THREAD_STACK_OVERFLOW_MARGIN and use MICROPY_STACK_CHECK_MARGIN instead. Or just increase THREAD_STACK_OVERFLOW_MARGIN if it really needs to be increased for sanitizer builds.

Copy link

codecov bot commented Jul 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@255d74b). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #17735   +/-   ##
=========================================
  Coverage          ?   98.38%           
=========================================
  Files             ?      171           
  Lines             ?    22283           
  Branches          ?        0           
=========================================
  Hits              ?    21924           
  Misses            ?      359           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@projectgus
Copy link
Contributor Author

I'm confused why the stack changes are needed here:

This PR with the cherry-picked commit also hit "RuntimeError: maximum recursion depth exceeded" when testing longlong+UBSan, so you're right that it's not the right fix for this case.

It's a bit hard to follow the logic of the stack limit for threads. So maybe we should either get rid of THREAD_STACK_OVERFLOW_MARGIN and use MICROPY_STACK_CHECK_MARGIN instead. Or just increase THREAD_STACK_OVERFLOW_MARGIN if it really needs to be increased for sanitizer builds.

Seems reasonable, I'll have a look along these lines.

@dpgeorge
Copy link
Member

This PR with the cherry-picked commit also hit "RuntimeError: maximum recursion depth exceeded" when testing longlong+UBSan

So maybe there just needs to be more stack overall, rather than a change in stack margin? Maybe ports/unix/mpthreadport.c:mp_thread_create just needs to have a larger minimum stack size for sanitizer builds?

Also rewrite the sanitizer argument variables to not assume a variant.

longlong variant currently fails in this config, due to a bug fixed
in follow-up commit.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
Clang and gcc>=14 can use __has_feature() to detect if a sanitizer
is enabled, but older GCC has no mechanism - need to set a macro
explicitly for this to be recognised.

Necessary for increasing some resource limits in sanitizer builds.
Important not to use to avoid real issues!

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
@projectgus projectgus force-pushed the ci/longlong_ubsan branch 3 times, most recently from f7b8aee to 86ec523 Compare August 6, 2025 00:37
@projectgus projectgus changed the title ci: Enable UBSan for 'longlong' builds in CI, add stack margin for sanitizer builds. ci: Enable UBSan for 'longlong' builds in CI, add stack size for sanitizer builds. Aug 6, 2025
@projectgus projectgus force-pushed the ci/longlong_ubsan branch 2 times, most recently from b354b70 to b176fef Compare August 6, 2025 01:55
Includes a refactor to apply the same stack size multipliers
for the default thread stack size same as the main stack size.

This goes in a new port-specific header as it depends on macros
in misc.h, so can't be in mpconfigport.h.

A side effect of this is that the default thread stack size is
now doubled on ARM, same as the main stack size.

This a fix for 'RuntimeError: maximum recursion depth exceeded' when
running some tests under UBSan and/or ASan with some older GCC
versions (observed on gcc 11.4 as used in CI).

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
Signed-off-by: Angus Gratton <angus@redyak.com.au>
Otherwise no exceptions are raised when doubling the stack size on unix
ports with sanitizers enabled.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
port-unix tests Relates to tests/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants