Bird
Raised Fist0
FreeRTOSprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Stack overflow detection (method 1 and 2)
Task starts
↓
Stack allocated
↓
Method 1: Check guard value
↓
Is guard value intact?
No→Stack overflow detected
↓
Method 2: Check stack pointer limits
↓
Is stack pointer within limits?
No→Stack 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.

Practice

(1/5)
1. What is the main purpose of stack overflow detection in FreeRTOS?
easy
A. To reduce RAM usage by tasks
B. To prevent tasks from using more stack memory than allocated
C. To increase CPU clock speed
D. To speed up task switching

Solution

  1. Step 1: Understand stack overflow concept

    Stack overflow happens when a task uses more stack memory than allocated, causing memory corruption.
  2. Step 2: Identify purpose of detection

    Stack overflow detection helps catch this problem early to prevent crashes or unpredictable behavior.
  3. Final Answer:

    To prevent tasks from using more stack memory than allocated -> Option B
  4. Quick Check:

    Stack overflow detection = prevent excess stack use [OK]
Hint: Stack overflow detection stops tasks using too much stack [OK]
Common Mistakes:
  • Confusing stack overflow with CPU speed
  • Thinking it reduces RAM usage
  • Assuming it speeds up task switching
2. Which FreeRTOS configuration enables Method 1 stack overflow detection?
easy
A. configMAX_PRIORITIES set to 5
B. configUSE_PREEMPTION set to 0
C. configUSE_IDLE_HOOK set to 1
D. configCHECK_FOR_STACK_OVERFLOW set to 1 or 2

Solution

  1. Step 1: Identify config for stack overflow detection

    FreeRTOS uses configCHECK_FOR_STACK_OVERFLOW to enable Method 1 detection.
  2. Step 2: Understand valid values

    Setting configCHECK_FOR_STACK_OVERFLOW to 1 or 2 activates automatic detection on context switches.
  3. Final Answer:

    configCHECK_FOR_STACK_OVERFLOW set to 1 or 2 -> Option D
  4. Quick Check:

    configCHECK_FOR_STACK_OVERFLOW = 1 or 2 [OK]
Hint: Method 1 uses configCHECK_FOR_STACK_OVERFLOW = 1 or 2 [OK]
Common Mistakes:
  • Confusing unrelated config options
  • Using configUSE_PREEMPTION for stack checks
  • Setting configMAX_PRIORITIES thinking it affects stack
3. Given this FreeRTOS hook function for Method 1:
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
    printf("Stack overflow detected in task: %s\n", pcTaskName);
    while(1); // Halt
}
What will happen if a stack overflow is detected?
medium
A. Prints task name and halts the system
B. Resets the task automatically
C. Ignores the overflow and continues
D. Logs overflow but continues running

Solution

  1. Step 1: Analyze the hook function code

    The function prints the task name where overflow occurred and then enters an infinite loop.
  2. Step 2: Understand effect of infinite loop

    The infinite loop halts the system to prevent further damage after overflow detection.
  3. Final Answer:

    Prints task name and halts the system -> Option A
  4. Quick Check:

    Stack overflow hook prints and halts [OK]
Hint: Stack overflow hook prints and stops system [OK]
Common Mistakes:
  • Thinking system resets automatically
  • Assuming overflow is ignored
  • Believing task restarts without halt
4. In Method 2 stack overflow detection, which mistake will cause incorrect detection?
medium
A. Using vApplicationStackOverflowHook function
B. Enabling configCHECK_FOR_STACK_OVERFLOW to 2
C. Not initializing the stack pointer check value properly
D. Setting configUSE_PREEMPTION to 1

Solution

  1. Step 1: Understand Method 2 detection

    Method 2 manually checks stack pointer against a known value to detect overflow.
  2. Step 2: Identify common error

    If the initial stack pointer check value is not set correctly, the comparison will be invalid, causing false negatives or positives.
  3. Final Answer:

    Not initializing the stack pointer check value properly -> Option C
  4. Quick Check:

    Method 2 needs correct stack pointer init [OK]
Hint: Method 2 needs proper stack pointer initialization [OK]
Common Mistakes:
  • Confusing Method 1 config with Method 2
  • Ignoring stack pointer initialization
  • Assuming hook function is used in Method 2
5. You want to implement stack overflow detection in your FreeRTOS project using Method 1. Which steps should you follow to ensure it works correctly?
hard
A. Set configCHECK_FOR_STACK_OVERFLOW to 1 or 2 and implement vApplicationStackOverflowHook
B. Manually compare stack pointers in each task without config changes
C. Disable preemption and use configUSE_IDLE_HOOK for detection
D. Increase task stack size to avoid overflow without detection

Solution

  1. Step 1: Enable Method 1 detection in config

    Set configCHECK_FOR_STACK_OVERFLOW to 1 or 2 to activate automatic detection during context switches.
  2. Step 2: Implement the hook function

    Define vApplicationStackOverflowHook to handle overflow events, e.g., log and halt.
  3. Final Answer:

    Set configCHECK_FOR_STACK_OVERFLOW to 1 or 2 and implement vApplicationStackOverflowHook -> Option A
  4. Quick Check:

    Method 1 = config + hook function [OK]
Hint: Method 1 needs config and hook function implemented [OK]
Common Mistakes:
  • Trying manual pointer checks for Method 1
  • Disabling preemption thinking it helps detection
  • Ignoring hook function implementation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard