Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Stack overflow detection (method 1 and 2) in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Overflow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS stack overflow hook example (Method 1)?

Consider this FreeRTOS code snippet using stack overflow detection Method 1. What will be the output when a stack overflow is detected?

FreeRTOS
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
    printf("Stack overflow detected in task: %s\n", pcTaskName);
    for(;;); // Infinite loop to halt system
}

// Assume a task named "TaskA" causes stack overflow triggering this hook.
APrints "Stack overflow detected in task: TaskA" and halts execution in infinite loop
BPrints "Stack overflow detected" without task name and continues running
CSystem resets immediately without any print output
DNo output; the hook is not called automatically
Attempts:
2 left
💡 Hint

Method 1 uses a hook function called automatically on stack overflow with task name info.

❓ Predict Output
intermediate
2:00remaining
What happens when stack overflow detection Method 2 is enabled and overflow occurs?

Given FreeRTOS configured with stack overflow detection Method 2, what is the expected behavior when a task overflows its stack?

FreeRTOS
#define configCHECK_FOR_STACK_OVERFLOW 2

void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
    printf("Overflow detected in %s\n", pcTaskName);
    taskDISABLE_INTERRUPTS();
    for(;;); // Halt
}

// Assume "TaskB" causes overflow.
AThe hook prints "Overflow detected in TaskB" and halts system
BThe system ignores overflow and continues running
CThe hook is not called because Method 2 disables it
DThe system resets without any print
Attempts:
2 left
💡 Hint

Method 2 checks stack overflow on each context switch and calls the hook.

🧠 Conceptual
advanced
2:00remaining
Why does Method 2 stack overflow detection have higher CPU overhead than Method 1?

Choose the best explanation for why FreeRTOS stack overflow detection Method 2 causes more CPU overhead than Method 1.

AMethod 2 disables interrupts causing delays in task switching
BMethod 2 requires manual calls in each task increasing code size
CMethod 2 uses hardware breakpoints which slow down CPU
DMethod 2 checks the stack pointer and limits on every context switch, adding extra instructions each time
Attempts:
2 left
💡 Hint

Think about when the overflow check happens in each method.

❓ Predict Output
advanced
2:00remaining
What error occurs if configCHECK_FOR_STACK_OVERFLOW is set to 1 but vApplicationStackOverflowHook is missing?

Given FreeRTOS with configCHECK_FOR_STACK_OVERFLOW = 1 but no vApplicationStackOverflowHook defined, what happens when a stack overflow occurs?

ASystem resets automatically without hook
BStack overflow detected but no hook called, system continues silently
CLinker error due to missing hook function
DRuntime crash with undefined reference error
Attempts:
2 left
💡 Hint

Check if the hook function is required by the FreeRTOS config.

🔧 Debug
expert
3:00remaining
Identify the cause of incorrect stack overflow detection in this Method 1 implementation

Review this Method 1 stack overflow hook code. Why might the stack overflow not be detected correctly?

FreeRTOS
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
    // Incorrectly uses pcTaskName pointer without checking
    if(pcTaskName != NULL) {
        printf("Overflow in task: %s\n", pcTaskName);
    } else {
        printf("Overflow detected but task name missing\n");
    }
}

// Stack overflow occurs but pcTaskName is NULL.
AThe stack overflow detection is correct; pcTaskName can be NULL in Method 1
BpcTaskName is NULL because the FreeRTOS config does not store task names, causing incorrect detection
CThe hook is not called because configCHECK_FOR_STACK_OVERFLOW is set to 0
DThe hook causes a crash because it dereferences a NULL pointer without handling
Attempts:
2 left
💡 Hint

Consider how task names are configured and passed to the hook.

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