Skip to content

android: Fix crashes when unloading active Java hooks - #415

Open
LanceaKing wants to merge 2 commits into
frida-packages:mainfrom
LanceaKing:fix/7.0.13-art-teardown
Open

LanceaKing wants to merge 2 commits into
frida-packages:mainfrom
LanceaKing:fix/7.0.13-art-teardown

Conversation

@LanceaKing

Copy link
Copy Markdown

Unloading a script while Java hooks are running can free callbacks, cloned ArtMethods, and JNI handles before those calls finish. This can crash the app during or after unload.

Restore the original methods first, then keep those resources alive until active callbacks and ART frames finish. Release JNI handles before detaching from the VM. Suspend ART threads while restoring inline patches.

On Android 13 arm64, 50 concurrent unloads passed with four Java threads and object-return hooks. Another 30 attach/detach sessions, 22 spawns, and two canceled spawns also passed. No new crashes. These tests used official Python bindings and a patched Frida 17.26.2 server. Other Android versions and architectures remain untested. npm run lint and node --check index.js also pass.

Related: frida/frida#545 and #370.

@LanceaKing
LanceaKing marked this pull request as ready for review November 11, 2026 08:49
The ART quick entrypoint and inline hooks span multiple instructions,
so a thread running through one while it is being restored could
execute a mix of old and new code.
Unloading while hooked methods were running freed their NativeCallbacks,
cloned ArtMethods, and JNI handles out from under them. Revert all
replacements first, and only dispose once no thread is inside one, or
has one on its stack. Until then, defer releasing JNI handles, as
unload runs every weak callback regardless of reachability.
@oleavr
oleavr force-pushed the fix/7.0.13-art-teardown branch from 54d0f88 to 5649006 Compare November 16, 2026 21:17
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