Skip to content

Commit 3d60dfa

Browse files
committed
fix(DexFactory): collect before registering a proxy with the app class loader
ART's mark-compact GC can lose the store of DexCache::location_ when a collection overlaps ClassLinker::RegisterDexFile. RegisterDexFileLocked then dereferences the null location and the app dies with SIGSEGV (fault addr 0x8). Registering into the app's long-lived PathClassLoader, as DexFactory does for main-runtime proxies since #1968, is the case that reproduces it; a fresh DexClassLoader per jar never crashed. A blocking Runtime.gc() right before the injected dex is registered leaves no collection in flight. In a stress harness and a standalone repro (no NativeScript code) it took the crash rate from 22-73% of runs to 0 when the registering thread is the main allocator. It still crashed once when another thread allocated non-stop, so it narrows the window rather than closing it. Cost is one explicit GC (about 4-14 ms on test-suite heaps) on the first use of each runtime-generated proxy.
1 parent 8fe217a commit 3d60dfa

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,17 @@ public Class<?> resolveClass(String baseClassName, String name, String className
177177
Class<?> result = null;
178178
String classNameToLoad = isInterface ? fullClassName : desiredDexClassName;
179179

180-
if (injectIntoParentClassLoader && classLoader instanceof BaseDexClassLoader
181-
&& injectDexIntoClassLoader((BaseDexClassLoader) classLoader, jarFilePath)) {
180+
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.
185+
// The dex is registered by the first loadClass below, so the blocking
186+
// collection has to stay immediately before it.
187+
java.lang.Runtime.getRuntime().gc();
188+
}
189+
190+
if (injectIntoParent && injectDexIntoClassLoader((BaseDexClassLoader) classLoader, jarFilePath)) {
182191
try {
183192
result = classLoader.loadClass(classNameToLoad);
184193
} catch (ClassNotFoundException e) {

0 commit comments

Comments
 (0)