Title: fix(DexFactory): GC before registering injected proxies by edusperoni · Pull Request #2110 · NativeScript/android · GitHub
Open Graph Title: fix(DexFactory): GC before registering injected proxies by edusperoni · Pull Request #2110 · NativeScript/android
X Title: fix(DexFactory): GC before registering injected proxies by edusperoni · Pull Request #2110 · NativeScript/android
Description: Description The crash. Apps intermittently die with a native crash when the main runtime generates a proxy class at runtime, i.e. an extend() the static binding generator didn't see: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x8 #00 libart.so art::mirror::String::ToModifiedUtf8()+16 #01 libart.so art::ClassLinker::RegisterDexFileLocked(...)+148 #02 libart.so art::ClassLinker::RegisterDexFile(...) #03 libart.so art::ClassLinker::DefineClass(...) #04 libart.so art::ClassLinker::FindClassInBaseDexClassLoader(...) #05 libart.so VMClassLoader_findLoadedClass java.lang.ClassLoader.loadClass ← com.tns.DexFactory.resolveClass ← MetadataNode::ExtendMethodCallback It shows up in our test suite on the API 36 emulator, a few times a day across runs. Cause: an ART GC bug. ClassLinker::AllocDexCache stores the dex location into the new DexCache, then RegisterDexFile waits in a ScopedGCCriticalSection for an in-flight collection. After that wait, DexCache::location_ reads back as null, while class_loader_ and dex_file_ (stored after the wait) are intact. RegisterDexFileLocked then dereferences the null location. The tombstone heap dumps show this directly, and in some crashes the location String is still on the heap right after the DexCache. What we found out: It reproduces in a plain Android app with no NativeScript code. It happens with ART's mark-compact collector (CMC), in generational mode (the default on this image) and, less often, in non-generational mode. It needs the dex to be registered with a long-lived class loader, which is what DexFactory does for main-runtime proxies since #1968 (addDexPath into the app's PathClassLoader, so Class.forName finds them). With a fresh DexClassLoader per jar it never crashed, but that would lose Class.forName visibility. It needs a burst of reference-heavy allocation right before registration. Proxy generation (ASMDEX) is exactly that. We're filing it with the ART team separately, with the standalone repro. The workaround. A blocking Runtime.getRuntime().gc() immediately before the injected dex is registered, so no collection is in flight when RegisterDexFile runs. Results: Setup Without With gc() Stress harness in the test app, realistic allocation 37/100 runs crashed 0/40 Stress harness, adversarial background allocator, n=800 7/30 0/20 Standalone repro app (no NativeScript), generational CMC 29/40 0/30 Standalone repro app, non-generational CMC 9/40 0/20 Stress harness, adversarial background allocator, n=3000 3/6 1/4 It isn't a guarantee. When another thread allocates non-stop, a background GC queued behind the explicit one can start right after it. That still cut crashes about tenfold (1 crash in ~27k registrations). Cost. One explicit GC, about 4–14 ms on test-suite-sized heaps and more on large heaps, on the first use of each runtime-generated proxy in a process. Since #2101 the prebuilt classes are found, so runtime generation only happens for extends the generator can't see: rare in production builds, but every extend in HMR dev mode. The full test suite ran in 116 s with the change, against 117–121 s without. Only where the collector may be mark-compact. ProxyRegistrationGc decides once per process, on the first injection, whether the collection is needed: Below API 31: skipped. ART only became updatable through Play system updates in Android 12, so these devices can't be running mark-compact. API 31+: skipped only when /proc/self/maps positively identifies the concurrent-copying collector. That means [anon:dalvik-...] mappings containing concurrent copying or (region space), and none containing mark-compact / Mark Compact. Both sets of names come from ART's collector setup; I checked them on API 33 (ART module 331711080) and API 36 (361153460) processes. Anything else collects: mark-compact (generational or not), an unrecognized collector, renamed mappings in a future ART, kernels without anonymous VMA names, or an unreadable maps file. Being wrong in that direction costs a few milliseconds; being wrong the other way would mean a crash. Does your commit message include the wording below to reference a specific issue in this repo? No issue. Related Pull Requests #1951 / #1968 added the injection into the app's PathClassLoader. #2101 made prebuilt classes load for anonymous extends, which keeps runtime generation rare. Does your pull request have unit tests? Yes, for the guard. testProxyRegistrationGc.js checks the decision on synthetic maps: mark-compact collects concurrent copying skips below API 31 skips unnamed mappings collect an unknown collector collects mixed names collect the live check: the decision matches the collector the test process is actually running The GC race itself needs hundreds of registrations under allocation pressure to show up, so it's covered by the stress numbers above rather than a suite spec. ./gradlew runtestsAndVerifyResults -Pabis=arm64-v8a, full suite: Emulator Collector Specs Failures Explicit GCs logged by the test app API 36 (ART 361153460) mark-compact 1552 0 134 API 33 (ART 331711080) concurrent copying 1552 0 20 (the tests' own System.gc() calls) Summary by CodeRabbit Bug Fixes Improved runtime reliability when loading generated classes in supported Android configurations by performing garbage collection before class-loader injection when required. Existing isolated class loading remains available when injection conditions are not met.
Open Graph Description: Description The crash. Apps intermittently die with a native crash when the main runtime generates a proxy class at runtime, i.e. an extend() the static binding generator didn't see: signal 11 ...
X Description: Description The crash. Apps intermittently die with a native crash when the main runtime generates a proxy class at runtime, i.e. an extend() the static binding generator didn't see: signal...
Opengraph URL: https://github.com/NativeScript/android/pull/2110
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:2ebdc4b8-9069-a872-1476-ba0fb11528cd |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 93AC:23CF06:2099D4A:298B7B8:6B015F7D |
| html-safe-nonce | 23883e180f57f3dce624e6b9484bc222983c4f0bb6515608e76ab07a7e661efc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0FDOjIzQ0YwNjoyMDk5RDRBOjI5OEI3Qjg6NkIwMTVGN0QiLCJ2aXNpdG9yX2lkIjoiNjMxNjA0NTkzMDgxMDQwMDYzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 518d4e7dbbd95ba2f7424e436cd3a9828adf2d6ad156119e9e6a590c3467a905 |
| hovercard-subject-tag | pull_request:5280852217 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/android/pull/2110/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | Description The crash. Apps intermittently die with a native crash when the main runtime generates a proxy class at runtime, i.e. an extend() the static binding generator didn't see: signal 11 ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/android git https://github.com/NativeScript/android.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 29137950 |
| octolytics-dimension-repository_nwo | NativeScript/android |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 29137950 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/android |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width