From aa423b1f1b30a4c14718edd5b99449dc891efd41 Mon Sep 17 00:00:00 2001 From: LanceaKing Date: Mon, 16 Nov 2026 22:14:53 +0100 Subject: [PATCH 1/2] android: Revert inline patches with ART suspended 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. --- lib/android.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/android.js b/lib/android.js index 70b99f1..24a6995 100644 --- a/lib/android.js +++ b/lib/android.js @@ -3194,6 +3194,15 @@ class Backtrace { } export function revertGlobalPatches () { + const hasInlinePatches = artQuickInterceptors.length !== 0 || inlineHooks.length !== 0; + if (hasInlinePatches) { + withAllArtThreadsSuspended(revertGlobalPatchesNow); + } else { + revertGlobalPatchesNow(); + } +} + +function revertGlobalPatchesNow () { patchedClasses.forEach(entry => { entry.vtablePtr.writePointer(entry.vtable); entry.vtableCountPtr.writeS32(entry.vtableCount); From 56490068a700266925c1632e99940368d8572157 Mon Sep 17 00:00:00 2001 From: LanceaKing Date: Mon, 16 Nov 2026 22:15:36 +0100 Subject: [PATCH 2/2] android: Drain replacements before disposing 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. --- index.js | 36 +++++++++++++++++++++++++ lib/android.js | 63 ++++++++++++++++++++++++++++++++++++++++++++ lib/class-factory.js | 11 ++++++++ lib/env.js | 10 +------ lib/vm.js | 36 +++++++++++++++++++++++-- 5 files changed, 145 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 46dbaee..bc35ce1 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ import getApi from './lib/api.js'; import { getAndroidVersion, + hasActiveReplacementFrames, withAllArtThreadsSuspended, withRunnableArtThread, makeArtClassVisitor, @@ -93,6 +94,41 @@ class Runtime { return; } + if (this.api.flavor === 'art') { + this._disposeOnceReplacementsDrain(); + } else { + this._disposeNow(); + } + } + + _disposeOnceReplacementsDrain () { + const { vm } = this; + + Script.pin(); + vm.deferHandleReleases(); + + const replacements = vm.perform(() => ClassFactory._revertAll()); + const replacementMethodIds = new Set(replacements.map(r => r._m.replacementMethodId.toString())); + + const disposeWhenDrained = () => { + const replacementsActive = replacementMethodIds.size !== 0 && + (vm.perform(env => hasActiveReplacementFrames(replacementMethodIds, env)) || + replacements.some(r => r._c.size !== 0)); + if (replacementsActive) { + setTimeout(disposeWhenDrained, 10); + return; + } + + vm.perform(env => { + vm.releaseDeferredHandles(env); + }); + this._disposeNow(); + Script.unpin(); + }; + setImmediate(disposeWhenDrained); + } + + _disposeNow () { const { vm } = this; vm.perform(env => { ClassFactory._disposeAll(env); diff --git a/lib/android.js b/lib/android.js index 24a6995..ee22bd6 100644 --- a/lib/android.js +++ b/lib/android.js @@ -219,6 +219,9 @@ function _getApi () { }, _ZN3art10ThreadList9ResumeAllEv: ['art::ThreadList::ResumeAll', 'void', ['pointer']], + _ZN3art10ThreadList7ForEachEPFvPNS_6ThreadEPvES3_: ['art::ThreadList::ForEach', 'void', ['pointer', 'pointer', 'pointer']], + _ZN3art5Mutex13ExclusiveLockEPNS_6ThreadE: ['art::Mutex::ExclusiveLock', 'void', ['pointer', 'pointer']], + _ZN3art5Mutex15ExclusiveUnlockEPNS_6ThreadE: ['art::Mutex::ExclusiveUnlock', 'void', ['pointer', 'pointer']], // Android >= 7 _ZN3art11ClassLinker12VisitClassesEPNS_12ClassVisitorE: ['art::ClassLinker::VisitClasses', 'void', ['pointer', 'pointer']], @@ -332,6 +335,9 @@ function _getApi () { }, _ZN3art15gUseUserfaultfdE: function (address) { this.isUserfaultfdGcActive = !!address.readU8(); + }, + _ZN3art5Locks17thread_list_lock_E: function (address) { + this.artThreadListLock = address; } }, optionals: new Set([ @@ -372,6 +378,10 @@ function _getApi () { '_ZN3art31InternalDebuggerControlCallback13StartDebuggerEv', '_ZN3art3Dbg15gDebuggerActiveE', '_ZN3art15gUseUserfaultfdE', + '_ZN3art10ThreadList7ForEachEPFvPNS_6ThreadEPvES3_', + '_ZN3art5Mutex13ExclusiveLockEPNS_6ThreadE', + '_ZN3art5Mutex15ExclusiveUnlockEPNS_6ThreadE', + '_ZN3art5Locks17thread_list_lock_E', '_ZN3art15instrumentation15Instrumentation20EnableDeoptimizationEv', '_ZN3art15instrumentation15Instrumentation20DeoptimizeEverythingEPKc', '_ZN3art15instrumentation15Instrumentation20DeoptimizeEverythingEv', @@ -1730,6 +1740,59 @@ export class ArtMethod { } } +export function hasActiveReplacementFrames (replacementMethodIds, env) { + let found = false; + + withAllArtThreadsSuspended(() => { + forEachArtThread(env, thread => { + if (found) { + return; + } + + const finder = new ReplacementFrameFinder(thread, replacementMethodIds); + finder.walkStack(); + found = finder.found; + }); + }); + + return found; +} + +function forEachArtThread (env, fn) { + const api = getApi(); + + const self = getArtThreadFromEnv(env); + const threadListLock = api.artThreadListLock.readPointer(); + const visit = new NativeCallback(thread => { + fn(thread); + }, 'void', ['pointer', 'pointer']); + + api['art::Mutex::ExclusiveLock'](threadListLock, self); + try { + api['art::ThreadList::ForEach'](api.artThreadList, visit, NULL); + } finally { + api['art::Mutex::ExclusiveUnlock'](threadListLock, self); + } +} + +class ReplacementFrameFinder extends ArtStackVisitor { + constructor (thread, replacementMethodIds) { + super(thread, NULL, 'skip-inlined-frames'); + + this.found = false; + this._replacementMethodIds = replacementMethodIds; + } + + visitFrame () { + const method = this.getMethod(); + if (method !== null && this._replacementMethodIds.has(method.handle.toString())) { + this.found = true; + return false; + } + return true; + } +} + function makeArtQuickFrameInfoGetter (impl) { return function (self) { const result = Memory.alloc(12); diff --git a/lib/class-factory.js b/lib/class-factory.js index 29c0dd0..29a0005 100644 --- a/lib/class-factory.js +++ b/lib/class-factory.js @@ -69,6 +69,17 @@ export default class ClassFactory { } } + static _revertAll () { + const replacements = []; + for (const factory of factoryCache.factories) { + for (const method of Array.from(factory._patchedMethods)) { + replacements.push(method._r); + method.implementation = null; + } + } + return replacements; + } + static _disposeAll (env) { factoryCache.factories.forEach(factory => { factory._dispose(env); diff --git a/lib/env.js b/lib/env.js index b8a5a7d..9d26f10 100644 --- a/lib/env.js +++ b/lib/env.js @@ -229,19 +229,11 @@ Env.prototype.throwIfExceptionPending = function () { const error = new Error(descriptionStr); error.$h = handle; - Script.bindWeak(error, makeErrorHandleDestructor(this.vm, handle)); + Script.bindWeak(error, this.vm.makeHandleDestructor(handle)); throw error; }; -function makeErrorHandleDestructor (vm, handle) { - return function () { - vm.perform(env => { - env.deleteGlobalRef(handle); - }); - }; -} - Env.prototype.fromReflectedMethod = proxy(7, 'pointer', ['pointer', 'pointer'], function (impl, method) { return impl(this.handle, method); }); diff --git a/lib/vm.js b/lib/vm.js index 4bac9b9..b1dab89 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -123,14 +123,46 @@ export default function VM (api) { return envBuf.readPointer(); }; + let deferredHandles = null; + this.makeHandleDestructor = function (handle) { + const release = () => { + if (deferredHandles !== null) { + deferredHandles.push(handle); + } else { + this.perform(env => { + env.deleteGlobalRef(handle); + }); + } + }; + + if (api.flavor !== 'art') { + return release; + } + return () => { - this.perform(env => { - env.deleteGlobalRef(handle); + Script.pin(); + Script.nextTick(() => { + try { + release(); + } finally { + Script.unpin(); + } }); }; }; + this.deferHandleReleases = function () { + deferredHandles = []; + }; + + this.releaseDeferredHandles = function (env) { + for (const handle of deferredHandles) { + env.deleteGlobalRef(handle); + } + deferredHandles = null; + }; + this.link = function (tid, env) { const entry = activeEnvs.get(tid); if (entry === undefined) {