Files
u-boot/common/stackprot.c
Simon Glass a527909208 malloc: Skip backtrace when stack is corrupted
When the stack is corrupted (e.g., by the stack protector test),
collecting a backtrace during malloc causes a crash because the
backtrace code walks the invalid stack frames.

Update __stack_chk_fail() to set the flag before calling panic()

Also update stackprot_test() to set the flag before intentionally
corrupting the stack. This is needed because of the printf() in the
test: on sandbox printf() results in truetype allocations due to the
console output.

These fixes allow the stack protector test to pass with mcheck enabled.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-12-10 05:53:03 -07:00

31 lines
640 B
C

// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021 Broadcom
*/
#include <asm/global_data.h>
#include <malloc.h>
DECLARE_GLOBAL_DATA_PTR;
unsigned long __stack_chk_guard = (unsigned long)(0xfeedf00ddeadbeef & ~0UL);
void __stack_chk_fail(void)
{
void *ra;
/*
* When the stack is corrupted, backtrace collection will crash.
* Skip it before calling panic().
*/
malloc_backtrace_skip(true);
ra = __builtin_extract_return_addr(__builtin_return_address(0));
panic("Stack smashing detected in function:\n%p relocated from %p",
ra, ra - gd->reloc_off);
}
void __stack_chk_fail_local(void)
{
__stack_chk_fail();
}