Skip to content

Commit cb23991

Browse files
oleavrhsorbo
andcommitted
android: Synchronize ArtMethod class field post GC
So our replacement ArtMethod instances don't go stale, and cause undefined behavior. Co-authored-by: Håvard Sørbø <havard@hsorbo.no>
1 parent 2cec495 commit cb23991

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

lib/android.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,23 @@ set_replacement_method (gpointer original_method,
18661866
g_mutex_unlock (&lock);
18671867
}
18681868
1869+
void
1870+
on_gc_performed (void)
1871+
{
1872+
GHashTableIter iter;
1873+
gpointer hooked_method, replacement_method;
1874+
1875+
g_mutex_lock (&lock);
1876+
1877+
g_hash_table_iter_init (&iter, methods);
1878+
while (g_hash_table_iter_next (&iter, &hooked_method, &replacement_method))
1879+
{
1880+
*((uint32_t *) replacement_method) = *((uint32_t *) hooked_method);
1881+
}
1882+
1883+
g_mutex_unlock (&lock);
1884+
}
1885+
18691886
void
18701887
on_fixups_applied (guint quick_code_offset,
18711888
void * nterp_entrypoint,
@@ -2045,6 +2062,7 @@ on_leave_gc_concurrent_copying_copying_phase (GumInvocationContext * ic)
20452062
isReplacement: new NativeFunction(cm.is_replacement_method, 'bool', ['pointer'], fastOptions),
20462063
get: new NativeFunction(cm.get_replacement_method, 'pointer', ['pointer'], fastOptions),
20472064
set: new NativeFunction(cm.set_replacement_method, 'void', ['pointer', 'pointer'], fastOptions),
2065+
onGcPerformed: new NativeFunction(cm.on_gc_performed, 'void', [], fastOptions),
20482066
onFixupsApplied: new NativeFunction(cm.on_fixups_applied, 'void', ['uint', 'pointer', 'pointer'], fastOptions),
20492067
delete: new NativeFunction(cm.delete_replacement_method, 'void', ['pointer'], fastOptions),
20502068
translate: new NativeFunction(cm.translate_method, 'pointer', ['pointer'], fastOptions),
@@ -2079,6 +2097,7 @@ function ensureArtKnowsHowToHandleMethodInstrumentation (vm) {
20792097

20802098
instrumentArtQuickEntrypoints(vm);
20812099
instrumentArtMethodInvocationFromInterpreter();
2100+
instrumentArtGarbageCollection();
20822101
instrumentArtFixupStaticTrampolines();
20832102
}
20842103

@@ -2131,6 +2150,16 @@ function instrumentArtMethodInvocationFromInterpreter () {
21312150
}
21322151
}
21332152

2153+
function instrumentArtGarbageCollection () {
2154+
const gc = getApi().module.findSymbolByName("_ZN3art2gc4Heap22CollectGarbageInternalENS0_9collector6GcTypeENS0_7GcCauseEbj");
2155+
if (gc === null)
2156+
return;
2157+
2158+
Interceptor.attach(gc, {
2159+
onLeave: artController.replacedMethods.onGcPerformed,
2160+
});
2161+
}
2162+
21342163
function instrumentArtFixupStaticTrampolines () {
21352164
const patterns = [
21362165
['_ZN3art11ClassLinker26VisiblyInitializedCallback22MarkVisiblyInitializedEPNS_6ThreadE', 'e90340f8 : ff0ff0ff'],

0 commit comments

Comments
 (0)