Skip to content

Commit

Permalink
android: Handle GC cycle handlers not being exported (#330)
Browse files Browse the repository at this point in the history
Which happens on some custom ROMs. This does however mean that our hooks
become unreliable on such systems, since we fail to tie into the GC
cycle. We should consider scanning libart's memory as a fallback.
  • Loading branch information
thinhbuzz authored Aug 30, 2024
1 parent b0baa48 commit 5fcd910
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -1894,21 +1894,27 @@ function ensureArtKnowsHowToHandleReplacementMethods (vm) {
const apiLevel = getAndroidApiLevel();

const mayUseCollector = (apiLevel > 28)
? new NativeFunction(Module.getExportByName('libart.so', '_ZNK3art2gc4Heap15MayUseCollectorENS0_13CollectorTypeE'), 'bool', ['pointer', 'int'])
? (type) => {
const impl = Module.findExportByName('libart.so', '_ZNK3art2gc4Heap15MayUseCollectorENS0_13CollectorTypeE');
if (impl === null) {
return false;
}
return new NativeFunction(impl, 'bool', ['pointer', 'int'])(getApi().artHeap, type);
}
: () => false;
const kCollectorTypeCMC = 3;

if (mayUseCollector(getApi().artHeap, kCollectorTypeCMC)) {
if (mayUseCollector(kCollectorTypeCMC)) {
Interceptor.attach(Module.getExportByName('libart.so', '_ZN3art6Thread15RunFlipFunctionEPS0_b'), artController.hooks.Gc.runFlip);
} else {
let exportName = null;
let copyingPhase = null;
if (apiLevel > 28) {
exportName = '_ZN3art2gc9collector17ConcurrentCopying12CopyingPhaseEv';
copyingPhase = Module.findExportByName('libart.so', '_ZN3art2gc9collector17ConcurrentCopying12CopyingPhaseEv');
} else if (apiLevel > 22) {
exportName = '_ZN3art2gc9collector17ConcurrentCopying12MarkingPhaseEv';
copyingPhase = Module.findExportByName('libart.so', '_ZN3art2gc9collector17ConcurrentCopying12MarkingPhaseEv');
}
if (exportName !== null) {
Interceptor.attach(Module.getExportByName('libart.so', exportName), artController.hooks.Gc.copyingPhase);
if (copyingPhase !== null) {
Interceptor.attach(copyingPhase, artController.hooks.Gc.copyingPhase);
}
}
}
Expand Down

0 comments on commit 5fcd910

Please sign in to comment.