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.
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.
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:
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
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.
// 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 onlytimer_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.
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):
Non-finite & Kinematic Guard: Rejects NaN/Inf (kShieldNonFinite) and clamps displacement max_step_delta and acceleration max_step_accel.
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.
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)
Expose the supervisor as standard files via resmgr_attach() and isolate the robot driver behind synchronous _IO_MSGMsgSend/MsgReply with client priority & runmask inheritance.
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 / CONDDEATHham_attach_selfslog2_register / vslog2fTelemetryFrame (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:
// Layer 1 (apps/relmw_ham_setup.cpp): respawn supervisor & robotd on process deathham_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).
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.