casaroli opened a new pull request, #21331:
URL: https://github.com/apache/nuttx/pull/21331
## Summary
In a protected or kernel build, the heap code is also built for user space:
the user image (protected) and the programs (kernel) link their own copy of
`libmm`. Some heap debug code in it calls scheduler functions that only the
kernel has:
- `mm_memdump()` calls `nxsched_get_tcb()` for a task dump
(`mm/mm_heap/mm_memdump.c`, `mm/tlsf/mm_tlsf.c`).
- `MM_DUMP_LEAK()` in `include/nuttx/mm/mm.h` calls `nxsched_get_tcb()` when
`CONFIG_MM_BACKTRACE >= 0`. `mallinfo_task()` and `mm_memdump()` use it.
- `MM_ADD_BACKTRACE()` and the TLSF `memdump_backtrace()` call
`nxsched_get_tcb()` when `CONFIG_MM_BACKTRACE > 0`.
- `mm_malloc()` calls `nxsched_foreach()` on a failure with
`CONFIG_MM_DUMP_ON_FAILURE` and `CONFIG_MM_BACKTRACE >= 0`.
So mps2-an500:knsh does not link with `CONFIG_MM_BACKTRACE=0`, with
`CONFIG_MM_DUMP_DETAILS_ON_FAILURE`, or with the TLSF heap and no other option
(TLSF has `mm_memdump()` in the same object as `mm_malloc()`):
```
mm/mm_heap/mm_memdump.c:271:(.text.mm_memdump+0x68): undefined reference to
`nxsched_get_tcb'
```
In the kernel build, the programs get the same undefined symbols. On
qemu-armv8a:knsh, `init` then does not start.
The two commits:
- **mm/mm_heap: Fix the nxstyle errors.** Older errors in `mm_malloc.c` and
`mm_memdump.c` (blank lines and one indentation only).
- **mm: Do not call scheduler functions in the user-space heap.** In user
space, `MM_DUMP_LEAK()` uses `kill(pid, 0)`, which does the same check as
`nxsched_get_tcb()`. The other places do not read the TCB in user space: the
task dump prints no task name, the backtrace follows only the heap flag, and
the failure dump prints only the leak line.
## Impact
- Protected and kernel builds with these options: the user image and the
programs link, and the leak dump works in user space. The kernel copy of the
heap does not change.
- Flat builds: no change (the preprocessor gives the same code). Not a
breaking change.
- All architectures: the change is in common code. It uses only the
preprocessor and `kill()`.
- No documentation change: Documentation/ does not describe these debug
dumps.
## Testing
Base: master 5c441693fa8. "master" in the results is this base.
Builds ("dump" = `CONFIG_DEBUG_FEATURES`, `CONFIG_DEBUG_MM`,
`CONFIG_MM_DUMP_ON_FAILURE`, `CONFIG_MM_DUMP_DETAILS_ON_FAILURE`; "bt N" =
`CONFIG_MM_BACKTRACE=N`, bt 8 with `CONFIG_SCHED_BACKTRACE`):
| Configuration | master | this PR |
| --- | --- | --- |
| mps2-an500:knsh (protected), dump | link fails, 1 undefined reference |
builds |
| mps2-an500:knsh, bt 0 | link fails, 3 undefined references | builds |
| mps2-an500:knsh, `DEBUG_MM`, `MM_DUMP_ON_FAILURE`, bt 0 | link fails, 4
undefined references | builds |
| mps2-an500:knsh, bt 8 | link fails, 5 undefined references | builds |
| mps2-an500:knsh, TLSF; TLSF + bt 0; TLSF + bt 8; TLSF + dump | link fails
| builds |
| qemu-armv8a:knsh (kernel), dump + bt 0 | programs have undefined
`nxsched_get_tcb` and `nxsched_foreach`, `init` does not start | programs link
and run |
| rv-virt:knsh64 (kernel), dump + bt 8 | builds | builds |
| qemu-armv8a:nsh (flat): dump + bt 0; dump + bt 8; TLSF + dump + bt 0 |
builds | builds |
| sim:nsh (flat, host clang), dump + bt 0 | builds | builds |
A test program lets a thread allocate 4000 bytes and exit without `free()`.
Then `mallinfo_task()` with `PID_MM_LEAK` must count the block,
`mallinfo_task()` with its own thread ID must count its own block, and
`malloc()` of 3 GiB must fail (this runs the failure dump).
- qemu-armv8a:knsh, dump + bt 0: the test passes (leak 0 -> 1 block, 4024
bytes). The failure dump in the program lists the block of the dead thread
under "Memdump leak". ostest exits with status 0 on qemu-armv8a:knsh and
qemu-armv8a:nsh. On master, `init` does not start.
- qemu-armv8a:knsh, TLSF + bt 0 (with the PR "mm/tlsf: Build mm_lock_irq()
only where the delay list uses it."): the test passes (leak 0 -> 1 block, 4016
bytes); ostest exits with status 0 on qemu-armv8a:knsh and qemu-armv8a:nsh.
- mps2-an500:knsh, dump: the test passes and the user image prints the
failure dump. On master the image does not link.
mps2-an500:knsh with bt 0 or with TLSF now links, but `nsh_main` does not
start: with bt 0 the boot stops in `task_spawn()`, with TLSF the assertion in
`sem_wait.c:141` fails. Master with stub functions for `nxsched_get_tcb()` and
`nxsched_foreach()` in the user image does the same, so this is a separate
problem.
`tools/checkpatch.sh -c -u -m -g` passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]