Repository navigation
feat(fspy): add userland ELF loader and freestanding target-none foundations - #1015
Conversation
Co-authored-by: GPT-5 Codex <codex@openai.com>
Co-authored-by: GPT-5 Codex <codex@openai.com>
Co-authored-by: GPT-6 <codex@openai.com>
Add borrowed stdout/stderr accessors, formatted descriptor writes, and an opt-in Linux panic handler. Define the thin C string layout and cover write retries and errors. Co-authored-by: GPT-5 Codex <codex@openai.com>
Move InitialStack and AuxEntry into env and add main! with parsed stack input and the default panic handler. Preserve layout tests and the callback lifetime compile-fail test. Co-authored-by: GPT-5 Codex <codex@openai.com>
Use fspy_nostd::env::AuxEntry for inherited auxv and restrict ELF execution to the Linux and none targets. Co-authored-by: GPT-5 Codex <codex@openai.com>
Add a target-none launcher using fspy_nostd's entry point and panic handler to exercise exec_elf. Leave the separate host-side test crate for a later commit. Co-authored-by: GPT-5 Codex <codex@openai.com>
Add the allocator registration macro and link regression tests. Expose MAP_FIXED on Linux/none and clarify the entry point's relocation requirements. Co-authored-by: GPT-5 Codex <codex@openai.com>
Map executable and interpreter segments, build the initial stack using an explicit page-bump allocator, and enter the interpreter without a kernel exec. Add freestanding launcher integration tests with ntest timeouts for AArch64 and x86-64. Co-authored-by: GPT-5 Codex <codex@openai.com>
Co-authored-by: GPT-5 Codex <codex@openai.com>
Load dynamic ET_EXEC images at their declared addresses without replacing existing mappings. Add fixed-address runtime and collision tests, plus an optional Node.js execution check. Co-authored-by: GPT-6 <codex@openai.com>
Place the freestanding launcher inside sanitizer-friendly application regions while keeping low executable addresses available. Verify the static ELF layout and fixed-address execution; drop a unit test covered by integration coverage. Co-authored-by: GPT-6 Codex <codex@openai.com>
Map zero-filesz loads like elf_map so a mid-page BSS segment does not wipe the previous page. Padzero whenever filesz is not page-aligned, including filesz==memsz. Accept padded PT_INTERP paths and unordered non-overlapping PT_LOADs. Clear leftover GPRs in the enter stubs. Co-authored-by: Cursor Grok 4.6 <cursoragent@cursor.com> Co-authored-by: Charles Wang <wan9chi@users.noreply.github.com>
Treat PT_INTERP as optional. Map only the executable when it is absent, set AT_BASE to 0, and enter e_entry. Add cc -static and -static-pie runtime fixtures covering traditional static and static-PIE. Co-authored-by: Cursor Grok 4.6 <cursoragent@cursor.com> Co-authored-by: Charles Wang <wan9chi@users.noreply.github.com>
The undefined __fspy_global_allocator_must_not_be_used symbol is for freestanding Linux/none binaries. Linking it into fspy_preload_windows fails MSVC because the rlib object is retained. Also align cargo-shear with the rustc-only allocator fixture and skip lib doctests that rustdoc compiles with std. Co-authored-by: Cursor Grok 4.6 <cursoragent@cursor.com> Co-authored-by: Charles Wang <wan9chi@users.noreply.github.com>
Co-authored-by: Cursor Grok 4.6 <cursoragent@cursor.com> Co-authored-by: Charles Wang <wan9chi@users.noreply.github.com>
…lamp shared pages Avoid zeroing past filesz == memsz on read-only and executable segments, which unnecessarily COW-dirtied shared pages and triggered RW -> RX mprotect transitions under W^X policies. Clamp the padzero range to the start of any higher-vaddr PT_LOAD sharing the same page so unordered segments cannot wipe each other. Co-authored-by: Gemini 4 Neon <gemini@google.com>
Expose fspy_nostd::param::seed_page_size on Linux/none and seed it from AT_PAGESZ in fspy_elf_loader::exec_elf so temporary allocations in the freestanding launcher do not trigger a redundant 2 MiB mmap/mprotect page-size probe. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…room, and entry GPRs - Reject fixed-address (ET_EXEC) PT_INTERP images instead of emitting AT_BASE = 0. - Relax segment congruence to page granularity, matching Linux binfmt_elf so patchelf-modified binaries load cleanly. - Break on AT_NULL in inherited auxv, guard against null AT_RANDOM / platform pointers, 16-byte align AT_RANDOM, and reserve 64 KiB of call-stack headroom. - Zero all remaining GPRs in x86-64 and AArch64 enter stubs to match Linux process entry state. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…d leaks in static fixture Do not hardcode that the host's /bin/echo is a dynamic PIE (which can fail on Alpine/static-busybox or non-PIE distros; FIXTURE already asserts ET_DYN with PT_INTERP), and add the /proc/self/fd leak check to static_runtime.c to match fixture.rs. Co-authored-by: Gemini 4 Neon <gemini@google.com>
Keep fspy_elf_loader as a pure no_std library without a test-only [[bin]], build.rs, or dummy host main(). Put the freestanding test launcher in crates/fspy_elf_loader_test_launcher and depend on it from fspy_elf_loader_tests. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…re cc resolution
Compile one C runtime fixture (fixtures/runtime.c) across all four ELF64
cells with explicit PIE/non-PIE flags (-fPIE -pie, -fno-pie -no-pie,
-fno-pie -static, -fPIE -static-pie), eliminating protocol drift between
fixture.rs and static_runtime.c and removing the brittle rustc -no-pie
link-arg override in fixture_exec.rs. Keep fixture.rs as a dedicated Rust
std runtime check.
Use the cc crate for target-aware C compiler discovery (CC_<target>, CC)
with rerun-if-env-changed instead of hardcoding Command::new("cc").
Co-authored-by: Gemini 4 Neon <gemini@google.com>
…rnel exec Execute each runtime fixture directly via kernel execve (with matching arg0 and environment) before running it through the freestanding launcher so fixture or toolchain regressions are separated from loader bugs. Co-authored-by: Gemini 4 Neon <gemini@google.com>
cargo-zigbuild's gnu wrapper cannot link static glibc binaries, so detect zigcc wrappers in build.rs and compile the static and static-PIE C fixtures via `cargo-zigbuild zig cc` targeting `<arch>-linux-musl`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
… instead of /bin/echo Replace raw ELF header/program-header byte offsets with `offset_of!` / `size_of` over `libc::Elf64_Ehdr`/`Elf64_Phdr` in integration tests and a typed `Phdr` builder in unit tests. Mutate our own `FIXTURE_DYN_PIE` binary instead of host `/bin/echo` in malformed-image and section-header tests so those cases no longer depend on host system binary layout. Co-authored-by: Gemini 4 Neon <gemini@google.com>
The unified C fixture (`fixtures/runtime.c`) already covers the same startup contract across all four ELF64 cells, making the extra dynamic-PIE Rust `fixture` binary redundant. Move `libc` to Linux dev-dependencies. Co-authored-by: Gemini 4 Neon <gemini@google.com>
Run `fspy_elf_loader_tests` in `--release` on both Linux glibc and musl CI jobs, run `clippy` on `fspy_elf_loader` and `fspy_elf_loader_test_launcher` for both bare-metal targets (`x86_64-unknown-none` and `aarch64-unknown-none-softfloat`), and update `fspy_elf_loader_tests/README.md`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…in, link args, and C ehdr read - Share `EHDR_SIZE` and `PHDR_SIZE` constants from `image.rs` across `image/tests.rs` (and `stack.rs`). - Simplify `Mapping::retain` to return `()` since no caller used the pointer. - Use `cargo::rustc-link-arg=` in `fspy_elf_loader_test_launcher/build.rs`. - Read `Elf64_Ehdr` fields directly in `fixtures/runtime.c` instead of raw offsets `64`/`32`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…ng without allocator Stage `argc`/`argv`/`envp`/`auxv` words directly at the bottom of the reserved stack mapping and shift them up next to the payloads in `finish()` via `ptr::copy` (zeroing the vacated scratch prefix below SP). This drops the `allocator-api2` and `fspy_nostd_alloc` dependencies and an extra `mmap`/`munmap` pair per `exec_elf`, gates the remaining `fspy_elf_loader` dependencies to `linux`/`none`, and removes the now-unused `fspy_nostd::param::seed_page_size`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…ookup Resolve the C compiler directly from `CC_<target>`, `CC`, or `cc` in `fspy_elf_loader_tests/build.rs`, removing the `cc` build dependency and eliminating the spurious `zig: error: version '.2.17' in target triple` warning emitted by `cc`'s compiler probe under `cargo-zigbuild`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
…fixture helper Remove `echo_preserves_empty_and_non_utf8_arguments` (already verified across all four runtime fixtures differentially against kernel `execve` and in `stack::tests`) and share fixture setup between `check_runtime_image` and `does_not_require_section_headers`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
Format `crates/fspy_elf_loader/README.md` with `vp fmt` and restore `crates/fspy_nostd/src/param.rs` to eliminate the leftover refactor from adding and removing `seed_page_size`. Co-authored-by: Gemini 4 Neon <gemini@google.com>
fspy benchmarklinuxmacoswindows |
Cross-compiled Windows nextest archives are built on Linux and extracted on Windows runners where the compile-time `CARGO_BIN_FILE_*` Linux path does not exist. Embed the freestanding launcher ELF with `include_bytes!` so `launcher_image` works portably across archived test runs. Co-authored-by: Gemini 4 Neon <gemini@google.com>
Cross-compiled Windows nextest archives remap the workspace at runtime
via `CARGO_MANIFEST_DIR`, whereas `env!("CARGO_MANIFEST_DIR")` baked
in the Linux build host path. Read `std::env::var_os("CARGO_MANIFEST_DIR")`
at runtime with a fallback to the compile-time path.
Co-authored-by: Gemini 4 Neon <gemini@google.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 3279a12. Configure here.
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Shared-page remap undoes BSS zeros
Medium Severity
map_segment padzeros BSS (and writable tails) while walking PT_LOAD headers, then a later header that shares that page issues MAP_FIXED over the whole file page. That remap restores file bytes and wipes the zeros already written for the earlier segment. The clamp only stops padzero from touching a higher vaddr; it does not re-zero after a later remap. Standard page-aligned GNU ld images avoid this, but the loader claims to accept shared-page and unordered loads, and those layouts can start with a non-zero BSS.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 3279a12. Configure here.


Note
Status: The userland ELF loader (
fspy_elf_loader) is still unfinished and is not wired intofspyyet — it is currently unused outside offspy_elf_loader_tests.vp runcontinues to spawn Linux processes using the existingLD_PRELOAD/seccomp_unotifypath.Motivation
On Linux,
fspycurrently splits process tracking betweenLD_PRELOAD(for dynamically linked binaries) andseccomp_unotify(for static binaries), because kernelexecveresets in-process intercept state. The target Linux architecture replaces this split with a single path: kernel-exec a freestanding (target_os = "none") launcher that installsSECCOMP_RET_TRAP/SIGSYSinterception and then maps and enters the target ELF in userland so intercept state survives into the target process.This PR lands the freestanding
target_os = "none"foundations and the initial userland ELF64 loader crate ahead of wiring interception and process spawning intofspy:fspy_nostd,fspy_shm,fspy_shared,vt_ipc_str):target_os = "none"entry point, initial stack views (argv/envp/auxv), raw stdio/panic handling, link-time global-allocator guard, andno_stdshared-memory IPC sender support.fspy_elf_loader): Pure#![no_std], allocator-free loader for little-endian ELF64 on x86-64 and AArch64 covering all four image shapes (ET_DYN/ET_EXEC, with or withoutPT_INTERP),PT_LOADmapping with shared-page/BSSpadzero, fixed-address overlap rejection, rebuilt startup stack (argv/envp/auxv), and naked entry stubs.fspy_elf_loader_test_launcher,fspy_elf_loader_tests): Freestanding static high-address test launcher and differential integration tests comparing all four C runtime ELF cells against kernelexecve, plus CI coverage for bare-metalclippyand release-mode loader tests.Remaining work (follow-up PRs)
PATHand shebang (#!) resolution aboveexec_elfand nestedexecvehandling./proc/self/exevirtualization so targets observe their own executable path.SIGSYSinterception in the launcher and wiringfspyto use it instead of theLD_PRELOAD/seccomp_unotifysplit.Note
Medium Risk
New low-level process startup and memory-mapping code on Linux with extensive tests, but no change yet to live fspy spawn paths; workspace dependency/feature tweaks may affect cross-target builds.
Overview
Introduces a Linux userland ELF64 loader (
fspy_elf_loader) that maps targets in-process and transfers control without kernelexecve, plus freestanding plumbing so a future high-address launcher can keep intercept state across entry. The loader is not wired intofspyyet; production Linux spawning still usesLD_PRELOAD/seccomp_unotify.fspy_elf_loaderimplementsexec_elffor x86-64 and AArch64:PT_LOADmapping (shared pages, BSS, Linux-style padzero), optionalPT_INTERP, rebuilt startup stack (argv/envp/auxv), and naked entry stubs. FixedET_EXECimages that overlap existing mappings are rejected.Freestanding support expands
fspy_nostd(main!,InitialStack, panic/stderr handling, link-timeno_global_alloc!guard) and teachesfspy_shm,fspy_shared, andvt_ipc_strto build undertarget_os = "none"where needed for launcher IPC writers.Testing & CI:
fspy_elf_loader_test_launcherplusfspy_elf_loader_testsdifferentially compare four C runtime ELF shapes against kernelexecve; workflows add bare-metal clippy and release-mode loader tests on Linux (glibc/musl).Reviewed by Cursor Bugbot for commit 3279a12. Bugbot is set up for automated code reviews on this repo. Configure here.