Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getApi from './lib/api.js';
import {
getAndroidVersion,
hasActiveReplacementFrames,
withAllArtThreadsSuspended,
withRunnableArtThread,
makeArtClassVisitor,
Expand Down Expand Up @@ -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);
Expand Down
72 changes: 72 additions & 0 deletions lib/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -372,6 +378,10 @@ function _getApi () {
'_ZN3art31InternalDebuggerControlCallback13StartDebuggerEv',
'_ZN3art3Dbg15gDebuggerActiveE',
'_ZN3art15gUseUserfaultfdE',
'_ZN3art10ThreadList7ForEachEPFvPNS_6ThreadEPvES3_',
'_ZN3art5Mutex13ExclusiveLockEPNS_6ThreadE',
'_ZN3art5Mutex15ExclusiveUnlockEPNS_6ThreadE',
'_ZN3art5Locks17thread_list_lock_E',
'_ZN3art15instrumentation15Instrumentation20EnableDeoptimizationEv',
'_ZN3art15instrumentation15Instrumentation20DeoptimizeEverythingEPKc',
'_ZN3art15instrumentation15Instrumentation20DeoptimizeEverythingEv',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -3194,6 +3257,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);
Expand Down
11 changes: 11 additions & 0 deletions lib/class-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 1 addition & 9 deletions lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
36 changes: 34 additions & 2 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down