Skip to content

test(fspy): add an access-relative benchmark suite - #602

Merged
wan9chi merged 1 commit into
mainfrom
claude/fspy-benchmark-access-relative
Aug 9, 2026
Merged

test(fspy): add an access-relative benchmark suite#602
wan9chi merged 1 commit into
mainfrom
claude/fspy-benchmark-access-relative

Conversation

@wan9chi

@wan9chi wan9chi commented Aug 9, 2026

Copy link
Copy Markdown
Member

Motivation

The access suite opens an absolute path, which the tracker serves with a borrowed pointer — the code that resolves a file descriptor's directory and joins it with a relative pathname never runs, so that lane has been unmeasured. Upcoming changes (#596) modify exactly that lane, so it needs a benchmark row before those land.

What this does

Adds an access-relative suite: the target opens a bare filename with the filesystem root as its working directory, forcing the working-directory lookup and the join. Root as the working directory makes the resolved path byte-identical to the absolute suite's, so the two rows differ only in the work being priced, not in what gets captured — and validation asserts the same captured path in both modes.

The launcher grows a --relative flag instead of a new binary: the harness compares two builds of the same launcher source, so one parameterized binary keeps both suites measured by identical code.

🤖 Generated with Claude Code

The existing access suite opens an absolute path, which the tracker
serves with a borrowed pointer — the code that resolves a file
descriptor's directory and joins it with a relative pathname never runs,
so that lane was unmeasured. The new suite opens a bare filename with
the filesystem root as the working directory, forcing the
working-directory lookup and the join. Root makes the resolved path
byte-identical to the absolute suite's, so the two rows differ only in
the work being priced, not in what gets captured, and validation asserts
the same captured path in both modes.

The launcher grows a --relative flag instead of a new binary; the
harness compares two builds of the same launcher source, so one
parameterized binary keeps both suites measured by identical code.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wan9chi

wan9chi commented Aug 9, 2026

Copy link
Copy Markdown
Member Author

Recreating this PR through gh stack so it joins stack #598 properly.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

fspy benchmark

linux

dynamic/launch             change  +0.09%  [ -2.72% ..  +4.40%]  overhead   +59.66%
dynamic/access             change  -0.25%  [ -1.07% ..  +0.54%]  overhead    +6.37%
dynamic/access-relative    change  -0.09%  [ -8.75% ..  +3.64%]  overhead   +56.32%
static/launch              change  +0.58%  [ -3.82% ..  +6.68%]  overhead  +173.68%
static/access              change  -0.05%  [ -1.85% ..  +1.15%]  overhead  +830.98%
static/access-relative     change  +0.11%  [ -0.96% ..  +1.68%]  overhead +1344.10%

macos

dynamic/launch             change  +0.38%  [ -2.22% ..  +3.54%]  overhead  +210.76%
dynamic/access             change  +0.00%  [ -1.16% ..  +1.55%]  overhead    +2.35%
dynamic/access-relative    change  +0.08%  [ -0.86% ..  +0.86%]  overhead  +409.87%

windows

dynamic/launch             change  -0.29%  [ -2.79% ..  +2.24%]  overhead   +27.87%
dynamic/access             change  +0.21%  [ -1.72% ..  +8.92%]  overhead    +1.79%
dynamic/access-relative    change  +0.32%  [ -1.76% ..  +6.08%]  overhead    +1.57%

@wan9chi
wan9chi merged commit ca68894 into main Aug 9, 2026
22 of 24 checks passed
@wan9chi
wan9chi deleted the claude/fspy-benchmark-access-relative branch August 9, 2026 03:33
wan9chi added a commit that referenced this pull request Aug 9, 2026
Part of #605 — this lands the malloc-class fix (the largest of the
hazards there); the lazy-dlsym, hot-path panic, TLS reentrancy, and
posix_spawn-thread items remain follow-ups.

The benchmark suite that prices this change merged in #602.

## Motivation

The preload library runs inside libc calls such as `open`, `stat`, and
`execve`. Programs are allowed to make these calls from a signal
handler, or in the child of `fork()` in a program with many threads. In
both situations, using libc's `malloc` can hang the program forever: the
lock inside `malloc` may be held by a thread that is paused or no longer
exists. The preload library still allocates through `malloc` today, so a
traced program can hang in exactly these situations.

## What this does

Adds a new crate, `sigsafe`: Unix syscall wrappers that are safe to call
where libc is not — in signal handlers, in fork children, before libc
has finished initializing. Its
[README](https://github.com/voidzero-dev/vite-task/blob/claude/fspy-libc-async-signal-safe-129134/crates/sigsafe/README.md)
states the three rules everything in it follows: syscalls only (never
through libc on Linux), no locks and no hidden state, and no global
allocation.

**The no-libc rule is enforced at compile time.** rustix can be built
with a libc backend, and anything in the dependency graph — including
crates outside this repository — can select it; no build script can
detect the feature-unification case. So `sigsafe`'s `lib.rs` references
`rustix::runtime`, a module that exists only in rustix's raw-syscall
build: selecting the libc backend makes the crate fail to compile
instead of silently losing the guarantee.

On top of the first wrappers (`mm::mmap_anonymous`, `mm::munmap`,
`param::page_size`) sits `sigsafe::alloc`, allocation that never touches
malloc, in three layers with only the top exposed:

- `MmapAllocator` — every allocation asks the kernel for fresh memory
pages through `sigsafe::mm`. It keeps no state of its own, so there is
nothing a signal or a `fork()` can catch locked or half-written.
- `ChunkPool` — keeps up to 64 freed 64 KiB chunks in a fixed array of
atomic pointers, so the next call can reuse memory without asking the
kernel again. Taking or returning a chunk is one atomic swap per slot,
never a lock, and a thread that disappears mid-operation can strand at
most the one chunk it held.
- `alloc::arena()` — the only public function. It hands one intercepted
call its own bump arena (a `bump_scope::Bump`) that draws chunks from
the pool and returns them when the call ends. Values allocated in the
arena cannot outlive the call; the borrow checker enforces it.

Uses the arena in one place to start: `RawExec::to_c_str_array`, which
builds the NULL-terminated argv/envp pointer arrays that an intercepted
exec hands to the real call, then drops them when it returns. That
temporary's lifetime is already exactly a bump arena's, so the change is
nine lines and adds no `unsafe`.

It has to come off malloc because exec runs in the child of `fork()` in
multithreaded programs — `posix_spawn` forks then execs — where malloc's
lock may be held by a thread that no longer exists. Both platforms take
this path on every intercepted exec.

The strings the array points at are still owned by `Exec` and still come
from malloc, as does the rest of the preload; converting them is
follow-up. This change establishes the crate, the layers, and the
lifetime discipline in the smallest place all three apply.

## Benchmark

The `access-relative` suite (#602) was added while this PR still used
the arena for the fd-relative join, and it earned its keep twice: an
early run showed **+29%** on Linux, which turned out to be the preload
building without optimizations (fixed by #597), and the corrected runs
showed the arena join costing ~+3% over `PathBuf::push` — which is why
the join reverted and the arena moved to `execveat`. The investigation
is written up in [this
comment](#596 (comment)
thread. With the join reverted, both suites should sit at baseline.

## Commits

The first commit is an earlier version of the allocator — one lock-free
size-class allocator installed as the preload's `#[global_allocator]` —
kept so the two designs can be compared; #599 measured that design end
to end and lost. The second commit replaces it with the arena design
above. The third moves the allocator into the new `sigsafe` crate as
`sigsafe::alloc`, adds `mm`/`param` and the compile-time backend
enforcement, and adds the README. The fourth moves the arena use from
the join to `execveat` and fixes the dangling pointer there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant