xhh233 opened a new issue, #22172:
URL: https://github.com/apache/nuttx/issues/22172
# Case: pm_runtime_init() leaves suspend_work uninitialized (host-modeled
consequence)
Status: prepared, not submitted to upstream.
Scope: public NuttX sources only; no vendor identifiers, no private
addresses.
Evidence level: E1_src for this case (hosted; the unmodified upstream `.c` is
compiled before the patch, and the patched build applies `fix.patch` to a
throw-away copy of the same source); not a real board / `sim:nsh` run. The
reported crash is a **host SIGSEGV under a modeled work queue**, not an
observed
kernel work-list corruption.
## Summary
`pm_runtime_init()` initializes the mutex, use count, delay, state, and ops
of
a `struct pm_runtime_s` but never clears `rpm->suspend_work`. Since
`work_available(work)` is defined as `work->worker == NULL`, a `pm_runtime_s`
backed by uncleared heap or stack memory can retain a non-NULL `worker`. In
that case `work_queue()` skips the available fast path and calls
`work_remove(wqueue, work)` on a node that was never queued; `work_remove()`
then executes `list_delete(&work->node)`.
In the hosted model this results in a SIGSEGV while dereferencing an invalid
`node.next/prev`. No kernel work-list corruption and no real-target crash
were
observed.
## Preconditions (all three required)
1. **Residual non-NULL worker**: `pm_runtime_s` comes from uncleared
heap/stack
memory and `suspend_work.worker` is non-NULL, so `work_available()` is
false.
2. **Invalid link pointer**: the embedded `work_s.node.next/prev` are not a
valid queue membership (garbage or NULL), so `list_delete()` dereferences
an
invalid address.
3. **Autosuspend reached**: the driver actually enters
`pm_runtime_get()` -> `pm_runtime_put_autosuspend()` ->
`rpm_changestate(RPM_SUSPENDING)` ->
`work_queue(HPWORK, &rpm->suspend_work, ...)`.
Boundary statements kept from the local report:
- **No `pm_runtime_init()` caller in the pinned tree**: only the definition
(`drivers/power/pm/pm_runtime.c:185`) and declaration
(`include/nuttx/power/pm/pm_runtime.h:82`). Reachability depends on
external
or board-level drivers allocating/initializing `pm_runtime_s`.
- **Already-zeroed objects are unaffected**: BSS/static objects or objects
cleared by the caller have `worker == NULL`, so `work_available()` is true
and
the `work_remove()` path is not taken.
## Affected code
Pinned commit `2b5c0de5e933281c209f954a63b99cf10c132346`:
- `drivers/power/pm/pm_runtime.c:185-195`: `pm_runtime_init()` sets `lock`,
`use_count`, `suspend_delay`, `state`, `ops` and leaves `suspend_work`
untouched.
- `include/nuttx/wqueue.h:531`: `work_available()` is
`(work)->worker == NULL`.
- `sched/wqueue/kwork_queue.c:177-187`: under the queue lock,
`retimer = work_available(work) ? false : work_remove(wqueue, work);`
followed by writing `worker/arg/qtime`.
- `sched/wqueue/wqueue.h:228-243`: `work_remove()` clears `work->worker` and
calls `list_delete(&work->node)`.
The last two files are copied for cross-checking under `upstream-sched/`
(see `upstream-sched/NOTES.md`), with SHA-256 values recorded there. They are
reference material and are not compiled by `build.sh`.
## Reproduction (host)
The reproducer fills a `pm_runtime_s` with `0xa5` to emulate uncleared
memory,
runs the real `pm_runtime_init()`, then calls the real `pm_runtime_get()` and
`pm_runtime_put_autosuspend()`. The shim reproduces the upstream
`work_available ? ... : work_remove` decision and the upstream
`list_delete()`
macro is used unchanged.
```
worker_initialized_after_init=0 work_available_after_init=0
remove_on_unqueued=1 list_delete_sigsegv=1 work_queued=0
```
After zeroing `suspend_work` in init:
```
worker_initialized_after_init=1 work_available_after_init=1
remove_on_unqueued=0 list_delete_sigsegv=0 work_queued=1
```
The SIGSEGV is captured with a handler only to make the host output
deterministic; it is a modeled result, not an observed kernel work-list
failure.
## Suggested fix
Zero `rpm->suspend_work` in `pm_runtime_init()` (for example with `memset`)
so
that the first `work_queue()` sees an available work item regardless of the
backing memory. This only covers paths that go through `pm_runtime_init()`.
## Novelty
As of 2026-11-23, local searches found no corresponding public report (search
expressions and request counts are recorded in `../NOVELTY.md`). This has not
been confirmed by upstream. Public NuttX documentation already requires the
caller to zero-initialize `work_s`; the local report is about
`pm_runtime_init()` not satisfying that initialization contract.
## Evidence files
`README.md`, `ISSUE-DRAFT-EN.md`, `before.log`, `after.log`, `expected.txt`,
`fix.patch`, `driver.c`, `build.sh`, `run.sh`, `validate.sh`, `src/`,
`shim/`,
`upstream-sched/`, `PROVENANCE.md`, `SRC-SHA256SUMS`, `SHA256SUMS`.
<details><summary>Host reproducer: driver.c + build.sh + logs</summary>
```c
/* SPDX-License-Identifier: Apache-2.0
*
* PM-09 hosted driver: exercises the UNMODIFIED upstream
* drivers/power/pm/pm_runtime.c against a minimal kernel shim.
*
* Scenario:
* 1. Fill a struct pm_runtime_s with 0xa5 (simulated un-zeroed
heap/stack).
* 2. Call the real pm_runtime_init().
* 3. Drive the real pm_runtime_get() + pm_runtime_put_autosuspend() path,
* which reaches rpm_changestate(RPM_SUSPENDING) -> work_queue(HPWORK,
* &rpm->suspend_work, ...).
* 4. Record whether suspend_work.worker is NULL (work_available) and
* whether the shim work queue had to work_remove() a never-queued node.
*
* Upstream (unpatched) behaviour: pm_runtime_init() never clears
* suspend_work, so the garbage worker makes work_available() false; the
* kernel then takes the work_remove() path on a node that was never queued
* and list_delete() dereferences the garbage next/prev links.
*/
#include <nuttx/config.h>
#include <nuttx/power/pm_runtime.h>
#include <nuttx/wqueue.h>
#include <setjmp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "shim_pm09.h"
#define SHIM_GARBAGE UINT64_C(0xa5a5a5a5a5a5a5a5)
#define SHIM_PATTERN 0xa5
static sigjmp_buf g_jb;
static volatile sig_atomic_t g_segv;
static void on_segv(int signo)
{
(void)signo;
g_segv = 1;
siglongjmp(g_jb, 1);
}
static int op_suspend(FAR struct pm_runtime_s *rpm)
{
(void)rpm;
return 0;
}
static int op_resume(FAR struct pm_runtime_s *rpm)
{
(void)rpm;
return 0;
}
int main(void)
{
struct pm_runtime_s rpm;
struct pm_runtime_ops_s ops;
struct sigaction sa;
int worker_cleared;
int avail;
int worker_garbage;
int next_garbage;
memset(&ops, 0, sizeof(ops));
ops.runtime_suspend = op_suspend;
ops.runtime_resume = op_resume;
/* Simulate a pm_runtime_s carved from un-zeroed memory. */
memset(&rpm, SHIM_PATTERN, sizeof(rpm));
memset(&sa, 0, sizeof(sa));
sa.sa_handler = on_segv;
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
pm_runtime_init(&rpm, RPM_ACTIVE, &ops);
worker_cleared = (rpm.suspend_work.worker == NULL);
avail = work_available(&rpm.suspend_work);
printf("case=PM-09\n");
printf("upstream_object=src/drivers/power/pm/pm_runtime.c\n");
printf("init_mem_pattern=0x%02x\n", SHIM_PATTERN);
printf("worker_initialized_after_init=%d\n", worker_cleared);
printf("work_available_after_init=%d\n", avail);
printf("trigger=pm_runtime_get+pm_runtime_put_autosuspend\n");
if (sigsetjmp(g_jb, 1) == 0)
{
(void)pm_runtime_get(&rpm);
(void)pm_runtime_put_autosuspend(&rpm);
}
worker_garbage = (g_shim_worker_before_remove == (uintptr_t)SHIM_GARBAGE);
next_garbage = (g_shim_node_next_before_remove ==
(uintptr_t)SHIM_GARBAGE);
printf("work_queue_calls=%d\n", g_shim_work_queue_calls);
printf("remove_on_unqueued=%d\n", g_shim_remove_on_unqueued);
printf("worker_before_remove_is_pattern=%d\n", worker_garbage);
printf("node_next_before_remove_is_pattern=%d\n", next_garbage);
printf("list_delete_sigsegv=%d\n", (int)g_segv);
printf("work_queued=%d\n", g_shim_queued_count);
printf("verdict=%s\n",
(worker_cleared && avail && !g_shim_remove_on_unqueued &&
!g_segv && g_shim_queued_count == 1)
? "SUSPEND_WORK_INITIALIZED"
: "SUSPEND_WORK_UNINITIALIZED");
return 0;
}
```
```sh
#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
#
# Build the PM-09 reproducer. The single compile command below takes the
# UNMODIFIED upstream file src/drivers/power/pm/pm_runtime.c from this case's
# byte-identical source copy; shim/ only satisfies the kernel API symbols it
# references. Nothing in src/ is edited.
set -eu
cd "$(CDPATH= cd -- "$(dirname "$0")" && pwd)"
CC=${CC:-cc}
$CC -O0 -g -Wall -Wextra -Werror -Wno-unused-parameter \
-Ishim -Ishim/include -Isrc/include \
src/drivers/power/pm/pm_runtime.c shim/kernel_shim.c driver.c \
-o repro
echo "built: repro (upstream src/drivers/power/pm/pm_runtime.c)"
```
```text
# before.log
case=PM-09
upstream_object=src/drivers/power/pm/pm_runtime.c
init_mem_pattern=0xa5
worker_initialized_after_init=0
work_available_after_init=0
trigger=pm_runtime_get+pm_runtime_put_autosuspend
work_queue_calls=1
remove_on_unqueued=1
worker_before_remove_is_pattern=1
node_next_before_remove_is_pattern=1
list_delete_sigsegv=1
work_queued=0
verdict=SUSPEND_WORK_UNINITIALIZED
# after.log
case=PM-09
upstream_object=src/drivers/power/pm/pm_runtime.c
init_mem_pattern=0xa5
worker_initialized_after_init=1
work_available_after_init=1
trigger=pm_runtime_get+pm_runtime_put_autosuspend
work_queue_calls=1
remove_on_unqueued=0
worker_before_remove_is_pattern=0
node_next_before_remove_is_pattern=0
list_delete_sigsegv=0
work_queued=1
verdict=SUSPEND_WORK_INITIALIZED
```
</details>
The reproducer compiles the unmodified upstream C file from a pinned
snapshot (see PROVENANCE.md in the attached case); the shim only provides
kernel API stubs. Full bundle (source, shim, logs, patch, sha256) available on
request.
--
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]