nuttx/sched/timer
Marco Casaroli 43694933ce libc, sched: Resolve FDPIC descriptors at module callback entry points.
The base firmware and an FDPIC module disagree about what a function
pointer is.  Firmware is not built FDPIC, so to it a pointer is a code
address and it branches there.  A module passes the address of a two word
descriptor instead, because its code and data are placed independently and
a bare code address would leave the callee unable to find its own data.  A
firmware routine that takes a callback therefore branches into the
module's data segment and faults.

So the ten entry points that can be handed a callback by a module resolve
the descriptor before storing or branching to it: qsort, bsearch,
pthread_create, signal, sigaction, task_create and task_create_with_stack,
task_spawn, pthread_once, scandir, and mq_notify and timer_create with
SIGEV_THREAD.

Which one resolves matters as much as that one does.  Resolving twice would
take an already resolved code address for a descriptor and read two words
from the instruction stream, so each pointer is resolved exactly once, at
the outermost point that sees it.  signal() passes its argument through
untouched because sigaction() and then nxsig_action() will resolve it,
which covers a module calling sigaction() directly as well.  qsort() is
split so that the public entry resolves and the recursive implementation
does not.  scandir() resolves its filter but not its comparison function,
which it hands to qsort().

Whether a caller is a module at all is asked of the PIC base register,
which up_initial_state() sets only for a task that has a D-Space.  A plain
kernel task therefore reads zero and is left alone.

SIGEV_THREAD is the case the register cannot answer, because the callback
runs later on a work queue worker that carries no module's base at all.
The base is captured instead when the notification is registered, in the
module's own context, and installed around the call.

All of it is behind CONFIG_FDPIC, which defaults off.  Built for
mps3-an547:picostest both ways; with it off the entry points compile to
what they were.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-09-25 10:46:48 -03:00
..
CMakeLists.txt sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
Make.defs sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
timer.h sched/timer: Fix MISRA Rule 10.3 2026-01-22 22:14:00 +08:00
timer_create.c libc, sched: Resolve FDPIC descriptors at module callback entry points. 2026-09-25 10:46:48 -03:00
timer_delete.c sched/timer: Fix timer multiple return. 2026-01-22 22:14:00 +08:00
timer_getitimer.c sched/timer: Fix timer multiple return. 2026-01-22 22:14:00 +08:00
timer_getoverrun.c sched/timer: Fix timer multiple return. 2026-01-22 22:14:00 +08:00
timer_gettime.c !sys/types.h: change time_t and clock_t to int64_t to align with other OSes 2026-05-19 16:21:28 +08:00
timer_initialize.c sched/timer: remove critical section in setitimer 2025-01-19 17:08:32 +08:00
timer_release.c sched/timer: Fix MISRA Rule 10.4 2026-01-22 22:14:00 +08:00
timer_setitimer.c sched/timer: Fix timer multiple return. 2026-01-22 22:14:00 +08:00
timer_settime.c sched/timer: Fix MISRA Rule 10.3 2026-01-22 22:14:00 +08:00