elidot· relmw-qnx
QNX OS 8.0 Codelab · 6 Steps 0 / 6 Verified
BlackBerry QNX Global Hackathon · Robotics & Medical + Best Codelab / Tutorial · Apache-2.0

Build a 1 kHz Zero-Allocation Simplex Safety Supervisor on QNX OS 8.0

A complete 6-step hands-on engineering tutorial covering QNX OS 8.0 MsgReceivePulse real-time control loops, resmgr_attach device virtualisation, HAM dual-layer self-healing, slog2 structured telemetry, least-privilege secpol hardening, and an O(1) Supporting-Hyperplane Control Barrier Function (CBF) safety shield.

Engineering Honesty Boundary: Every benchmark metric and command output in this Codelab comes from real compiled runs of relmw-qnx (-std=c++17 -O2 -Wall -Wextra -Werror, 40/40 CMake CTest · 39/39 direct suites PASS). compat/qnx_sim/ compiles and executes the actual #ifdef __QNX__ files on Linux/macOS to rehearse IPC and control flow; it is not a QNX kernel latency benchmark. Live QNX SDP 8.0 QEMU/hardware metrics are captured on target with scripts/run_qnx80_evidence_pack.sh.
Hot-Path Heap Allocs
0 / tick
Verified over 100k CBF + 20k MLP calls
Surgical Zero-Intrusion
100 / 100
0 intrusions vs 1,345 naive retry
MLP Field Recovery
93 / 100
+15.0 pp · McNemar p = 0.000275
Automated Test Gate
40 / 40
22 Host + 17 QNX_SIM · 0 warnings

All 6 QNX OS 8.0 Codelab Steps Verified

You have verified the full QNX 8.0 Simplex supervisor pipeline: pulse loop, 0-alloc CBF shield, /dev resource managers, HAM + slog2, ROS 2 telemetry, and secpol hardening.

Open Dual-Screen Replay Dashboard
Step 01 / 06 10 min In Progress

Workspace Setup, Dual-Target Build & Strict QNX 8.0 Syntax Gate

Separate portable C++17 safety math from QNX 8.0 microkernel syscalls, compile both POSIX and RELMW_QNX_SIM with -Werror, and prove all 12 QNX translation units pass clean-room 8.0 signature checks.

-D__QNX__ -fsyntax-only -DRELMW_QNX_SIM=ON scripts/build_direct.sh tests/test_rt_noalloc.cpp

1.1 Microkernel Two-Process Architecture

Rather than packing neural inference, physical safety filtering, device I/O, and operator CLI into one fragile process, relmw-qnx splits the cell across two isolated QNX OS 8.0 microkernel processes supervised by HAM and hardened by secpol:

relmw_operator_t Operator / HMI cat /dev/relmw/status echo "shield on" > ctl 128-B CRC-32 UDP ROS 2 / Replay /relmw/diagnostics docs/dashboard.html relmw_supervisor type: relmw_supervisor_t · uid 1001 RESMGR THREAD /dev/relmw/* resmgr_attach · io_read64 qnx_resmgr.cpp 1 kHz CONTROL THREAD MsgReceivePulse() · _cpu-1 TinyMLPPolicy (FP32 / Q12) SafetyShield::project() O(1) CBF Circle+Polygon · 0 alloc Correlation-Aware Retry PERSISTENT STATE Beta Failure Library atomic tmp + rename slog2 "relmw" (8x4 KB) SCHED_FIFO / SPORADIC explicit pulse prio 40 relmw_robotd relmw_robotd_t · /dev/robot0 _IO_MSG fixed 64-B / 88-B _NTO_CHF_INHERIT_RUNMASK Deterministic PushSim2D Survives kill -9 reconnect QNX HAM (ham_t) CONDDEATH -> restart HBEATMISSEDHIGH -> slay apps/relmw_ham_setup.cpp MsgSend MsgReply

1.2 Clone & Build Both Targets (build-host + build-sim)

bash · host + simZero-dependency C++17 build & 39-suite direct (40 CTest) run
git clone https://github.com/Qidianyan/relmw-qnx.git
cd relmw-qnx

# Builds POSIX host (build-host) + -D__QNX__ simulation (build-sim) with -Wall -Wextra -Werror
CXX=clang++ bash scripts/build_direct.sh all

1.3 Verify All 12 QNX 8.0 Translation Units & 9 Negative Stub Guards

scripts/qnx_syntax_check.sh compiles every QNX file with -D__QNX__ -fsyntax-only -Wall -Wextra -Werror against clean-room QNX SDP 8.0 headers (tools/qnx_stubs/include/), verifies no discontinued __QNXNTO__ macros exist, and confirms 9 deliberately malformed QNX calls fail compilation.

bash · static gatescripts/qnx_syntax_check.sh
CXX=g++ bash scripts/qnx_syntax_check.sh
# Expected tail:
#   OK    apps/relmw_robotd.cpp
#   OK    stub rejects: pthread_cluster_get_runmask() with unsigned*
#   qnx_syntax_check: all QNX translation units pass -fsyntax-only against 8.0 stubs

Verification Checkpoint 1

0 / 2 checked
Step 1 of 6
Step 02 / 06 10 min Not Started

Deterministic 1 kHz Kernel-Timer Pulse Loop & SCHED_SPORADIC

Wire ChannelCreate(_NTO_CHF_PRIVATE), MsgRegisterEvent, MsgReceivePulse, budgeted sporadic scheduling, and QNX 8.0 CPU cluster pinning without falling into the SIGEV_PULSE_PRIO_INHERIT trap.

ChannelCreate(_NTO_CHF_PRIVATE) MsgRegisterEvent MsgReceivePulse SCHED_SPORADIC pthread_cluster_set

2.1 Private Channel + Secure Kernel-Timer Pulse (src/platform/qnx/qnx_timer.cpp)

C++17 · QNX 8.0src/platform/qnx/qnx_timer.cpp
// Private channel: no external process can ConnectAttach() to inject spoofed ticks.
impl_->chid = ChannelCreate(_NTO_CHF_PRIVATE);
impl_->coid = ConnectAttach(0, 0, impl_->chid, _NTO_SIDE_CHANNEL, 0);

// Read thread priority AFTER set_thread_sched() — NEVER pass SIGEV_PULSE_PRIO_INHERIT!
struct sched_param cur; int cur_policy = SCHED_OTHER; int prio = 10;
if (pthread_getschedparam(pthread_self(), &cur_policy, &cur) == EOK)
  prio = cur.sched_priority;

struct sigevent ev;
SIGEV_PULSE_INIT(&ev, impl_->coid, prio, kPulseTick, 0);
MsgRegisterEvent(&ev, SYSMGR_COID);       // secure event: kernel/procnto only
timer_create(CLOCK_MONOTONIC, &ev, &impl_->timer);
nsec2timespec(&its.it_value, (uint64_t)period_ns);
nsec2timespec(&its.it_interval, (uint64_t)period_ns);
timer_settime(impl_->timer, 0, &its, nullptr);

// Hot loop (0 heap allocations):
struct _pulse p;
MsgReceivePulse(impl_->chid, &p, sizeof p, nullptr);
QNX 8.0 Priority Pitfall (SIGEV_PULSE_PRIO_INHERIT): In QNX SDP 8.0, SIGEV_PULSE_PRIO_INHERIT makes the receiving thread run at the process's initial priority (typically 10), silently demoting a SCHED_FIFO 40 control thread on every timer tick. Always configure scheduling first, then construct PeriodicTimer with the thread's explicit priority.
Interactive Lab · 1 kHz Timing, Jitter & SCHED_SPORADIC Verifier
Checking…
Source: Host Linux Measured (idle SCHED_OTHER 1 kHz baseline)
generated CLICopy to QNX 8.0 target

            

Verification Checkpoint 2

0 / 2 checked
Step 03 / 06 10 min Not Started

1 kHz Zero-Allocation Simplex Safety Shield (CBF) & TinyMLPPolicy

Wrap a 1,410-parameter neural manipulation policy (FP32 & FPU-free Q12) behind an O(1) Supporting-Hyperplane Control Barrier Function filter achieving 100/100 zero-intrusion episodes in surgical virtual fixtures.

SafetyShield::project() noexcept medical_surgical_fixture() TinyMLPPolicy (FP32 / Q12) SHIELD_INTERVENTION

3.1 Supporting-Hyperplane CBF Math (src/core/safety_shield.cpp)

Every 1 ms tick, SafetyShield::project(current_pos, desired_target) noexcept runs purely on stack scalars (verified 0 heap allocations over 100,000 iterations in test_rt_noalloc):

  1. Non-finite & Kinematic Guard: Rejects NaN/Inf (kShieldNonFinite) and clamps displacement max_step_delta and acceleration max_step_accel.
  2. 3-Pass Supporting-Hyperplane CBF: Projects p onto (p - c) · n ≥ (1 - α) r0 + α r_eff for every circular & CCW convex-polygonal fixture. Tangential velocity along n.perp() is preserved so the end-effector slides cleanly around sterile ampoules and surgical trays without deadlocking.
  3. Continuous-Chord Invariant + Pulse Hook: Re-checks segment_zone_clearance(current_pos, p) ≥ 0 and fires an allocation-free function pointer ShieldPulseHook.
Interactive Lab · Live SafetyShield::project() CBF Sandbox (medical_vial)
SAFE · 0.0 mm Intrusion

Click or drag anywhere in the cell to steer the unshielded neural target (crimson). Watch the 1 kHz CBF shield project it tangentially (forest green) outside both virtual fixtures.

Unshielded Clearance+0.0 mm
Projected Safe Clearance+0.0 mm
ShieldReasonBits Mask0x00 (NONE)
SHIELD_INTERVENTION Pulses0
Desired Target (mm)(0, 0)
Safe Target (mm)(0, 0)
Exact JavaScript port of SafetyShield::project() (src/core/safety_shield.cpp). Circle 0: (45, -185) mm, r=22 mm + 15 mm tool + 4 mm margin; Polygon 0: [10..70, 55..95] mm.

3.2 Run the Paired S+ Neural Benchmark (N = 100 Unseen Seeds)

bash · benchmarkTinyMLPPolicy + medical_vial + 1 kHz CBF shield
./build-host/relmw_bench --n 100 --warmup 40 --bias 0.12 \
  --policy mlp --preset medical_vial --shield \
  --export-replay results/host_splus/replay_showcase.bin \
  --out results/host_splus
Condition · Arm (N=100 paired) Task Success Zero-Intrusion Intrusions Shield Pulses Min Clearance McNemar p
Field · single_pass (no shield)77/1001.0%7620-41.91 mm—
Field · naive_retry (no shield)78/1000.0%1,345 (1.77x!)0-41.91 mm1.000
Field · corr_aware_retry (relmw-qnx)93/100 (+16 pp)100.0%06,662+3.00 mm0.000145

Verification Checkpoint 3

0 / 2 checked
Step 04 / 06 10 min Not Started

POSIX-Path Resource Managers (/dev/relmw & /dev/robot0)

Expose the supervisor as standard files via resmgr_attach() and isolate the robot driver behind synchronous _IO_MSG MsgSend/MsgReply with client priority & runmask inheritance.

resmgr_attach read64 / write64 resmgr_msgget _IO_MSG _NTO_CHF_INHERIT_RUNMASK

4.1 Multi-File Control Surface (src/platform/qnx/qnx_resmgr.cpp)

C++17 · QNX 8.0src/platform/qnx/qnx_resmgr.cpp
iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs, _RESMGR_IO_NFUNCS, &io_funcs);
io_funcs.read    = relmw_io_read;
io_funcs.read64  = relmw_io_read;    // QNX 8.0 routes 64-bit _IO_READ64 here
io_funcs.write   = relmw_io_write;
io_funcs.write64 = relmw_io_write;   // QNX 8.0 routes 64-bit _IO_WRITE64 here

resmgr_attach(dpp, &rattr, "/dev/relmw/status", _FTYPE_ANY, 0, &connect_funcs, &io_funcs, &attr_status);
resmgr_attach(dpp, &rattr, "/dev/relmw/events", _FTYPE_ANY, 0, &connect_funcs, &io_funcs, &attr_events);
resmgr_attach(dpp, &rattr, "/dev/relmw/ctl",    _FTYPE_ANY, 0, &connect_funcs, &io_funcs, &attr_ctl);

// In relmw_io_write(): prevent unsigned underflow & reject truncated IPC payloads
if (ctp->info.srcmsglen < (decltype(ctp->info.srcmsglen))(off + sizeof(io_write_t))) return EBADMSG;
if (nbytes > (size_t)ctp->info.srcmsglen - off - sizeof(io_write_t)) return EBADMSG;
resmgr_msgget(ctp, buf, n, sizeof(msg->i));

4.2 Process-Isolated Driver /dev/robot0 (src/platform/qnx/qnx_robot_link.cpp)

C++17 · QNX 8.0src/platform/qnx/qnx_robot_link.cpp
// Driver executes each 1 kHz step at the client's priority AND on the client's pinned CPU cluster:
int chid = ChannelCreate(_NTO_CHF_UNBLOCK | _NTO_CHF_DISCONNECT | _NTO_CHF_INHERIT_RUNMASK);
dispatch_t* dpp = dispatch_create_channel(chid, DISPATCH_FLAG_NOLOCK);

int robot_io_msg(resmgr_context_t* ctp, io_msg_t* msg, RESMGR_OCB_T* ocb) {
  if (ctp->rcvid == 0) return ENOTSUP;                  // refuse combine messages
  if (msg->i.mgrid != kQnxRobotMgrId) return ENOSYS;    // _IOMGR_PRIVATE_BASE + 0x52
  if (!(ocb->ioflag & _IO_FLAG_WR)) return EPERM;       // _IO_MSG bypasses iofunc_*_verify!
  resmgr_msgget(ctp, &req, sizeof req, 0);
  g_ctx.server->handle(req, sizeof req, rep);
  MsgReply(ctp->rcvid, EOK, &rep, sizeof rep);
  return _RESMGR_NOREPLY;
}

4.3 Hands-On Operator Workflow (RELMW_QNX_SIM=1 & Native QNX 8.0)

bash · QNX API simulationRehearse the real #ifdef __QNX__ resmgr path locally
export RELMW_QNX_SIM=1 QNXSIM_IO="$PWD/build-sim/qnxsim_io" QNXSIM_ROOT=/tmp/qnxsim-lab
rm -rf "$QNXSIM_ROOT" && mkdir -p "$QNXSIM_ROOT"

./build-sim/relmw_robotd --path /dev/robot0 &
ROBOTD_PID=$! ; sleep 0.5
./build-sim/relmw_supervisor --episodes 0 --period-us 1000 --sched other \
  --preset medical_vial --policy mlp --shield \
  --robot /dev/robot0 --mount /dev/relmw --library /tmp/qnxsim-lib.txt &
sleep 1

scripts/relmwctl status
scripts/relmwctl fault percep_bias
scripts/relmwctl plan push_B,push_A
scripts/relmwctl events 10
scripts/relmwctl quit
kill "$ROBOTD_PID" 2>/dev/null || true

Verification Checkpoint 4

0 / 2 checked
Step 05 / 06 10 min Not Started

Dual-Layer HAM Self-Healing, slog2 & 128-B ROS 2 Telemetry

Pair HAM process resurrection and hang-to-death heartbeats with lock-light slog2 shared-memory logging and zero-copy 128-byte IEEE CRC-32 telemetry frames.

ham_attach / CONDDEATH ham_attach_self slog2_register / vslog2f TelemetryFrame (128 B)

5.1 Dual-Layer HAM Self-Healing (apps/relmw_ham_setup.cpp + qnx_timer.cpp)

In QNX OS 8.0, a heartbeat alone triggers nothing — HAM only reacts to registered conditions. We attach two complementary entities so both hard crashes and silent infinite loops self-heal cleanly:

C++17 · libhamapps/relmw_ham_setup.cpp + qnx_timer.cpp
// Layer 1 (apps/relmw_ham_setup.cpp): respawn supervisor & robotd on process death
ham_connect(0);
ham_entity_t* e    = ham_attach(name, 0, -1, line, 0);
ham_condition_t* c = ham_condition(e, CONDDEATH, "death", HREARMAFTERRESTART);
ham_action_restart(c, "restart", line, HREARMAFTERRESTART);

// Layer 2 (qnx_timer.cpp watchdog_attach): turn a silent hang into a death Layer 1 restarts
ham_entity_t* hb   = ham_attach_self("relmw_supervisor_hb", heartbeat_ns, 3, 10, 0);
ham_condition_t* lo = ham_condition(hb, CONDHBEATMISSEDLOW,  "hb_missed_low",  0);
ham_condition_t* hi = ham_condition(hb, CONDHBEATMISSEDHIGH, "hb_missed_high", 0);
ham_action_execute(hi, "kill_hung", "/system/bin/slay -f -9 -m pid <pid>", 0);

5.2 Structured Logging with slog2 (src/platform/qnx/qnx_log.cpp)

QNX OS 8.0 removed syslogd. log_init("relmw") allocates an 8-page (32 KB) shared-memory ring buffer in slogger2 and streams structured severity logs via vslog2f(). Inspect live logs on target with slog2info -b relmw (or build-sim/qnxsim_slog2info -b relmw locally).

5.3 128-Byte Zero-Copy Binary Telemetry & ROS 2 Bridge

bash · telemetry + ROS 2Decode 642-frame CRC-32 replay stream
# Inspect verified 128-byte IEEE CRC-32 TelemetryFrame records (82,176 B total)
python3 tools/ros2_qnx_bridge.py decode results/host_splus/replay_showcase.bin | head -n 5

# Run UDP bridge (auto-publishes /relmw/diagnostics & /relmw/pose with rclpy, or JSONL offline)
python3 tools/test_ros2_bridge.py

Verification Checkpoint 5

0 / 2 checked
Step 06 / 06 10 min Not Started

Least-Privilege secpol Hardening & One-Click QNX SDP 8.0 Deployment

Lock down every path attachment and IPC channel with QNX 8.0 Mandatory Access Control, lint the policy statically, and package the complete QEMU x86_64 / Raspberry Pi 5 evidence bundle.

secpolcompile allow_attach PROCMGR_AID_PATHSPACE run_qnx80_evidence_pack.sh tracelogger
Interactive Lab · QNX 8.0 secpol Least-Privilege Access Verifier
ALLOW

6.1 Lint relmw_secpol.txt & Run One-Click QNX SDP 8.0 Evidence Packager

bash · host auditVerify secpol + full 40-test CTest offline release gate
# 1. Static QNX 8.0 secpol audit (21 assertions)
python3 tools/secpol_lint.py config/secpol/relmw_secpol.txt --repo .
python3 tools/test_secpol_lint.py

# 2. Complete offline verification (39/39 direct tests / 40/40 CTest + S+ benchmark + dashboard)
CXX=clang++ bash scripts/run_qnx80_evidence_pack.sh --host-verify
bash · QNX SDP 8.0 targetOne-click qcc/q++ cross-build, 4-vCPU QEMU boot, tracelogger & HAM proof
# On a Linux/KVM host with free QNX Everywhere SDP 8.0 installed at ~/qnx800:
QNX_SDP=~/qnx800 bash scripts/run_qnx80_evidence_pack.sh --preflight
QNX_SDP=~/qnx800 bash scripts/run_qnx80_evidence_pack.sh --keep-vm

# Raspberry Pi 5 (aarch64le) cross-build:
source ~/qnx800/qnxsdp-env.sh
cmake -S . -B build-qnx-aarch64le -G Ninja \
  -DCMAKE_TOOLCHAIN_FILE=cmake/qnx-aarch64le.cmake -DCMAKE_CXX_FLAGS="-Wall -Wextra -Werror"
cmake --build build-qnx-aarch64le --parallel

Verification Checkpoint 6 (Final Sign-Off)

0 / 2 checked
Explore Replay Dashboard →