8000 Keep build resources in stable, self-reclaiming directories by oleavr · Pull Request #1304 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content

Keep build resources in stable, self-reclaiming directories - #1304

Merged
oleavr merged 8 commits into
mainfrom
session-dirs
Nov 25, 2026
Merged

oleavr merged 8 commits into
mainfrom
session-dirs

Conversation

@oleavr
@oleavr oleavr commented Nov 25, 2026 •
Copy link
Copy Markdown
Member

Every Frida session on macOS leaves a frida-<random> directory with an extracted agent and helper (about 68 MB) in the cache directory whenever its process is killed, calls _exit, or exits without shutting Frida down, and nothing ever reclaims them. A machine running tests in a loop accumulated 142 GB that way in a day. Helpers and idund instances whose client died keep their directories in use on top of that.

This makes build resources live in one directory per build and user, and lets the next process clean up after a dead one.

Design

  • Naming. Build resources live in <base>/frida-<version>-<id>, where the id is a SHA-256 over the user id plus the names and digests of the resources that may go in that directory. The digests are computed at build time by the resource compiler, so nothing is hashed when a session starts.
  • Still lazy. Each file is written only when something first needs it, through the same call sites as before. It is written to a temporary name and renamed into place, with a small record beside it (digest, size, device, inode, mtime, ctime). Files are never modified in place, so anything that has one mapped keeps its inode. memfd on Linux and section injection on Windows are untouched: where nothing was written before, nothing is written now.
  • Locks. Every process using a resource directory holds a shared lock on its .lock, whose mtime records the last use. Session directories (user blobs and the like) keep their random names, hold an exclusive lock for their owner's lifetime, and are now emptied completely on destroy.
  • Reclamation. Once per process, in the background, over the user's own entries only: another build's directory once it is unlocked and unused for 24 hours; a dead session's directory at once; a leftover temporary after an hour; an old-format frida-<32 hex> directory, which has no lock, after three days.
  • Trust before reuse. A directory or file is reused only if it is owned by the user, is not a symlink, is not group- or other-writable, and matches its record. The fixed root used by frida-server is checked without following symlinks. Sticky shared bases such as /tmp get the same checks, and a failed check falls back to a random session directory. Shared bases that are not sticky never get stable names.
  • always_use () now names a checked root that holds resource and session directories, instead of one flat directory wiped at start.
  • The helper, idund and injected agents do not take the lock; removing their files under them is harmless on Unix, and refused on Windows.

The last commit is separable: it implements the missing half of DarwinHelperProcess.try_mmap (), which has returned null since in-memory injection was added. The client hands the blob to the helper as an unlinked shared memory object over the descriptor passing the helper already supports, and the helper maps it the way frida-server does. A local macOS session then writes no frida-agent.dylib at all; only frida-helper and idund remain on disk. The system session still writes the agent, since it loads it in-process through dyld.

Tested (macOS, arm64)

  • 1,000 sessions in sequence and 1,000 with 25 in parallel, about half exiting cleanly, a quarter through _exit and a quarter killed with SIGKILL at a random point: exactly one resource directory per set, no temporaries left, contents matching the build digests, no helper left running. 200 sessions of 17.27.5 left 85 directories, 4.4 GB.
  • SIGKILL at random points of a cold start, 250 times: every following session worked.
  • Reclamation, 15 cases: kept when locked by a live user, when used 23 hours ago, a live session's directory, a two-day-old legacy directory, and anything not named like ours; reclaimed when unlocked and 25 hours stale, a dead session's directory, a four-day-old legacy directory, half-created directories past their age.
  • Two builds coexist; the other build's directories are kept while its controller lives and reclaimed after it is killed, with the target surviving and a new controller attaching.
  • Tampering, 11 cases (a flipped byte with mtime restored, a truncated helper, a symlink swapped in, a missing record, a half-written file, a group-writable directory): all detected and replaced, or fallen back from.
  • /System including the two new tests, /Injector and /HostSession (Local, Service, Darwin with ChildGating, ExitMonitor and UnwindSitter): 39 ok.
  • In-memory agent: a plain target, a hardened-runtime target, a sandboxed App Store app and an arm64e target all attach, and spawn works, with no agent file on disk.
  • Attach latency, median of 15 to 30: 131–134 ms before; 125 ms for the first session into an empty cache; 119 ms warm.

Not verified

  • Linux and Android (helpers only, and only without memfd), Windows (helpers and LockFileEx locking), FreeBSD, QNX and Prospero (session directory locking and reclamation): built by CI on this pull request, never run.
  • The two new /System/ResourceDirectory tests pass in CI on Linux x86_64 and on Windows x86 and x86_64, which exercises the Windows locking; reclamation has not been exercised on either.
  • On Windows, renaming a directory while holding a handle to its .lock may fail, in which case nothing would be reclaimed there.
  • On QNX the temporary directory cannot hold subdirectories, so a resource directory falls back to the base itself; the two new tests are skipped there.
  • Linux and Windows agents are unchanged and stay in session directories.

Where to look first

  1. ResourceDirectory.claim (): the retry loop that checks the .lock inode after waiting for the lock, which is the race with the reclaimer.
  2. maybe_reclaim (): it touches only the four name patterns and only entries owned by the user, and renames before deleting.
  3. root_directory and is_trusted_root ().
  4. In the last commit, the shared memory object's lifetime, and MAP_SHARED in the helper (macOS refuses MAP_PRIVATE for shared memory).
  5. The two Windows C functions.

Lets consumers name and verify extracted copies without hashing at run
time.
Build resources are shared per build and user under a stable name,
verified before reuse, and session directories are locked so that
whatever a dead process left behind can be reclaimed by the next one.
The out-of-process helper now gets the blob through an unlinked shared
memory object, so local sessions no longer write frida-agent.dylib to
disk.
@oleavr
oleavr merged commit a0b9b89 into main Nov 25, 2026
59 of 72 checks passed
@oleavr
oleavr deleted the session-dirs branch November 25, 2026 21:59
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

0