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

   ## Summary
   
   `riscv_fork()` of a build with system calls read the thread pointer of the 
child (`REG_TP` of its initial context) after it copied the used part of the 
parent stack to the child stack. Without a kernel stack 
(`CONFIG_ARCH_KERNEL_STACK=n`, a protected build such as `rv-virt:pnsh64`), 
`up_initial_state()` puts the initial context at the top of the child stack, 
and that copy overwrites it when the parent uses more stack than one register 
context. The child then starts with a word of the parent stack as its thread 
pointer, and its `__thread` variables are at a wrong address.
   
   This PR reads the thread pointer before the copy. The code stays under 
`CONFIG_SCHED_THREAD_LOCAL`.
   
   ## Impact
   
   RISC-V protected builds with `CONFIG_SCHED_THREAD_LOCAL`: a `vfork()` child 
gets its own thread pointer. Kernel builds always have a kernel stack on RISC-V 
(`riscv_exception_common.S` stops the build without one), so the initial 
context is on the kernel stack and the behaviour does not change. Flat builds 
use the other `riscv_fork()` (no system call path) and do not change. arm64 and 
x86_64 do not read the thread pointer from the child stack: arm64 sets 
`TPIDR_EL0` from `child->stack_alloc_ptr`, x86_64 builds the child frame from 
its initial frame. Not a breaking change.
   
   ## Testing
   
   Built in the CI image (`-Werror`) on Linux, run under QEMU 10.2.2 (TCG, 
`-smp 1`).
   
   `rv-virt:pnsh64` with `CONFIG_SCHED_THREAD_LOCAL=y`. On master this 
configuration does not link: `ld-protected.script` has no `.tdata`/`.tbss`, so 
`riscv_tls.c` has undefined `_stdata`, `_etdata`, `_stbss` and `_etbss`. For 
this test only, the two sections were added to the script in the build tree, as 
`ld.script` has them. A test program calls `vfork()` under a 1 KiB stack frame. 
The child compares its thread pointer with `TLS_INFO(sp) + sizeof(struct 
tls_info_s)` (`CONFIG_TLS_ALIGNED`):
   
   | | master | this PR |
   |---|---|---|
   | child thread pointer | `0xa5a5a5a5a5a5a5a5` (the fill of the parent 
frame), expected `0x8020a030`: FAIL | `0x8020a030`: PASS |
   | ostest | exits with status 0 | exits with status 0 |
   
   `rv-virt:knsh64`: ostest exits with status 0 (vfork and fork tests pass). 
Build only: `rv-virt:nsh64` (flat), `rv-virt:pnsh` (32-bit, protected, 
`CONFIG_SCHED_THREAD_LOCAL`, with the same test-only script change).
   
   `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