Skip to content

Commit 3b9dc89

Browse files
committed
feat(runtime): plain canaries keep their JS properties in the state only
Under the canary wrapper lifetime mode with scope "all", wrappers of plain (not JS-extended) objects now carry a non-masking forwarding handler set (getter, setter, query, deleter, enumerator, definer and descriptor) instead of the masking mirror. Their JS properties live in the object's state object only, so a new wrapper for the object needs no copy, and native calls and native property stores never reach the handler. JS-extended instances keep the masking mirror, and allowShadowingNativeMembers keeps plain wrappers on it too. Names the prototype chain has never reach a non-masking handler: an instance assignment or Object.defineProperty over an inherited data property (a native method, toString, valueOf, constructor, a member JS added to a prototype) stays on that wrapper and is gone with it, deleting such a name deletes nothing, and a state property whose name the chain gains later is hidden. A state accessor reached through another receiver (a Proxy, Reflect with a receiver, an inheriting object) runs with the wrapper as `this` and its setter is skipped. These are documented limitations. The setter honours read-only properties and getter-only accessors in the state, which v8::Object::Set would report as written. A record that dies under a live forwarding canary (objc_removeAssociatedObjects) keeps its state while the canary holds it, the leak probe still sees it, and the canary's next write moves the state object, weak-collection key included, into a new record; it is let go once the canary is collected or released. A released forwarding canary gets the state's properties, accessors and attributes included, copied onto it. Release simulator build, plain NSObject, forwarding vs masking: expando read 64 vs 19 ns, miss 47 vs 20, `in` 111 vs 21, expando rewrite 81 vs 105, first write 544 vs 676, UIView tag store 112 vs 219, rebuilding a wrapper with five properties 903 vs 2496 (925 without state).
1 parent 21a009d commit 3b9dc89

9 files changed

Lines changed: 749 additions & 46 deletions

File tree

‎NativeScript/runtime/ArgConverter.mm‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,10 +1341,10 @@ bool IsKVOSubclassOf(Class klass, Class base) {
13411341
// wrapper is neither cached nor owning, and the caller detaches it.
13421342
wrapper->MarkBorrowed();
13431343
// Its writes reach the object's JS state through the canary handlers.
1344-
Local<Object> receiver = NewEmptyInstance(
1345-
context, EmptyInstanceFunction(isolate, WrapperLifetimeCanary::KindOf(self)));
1344+
CanaryKind kind = WrapperLifetimeCanary::KindOf(self);
1345+
Local<Object> receiver = NewEmptyInstance(context, EmptyInstanceFunction(isolate, kind));
13461346
CreateJsWrapper(context, wrapper, receiver);
1347-
WrapperLifetimeCanary::Hydrate(isolate, receiver);
1347+
WrapperLifetimeCanary::Hydrate(isolate, receiver, kind);
13481348
*borrowed = wrapper;
13491349
return receiver;
13501350
}

‎NativeScript/runtime/ObjectManager.mm‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,9 @@ void DisposeHandle(v8::Isolate* isolate,
856856
WrapperLifetime::Forget(isolate, adopted);
857857
objcWrapper->SetLifetimeState(nullptr);
858858
UnlinkRegistered(adopted);
859+
std::shared_ptr<CanaryRecord> record = std::move(adopted->canaryRecord_);
859860
delete adopted;
861+
WrapperLifetimeCanary::DropRecord(isolate, std::move(record));
860862
} else if (it != cache->Instances.end()) {
861863
std::shared_ptr<Persistent<Value>> handle = it->second;
862864
bool wasWeak = handle->IsWeak();

‎NativeScript/runtime/WrapperLifetime.mm‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ void PrologueCallback(Isolate* isolate, GCType type, GCCallbackFlags) {
276276
}
277277
}
278278

279-
void ReleaseQueued(const std::shared_ptr<Caches>& cache, ObjectWeakCallbackState* state) {
279+
void ReleaseQueued(Isolate* isolate, const std::shared_ptr<Caches>& cache,
280+
ObjectWeakCallbackState* state) {
280281
id target = state->lifetimeTarget_;
281282
// A wrapper built for the object after this one died owns the entry now.
282283
auto it = cache->Instances.find(target);
@@ -285,7 +286,9 @@ void ReleaseQueued(const std::shared_ptr<Caches>& cache, ObjectWeakCallbackState
285286
}
286287
delete state->lifetimeWrapper_;
287288
std::atomic<uint64_t>* counter = state->runtimeRetains_;
289+
std::shared_ptr<CanaryRecord> record = std::move(state->canaryRecord_);
288290
delete state;
291+
WrapperLifetimeCanary::DropRecord(isolate, std::move(record));
289292
DropRuntimeRetain(target, counter);
290293
[target release];
291294
}
@@ -313,7 +316,7 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {
313316
if (rooted->releases.empty()) {
314317
WrapperLifetime::pendingReleaseIsolates_.fetch_sub(1, std::memory_order_relaxed);
315318
}
316-
ReleaseQueued(cache, state);
319+
ReleaseQueued(isolate, cache, state);
317320
}
318321
}
319322

@@ -380,7 +383,7 @@ void ReleaseAll(Isolate* isolate, RootedState* rooted) {
380383
if (state->lifetimeCanary_) {
381384
state->canaryRecord_ = WrapperLifetimeCanary::Find(isolate, target);
382385
if (state->canaryRecord_ != nullptr) {
383-
WrapperLifetimeCanary::Hydrate(isolate, handle->Get(isolate).As<Object>());
386+
WrapperLifetimeCanary::Hydrate(isolate, handle->Get(isolate).As<Object>(), kind);
384387
}
385388
}
386389
MakeWeak(state);

‎NativeScript/runtime/WrapperLifetimeCanary.h‎

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,29 @@ enum class CanaryKind : uint8_t {
2828
// The canary wrapper lifetime mode (WrapperLifetimeMode::kCanary).
2929
//
3030
// An ObjC object's wrapper is a canary: a phantom-weak JS object that dies
31-
// with its JS references, releasing the runtime's retain on the object. A
32-
// masking named interceptor with only a setter, deleter and definer mirrors
33-
// every JS property written to a canary into a state object and then declines,
34-
// so the property also lands on the canary and reads never leave V8. Writes to
35-
// native properties and other inherited accessors are not state. A canary
36-
// built later for the same object gets the state's properties copied back
37-
// (Hydrate). The state object is the source of truth.
31+
// with its JS references, releasing the runtime's retain on the object. Its JS
32+
// properties live in a state object that outlives it, which is the source of
33+
// truth. Writes to native properties and other inherited accessors are not
34+
// state. Two handler sets keep the state:
35+
//
36+
// - Mirroring (kExtended canaries, and kPlain ones when the app config allows
37+
// shadowing native members): a masking named interceptor with only a
38+
// setter, deleter and definer mirrors every JS property written to the
39+
// canary into the state and then declines, so the property also lands on
40+
// the canary and reads never leave V8. A canary built later for the same
41+
// object gets the state's properties copied back (Hydrate). Masking, so it
42+
// also sees an instance write over a name its prototype chain has.
43+
// - Forwarding (kPlain canaries under scope "all" otherwise): a non-masking
44+
// interceptor keeps the properties in the state only, and every access to
45+
// them goes through it; a canary built later needs no copy. V8 calls a
46+
// non-masking interceptor only for names absent from the object and its
47+
// prototype chain, so names the chain has never reach it: an instance write
48+
// over an inherited data property (a native method, toString, constructor,
49+
// a member JS added to a prototype), and Object.defineProperty of such a
50+
// name, land on the canary and die with it; deleting such a name deletes
51+
// nothing. Native members cannot be overridden per instance anyway: native
52+
// code sends messages, which never read JS own properties of a plain
53+
// object.
3854
//
3955
// The state object is rooted from its first write until the native object is
4056
// deallocated. A JS-extended class's own -dealloc reports its instances; for
@@ -97,18 +113,31 @@ class WrapperLifetimeCanary {
97113
// it, and copies the object's weak-collection key onto the canary, so what
98114
// was keyed on the object is still found through the released canary. With
99115
// no key to copy, the canary is marked so that weak collections report it as
100-
// a released-object access.
116+
// a released-object access. A forwarding canary also gets the state's
117+
// properties copied onto it: once released, its handlers no longer reach the
118+
// state.
101119
static void KeepWeakCollectionKey(
102120
v8::Isolate* isolate, v8::Local<v8::Object> canary,
103121
const std::shared_ptr<CanaryRecord>& record);
104122

123+
// Takes a collected or released canary's reference to its record. A record
124+
// that died under that canary kept its state for it (it is the forwarding
125+
// canary's only copy), which can go now. Isolate thread.
126+
static void DropRecord(v8::Isolate* isolate,
127+
std::shared_ptr<CanaryRecord> record);
128+
105129
// The record of an object that already has state in this isolate, for a new
106130
// canary to adopt; it is no longer released. Null otherwise.
107131
static std::shared_ptr<CanaryRecord> Find(v8::Isolate* isolate, id object);
108132

133+
// Whether `kind`'s canaries carry the forwarding handlers.
134+
static bool Forwards(CanaryKind kind);
135+
109136
// Copies the state of a new canary's object, or of a -dealloc receiver's,
110-
// onto that wrapper. No-op without state, or unless the mode is canary.
111-
static void Hydrate(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
137+
// onto that wrapper of kind `kind`. No-op without state, for a forwarding
138+
// kind, or unless the mode is canary.
139+
static void Hydrate(v8::Isolate* isolate, v8::Local<v8::Object> wrapper,
140+
CanaryKind kind);
112141

113142
// Teardown: lets go of every state object while the isolate is alive. Tokens
114143
// that outlive the isolate find the registry closed.

0 commit comments

Comments
 (0)