# Native architecture

Each capability has an independent TypeScript entry point and a Rust module under native/src.
The [systemd capability](systemd.md) loads `libsystemd.so.0` lazily through `dlopen`,
with no link-time dependency. Each connection's dedicated native thread owns its
bus, messages, credentials, and cleanup. N-API asynchronous tasks deliver owned
results; no sd-bus work or caller callbacks execute on the JavaScript thread.
The common loader selects exactly one matching optional package. It captures the import-time
async context so native registration does not retain the first operation's request scope.
It caches load attempts, never starts child processes, and never downloads or compiles code.

Windows jobs live in `windows_job.rs`. Native owners close job, process, and pipe handles;
JavaScript never receives numeric handles. Child creation supplies both JOB_LIST and
HANDLE_LIST to CreateProcessW, so admission precedes execution and only the selected stdin
reader (NUL by default) and the two output writers are inherited. Opt-in private input uses
a local byte-mode named pipe: a non-inheritable NOWAIT server writer makes one bounded
write attempt and reports its accepted prefix, while the child reader keeps blocking read semantics.
The system cryptographic RNG supplies an unpredictable name; first-instance protection,
remote-client rejection, and creator-PID validation precede exposing the writer. No background
I/O owns a borrowed JavaScript buffer. The native child owner closes the writer on explicit
input close, child close, or finalization. Root exit does not decide stream state. Each pipe has one reader: a peek followed by a read
bounded to the available bytes cannot wait for a writer. Root exit, pipe EOF, and job
extinction are separate observations. Pinned processes reuse identity's handle-based reader.
Their intended access mode determines the exact OpenProcess rights. Native mode checks
precede job creation, handle duplication, or assignment. Job-member exit checks use the
retained identity reader so they do not require synchronization rights.

Lifetime binding duplicates a fresh non-inheritable kill-on-close job into a pinned live
owner before assigning the caller, then releases the caller's handle. Failed admission
closes the remote duplicate; an exited owner has already closed it. Retaining a current
process job deliberately relinquishes native finalization, leaving its handle to OS process
exit rather than Worker exit. [Windows job contracts](windows-job.md) describe both moves.
Linux libc detection uses the process report, executable ELF interpreter, then conventional
musl library names. Unknown libc attempts the GNU binding and fails explicitly if incompatible.

Darwin reads public proc_bsdinfo through libc's C layout on both arm64 and x64 and validates
size, PID and timeval fields. Its ESRCH path checks `kern.proc.pid` using the SDK
layout because libproc excludes zombies and processes already exiting. This second observation
distinguishes a retained object from a missing process. Coalitions use PROC_PIDCOALITIONINFO with exact returned-size
checking. Private XPC symbols are resolved only while reading the optional name; all required
symbols, dictionary type, and matching coalition ID are checked before consuming it.

Windows retains a process handle through GetProcessTimes and the parent observation. FILETIME
is divided as a 64-bit integer before conversion to a JavaScript number. Handles close even
when an observation fails. GetExitCodeProcess reports retained exited objects; an exit timestamp disambiguates
the reserved STILL_ACTIVE value from an application that actually exits with code 259. Access denial is never reported as a missing process.

Windows x64 and ARM64 release bindings use dynamic OS-provided UCRT and static
compiler-specific VCRUNTIME. This avoids an external VC redistributable prerequisite
while retaining Windows servicing of the UCRT. The producer preserves inherited
Rust flags, including Cargo's encoded-flags precedence, and applies this policy only
to MSVC targets; other platforms' linkage is unchanged.
Before staging or packing, PE machine and import/delay-import checks reject external
VC runtime dependencies and require OS UCRT linkage. These artifact checks do not
prove runtime loading: test the exact packed root/platform pair on a separate clean
Windows runtime environment without Build Tools or a VC redistributable, using Node
22 and 24, real self identity and inherited-option Workers. Record each architecture
separately; cross-compilation is not ARM64 runtime proof.

Linux reads procfs stat using the final comm parenthesis and brackets it with samples of
`CLOCK_REALTIME` and `CLOCK_BOOTTIME`. Intersecting the samples' offset intervals detects
observed wall-clock changes and bounds sampling uncertainty to one clock tick; a disjoint
or wider interval throws `incomplete`. The lower offset bound plus the tick-quantized birth
estimates epoch time with less than two ticks plus one microsecond of uncertainty relative
to the current wall clock. `/proc/stat`'s integer-second `btime` is not used: its truncation
adds to start-tick truncation and can exceed a whole second. `startTimeSinceBootMicros`
comes directly from start ticks and is preferred for PID-reuse comparisons. Both birth and
`CLOCK_BOOTTIME` include suspend time. Zombie/dead states set `exited`. A concealed
procfs entry is not absence unless kill(pid, 0) proves ESRCH.

FreeBSD reads `kern.proc.pid` using libc's kinfo_proc layout, requiring 1088 bytes,
layout version zero, and the requested PID. Exact boot timevals bracket the query;
a changed bracket fails closed. `startTimeMicros` is the kernel epoch start time;
`startTimeSinceBootMicros` subtracts that same bracketed boot timeval. `ki_stat`
identifies zombies. Visibility denial can also return ESRCH. A cached policy snapshot determines whether
that means absence or `access-denied` with a `visibility-policy` reason. Policy
read failures are restricted. See [identity and visibility](identity.md).

FreeBSD CI uses 14.4. Its major-line pkg installation must pass runtime admission
before native proof: the actual Node executable must match the guest platform,
architecture, selected major, and patched matrix floor (22.23.4 or 24.22.1).
Newer stable patches in that selected major are accepted and the actual identity
is logged. An older package fails with pkg-upgrade guidance; a matrix label alone
is not evidence of the installed patch.
Dependencies and TypeScript output are prepared on Linux and
synced into the VM; Cargo builds the addon under the FreeBSD kernel. The portable
native suite runs directly with Node, including the unreaped-zombie test and real
Workers, followed by a root-only npm consumer install. ARM64 CPU execution uses
QEMU emulation with a real FreeBSD kernel. No pnpm binary runs inside either VM.

Run `PROC_SAFE_NATIVE=require pnpm test` and `node scripts/runtime-proof.mjs` after building.
`node scripts/p2-proof.mjs` exercises page-cache observations and test-only fixtures on
the main thread and real Workers. Linux seccomp proof runs in disposable child processes.
Page-cache sampling uses one bounded `PROT_NONE` mapping at a time and always unmaps
through Rust. Windows raw-spawn fixtures are independent of production launch owners,
disable handle inheritance, and close their process/thread handles in Rust.
Linux reaping uses non-consuming `waitid` observations and PID-specific `waitpid`,
both with `__WALL`; it never performs a reap-any-child operation. The Node-only
`scripts/reaper-proof.mjs` runs dedicated owners on the main thread and a Worker,
including a non-SIGCHLD clone child retained across exec, adopted grandchildren,
and libuv-owned Node children whose exit events must retain their real status.
Both the main thread and a real Worker query native identities. Packed npm and pnpm root-only
installs test native loading, platform filtering, and Worker behavior outside the workspace.
Run `pnpm benchmark:imports` and `bun scripts/benchmark-imports.mjs` for fresh-process import
measurements. FreeBSD uses system Node; there is no supported Bun binary for FreeBSD.

Command [inspection](inspect.md) uses Darwin `KERN_PROCARGS2` and Windows PEB parameters,
including WOW64 layouts. Bounds and returned lengths are validated in Rust. Only requested
environment entries cross N-API. Windows memory reads use one retained process handle;
Darwin arm64 builds need no translation check; x64 builds query translation in Rust
and refuse Rosetta or an unreadable status before reading argument data.

Windows census enumeration retains all observed PIDs within a 65,536-entry bound, including
inaccessible and vanished rows. Each row uses one retained process object for validated token
SID comparison, existing identity decoding, and optional command inspection. Native monotonic
budget checks and a final wrapper check reject expired calls without a partial result. Windows
environment selection uses ordinal case-insensitive comparison and preserves requested casing.

Darwin census uses the same PID-count bound and validates the caller, signs, and uniqueness
before filtering requested PIDs. Only selected PIDs receive per-process reads. Census
without commands never queries translation status and works under Rosetta or a sandbox
that denies that query. Short BSD records supply topology, effective UID, and state
without requiring access to birth identity. The existing SDK kernel-record bridge supplies
topology for zombies and processes already exiting when libproc returns ESRCH; only proven
disappearance omits a PID, including disappearance proven by the trailing birth-identity read.
Birth-time brackets fence optional identity and command combinations against PID reuse.
Both native monotonic budget checks and the wrapper reject expiry without returning a
partial census.
`scripts/darwin-census-proof.mjs` exercises root ownership, a setsid child, stopped state,
unreaped zombie state, disappearance, PID selections, and native deadlines on main and Worker.
It also verifies topology for an exiting process on main while the child's last pty close drains.
The Vitest wrapper compiles that fixture in suite setup and passes its executable to the proof,
so compiler and SDK startup do not consume the unchanged five-second test deadline.
A Rust unit test reaps an unreaped zombie between its topology and trailing identity reads
to prove that the row is omitted rather than reported without identity.
`scripts/check-darwin-seatbelt.mjs` verifies a real `EPERM` from the translation sysctl
(or its pre-existing absence on Intel kernels),
then proves census and architecture-specific command behavior in that same sandbox on
main and Worker with Node and Bun. The Rosetta CI step repeats the sandbox proof on x64.

The root [`getProcSafeNativeStatus()`](native-status.md) entry defers the status and
loader modules until called. Its default reads the cached loader outcome; an
explicit `{ probe: true }` attempts native loading. Reports contain only named
reasons, allowlisted error codes, and package metadata, never loader paths.

The [`windows-token` capability](windows-token.md) queries the current process token
with `TokenIsAppContainer`, then `TokenAppContainerSid` only when contained. Rust
validates returned lengths and the SID bounds before converting it to a string,
and closes the token handle on every path. Queries are fresh on the calling
thread; thread impersonation does not change which process token is inspected.

Filesystem-restricted launch uses a separate single-threaded binary, packaged alongside
the Linux addon. It creates a Landlock ruleset and sets no-new-privileges before exec,
so all target runtime threads inherit the same domain. The TypeScript API prepares
arguments without spawning or loading the addon; the existing caller owns descriptors,
child supervision and filesystem policy. See [filesystem launch](filesystem-launch.md).

The Windows platform packages also contain `proc-safe-spawn.exe`. This native-only
launcher preserves libuv's standard handles and CRT descriptor table while creating
an explicitly requested breakaway payload. It neither consumes Node IPC environment
variables nor interprets stream bytes. The package build, artifact collection, and
consumer package checks include the executable alongside the addon. No executable
is included in the platform-independent root package.
