suoyuanG opened a new pull request, #17056:
URL: https://github.com/apache/nuttx/pull/17056
## Summary
I add a software method for stack overflow detection, which will detect the
sp register and the bottom stack memory of the thread.
## Impact
Add stack overflow detection on context switching. If enable
`STACKCHECK_SOFTWARE`, this check will be turned on. By default, the bottom 16
bytes of the stack will be checked. This parameter can be configured through
`STACKCHECK_MARGIN`
## Testing
```c
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define STACK_SIZE (8192)
static unsigned char stack[STACK_SIZE];
void *thread_func(void *arg) {
puts("haha");
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t thread;
pthread_attr_t attr;
uint32_t stack_color = 0xdeadbeef;
pthread_attr_init(&attr);
pthread_attr_setstack(&attr, stack, STACK_SIZE);
printf("stack: %p\n", stack);
if (pthread_create(&thread, &attr, thread_func, NULL) != 0) {
perror("pthread_create");
return 1;
}
const uintptr_t stack_bottom_align = ((uintptr_t)stack);
uint32_t *start = (uint32_t *)stack_bottom_align;
const uint32_t *end = (uint32_t *)(stack + STACK_SIZE - sizeof(uint32_t));
int found = 0;
for (; start <= end; ++start) {
if (*start == stack_color) {
*start = 0xabcdabcd;
printf("fetch the stack color: %p\n", start);
found = 1;
break;
}
}
pthread_join(thread, NULL);
if (!found) {
fprintf(stderr, "stack color not found\n");
}
pthread_attr_destroy(&attr);
return 0;
}
```
```bash
$ qemu-system-arm -cpu cortex-a7 -nographic -machine virt -kernel
build/nuttx -s
NuttShell (NSH) NuttX-12.10.0
nsh> stack_canarytest
stack: 0x400053b8
fetch the stack color: 0x400053c8
haha
dump_assert_info: Current Version: NuttX 12.10.0 0d62d83134-dirty Sep 22
2025 19:37:18 arm
dump_assert_info: Assertion failed : at file:
/sched/sched/sched_suspendscheduler.c:78 task: stack_canarytest process: Kernel
0x19a6c
up_dump_register: R0: 400035f0 R1: 00000001 R2: 00000000 R3: 00000001
up_dump_register: R4: 4000df38 R5: 4000df38 R6: 400035f0 R7: 00000000
up_dump_register: R8: 00039479 SB: 40002e94 SL: 00000006 FP: 0000004e
up_dump_register: IP: 00007ffe SP: 40007268 LR: 00005fcc PC: 00005fcc
...
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]