Files
apk-reverse/skills/apk-reverse/references/precedents/frida-spawn-hangs-zero-events-case-2.md
T
inliver233 2beade67f2 Reconstruction pass: make the gates tell the truth, make the package self-contained, keep the one-hop symptom index
An external audit of this repository landed on four structural findings, and this pass is the
response to them. Everything below was re-measured on this tree before being acted on; two of the
audit's claims needed a correction of my own, and both corrections are noted in place.

**The gates were optimistic, and one of them did not read content at all.**

  doctor.py    the capability table was hard-coded (`repack + sign = bool(java) and python`), so a
               machine with a JDK and no zipalign/apksigner was told it could re-sign; the script
               inventory was a hand-written list of 28 against 51 files on disk, omitting the nine
               most important scripts. It now scans the directory (`registered/checked = 52 / 52`,
               unjudgeable entries counted as `unknown`, never skipped) and resolves capability as a
               real requires-closure, reporting BLOCKED with the missing piece and an installable
               next step. Measured: the old doctor reports `[OK] repack + sign` on a JDK-only PATH
               where keytool, jarsigner, zipalign, apksigner and all eight smali jars are absent.
               The report is allowed to look bad; that is the point.
  check_commands.py (new)
               anchor checks prove a section exists, never that a command runs. This parses each
               script's argparse table with `ast` and checks every documented invocation against it.
               146 commands / 41 interfaces, 0 drift. It caught `sig_probe.py --live --pkg`, which
               does not exist -- the reference had been wrong since it was written.
  check_routing.py (new)
               the symptom index now has one home (`SKILL.md`, because symptom -> file must stay one
               hop) and the inventory has another (`references/routing.md`). This gate proves the
               mirror agrees, that every reference file is named, and that every script is named.

**so_constpatch.py was corrupting APKs it rebuilt.** The writer copied filename and timestamp onto a
fresh ZipInfo and recompressed the whole archive: 435-entry real package, `extra` lost on 205 entries,
`external_attr` rewritten on all 435, 132 untouched entries recompressed, and `resources.arsc` pushed
to an unaligned offset. Android's own `zipalign -c -v 4` fails the old output and passes the new one;
a 26-case fixture suite pins it, and `extractNativeLibs="false"` is now refused by default with a
pointer at `repack.py`. Fixing it exposed the same defect in `repack.py`, where `^lib/[^/]+\.so$`
cannot match `lib/arm64-v8a/*.so` -- so its alignment handling had never once fired on a real APK.

**The package was not self-contained.** 59 references pointed at the repository root, which `npx
skills add` does not install. They now converge on `references/evidence-summary.md` (capability,
conclusion, strength, evidence that ships) plus `evidence/` with the machine-readable companions: 8
references remain and each is explicitly labelled "repository root, not shipped". A drill in which
the new files were copied to an isolated directory and asked three questions answered all three.

**SKILL.md: 572 lines / 15,733 tokens -> 398 lines / ~10,900 tokens**, without losing the two things
that had to stay. The symptom index remains inline; the reference and script indexes moved to the
on-demand routing file. The freed budget went into making the rules *stronger*, not shorter: the
constraints section is now a table of thirteen non-negotiable constraints each paired with the check
that catches a violation, and the tooling obligations are instructions whose violation is a defect
(hedging language in SKILL.md: 1 line, down from a paragraph of hedged advice). Long references gained
a navigation head so a reader can decide from the top whether to load one.

**New: an executable test layer.** `tests/` (unit, CLI contract, no-device integration) plus
`pytest.ini`, `tests/run_tests.py`, a minimal CI job, and `evals/evals.json` written but never run --
stated as such in the file. 608 passed / 158 skipped / 25 xfailed. The 25 xfails are a work order, not
a pass: thirteen scripts report a missing input by exiting 1 instead of 2, seven let a traceback
escape, four treat `--help` as an error, and `preflight --json` lacks a `status` field. A regression
row for `dex_patch_bytes.py` reproduces a previous artifact byte-for-byte
(`67ae1e58e7a44777e5ac8f9f823ab21e…`) and additionally pins the four bytes that may move.

**Two real defects the new tests found in the tooling itself.** `check_repo.py` decoded subprocess
output with the locale encoding, so on a GBK console a script printing an em dash in its usage line
was reported as crashed; it now decodes UTF-8 explicitly. And a CLI contract test drove a *real
attached device* through `coldstart.py` -- logcat, screenshots, five orphan `adb logcat` processes --
which is fixed twice over: child processes run in a private temporary cwd, and the fifteen
device-bound scripts are never executed beyond `--help`/argument parsing.

**Quoting on the device side is now one implementation.** `devsh.su` escaped only double quotes, so
`;`, `$( )` and backticks in a caller-supplied value still reached the device shell while
`coldstart.Device.shell` quoted correctly. Both use `scripts/device_shell.py` now: POSIX single-quote
escaping, argv arrays by default, and identifier validation that refuses a malformed package, path or
component with exit 2 instead of a traceback. The integration test that used to pin the *defect* (it
asserted `su -c "…"` with `\"` escapes) now asserts the property, across five metacharacter payloads.

Gates: check_repo 0 problems (including the content-reading identity scan over git ls-files),
check_refs 0 dangling / 0 warning, check_routing coherent (53 symptom / 45 reference / 54 script rows),
check_budget 0 failure / 0 warning with the always-loaded body now measured in tokens rather than in
"narrative lines with the index excluded", check_commands 146 checked / 0 drift, pytest 608 passed.

Not done, and stated rather than implied: CI has never run on a runner; the evals have never been
executed; the 25 xfails are open; no device-side install/launch was performed in this pass.
2026-09-22 01:37:51 +08:00

7.2 KiB

A zero-event trace was manufactured by the harness, not by the target

Metadata

Field Value
Context instruction-level tracing with Frida Stalker on Android 11 / arm64-v8a, on system processes, to test whether Stalker.exclude() fixes the community-reported zero-event and crash behaviour
Cost three arms plus one harness correction; the two defects below both produce a believable zero-event trace, so they would have been read as a target finding
Outcome the crash half has a clean cause (exclude fixes it); the zero-event half does not, and is now an explicit warning in the script rather than a silent empty log
Evidence references/evidence-summary.md §The capability matrix; references/evidence-summary.md §The capability matrix (device attempts A and B)
Related references/native-dbi-and-deobfuscation.md; scripts/stalker_trace.js, scripts/stalker_report.py; pitfalls.md P26

Assertions and grade

# Assertion Grade Evidence
1 A follow with an empty exclusion list killed the target process; the same process survived an attach + resume with no follow at all observed three-arm run: baseline alive=True, control alive=False err=script has been destroyed, treatment alive=True
2 Excluding 20 system modules kept the process alive through the follow window observed treatment … DONE|reason=timeout with the process still running, EXCL|excluded=20/20 […] not-loaded=3
3 Exclusion did not restore event delivery observed treatment still reported blocks=0 blk=0 calls=0 truncated=0 after the full 6 s window
4 The zero-event warning added to the script fires correctly observed treatment emitted WARN|zero events 1500ms after follow … -- the pipeline is NOT proven; control could not, because its process was already gone
5 CONFIG.autoStart defaults to true, so loading the script starts a follow with default options before any runtime configuration can land observed the design section of references/evidence-summary.md §The capability matrix; the harness patches it to false and asserts the patch count
6 frida spawns a process suspended: without an explicit device.resume(pid) the followed thread executes nothing and the run reports zero events observed the same design section, and the DONE … blocks=0 transcript in references/evidence-summary.md §The capability matrix
7 Process.getMainThreadId() returned a tid that was not the pid on Android (26261 for a pid of 19938), so the script's default thread selection watched the wrong thread observed TRIG following tid=26261 (main-thread follow (pid=19938))
8 Re-running with the real main thread start(19938) produced the same zero observed DONE reason=timeout blocks=0 blk=0 calls=0
9 A hot-function trigger (malloc in libc.so) produced 201 complete follow cycles with zero blocks in any of them observed 609 log lines, 201 follows, 201 DONEs, nonzero_blk = 0
10 A per-call follow/unfollow cycle on that hot function crashed a system process observed once systemui pid 18030 → 26932, SIGSEGV with frame #00 in an anonymous executable region and the return path into libart
11 The crash is the explanation for the zero-event results observed negative — those runs produced their zeros before any death, on two other processes, and one never touched an export trigger the separation stated in the same native-DBI evidence file
12 Why events do not arrive unverified the measurement says it is not the exclusion list; it does not identify the cause

Execution chain (including the dead ends)

  1. The question was framed as a two-claim question, which is why it resolved: does exclude stop the crash, and does it restore events? Three arms, one device, one package, one module, one follow window, exactly one variable moving between them.
  2. The baseline arm exists purely to make a death attributable: without it, "following killed it" and "the app dies under frida anyway" are the same observation.
  3. Two harness defects had to be removed before the measurement meant anything, and both were found by noticing that an arm produced the expected result for the wrong reason:
    • CONFIG.autoStart: true followed once at load() time with default options, which is neither the control nor the treatment, and therefore contaminated both.
    • spawn leaves the process suspended, so with no explicit device.resume(pid) the followed thread ran nothing at all.
  4. Result: the crash half is answered with a cause, and the zero-event half is answered as a negative — exclusion is not its fix, and the community framing that lists "no events" among the symptoms exclude cures is contradicted by this measurement.
  5. Independent corroboration that the pipeline itself was healthy: the export-trigger run produced 201 complete follow cycles. A trigger that never fired would look different; a filter that dropped everything would look the same, which is exactly why the claim is limited to "events do not arrive" rather than "Stalker is broken".

Pits

Pit Cost What it was mistaken for
autoStart following at load() it invalidated two arms at once a legitimate treatment result
No device.resume() after spawn a whole arm's worth of zero events the target refusing instrumentation
Reading a zero-event log as "the code did not run" the reason the WARN|zero events line was added to the script this pass a finding about the target
Process.getMainThreadId() trusted as "the main thread" one wasted run a mis-selected thread
A system process died mid-run from the trigger pattern one attribution round an unrelated crash that could have been folded into the wrong conclusion

Reusable pattern

  • A negative result needs its own control arm. baseline (attach, resume, never follow) is what converts "it died" into "the follow killed it".
  • Engineer the harness's own failure modes out before measuring. Both defects here produced the exact shape under investigation; a run that reports the expected symptom is not yet a run that measured anything.
  • Make a dead pipeline announce itself. A warning line the moment the follow window ends with zero events turns an ambiguous empty log into a labelled observation — and it is testable, because the arm whose process died cannot print it.
  • Separate "the follow is unsafe" from "the follow observes nothing". Two arms, two conclusions, never one.

Write back to the repository

  • references/native-dbi-and-deobfuscation.md §6 — keep the two measured boundaries; add the harness-defect paragraph if absent, because a reader who reproduces a zero event will otherwise attribute it to the device.
  • scripts/stalker_trace.js — already carries the zero-event warning; ensure the autoStart comment says why the default is dangerous (it is a trap, not a convenience).
  • references/precedents/README.md — indexed as case 2; move assertion 12 to observed only if a future pass identifies the cause.