Repository navigation
android: Share in-process helper between sessions - #1299
Merged
Merged
Conversation
kkuehl
marked this pull request as draft
November 21, 2026 17:56
kkuehl
force-pushed
the
fix/android-helper-vm-reuse
branch
from
November 21, 2026 22:48
58f756d to
1b83dea
Compare
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
force-pushed
the
fix/android-helper-vm-reuse
branch
from
November 23, 2026 15:13
1b83dea to
54ff6d5
Compare
Member
|
Thanks! 🙌 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Android the local backend runs its helper in a Java VM that it creates inside the host process.
AndroidHelperServicecreates the VM when aLinuxHostSessionfirst needs it, and destroys it withDestroyJavaVM()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: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
HelperBackend) is created once and shared by every session.frida_deinit()is done. The session that closes last waits for that without blocking its thread.HelperBackendprepares 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.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, withFORTIFY: 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
fullfails there for an unrelated reason that #1298 fixes, so everything below ran on this change together with #1298.FRIDA_SCOPE_FULLand closing it: both list, and ART starts once. On main the second round is the crash above.frida_shutdown()andfrida_deinit(), interrupting every thread's blocking call repeatedly does not crash; the helper's thread is inpipe_read.Exit, counted as aborts in the device's crash log. The exit status was 0 in every row but the last.
mainafter one roundfrida_shutdown()andfrida_deinit()exit()from another threadexit()from another thread while the helper is starting or in a request, 48 runs over six delaysWhat 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 hookingexit(), or registering again after every request; I did neither, and would rather hear what you think is right.Notes
helper.dexis 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.DestroyJavaVM()does not wait for it. It shows asfrida-android-helperinstead of ART'smain.SigchainCompat.disable()was only used when closing the helper, so it is removed.