Skip to content

Commit 350d48a

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 350d48a

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

lib/android.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,19 +1867,23 @@ set_replacement_method (gpointer original_method,
18671867
}
18681868
18691869
void
1870-
on_fixups_applied (guint quick_code_offset,
1871-
void * nterp_entrypoint,
1872-
void * quick_to_interpreter_bridge)
1870+
synchronize_replacement_methods (guint quick_code_offset,
1871+
void * nterp_entrypoint,
1872+
void * quick_to_interpreter_bridge)
18731873
{
18741874
GHashTableIter iter;
1875-
gpointer hooked_method;
1875+
gpointer hooked_method, replacement_method;
18761876
18771877
g_mutex_lock (&lock);
18781878
18791879
g_hash_table_iter_init (&iter, methods);
1880-
while (g_hash_table_iter_next (&iter, &hooked_method, NULL))
1880+
while (g_hash_table_iter_next (&iter, &hooked_method, &replacement_method))
18811881
{
1882-
void ** quick_code = hooked_method + quick_code_offset;
1882+
void ** quick_code;
1883+
1884+
*((uint32_t *) replacement_method) = *((uint32_t *) hooked_method);
1885+
1886+
quick_code = hooked_method + quick_code_offset;
18831887
if (*quick_code == nterp_entrypoint)
18841888
*quick_code = quick_to_interpreter_bridge;
18851889
}
@@ -2045,7 +2049,7 @@ on_leave_gc_concurrent_copying_copying_phase (GumInvocationContext * ic)
20452049
isReplacement: new NativeFunction(cm.is_replacement_method, 'bool', ['pointer'], fastOptions),
20462050
get: new NativeFunction(cm.get_replacement_method, 'pointer', ['pointer'], fastOptions),
20472051
set: new NativeFunction(cm.set_replacement_method, 'void', ['pointer', 'pointer'], fastOptions),
2048-
onFixupsApplied: new NativeFunction(cm.on_fixups_applied, 'void', ['uint', 'pointer', 'pointer'], fastOptions),
2052+
synchronize: new NativeFunction(cm.synchronize_replacement_methods, 'void', ['uint', 'pointer', 'pointer'], fastOptions),
20492053
delete: new NativeFunction(cm.delete_replacement_method, 'void', ['pointer'], fastOptions),
20502054
translate: new NativeFunction(cm.translate_method, 'pointer', ['pointer'], fastOptions),
20512055
findReplacementFromQuickCode: cm.find_replacement_method_from_quick_code
@@ -2079,6 +2083,7 @@ function ensureArtKnowsHowToHandleMethodInstrumentation (vm) {
20792083

20802084
instrumentArtQuickEntrypoints(vm);
20812085
instrumentArtMethodInvocationFromInterpreter();
2086+
instrumentArtGarbageCollection();
20822087
instrumentArtFixupStaticTrampolines();
20832088
}
20842089

@@ -2131,6 +2136,23 @@ function instrumentArtMethodInvocationFromInterpreter () {
21312136
}
21322137
}
21332138

2139+
function instrumentArtGarbageCollection () {
2140+
const api = getApi();
2141+
const art = api.module;
2142+
2143+
const gc = art.findSymbolByName("_ZN3art2gc4Heap22CollectGarbageInternalENS0_9collector6GcTypeENS0_7GcCauseEbj");
2144+
if (gc === null)
2145+
return;
2146+
2147+
const { artNterpEntryPoint, artQuickToInterpreterBridge } = api;
2148+
const quickCodeOffset = getArtMethodSpec(api.vm).offset.quickCode;
2149+
Interceptor.attach(gc, {
2150+
onLeave() {
2151+
artController.replacedMethods.synchronize(quickCodeOffset, artNterpEntryPoint, artQuickToInterpreterBridge);
2152+
}
2153+
});
2154+
}
2155+
21342156
function instrumentArtFixupStaticTrampolines () {
21352157
const patterns = [
21362158
['_ZN3art11ClassLinker26VisiblyInitializedCallback22MarkVisiblyInitializedEPNS_6ThreadE', 'e90340f8 : ff0ff0ff'],
@@ -2152,7 +2174,7 @@ function instrumentArtFixupStaticTrampolines () {
21522174
const { artNterpEntryPoint, artQuickToInterpreterBridge } = api;
21532175
const quickCodeOffset = getArtMethodSpec(api.vm).offset.quickCode;
21542176
Interceptor.attach(matches[0].address, function () {
2155-
artController.replacedMethods.onFixupsApplied(quickCodeOffset, artNterpEntryPoint, artQuickToInterpreterBridge);
2177+
artController.replacedMethods.synchronize(quickCodeOffset, artNterpEntryPoint, artQuickToInterpreterBridge);
21562178
});
21572179

21582180
return;

0 commit comments

Comments
 (0)