0
0
FreeRTOSprogramming~20 mins

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

Choose your learning style9 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.