mcheck: Add Kconfig option for caller string length

The mcheck heap protection stores a caller string in each allocation
header for debugging purposes. The length is hard-coded to 48 bytes in
mcheck_core.inc.h

Add a CONFIG_MCHECK_CALLER_LEN Kconfig option to make this configurable,
allowing users to adjust the trade-off between the amount of debugging
context and the memory overhead per allocation.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-31 08:31:54 -07:00
parent 8ff7005997
commit 6f810962d1
2 changed files with 13 additions and 3 deletions

11
Kconfig
View File

@@ -355,6 +355,17 @@ config MCHECK_HEAP_PROTECTION
significantly increases memory overhead and should only be used for
debugging.
config MCHECK_CALLER_LEN
int "Length of caller string in mcheck header"
depends on MCHECK_HEAP_PROTECTION
default 48
help
Sets the maximum length of the caller string stored in each
allocation header. This string records the function/file/line
that allocated the memory, useful for debugging memory leaks.
Larger values provide more context but increase memory overhead
per allocation.
config SPL_SYS_MALLOC_F
bool "Enable malloc() pool in SPL"
depends on SPL_FRAMEWORK && SYS_MALLOC_F && SPL

View File

@@ -73,7 +73,6 @@
// Full test suite can exceed 10000 concurrent allocations
#define REGISTRY_SZ 12000
#define CANARY_DEPTH 2
#define MCHECK_CALLER_LEN 48
// avoid problems with BSS at early stage:
static char mcheck_pedantic_flag __section(".data") = 0;
@@ -89,7 +88,7 @@ struct mcheck_hdr {
size_t size; /* Exact size requested by user. */
size_t aln_skip; /* Ignored bytes, before the mcheck_hdr, to fulfill alignment */
mcheck_canary canary; /* Magic number to check header integrity. */
char caller[MCHECK_CALLER_LEN]; /* caller info for debugging */
char caller[CONFIG_MCHECK_CALLER_LEN]; /* caller info for debugging */
};
static void mcheck_default_abort(enum mcheck_status status, const void *p)
@@ -212,7 +211,7 @@ static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz,
for (i = 0; i < CANARY_DEPTH; ++i)
hdr->canary.elems[i] = MAGICWORD;
if (caller)
strlcpy(hdr->caller, caller, MCHECK_CALLER_LEN);
strlcpy(hdr->caller, caller, CONFIG_MCHECK_CALLER_LEN);
else
hdr->caller[0] = '\0';