From cfd7b004c76ae014dac73f5c82df07489f7c04a0 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 17:42:47 -0300 Subject: [PATCH] fix(runtime): resolve fresh call arguments again after an explicit release Java registers an object argument it has not seen before for the call and passes its id with FreshArg set, so native wraps it from that id without asking Java again. JS that runs before the arguments are converted (the method lookup: a getter, a Proxy trap) can reach the same object another way, pass it to __releaseNativeCounterpart and let the wrapper be collected. The fast path then found no wrapper for the id and linked a new one to an id Java no longer knew, so the first use of the argument threw "No weak reference found". ObjectManager now counts explicit releases. A call records the count before any of its JS runs, and the fast path is taken only while it has not moved; otherwise the argument is resolved through Java as for any other object. --- .../app/tests/testReleaseNativeCounterpart.js | 21 +++++++++++++++++++ .../java/com/tns/tests/FreshArgCaller.java | 21 +++++++++++++++++++ .../runtime/src/main/cpp/ArgConverter.cpp | 3 +++ test-app/runtime/src/main/cpp/ArgConverter.h | 2 ++ .../runtime/src/main/cpp/ObjectManager.cpp | 1 + test-app/runtime/src/main/cpp/ObjectManager.h | 5 +++++ test-app/runtime/src/main/cpp/Runtime.cpp | 1 + 7 files changed, 54 insertions(+) create mode 100644 test-app/app/src/main/java/com/tns/tests/FreshArgCaller.java diff --git a/test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js b/test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js index 28b28dc15..fa02f6dc8 100644 --- a/test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js +++ b/test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js @@ -96,6 +96,27 @@ describe("Test native counterpart release", function () { expect(function () { global.__releaseNativeCounterpart(instance); }).toThrowError("Trying to release a non native object!"); }); + it("wraps a call argument whose link JS released and dropped during the method lookup", function () { + var Caller = com.tns.tests.FreshArgCaller; + var target = new Caller.Target({ take: function () { return "unreachable"; } }); + var lookups = 0; + Object.defineProperty(target, "take", { + configurable: true, + get: function () { + lookups++; + (function () { + global.__releaseNativeCounterpart(Caller.pending); + })(); + gc(); + return function (arg) { + return arg.toString(); + }; + } + }); + expect(Caller.callWithNewObject(target)).toBe("fresh"); + expect(lookups).toBe(1); + }); + it("Calling the __releaseNativeCounterpart function with 0 arguments should throw exception", function(){ var errorMessage = ""; diff --git a/test-app/app/src/main/java/com/tns/tests/FreshArgCaller.java b/test-app/app/src/main/java/com/tns/tests/FreshArgCaller.java new file mode 100644 index 000000000..90598eb29 --- /dev/null +++ b/test-app/app/src/main/java/com/tns/tests/FreshArgCaller.java @@ -0,0 +1,21 @@ +package com.tns.tests; + +// Passes JS an argument Java registers for that call only. The argument is also reachable +// through a field, which JS reads without a method call. +public class FreshArgCaller { + public interface Target { + String take(Object arg); + } + + public static Object pending; + + public static String callWithNewObject(Target target) { + StringBuilder arg = new StringBuilder("fresh"); + pending = arg; + try { + return target.take(arg); + } finally { + pending = null; + } + } +} diff --git a/test-app/runtime/src/main/cpp/ArgConverter.cpp b/test-app/runtime/src/main/cpp/ArgConverter.cpp index 20b8bf52a..e5d31e1d6 100644 --- a/test-app/runtime/src/main/cpp/ArgConverter.cpp +++ b/test-app/runtime/src/main/cpp/ArgConverter.cpp @@ -150,7 +150,10 @@ void ArgConverter::ConvertJavaArgsToJsArgs(Isolate* isolate, JNIEnv* env, const // hint Java read without the isolate lock, or -1) and the class index above. auto hintedObjectID = static_cast(bits); auto classIndex = static_cast((bits >> 32) & 0x7fffffff); + // JS run since the call started (the method lookup) may have reached the + // argument another way and released its id; it is then resolved again. if ((bits & JavaCallArgs::FreshArg) != 0 && + objectManager->ReleaseCount() == args.releasesAtStart && !objectManager->HasJsObject(hintedObjectID)) { JniLocalRef arg(env->GetObjectArrayElement(args.refs, i)); jsArg = objectManager->CreateJSWrapper(hintedObjectID, classIndex, arg); diff --git a/test-app/runtime/src/main/cpp/ArgConverter.h b/test-app/runtime/src/main/cpp/ArgConverter.h index 8714083e0..6da4ac2fd 100644 --- a/test-app/runtime/src/main/cpp/ArgConverter.h +++ b/test-app/runtime/src/main/cpp/ArgConverter.h @@ -26,6 +26,8 @@ struct JavaCallArgs { // Set once every argument has a JS value, so each object Java registered for this // call (FreshArg) has a wrapper and nothing is left to release if the call fails. mutable bool converted = false; + // ObjectManager::ReleaseCount() before any JS ran for this call. + mutable uint64_t releasesAtStart = 0; // Set in an object argument's packed value when Java registered the object for this // call; the low half is then its exact id and the next 31 bits its class index. diff --git a/test-app/runtime/src/main/cpp/ObjectManager.cpp b/test-app/runtime/src/main/cpp/ObjectManager.cpp index 71395e499..a8ceae9e5 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.cpp +++ b/test-app/runtime/src/main/cpp/ObjectManager.cpp @@ -901,6 +901,7 @@ void ObjectManager::ReleaseNativeCounterpart(v8::Local& object) { AssertIsolateLocked(); env.CallVoidMethod(m_javaRuntimeObject, RELEASE_NATIVE_INSTANCE_METHOD_ID, javaObjectID); + m_releaseCount++; // The registration outlives the link: it is dropped by the finalizer once // the wrapper is collected. diff --git a/test-app/runtime/src/main/cpp/ObjectManager.h b/test-app/runtime/src/main/cpp/ObjectManager.h index 469a18898..44c740acc 100644 --- a/test-app/runtime/src/main/cpp/ObjectManager.h +++ b/test-app/runtime/src/main/cpp/ObjectManager.h @@ -71,6 +71,10 @@ class ObjectManager { } } + // Ids JS released explicitly (ReleaseNativeCounterpart). While this does not move, an + // id Java registered for a call is still registered. + uint64_t ReleaseCount() const { return m_releaseCount; } + // Whether a wrapper was ever created for this id and not yet collected. bool HasJsObject(int javaObjectID) const { return m_idToObject.Find(javaObjectID) != nullptr; @@ -260,6 +264,7 @@ class ObjectManager { bool m_flushScheduled = false; // A flush failed; crossings leave the retry to the delayed task. bool m_flushBackingOff = false; + uint64_t m_releaseCount = 0; jweak GetJavaObjectByID(uint32_t javaObjectID); diff --git a/test-app/runtime/src/main/cpp/Runtime.cpp b/test-app/runtime/src/main/cpp/Runtime.cpp index 77e70b9d9..6698dc230 100644 --- a/test-app/runtime/src/main/cpp/Runtime.cpp +++ b/test-app/runtime/src/main/cpp/Runtime.cpp @@ -634,6 +634,7 @@ jobject Runtime::CallJSMethodNative(JNIEnv* _env, jobject obj, DEBUG_WRITE("CallJSMethodNative called javaObjectID=%d", javaObjectID); m_objectManager->FlushFinalizedBatchIfPending(); + args.releasesAtStart = m_objectManager->ReleaseCount(); // javaObjectID may be a hint read without the isolate lock; resolve it exactly when it // does not name a linked wrapper.