5 Commits

Author SHA1 Message Date
Simon Glass
1390d243bc malloc: Add function to dump the malloc()-traffic log
Add malloc_log_dump() to print all recorded malloc/free/realloc calls
with their addresses, sizes, and caller information. This provides a
way to inspect the log after recording.

The dump shows a summary line with entry counts, followed by a table
with sequence number, operation type, pointer, size, and caller for
each recorded operation.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2026-01-03 12:41:30 -07:00
Simon Glass
1d0df266dc malloc: Add a log for malloc() traffic
Add a malloc()-traffic log that records all malloc()-related calls with
their addresses, sizes, and caller information. This is useful for
debugging allocation patterns and finding the source of allocations that
lack caller info in heap dumps.

Each entry stores:
- Operation type (alloc/free/realloc/memalign)
- Pointer address
- Size (and old size for realloc)
- Full caller backtrace string

On sandbox, the log buffer is allocated from host memory using
os_malloc(), so it does not affect U-Boot's heap. The size is
controlled by CONFIG_MCHECK_LOG_SIZE (default 512K entries).

If the log fills up, it wraps around (circular buffer) and a warning
is shown when dumping to indicate how many entries were lost.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2026-01-03 12:40:44 -07:00
Simon Glass
bbea921ca7 test: malloc: Account for mcheck overhead in the large test
The malloc_very_large() test fails when mcheck is enabled with large
CONFIG_MCHECK_CALLER_LEN because the 64K margin does not account for
the per-allocation overhead (header + canaries).

Use a larger margin (256K) when mcheck is enabled to ensure the test
passes regardless of the mcheck caller length setting.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2026-01-03 12:39:14 -07:00
Simon Glass
95f10a6302 malloc: Make mcheck respect REALLOC_ZERO_BYTES_FREES
The mcheck wrapper for realloc() unconditionally frees memory and
returns NULL when size is 0. This differs from dlmalloc's default
behaviour which returns a minimum-sized allocation unless
REALLOC_ZERO_BYTES_FREES is defined.

Make the mcheck wrapper respect the same REALLOC_ZERO_BYTES_FREES
setting for consistent behavior with or without mcheck enabled.

Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-12-10 05:53:03 -07:00
Simon Glass
13197ab962 test: Add some tests for dlmalloc
We know or assume that dlmalloc itself works correctly, but there is
still the possibility that the U-Boot integration has bugs.

Add a test suite for the malloc() implementation, covering:
- Basic malloc/free operations
- Edge cases (zero size, NULL pointer handling)
- realloc() in various scenarios
- memalign() with different alignments
- Multiple allocations and fragmentation
- malloc_enable_testing() failure simulation
- Large allocations (1MB, 16MB)
- Full pool allocation (CONFIG_SYS_MALLOC_LEN plus environment size)
- Fill pool test with random sizes

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-12-01 17:00:23 +00:00