Repository navigation
fix: prevent crash when jweak points to null - #1881
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds cache validation functionality to the LRUCache implementation to check for stale weak global references in the ObjectManager. It introduces a callback mechanism to validate cached entries before they are used, preventing the use of invalidated Java weak references.
Key changes:
- Added a validation callback parameter to LRUCache constructor to check if cached entries are still valid
- Implemented validation logic that evicts invalid cache entries before returning them
- Switched from std::unordered_map to robin_hood::unordered_map for improved performance
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test-app/runtime/src/main/cpp/ObjectManager.h | Added declaration for ValidateWeakGlobalRefCallback method |
| test-app/runtime/src/main/cpp/ObjectManager.cpp | Implemented validation callback and updated cache initialization to use it; removed unused variable |
| test-app/runtime/src/main/cpp/LRUCache.h | Added validation callback support, implemented evictKey helper method, and switched to robin_hood hash map |
| test-app/runtime/src/main/cpp/JEnv.h | Added isSameObject helper method for JNI object comparison |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
NathanWalker
approved these changes
Nov 5, 2025
edusperoni
added a commit
that referenced
this pull request
Nov 5, 2025
edusperoni
added a commit
that referenced
this pull request
Nov 22, 2026
### Description Four changes to the JS→Java call path that cost something on every object argument, receiver lookup, overloaded call or extended-class construction. One commit each. #### 1. Skip cast and null markers when an argument is a Java wrapper Converting an object argument read two private symbols from it: the numeric cast marker, then the null-node marker after the Java lookup. Each read also walked to the object's creation context. Both markers are only ever put on plain objects: the `long()`/`byte()`/… cast objects (`Object::New`), `LongNumber` instances (no internal fields) and the `.null` sentinel (`Object::New`). So an argument with the runtime's internal fields (a Java wrapper or an extended-class instance) now goes straight to the Java lookup. Objects without internal fields keep the full path: TypeScript subclass instances, typed arrays, plain objects and Proxies. The same skip applies to the overload signature key (`MethodCache::AppendType`) and to the argument arrays built for overload resolution. The `Local<Private>` overloads of `V8GetPrivateValue`/`V8SetPrivateValue` now use the current context instead of the creation context. A private-symbol access is an own lookup that skips interceptors, proxy traps and access checks (`LookupIterator::ComputeConfiguration` → `OWN_SKIP_INTERCEPTOR`, and private names bypass `ACCESS_CHECK`). It runs no JS and does not depend on the context. The creation context remains the fallback when no context is entered. #### 2. Keep parsed signatures on resolved method cache entries Constructors with arguments and overloaded methods resolve through `MethodCache`. Each such call parsed the JNI signature into a `vector<string>` before converting its arguments. The tokens are now parsed once, stored on the `CacheMethodInfo` before it is published, and immutable afterwards, so no locking changes. Independent of #2137, which touches the signature-key scratch in the same file; the two merge cleanly. #### 3. Cache the resolved class of a JS-extended class Every `new` of a JS-extended class looked its generated class up in the process-wide class cache, taking its shared lock and hashing the full class name. The extended class's per-isolate callback data now keeps the global ref once resolved. The class cache never drops or replaces an entry, so the ref stays valid. Plain Java class construction is unchanged. #### 4. Skip the jweak check while Java holds the object strongly Each LRU cache hit in `ObjectManager::GetJavaObjectByJsObject` ran `ValidateWeakGlobalRefCallback`, which calls `IsSameObject(jweak, NULL)`: a JNI call with a thread-state transition. That cost lands on every instance call (receiver) and every object argument. It is now skipped while the wrapper's link has `javaWeakened == false`. Wrappers whose Java side was weakened keep the check. **Why the check exists (#1881).** #1881 added the validator because cached jweaks read null while the object "exists". That PR had no repro. Reading the code at the time shows two causes, neither an ART bug: - **Id rebinding during construction.** `Runtime.initInstance` took `currentObjectId` without clearing it or checking the class, and the id stayed published for the whole Java constructor. A binding instance that Java created inside a JS-driven constructor (a custom view inflated by it, an instance made by reflection) registered under the outer object's id. A lookup in that window cached a jweak to the nested instance. When the outer registration replaced it and the nested instance was collected, the jweak read null while the table held the live outer object. Re-fetching then "fixed" it. #2099 later gated the id by class and made it single-use. - **A weak Java side behind a live wrapper.** The old finalizer weakened the Java side synchronously and resurrected the wrapper. A wrapper handed back to JS later was never re-strengthened, so the jweak could be legitimately cleared while JS held the wrapper. For these the validator turns a null into a "cleared object reference" error. **Invariant on main.** If a linked state has `!javaWeakened`, Java's `ObjectTable` holds that id's object strongly, and that object is the one the cached jweak names: - `ObjectTable` is weakened only by `processFinalizedBatch` → `weakenAndCheckAlive`. - A wrapper reacquired while queued is dropped from the batch. A wrapper reacquired after weakening keeps `javaWeakened`. - Each `remove()` path either clears `linked` or deletes the state. - Ids are never reused, and every runtime has its own table and cache. Two cases broke the invariant and are closed in the same commit: - **A finalizer batch that failed part way.** Example: an OOM allocating a `WeakRef` could leave objects weak in Java with `javaWeakened` still false. The flag is now set before the Java call. - **Same-class nested construction, still possible on main.** Java can create an instance of exactly the class under construction during the outer constructor's `super()`, before the outer's `initInstance`. That instance takes the reserved id until `registerConstructedInstance` replaces it, and a jweak cached in that window names it. On main, calls on the constructed object reach the nested instance until it is collected, and then the validator re-fetches. `RegisterInstance` now drops the cached entry after registration. The new `SelfNesting` spec covers this. With B1 but without that eviction, the spec aborts with `JNI DETECTED ERROR IN APPLICATION: can't call int com.tns.tests.SelfNesting.getTag() on null object`. ### Measurements Release APKs (`-Poptimized -Prelease`, arm64) run on a phone through the #2092 harness (`debug.ns.jnibench`). The labels are cumulative: - base is `main` (56c7b0b); - then +B2, +B3, +B4 and +B1, one commit each. Every round reinstalls and speed-compiles each label. The first launch after an install is discarded and only the relaunch is measured. Each round runs base, +B2, +B3, +B4, +B1, then base2, which is the base APK again (A/A). 6 rounds. ns per call: median over launches of each launch's median (min..max over launches). | row | base | base2 (A/A) | +B2 | +B3 | +B4 | +B1 | A/A | B2 | B3 | B4 | B1 | total | |---|---|---|---|---|---|---|---|---|---|---|---|---| | instance.noop() | 229 (229..238) | 230 (228..231) | 230 (230..232) | 231 (229..232) | 231 (230..235) | 197 (194..200) | +0.1% | +0.4% | +0.1% | +0.1% | -14.6% | -14.1% | | instance.takeObject(Object) | 487 (482..490) | 487 (481..490) | 372 (368..374) | 368 (365..370) | 373 (369..374) | 306 (305..310) | -0.1% | -23.7% | -1.1% | +1.5% | -18.0% | -37.2% | | instance.takeObjects(Object x3) | 1046 (1035..1050) | 1048 (1043..1058) | 669 (665..700) | 663 (658..668) | 675 (668..684) | 548 (545..550) | +0.1% | -36.0% | -0.9% | +1.7% | -18.7% | -47.6% | | overloaded static Math.max(int,int) | 663 (659..666) | 667 (658..675) | 679 (662..692) | 551 (548..560) | 544 (539..547) | 537 (536..542) | +0.6% | +2.4% | -18.9% | -1.3% | -1.2% | -19.0% | | new Object.extend({})() | 3700 (3551..3901) | 3788 (3414..3856) | 3638 (3550..3752) | 3777 (3596..4015) | 3573 (3339..3675) | 3619 (3370..3717) | +2.4% | -1.7% | +3.8% | -5.4% | +1.3% | -2.2% | | static.noop() | 89 (88..89) | 89 (89..92) | 90 (89..90) | 88 (88..89) | 88 (88..89) | 89 (89..90) | +0.8% | +1.4% | -1.9% | +0.2% | +0.7% | +0.3% | Rows that are bimodal per launch on this phone. No deltas are given because the A/A pair alone differs by more than any step: | row | base | base2 (A/A) | +B1 (all four) | A/A gap | |---|---|---|---|---| | new java.lang.Object() | 1598 (1413..2782) | 1816 (1526..2112) | 1771 (1407..2855) | +13.6%: inconclusive | | new java.lang.StringBuilder(String) | 2047 (1886..6246) | 2868 (2403..4278) | 3162 (1760..3808) | +40.1%: inconclusive | | overloaded static String.valueOf(Object) | 2378 (2362..4163) | 3165 (2349..3913) | 2042 (1798..2231) | +33.1%: inconclusive | | field.get int | 158 (157..257) | 207 (157..257) | 140 (121..198) | +31.5%: inconclusive | - **Stable rows:** the A/A gap is at most 2.4%, and every label's launches fall in a narrow band. - B2 cuts `takeObject` by 24% and `takeObjects` by 36%. - B3 cuts the overloaded `Math.max` call by 19%. - B1 cuts `instance.noop` by 15% and the object-argument rows by 18-19%. - **B4:** `new Object.extend({})()` moves -5.4% at the B4 step, against a 2.4% A/A gap and ±4% at the other steps. That is suggestive, not conclusive. - **Bimodal rows:** each launch of these lands in one of two clusters. On `new java.lang.StringBuilder(String)` the clusters sit near 1.8-2.6 µs and 3.1-4.3 µs: - base had 1 of 6 launches in the slow cluster; - base2, the same APK, had 3 of 6; - +B1 had 4 of 6, and its fast launches (1760 and 1784 ns) are the fastest of any label. The medians therefore reflect which cluster each launch fell into, not the code. ### Related Pull Requests - #1881 added the jweak check that item 4 now skips for strongly held objects. - #2099 fixed the construction id reuse that caused most of #1881's null jweaks. - #2137 (open) touches `MethodCache.cpp` and `CallbackHandlers.cpp` too. This branch merges with it cleanly. ### Does your pull request have unit tests? Yes, see Testing. ### Testing - New `testObjectArgumentPath.js`: - wrapper, extended-class and TypeScript-subclass instances as object arguments, repeated; - a released wrapper as an argument; - `long`/`char`/`double`/`float` casts, typed nulls and wrappers through a warm overload cache; - a cast through a single-signature method; - constructor overloads on a warm cache; - repeated and interleaved extended-class construction, with and without arguments; - a wrapper reacquired after its Java side was weakened and then collected, which must throw rather than pass null; - a reachable wrapper after warm instance calls and a collection; - the same-class nested construction above, with a new `SelfNesting` fixture. - Full suite on an arm64 API 36.1 emulator, rebased on 56c7b0b: **1799 specs, 0 failures** with `enableMultithreadedJavascript` on, and **1799, 0 failures** with it off.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Occasionally (mostly on android API 26.1+) a jweak reference might point to NULL even when the actual Java object exists.
I believe this is a JNI/Android bug, but as a workaround, we now check if this reference is null, and if so we evict the key from the cache which then is going to re-fetch it and error out if it's actually null in Java