0
0
FreeRTOSprogramming~10 mins

Stack overflow detection (method 1 and 2) in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stack overflow detection (method 1 and 2)
Task starts
Stack allocated
Method 1: Check guard value
Is guard value intact?
NoStack overflow detected
Method 2: Check stack pointer limits
Is stack pointer within limits?
NoStack overflow detected
Task runs safely
Repeat checks periodically
The system uses two methods to detect stack overflow: checking a guard value and verifying the stack pointer stays within limits. If either fails, overflow is detected.
Execution Sample
FreeRTOS
// Example of stack overflow checks performed by FreeRTOS (simplified)
// Method 1: Check guard value
if (*guard_value != EXPECTED) {
  // Overflow detected - call vApplicationStackOverflowHook
}
// Method 2: Check stack pointer
if (stack_pointer < stack_start || stack_pointer > stack_end) {
  // Overflow detected - call vApplicationStackOverflowHook
}
This code snippet shows two checks: one for a guard value and one for stack pointer limits to detect stack overflow.
Execution Table
StepCheck TypeConditionResultAction
1Method 1: Guard Valueguard_value == EXPECTEDTrueNo overflow, continue
2Method 2: Stack Pointerstack_pointer within stack_start and stack_endTrueNo overflow, continue
3Method 1: Guard Valueguard_value == EXPECTEDFalseOverflow detected, call hook
4Method 2: Stack Pointerstack_pointer within stack_start and stack_endFalseOverflow detected, call hook
5EndN/AN/AStop checks or handle error
💡 Checks stop when overflow is detected or task ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
guard_valueEXPECTEDEXPECTEDEXPECTEDCORRUPTEDCORRUPTEDCORRUPTED
stack_pointerwithin limitswithin limitswithin limitswithin limitsout of limitsout of limits
Key Moments - 3 Insights
Why do we check a guard value in Method 1?
The guard value is a known fixed pattern placed at the stack boundary. If it changes (see execution_table row 3), it means the stack has overflowed into that area.
How does Method 2 detect overflow using the stack pointer?
Method 2 checks if the current stack pointer is outside the allocated stack range (see execution_table row 4). If it is, overflow has occurred.
Can both methods detect overflow at the same time?
Yes, both methods run independently and can detect overflow separately (see execution_table rows 3 and 4). This provides double safety.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of Method 1 check at Step 3?
ATrue - No overflow
BCondition not checked
CFalse - Overflow detected
DStack pointer out of limits
💡 Hint
Check the 'Result' column for Step 3 in execution_table.
At which step does Method 2 detect stack pointer out of limits?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Check Type' and 'Result' columns in execution_table for stack pointer checks.
If the guard value is corrupted, what will happen in Method 1?
AOverflow detected and hook called
BNo overflow detected
CStack pointer checked instead
DTask continues normally
💡 Hint
Refer to execution_table row 3 and key_moments about guard value.
Concept Snapshot
Stack overflow detection uses two methods:
1. Guard value check: a known pattern at stack boundary is verified.
2. Stack pointer check: verifies pointer stays within stack limits.
If either fails, overflow is detected and handled.
This ensures task stack safety in FreeRTOS.
Full Transcript
In FreeRTOS, stack overflow detection uses two main methods. Method 1 places a guard value at the edge of the stack. The system checks if this value remains unchanged; if it is corrupted, it means the stack overflowed into that area. Method 2 checks the current stack pointer to ensure it stays within the allocated stack memory range. If the pointer goes outside these limits, overflow is detected. Both methods run periodically during task execution. If either method detects overflow, a hook function is called to handle the error. This double-check approach helps keep tasks safe from stack overflows.