casaroli opened a new pull request, #20954:
URL: https://github.com/apache/nuttx/pull/20954

   ## Summary
   
   `nxtask_setup_fork()` calls `group_allocate()` before it copies the address 
environment of the parent. With `CONFIG_MM_KERNEL_HEAP`, `group_allocate()` 
puts the `task_info` of the new group in the current user heap, which is the 
heap of the parent. The copy gives the child the same block at the same 
address, but the parent never freed its own block: each `fork()` lost 1120 
bytes of the parent's heap on `qemu-armv8a:knsh`.
   
   This PR frees the parent's block in `nxtask_start_fork()`, after the 
parent's address environment is back. An error before this point still frees it 
through `nxsched_release_tcb()`, as before.
   
   ## Impact
   
   Only kernel builds with `CONFIG_ARCH_HAVE_FORK` (the code is in `#if 
defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_HAVE_FORK)`). `vfork()` and 
`posix_spawn()` do not change. Flat and protected builds do not compile this 
code.
   
   ## Testing
   
   Test program `forkleak.c` (`forkleak 100 fork`) on `qemu-armv8a:knsh` (QEMU 
with HVF, `CONFIG_SCHED_CHILD_STATUS=y`): the parent starts 100 children one 
after the other, each child exits at once, and the parent waits for it. The 
program prints the used bytes of its own heap (`mallinfo()`) before and after.
   
   | | master | this PR |
   |---|---|---|
   | parent heap after 100 x `fork()` | +112000 bytes (1120 per `fork()`) | +0 |
   | ostest, `qemu-armv8a:knsh` and `qemu-armv8a:nsh` | status 0 | status 0 |
   
   Build, each OK: `qemu-armv8a:nsh` (flat, arm64), `qemu-armv8a:knsh` (kernel, 
arm64), `lm3s6965-ek:qemu-protected` (protected, armv7-m), `qemu-armv7a:knsh` 
(kernel, armv7-a), `rv-virt:nsh64` (flat, rv64), `rv-virt:knsh64` (kernel, 
rv64). No new warnings.
   
   `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]

Reply via email to