Skip to content

Commit 1fe902b

Browse files
committed
fix(DexFactory): only collect when the collector may be mark-compact
The collection before registering an injected proxy only matters under ART's concurrent mark-compact collector. ProxyRegistrationGc skips it when the process's heap mappings positively identify the concurrent-copying collector ("concurrent copying" / "(region space)" names, no mark-compact names) and below API 31, where ART can't have been updated to a version with mark-compact. Anything it can't identify, including renamed mappings in future ART builds or kernels without anonymous VMA names, still collects. The decision is made once per process, on the first injection. On the API 33 emulator (concurrent copying) the suite's explicit GCs drop from 134 to the tests' own 20; on API 36 (mark-compact) they stay at 134.
1 parent 3d60dfa commit 1fe902b

4 files changed

Lines changed: 161 additions & 4 deletions

File tree

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ require("./tests/testTypedArrayArgs");
7171
require("./tests/dex-interface-implementation");
7272
require("./tests/testClassForNameDiscovery");
7373
require("./tests/testPrebuiltExtendClasses");
74+
require("./tests/testProxyRegistrationGc");
7475
require("./tests/testInterfaceImplementation");
7576
require("./tests/testPrimitiveCallbackReturns");
7677
require("./tests/testRuntimeImplementedAPIs");
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
describe("Proxy registration GC guard", function () {
2+
var Guard = com.tns.ProxyRegistrationGc;
3+
4+
function maps(names) {
5+
var text = names.map(function (name, i) {
6+
return "7f0000" + i + "000-7f0000" + i + "fff rw-p 00000000 00:00 0 " + name;
7+
}).join("\n");
8+
return new java.io.BufferedReader(new java.io.StringReader(text));
9+
}
10+
11+
var markCompact = [
12+
"[anon:dalvik-main space]",
13+
"[anon:dalvik-Concurrent mark-compact compaction buffers]",
14+
"[anon:dalvik-Concurrent Mark Compact live words bitmap]",
15+
"/apex/com.android.art/lib64/libart.so"
16+
];
17+
var concurrentCopying = [
18+
"[anon:dalvik-main space (region space)]",
19+
"[anon:dalvik-concurrent copying gc mark stack]",
20+
"[anon:dalvik-region space live bitmap]",
21+
"/apex/com.android.art/lib64/libart.so"
22+
];
23+
24+
it("collects under the mark-compact collector", function () {
25+
expect(Guard.isRequired(36, maps(markCompact))).toBe(true);
26+
});
27+
28+
it("skips the collection under the concurrent-copying collector", function () {
29+
expect(Guard.isRequired(33, maps(concurrentCopying))).toBe(false);
30+
});
31+
32+
it("never collects before ART became updatable", function () {
33+
expect(Guard.isRequired(30, maps(markCompact))).toBe(false);
34+
expect(Guard.isRequired(21, maps([]))).toBe(false);
35+
});
36+
37+
it("collects when the heap mappings are unnamed", function () {
38+
expect(Guard.isRequired(33, maps(["/apex/com.android.art/lib64/libart.so", "[stack]"]))).toBe(true);
39+
});
40+
41+
it("collects when no known collector is recognized", function () {
42+
expect(Guard.isRequired(36, maps(["[anon:dalvik-main space]", "[anon:dalvik-card table]"]))).toBe(true);
43+
});
44+
45+
it("collects when mark-compact and region space names both appear", function () {
46+
expect(Guard.isRequired(36, maps(concurrentCopying.concat(markCompact)))).toBe(true);
47+
});
48+
49+
it("matches the collector this process is running", function () {
50+
var reader = new java.io.BufferedReader(new java.io.FileReader("/proc/self/maps"));
51+
var sawMarkCompact = false;
52+
var sawConcurrentCopying = false;
53+
try {
54+
var line;
55+
while ((line = reader.readLine()) !== null) {
56+
var lower = String(line).toLowerCase();
57+
if (lower.indexOf("[anon:dalvik-") < 0) {
58+
continue;
59+
}
60+
if (lower.indexOf("mark-compact") >= 0 || lower.indexOf("mark compact") >= 0) {
61+
sawMarkCompact = true;
62+
}
63+
if (lower.indexOf("concurrent copying") >= 0 || lower.indexOf("(region space)") >= 0) {
64+
sawConcurrentCopying = true;
65+
}
66+
}
67+
} finally {
68+
reader.close();
69+
}
70+
71+
var sdk = android.os.Build.VERSION.SDK_INT;
72+
if (sdk < 31) {
73+
expect(Guard.isRequired()).toBe(false);
74+
} else if (sawMarkCompact) {
75+
expect(Guard.isRequired()).toBe(true);
76+
} else if (sawConcurrentCopying) {
77+
expect(Guard.isRequired()).toBe(false);
78+
} else {
79+
expect(Guard.isRequired()).toBe(true);
80+
}
81+
});
82+
});

‎test-app/runtime/src/main/java/com/tns/DexFactory.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,7 @@ public Class<?> resolveClass(String baseClassName, String name, String className
178178
String classNameToLoad = isInterface ? fullClassName : desiredDexClassName;
179179

180180
boolean injectIntoParent = injectIntoParentClassLoader && classLoader instanceof BaseDexClassLoader;
181-
if (injectIntoParent) {
182-
// ART's mark-compact GC can lose a DexCache field store (SIGSEGV in
183-
// ClassLinker::RegisterDexFileLocked) when a collection overlaps
184-
// registering a dex with this long-lived loader.
181+
if (injectIntoParent && ProxyRegistrationGc.isRequired()) {
185182
// The dex is registered by the first loadClass below, so the blocking
186183
// collection has to stay immediately before it.
187184
java.lang.Runtime.getRuntime().gc();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.tns;
2+
3+
import android.os.Build;
4+
5+
import java.io.BufferedReader;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.util.Locale;
9+
10+
/**
11+
* Whether DexFactory has to run a blocking collection before it registers a
12+
* generated dex with the app's long-lived class loader.
13+
*
14+
* ART's concurrent mark-compact collector can lose the DexCache location store
15+
* when a collection overlaps ClassLinker::RegisterDexFile, and the app then dies
16+
* with a SIGSEGV in RegisterDexFileLocked. That collector only reaches devices
17+
* through the ART module, which is updatable from API 31.
18+
*
19+
* The collection is skipped only when the process's heap mappings positively
20+
* identify the concurrent-copying collector. A collector that can't be
21+
* identified, including future ART builds with renamed mappings, is treated as
22+
* affected.
23+
*/
24+
public final class ProxyRegistrationGc {
25+
private static final int FIRST_UPDATABLE_ART_SDK = 31;
26+
27+
private static volatile Boolean required;
28+
29+
private ProxyRegistrationGc() {
30+
}
31+
32+
public static boolean isRequired() {
33+
Boolean result = required;
34+
if (result == null) {
35+
result = detect();
36+
required = result;
37+
}
38+
return result;
39+
}
40+
41+
private static boolean detect() {
42+
if (Build.VERSION.SDK_INT < FIRST_UPDATABLE_ART_SDK) {
43+
return false;
44+
}
45+
try (BufferedReader maps = new BufferedReader(new FileReader("/proc/self/maps"))) {
46+
return isRequired(Build.VERSION.SDK_INT, maps);
47+
} catch (IOException | RuntimeException e) {
48+
return true;
49+
}
50+
}
51+
52+
/**
53+
* @param maps the contents of /proc/self/maps; ART names its heap mappings
54+
* "[anon:dalvik-...]" on kernels that support anonymous VMA names
55+
*/
56+
public static boolean isRequired(int sdkInt, BufferedReader maps) throws IOException {
57+
if (sdkInt < FIRST_UPDATABLE_ART_SDK) {
58+
return false;
59+
}
60+
boolean concurrentCopying = false;
61+
String line;
62+
while ((line = maps.readLine()) != null) {
63+
int nameStart = line.indexOf("[anon:dalvik-");
64+
if (nameStart < 0) {
65+
continue;
66+
}
67+
String name = line.substring(nameStart).toLowerCase(Locale.ROOT);
68+
if (name.contains("mark-compact") || name.contains("mark compact")) {
69+
return true;
70+
}
71+
if (name.contains("concurrent copying") || name.contains("(region space)")) {
72+
concurrentCopying = true;
73+
}
74+
}
75+
return !concurrentCopying;
76+
}
77+
}

0 commit comments

Comments
 (0)