-
Notifications
You must be signed in to change notification settings - Fork 2k
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
riscv_common: Increase ISR stack size if DEVELHELP is enabled #16448
base: master
Are you sure you want to change the base?
Conversation
Without this change, the ISR stack will overflow if DEVELHELP is enabled and an unhandled trap is raised as cpu/riscv_common/irq_arch.c uses printf in this case and printf requires more than 256 bytes of stack space.
Just to check for milder means (as having ISR stack size depend on devhelp is correct here but may mask ISR overflow issues when devhelp is switched off): Would expressing the printf calls as |
Yes, that does seem to work. Though the problem then becomes that Lines 75 to 85 in 2692957
|
Though the problem then becomes that `core_panic` (as called by the
trap handler) indirectly uses newlib printf
I'm leaning towards that this is more of an indication that that code
should use puts-style debugging over printf style (all it does is a %s).
Started asking around at #13710 for easier ways to do that.
|
Does the stack also overflow with picolibc? There seems to be a strong consensus that we should switch to picolibc for all platforms that support it. It is currently only being blocked by the toolchain our CI runs having an out-of-date version of picolibc installed. |
@nmeum hey, still interested in getting this in? I remember running in this exact issue a couple months ago... |
Contribution description
The
handle_trap()
implementation forriscv_common
includes the following debugging code ifDEVELHELP
is enabled:RIOT/cpu/riscv_common/irq_arch.c
Lines 116 to 121 in 2692957
This code is executed on the ISR stack and not on any standard RIOT thread stack. The stack size of this ISR stack is defined in the ldscript for
riscv_common
as follows:RIOT/cpu/riscv_common/ldscripts/riscv_base.ld
Line 35 in 2692957
Unfortunately, 256 Bytes is insufficient for newlib
printf()
usage and thus causes a stack overflow on the ISR stack ifDEVELHELP
is enabled and this code path inhandle_trap()
is triggered. One solution to avoid this problem is to increase the size of the ISR stack ifDEVELHELP
is enabled, this is the solution this PR proposes. Alternatively, it would also be possible to simply remove theprintf()
invocations fromhandle_trap()
.Testing procedure
I discovered this through an external tool I am currently working on which I cannot share at this point. Similar to #16395, this bug can be confirmed independently by checking the value of the
sp
register after each instruction inhandle_trap()
using GDB. For instance, by using the following modified version of thehello-world
example application to trigger the code path with theprintf
statements inhandle_trap()
:I am not sure if there is any interest in these ISR stack overflow edge cases but I wanted to document this anyhow in case some else runs into them by accident. If you don't consider it worthwhile to fix these edge cases let me know.
Issues/PRs references
This is similar to the aforementioned issue #16395 which is also a stack overflow on the ISR stack which occurs due to
printf()
usage on this stack. This issue is, however, not fixed by this PR asENABLE_DEBUG
can be enabled independent ofDEVELHELP
.