8000 android: Share in-process helper between sessions by kkuehl · Pull Request #1299 · frida/frida-core · GitHub
[go: up one dir, main page]

Skip to content

android: Share in-process helper between sessions - #1299

Merged
oleavr merged 1 commit into
frida:mainfrom
kkuehl:fix/android-helper-vm-reuse
Nov 23, 2026
Merged

oleavr merged 1 commit into
frida:mainfrom
kkuehl:fix/android-helper-vm-reuse

Conversation

@kkuehl
@kkuehl kkuehl commented Nov 21, 2026 •
Copy link
Copy Markdown
Contributor

On Android the local backend runs its helper in a Java VM that it creates inside the host process. AndroidHelperService creates the VM when a LinuxHostSession first needs it, and destroys it with DestroyJavaVM() when the session closes. A process that closes its local session and later opens another therefore creates a second VM, which ART does not support, and it crashes while that VM initializes:

Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)
#00 libart.so       art::InvokeWithVarArgs<_jmethodID*>
#01 libart.so       art::JNI<false>::NewObjectV
#02 libjavacore.so  _JNIEnv::NewObject
#03 libjavacore.so  Linux_getpwuid
...
#08 core-oj.jar     java.lang.System.initUnchangeableSystemProperties
#10 core-oj.jar     java.lang.System.<clinit>

frida-server never sees this, as it keeps one session for its whole life. An embedder does: creating a DeviceManager, listing processes on the local device, closing the manager, and doing the same again is enough. I found it through frida-rust, whose tests each create their own manager.

The change

  • One helper per process. The helper (its thread, the VM and the HelperBackend) is created once and shared by every session.
  • Sessions still release it. When the last one has, the helper's thread leaves its main loop and blocks reading a pipe whose file descriptors are static, so that nothing of it depends on GLib or Gum once frida_deinit() is done. The session that closes last waits for that without blocking its thread.
  • A failed start can be retried. The next session tries again, reusing the VM if it was already created. To make that safe, HelperBackend prepares the main Looper and creates the system context once and remembers the outcome, so that constructing it again only repeats its lookups. If that one-time step itself fails, the failure is remembered, as it cannot be run twice.
  • The VM is shut down at exit, from an atexit() handler registered right after creating it, so that it runs before the static destructors of the libraries loaded until then. Keeping the VM alive otherwise lets ART's daemon threads race with those destructors: an earlier version of this change without the handler aborted at exit in 12 of 50 runs under load, with FORTIFY: pthread_mutex_lock called on a destroyed mutex.

Testing

Small C programs against the devkit, run as root on an arm64 device running Android 17. Listing processes with scope full fails there for an unrelated reason that #1298 fixes, so everything below ran on this change together with #1298.

  • Two rounds in one process, each creating a manager, listing with FRIDA_SCOPE_FULL and closing it: both list, and ART starts once. On main the second round is the crash above.
  • Two managers open at once both list.
  • Retry: with a failure injected before the VM exists, after it exists, and in the backend's constructor, the first round fails and the second lists, with ART started once.
  • A start cancelled anywhere from 0 to 2000 ms in leaves the helper usable, and another manager is delayed by 0 to 40 ms meanwhile.
  • After frida_shutdown() and frida_deinit(), interrupting every thread's blocking call repeatedly does not crash; the helper's thread is in pipe_read.
  • Through frida-rust: two full-scope listings in one process, 31 runs, all clean.

Exit, counted as aborts in the device's crash log. The exit status was 0 in every row but the last.

Scenario This change main
Return from main after one round 0/50 0/50
After two rounds 0/50 crashes in round two
After frida_shutdown() and frida_deinit() 0/50
With a session still open 0/50
exit() from another thread 0/50
Each of the above under load (four icon listings first) 0/50 each 0/50 for one round
exit() from another thread while the helper is starting or in a request, 48 runs over six delays 9/48 36/48

What is left

The last row. An exit() from another thread while the helper's thread is running Java code can still abort, as it can on main, where it does so more often. Exit handlers run newest first, so whatever a library registers after ours, such as a static first used during a request, is destroyed before the VM is. Closing that completely would take hooking exit(), or registering again after every request; I did neither, and would rather hear what you think is right.

Notes

  • helper.dex is rebuilt with the Makefile, on top of android: Omit user when uid has no passwd entry #1298, using d8 9.0.3-dev from build-tools 36.1.0, the android-36 jar (ANDROID_API_LEVEL=36), and JDK 25 — the same setup that produced the dex now on main. Rebuilding this PR's original source with it gives bytecode identical to the dex originally committed here, differing only in the D8 version marker.
  • The helper's thread is detached after the VM is created and attached again as a daemon, so that DestroyJavaVM() does not wait for it. It shows as frida-android-helper instead of ART's main.
  • Libraries loaded after the handler is registered have their destructors run before it. On this device those are four Conscrypt libraries, whose only such destructor is libc++'s iostream initializer.
  • If the process already has a VM for some other reason, this still stops at the existing assert, as before.
  • SigchainCompat.disable() was only used when closing the helper, so it is removed.

@kkuehl
kkuehl marked this pull request as draft November 21, 2026 17:56
@kkuehl
kkuehl force-pushed the fix/android-helper-vm-reuse branch from 58f756d to 1b83dea Compare November 21, 2026 22:48
@kkuehl
kkuehl marked this pull request as ready for review November 21, 2026 22:49
ART cannot create a second VM in the same process, so closing the local
session and opening another crashed while the new VM initialized.

Start the helper once per process and share it between sessions. While
unused, its thread leaves its main loop and blocks on a pipe, so it no
longer depends on GLib once frida_deinit() is done. A failed start is
retried by the next session, so HelperBackend now creates the system
context only once.

Destroy the VM from an atexit() handler, before the static destructors
of the libraries its daemon threads use. Reattach the helper's thread
as a daemon so DestroyJavaVM() does not wait for it.

Drop SigchainCompat.disable(), which is now unused.
@oleavr
oleavr force-pushed the fix/android-helper-vm-reuse branch from 1b83dea to 54ff6d5 Compare November 23, 2026 15:13
@oleavr
oleavr merged commit 9ca5973 into frida:main Nov 23, 2026
32 of 36 checks passed
@oleavr
oleavr commented Nov 23, 2026
Copy link
Copy Markdown
Member

Thanks! 🙌

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.

2 participants

0