Skip to content

Commit cfd7b00

Browse files
committed
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.
1 parent d468a3e commit cfd7b00

7 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎test-app/app/src/main/assets/app/tests/testReleaseNativeCounterpart.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ describe("Test native counterpart release", function () {
9696
expect(function () { global.__releaseNativeCounterpart(instance); }).toThrowError("Trying to release a non native object!");
9797
});
9898

99+
it("wraps a call argument whose link JS released and dropped during the method lookup", function () {
100+
var Caller = com.tns.tests.FreshArgCaller;
101+
var target = new Caller.Target({ take: function () { return "unreachable"; } });
102+
var lookups = 0;
103+
Object.defineProperty(target, "take", {
104+
configurable: true,
105+
get: function () {
106+
lookups++;
107+
(function () {
108+
global.__releaseNativeCounterpart(Caller.pending);
109+
})();
110+
gc();
111+
return function (arg) {
112+
return arg.toString();
113+
};
114+
}
115+
});
116+
expect(Caller.callWithNewObject(target)).toBe("fresh");
117+
expect(lookups).toBe(1);
118+
});
119+
99120
it("Calling the __releaseNativeCounterpart function with 0 arguments should throw exception", function(){
100121
var errorMessage = "";
101122

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.tns.tests;
2+
3+
// Passes JS an argument Java registers for that call only. The argument is also reachable
4+
// through a field, which JS reads without a method call.
5+
public class FreshArgCaller {
6+
public interface Target {
7+
String take(Object arg);
8+
}
9+
10+
public static Object pending;
11+
12+
public static String callWithNewObject(Target target) {
13+
StringBuilder arg = new StringBuilder("fresh");
14+
pending = arg;
15+
try {
16+
return target.take(arg);
17+
} finally {
18+
pending = null;
19+
}
20+
}
21+
}

‎test-app/runtime/src/main/cpp/ArgConverter.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ void ArgConverter::ConvertJavaArgsToJsArgs(Isolate* isolate, JNIEnv* env, const
150150
// hint Java read without the isolate lock, or -1) and the class index above.
151151
auto hintedObjectID = static_cast<jint>(bits);
152152
auto classIndex = static_cast<int>((bits >> 32) & 0x7fffffff);
153+
// JS run since the call started (the method lookup) may have reached the
154+
// argument another way and released its id; it is then resolved again.
153155
if ((bits & JavaCallArgs::FreshArg) != 0 &&
156+
objectManager->ReleaseCount() == args.releasesAtStart &&
154157
!objectManager->HasJsObject(hintedObjectID)) {
155158
JniLocalRef arg(env->GetObjectArrayElement(args.refs, i));
156159
jsArg = objectManager->CreateJSWrapper(hintedObjectID, classIndex, arg);

‎test-app/runtime/src/main/cpp/ArgConverter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ struct JavaCallArgs {
2626
// Set once every argument has a JS value, so each object Java registered for this
2727
// call (FreshArg) has a wrapper and nothing is left to release if the call fails.
2828
mutable bool converted = false;
29+
// ObjectManager::ReleaseCount() before any JS ran for this call.
30+
mutable uint64_t releasesAtStart = 0;
2931

3032
// Set in an object argument's packed value when Java registered the object for this
3133
// call; the low half is then its exact id and the next 31 bits its class index.

‎test-app/runtime/src/main/cpp/ObjectManager.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ void ObjectManager::ReleaseNativeCounterpart(v8::Local<v8::Object>& object) {
901901
AssertIsolateLocked();
902902
env.CallVoidMethod(m_javaRuntimeObject, RELEASE_NATIVE_INSTANCE_METHOD_ID,
903903
javaObjectID);
904+
m_releaseCount++;
904905

905906
// The registration outlives the link: it is dropped by the finalizer once
906907
// the wrapper is collected.

‎test-app/runtime/src/main/cpp/ObjectManager.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class ObjectManager {
7171
}
7272
}
7373

74+
// Ids JS released explicitly (ReleaseNativeCounterpart). While this does not move, an
75+
// id Java registered for a call is still registered.
76+
uint64_t ReleaseCount() const { return m_releaseCount; }
77+
7478
// Whether a wrapper was ever created for this id and not yet collected.
7579
bool HasJsObject(int javaObjectID) const {
7680
return m_idToObject.Find(javaObjectID) != nullptr;
@@ -260,6 +264,7 @@ class ObjectManager {
260264
bool m_flushScheduled = false;
261265
// A flush failed; crossings leave the retry to the delayed task.
262266
bool m_flushBackingOff = false;
267+
uint64_t m_releaseCount = 0;
263268

264269
jweak GetJavaObjectByID(uint32_t javaObjectID);
265270

‎test-app/runtime/src/main/cpp/Runtime.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ jobject Runtime::CallJSMethodNative(JNIEnv* _env, jobject obj,
634634
DEBUG_WRITE("CallJSMethodNative called javaObjectID=%d", javaObjectID);
635635

636636
m_objectManager->FlushFinalizedBatchIfPending();
637+
args.releasesAtStart = m_objectManager->ReleaseCount();
637638

638639
// javaObjectID may be a hint read without the isolate lock; resolve it exactly when it
639640
// does not name a linked wrapper.

0 commit comments

Comments
 (0)