15.3.2 Stack Callback Function

When the checking code detects that the guard variable on the stack has been modified, it notifies the run-time environment by calling the function: void __stack_chk_fail(void);

You must provide an implementation for this function. suitable for your application. Normally, such a function terminates the program, possibly after reporting a fault.

XC32 provides a default, weak implementation for __stack_chk_fail() with a minimal footprint. __stack_chk_fail() is called when an overflow is detected, as due to the canary being overwritten. This function should not return. If the overflow was caused by an attack, the suspended execution is a good way to block it.

void __attribute__((noreturn)) __stack_chk_fail (void)
{
    // XC32 default weak implementation
    while(1)
    {
        // Replace this code with application-specific code
        __builtin_software_breakpoint();
    }
}