without qshim                 with qshim                          replay (no driver)

 application                   application                         application
     | open("/dev/fakesensor")     | open("/dev/fakesensor")           | open("/dev/fakesensor")
     v                             v                                   v
 driver                        qshim -- faults: errno, delay,      qshim -R trace.jsonl
                                 |      short, eof, corrupt          answers from the recording
                                 |   -- trace.jsonl
                                 v
                               driver (unchanged, still running)

Inspiration

Code that talks to a device has error paths: a short read, an EAGAIN, a read that takes a second too long. Real devices rarely fail on demand, so those paths are rarely run before they matter. On QNX a driver is a resource manager, an ordinary process that owns a pathname, and the pathname space lets a second process attach in front of the first. That is enough to build a test tool that needs no change to the application and no change to the driver.

A 60-second example

thermostat polls a sensor and switches a heater. The scenario file has one line:

on read nth=3 do short 6

The same binary, first against the driver, then with qshim in front of the same running driver (output copied from scripts/demo.sh on the QNX target):

$ ./thermostat -n 5                     $ ./qshim -p /dev/fakesensor -s short-read.qsc &
cycle 3: temp=22 heater=OFF             qshim: attached before /dev/fakesensor
done: heater OFF                        $ ./thermostat -n 5
                                        cycle 3: temp=2 heater=ON  ** FROST ALARM **
                                        done: heater OFF

The third read returned 6 of the 8 bytes of temp=22\n, and the application took temp=2 as a full sample. The fixed version, under the same scenario, prints cycle 3: bad sample (6 bytes) ignored, heater stays ON. Two more one-line scenarios expose an early exit on a single EAGAIN and a control loop stalled by one slow read. Then the whole session is replayed from the trace with the driver stopped.

What it does

qshim attaches to the pathname of a running resource manager (for example /dev/fakesensor) with _RESMGR_FLAG_BEFORE. New open() calls of unmodified applications resolve to qshim, and qshim forwards every request to the real driver, which keeps running. When qshim exits, new opens reach the driver again.

  • Fault injection. A scenario file is a list of one-line rules, such as on read nth=3 do short 6 or on read nth=4 do delay 1500. Actions: an errno, a delay, a short read, end of file, corrupted or replaced data. Rules match by operation, devctl command, client pid, n-th / every n-th / after n / probability with a fixed seed, and a fire count.
  • Record. Every request and its answer is written to a JSON Lines trace.
  • Replay. qshim -R trace.jsonl serves the pathname from the trace alone, with the driver stopped. Recorded faults are part of the recording, so a bug found once can be run again on a machine with no device.
  • Control device. /dev/qshim/<name> reports counters and accepts pause, resume, clear, and new rules while the application runs.
  • Security policy. qshim -P prints the secpol rules a process type needs to interpose on a path. Without the allow_attach rule the kernel refuses, and qshim exits with status 2 and names the missing rule.
  • Host tools (Python, standard library only): a scenario lint, and a trace report that matches each injected fault to the scenario line that caused it.

Who it is for

  • Application teams that share a few hardware rigs. The error paths of a client can be tested on the QNX image they already run, before the rig is free, and the same scenario file goes into CI.
  • Bug reports that cannot be reproduced. A trace recorded at the moment of the failure can be replayed on a developer's machine without the device.
  • Integrators who cannot rebuild the driver. A BSP driver or a vendor binary stays as it is; qshim needs only the path and a policy rule.
  • Reviewers of a safety argument. "What does the client do on a short read?" becomes a test with a one-line scenario, not a claim.

Why this is a QNX tool

On Linux the usual ways to fake a device are LD_PRELOAD (does not reach statically linked or setuid programs, and changes the client's process), a FUSE or CUSE device (a new path, so the client must be pointed at it), or mocks in the source (a rebuild). On QNX open(), read(), write() and devctl() are messages to whichever process owns the path, and _RESMGR_FLAG_BEFORE puts a second owner in front of the first. qshim is therefore one more process: no kernel module, no preload library, no change to the client's binary or command line. The security policy decides who may do it, and qshim treats that as part of the tool rather than something to switch off.

How we built it

  • Target: QNX SDP 8.0, QNX 8.0.0 x86_64 under QEMU (mkqnximage --type=qemu --arch=x86_64). Toolchain: qcc -Vgcc_ntox86_64. Host: WSL2 Ubuntu on Windows 11.
  • About 2,000 lines of C for the target, plus the Python host tools.

Where QNX is used (files in the repository; the README has a table with file:line for every interface):

  • src/main.c: dispatch_create(), resmgr_attach() with _RESMGR_FLAG_BEFORE, and a thread_pool so that a delayed request does not hold up other clients.
  • src/backend.c: ConnectServerInfo() and /proc/mount to find the driver's channel before qshim attaches; then, for each client open, ConnectAttach_r() and an _IO_CONNECT message sent with MsgSend_r(), so that qshim holds its own connection to the driver per open.
  • src/passthru.c: iofunc handlers that forward read, write, devctl, stat and lseek with resmgr_msgread() and MsgSendv_r(); devctl payloads are forwarded by direction (DEVDIR_TO / DEVDIR_FROM) without knowing their type.
  • src/fault.c and src/passthru.c: unblock handling with MsgInfo() and _NTO_MI_UNBLOCK_REQ. A client that is interrupted while qshim delays its request is answered with EINTR, and an unblock during a driver call is passed on to the driver.
  • src/control.c: a second resource manager for the control device.
  • policy/qshim.example.txt and test/secpol/: the security policy fragment and the test that the kernel refuses qshim without it.

Challenges we ran into

  • A proxy that forwards messages on one shared connection breaks per-open state in the driver (offsets, modes). qshim opens the driver once per client open by sending the connect message itself.
  • A delay injected by qshim must not make the client unkillable, so the delay has to cooperate with the kernel's unblock request.
  • A refusal by the security policy arrives as EACCES, while a missing pathspace ability arrives as EPERM. qshim reports both with a hint.
  • No hardware: everything was built and tested on an emulated target, so the tests assert delays only as lower bounds and make no latency claims.

Accomplishments that we're proud of

  • The application and the driver in the demo are the same binaries with and without qshim.
  • Checked on the target under emulation: 123 target checks, 20 security policy checks, 17 demo checks, and 88 host unit tests.
  • A seven-step codelab (codelab/index.md) that builds a transparent _RESMGR_FLAG_BEFORE proxy from an empty file, with the code of every step in the repository and a script that builds and runs each step on the target (32 checks).

Try it

bash scripts/wsl.sh scripts/vm-build.sh --clean   # once: QNX 8.0 x86_64 image for QEMU
bash scripts/vm-up.sh                             # start the VM, wait for ssh
bash scripts/test.sh                              # build with qcc -Vgcc_ntox86_64, run the 123 target checks
bash scripts/demo.sh                              # the three bugs, the fixed client, the replay

These are the Git Bash commands on Windows 11 + WSL2, where every result came from. The README has the Linux shell variant, and the expected output (docs/demo-output.txt).

What we learned

The resource manager model makes interposition a matter of pathname resolution order. No kernel module and no preload library is involved; the tool is one more process, and the security policy decides whether it may attach.

What's next

  • Tests on hardware, on aarch64, and against a BSP driver. The target ran under emulation (QEMU without KVM), so the times shown in the video are not latency measurements.
  • Forward select/poll notification and requests above 64 KiB; keep reads above 256 bytes whole in the trace (they are truncated today).
  • A fully secure image (--secpol=secure), and clients that run as non-root or under restricted process types.

Built With

Share this project:

Updates

Submission history