Docs / Stack Overflow Detection

Stack Overflow Detection

Overview

bedrock[RTOS] implements stack overflow detection using a canary-based mechanism. A magic value (canary) is placed at the bottom of each task's stack and checked during context switches.

Implementation Details

Canary Value

#define BR_STACK_CANARY   0xDEADBEEF

How It Works

  1. Initialization: When a task is created (br_task_create), a canary value is written to the bottom (lowest address) of the task's stack.
  2. Storage: Each TCB (br_tcb_t) contains a pointer to the canary location:
    uint32_t *stack_canary;   /* Pointer to canary at stack bottom */
  3. Detection: Before every context switch (br_sched_reschedule), the scheduler checks if the canary value has been corrupted using br_hal_check_stack_overflow().
  4. Response: If corruption is detected, the system triggers a kernel panic and halts execution to prevent further damage.

Error Code

BR_ERR_STACK_OVF = -7  /* Stack overflow detected */

HAL Interface

br_hal_check_stack_overflow(br_tcb_t *tcb)

Checks the stack canary for the given task. If the canary value has been corrupted (indicating stack overflow), the function triggers a kernel panic.

Limitations

Testing

To test stack overflow detection, create a task with a small stack and trigger recursive calls or large local arrays:

static uint8_t test_stack[128];  /* Very small stack */

void overflow_task(void *arg) {
    volatile uint8_t large_buffer[256];  /* Larger than stack! */
    /* This will overflow and be detected on next context switch */
}

References