diff --git a/NativeScript/runtime/ObjectManager.h b/NativeScript/runtime/ObjectManager.h index beadf1186..7fbb8f52f 100644 --- a/NativeScript/runtime/ObjectManager.h +++ b/NativeScript/runtime/ObjectManager.h @@ -73,13 +73,16 @@ class ObjectManager { // getWrapperLifetimeStats(). struct LifetimeStats { // An ObjC wrapper's finalizer refused disposal because native code still - // retains its object (IsGcProtected), keeping the wrapper alive. + // retains its object (IsGcProtected, or an OwnershipState wrapper whose + // object is natively owned), keeping the wrapper alive. uint64_t resurrections = 0; // An ObjC wrapper's finalizer released its native object. uint64_t collected = 0; // __releaseNativeCounterpart released a native object. uint64_t releasedExplicitly = 0; - // JS handed a released wrapper to native code or used it as a receiver. + // A released wrapper was used: as a native argument, collection element or + // return value, as a receiver (toString included), or through a released + // pointer, reference or struct. uint64_t releasedAccesses = 0; // A finalized ObjC wrapper carried JS properties while something other // than the wrapper still retained its object, and whether that object was diff --git a/NativeScript/runtime/ObjectManager.mm b/NativeScript/runtime/ObjectManager.mm index 8fe282a48..5313898ed 100644 --- a/NativeScript/runtime/ObjectManager.mm +++ b/NativeScript/runtime/ObjectManager.mm @@ -17,6 +17,7 @@ #include "Helpers.h" #include "Runtime.h" #include "WrapperLifetime.h" +#include "WrapperLifetimeCanary.h" // Exported by libobjc; not declared in a public header for MRC code. extern "C" id objc_initWeak(id* location, id value); @@ -843,6 +844,11 @@ void DisposeHandle(v8::Isolate* isolate, // The teardown walk owns this state and releases the object. return; } + if (adopted->lifetimeCanary_) { + // Before the release below, which can run the object's -dealloc. + WrapperLifetimeCanary::KeepWeakCollectionKey(isolate, value.As(), + adopted->canaryRecord_); + } if (it != cache->Instances.end() && it->second == adopted->target_) { cache->Instances.erase(it); } diff --git a/NativeScript/runtime/WrapperLifetimeCanary.h b/NativeScript/runtime/WrapperLifetimeCanary.h index 06dfc2724..25ad85666 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.h +++ b/NativeScript/runtime/WrapperLifetimeCanary.h @@ -78,8 +78,19 @@ class WrapperLifetimeCanary { static bool RegisterWrapFunctions(v8::Local context, v8::Local binding); + // Before `canary`'s native object is released through + // __releaseNativeCounterpart: marks `record` released, so the weak + // collections hand out no wrapper of the object until native code returns + // it, and copies the object's weak-collection key onto the canary, so what + // was keyed on the object is still found through the released canary. With + // no key to copy, the canary is marked so that weak collections report it as + // a released-object access. + static void KeepWeakCollectionKey( + v8::Isolate* isolate, v8::Local canary, + const std::shared_ptr& record); + // The record of an object that already has state in this isolate, for a new - // canary to share. Null otherwise. + // canary to adopt; it is no longer released. Null otherwise. static std::shared_ptr Find(v8::Isolate* isolate, id object); // Copies the state of a new canary's object, or of a -dealloc receiver's, diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index bb878f945..db2fb319a 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -67,6 +67,9 @@ // record whose object became a weak-collection key (Wrap reads it). id object = nil; bool weakSet = false; + // __releaseNativeCounterpart released a canary of the object and no canary + // adopted the record since. Isolate thread only. + bool released = false; ~CanaryRecord() { objc_destroyWeak(&object); } @@ -105,7 +108,7 @@ bool ObjectAlive() const { struct CanaryState { std::shared_ptr registry = std::make_shared(); // The weak collections' key for a native object hangs off its state, and - // names the object it stands for. + // names the object it stands for. A released canary holds a copy. Global keyOfState; Global objectOfKey; }; @@ -712,8 +715,9 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { #endif // unwrap(value, create): the weak collections' key for a canary's native -// object, a frozen, null-prototype object that lives exactly as long as the -// object's state, that is, until the native object is deallocated. Lookups pass +// object, a frozen, null-prototype object that lives as long as the object's +// state, that is, until the native object is deallocated, or as long as a +// released canary of the object holding a copy. Lookups pass // `create` false and never make one: with no key yet, no entry can be keyed by // it. The value itself for anything that is not a canary, a Proxy included (it // is a key of its own, as in every other mode). @@ -727,13 +731,31 @@ void UnwrapCallback(const FunctionCallbackInfo& info) { if (canaryState == nullptr || !value->IsObject() || value->IsProxy()) { return; } + Local context = isolate->GetCurrentContext(); + Local keyOfState = canaryState->keyOfState.Get(isolate); + Local key; if (create ? !StateForWrite(isolate, value.As(), &state) : !ExistingState(isolate, value.As(), &state)) { + // A released canary keeps its key itself, or null when it had none + // (KeepWeakCollectionKey). + if (value.As()->InternalFieldCount() == 0 || + !value.As()->GetPrivate(context, keyOfState).ToLocal(&key)) { + return; + } + if (key->IsObject()) { + info.GetReturnValue().Set(key); + } else if (key->IsNull()) { + // Such a canary is a key of its own, which nothing keyed on its object + // can find: a released-object access. + ReleasedReason reason = tns::GetReleasedReason(value); + if (reason != ReleasedReason::kNone) { + tns::ReportReleasedObject( + isolate, value, reason, + "weak collection key: WeakMap, WeakSet, WeakRef or FinalizationRegistry", true); + } + } return; } - Local context = isolate->GetCurrentContext(); - Local keyOfState = canaryState->keyOfState.Get(isolate); - Local key; if (state->GetPrivate(context, keyOfState).ToLocal(&key) && key->IsObject()) { info.GetReturnValue().Set(key); return; @@ -819,6 +841,12 @@ void WrapCallback(const FunctionCallbackInfo& info) { info.GetReturnValue().Set(cached->Get(isolate)); return; } + if (record->released) { + // Released by JS and not handed back by native code: JS holds no wrapper + // of it until native code returns it. + [object release]; + return; + } ObjCDataWrapper* wrapper = new ObjCDataWrapper(object); Local result = ArgConverter::ConvertArgument(context, wrapper); tns::DeleteWrapperIfUnused(isolate, result, wrapper); @@ -861,6 +889,28 @@ void WrapCallback(const FunctionCallbackInfo& info) { binding->Set(context, tns::ToV8String(isolate, "wrap"), wrap).FromMaybe(false); } +void WrapperLifetimeCanary::KeepWeakCollectionKey(Isolate* isolate, Local canary, + const std::shared_ptr& record) { + CanaryState* canaryState = Caches::StateFor(isolate); + if (canaryState == nullptr || canaryState->keyOfState.IsEmpty()) { + return; + } + HandleScope scope(isolate); + Local context = isolate->GetCurrentContext(); + Local keyOfState = canaryState->keyOfState.Get(isolate); + Local copy = v8::Null(isolate); + if (Usable(record)) { + record->released = true; + Local key; + if (Local::New(isolate, record->state)->GetPrivate(context, keyOfState).ToLocal(&key) && + key->IsObject()) { + copy = key; + } + } + // Private symbols bypass the canary's handlers. + (void)canary->SetPrivate(context, keyOfState, copy); +} + void WrapperLifetimeCanary::InstallDeallocHook(Class extendedClass) { if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { return; @@ -948,6 +998,7 @@ void WrapCallback(const FunctionCallbackInfo& info) { } std::shared_ptr record = it->second; if (!RetireIfDead(state->registry.get(), it)) { + record->released = false; return record; } } diff --git a/NativeScript/runtime/js/primordials.js b/NativeScript/runtime/js/primordials.js index 9222d4e83..73c528bdc 100644 --- a/NativeScript/runtime/js/primordials.js +++ b/NativeScript/runtime/js/primordials.js @@ -38,11 +38,13 @@ const intrinsics = { Uint32Array, URIError, URL, + WeakMap, WeakRef, WeakSet, SymbolFor: Symbol.for, SymbolHasInstance: Symbol.hasInstance, SymbolIterator: Symbol.iterator, + SymbolKeyFor: Symbol.keyFor, SymbolToStringTag: Symbol.toStringTag, // Namespaces / prototypes. @@ -112,6 +114,15 @@ const intrinsics = { StringPrototypeStartsWith: uncurryThis(String.prototype.startsWith), StringPrototypeToLowerCase: uncurryThis(String.prototype.toLowerCase), SymbolPrototypeToString: uncurryThis(Symbol.prototype.toString), + WeakMapPrototypeDelete: uncurryThis(WeakMap.prototype.delete), + WeakMapPrototypeGet: uncurryThis(WeakMap.prototype.get), + // The upsert proposal: undefined where V8 does not ship it. + WeakMapPrototypeGetOrInsert: typeof WeakMap.prototype.getOrInsert === "function" + ? uncurryThis(WeakMap.prototype.getOrInsert) : undefined, + WeakMapPrototypeGetOrInsertComputed: typeof WeakMap.prototype.getOrInsertComputed === "function" + ? uncurryThis(WeakMap.prototype.getOrInsertComputed) : undefined, + WeakMapPrototypeHas: uncurryThis(WeakMap.prototype.has), + WeakMapPrototypeSet: uncurryThis(WeakMap.prototype.set), WeakRefPrototypeDeref: uncurryThis(WeakRef.prototype.deref), WeakSetPrototypeAdd: uncurryThis(WeakSet.prototype.add), WeakSetPrototypeDelete: uncurryThis(WeakSet.prototype.delete), diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js index 27d57b761..4eb595c5e 100644 --- a/NativeScript/runtime/js/weak-collections.js +++ b/NativeScript/runtime/js/weak-collections.js @@ -6,57 +6,135 @@ // references while the native object lives on, and a later wrapper for the same // object is a new JS object; keyed by the wrapper, entries and targets would be // lost with it. Every other value passes through unchanged. +// +// They are subclasses of the native collections, so what subclassing shows is +// left as it is: Object.getPrototypeOf(WeakMap.prototype) is the native +// prototype, and WeakMap.toString() is not native code. const { unwrap, wrap } = binding; -const { ObjectDefineProperty, TypeError } = primordials; const { FinalizationRegistry: NativeFinalizationRegistry, + FinalizationRegistryPrototypeRegister, + FinalizationRegistryPrototypeUnregister, + ObjectDefineProperty, + ObjectFreeze, + SymbolKeyFor, WeakMap: NativeWeakMap, + WeakMapPrototypeDelete, + WeakMapPrototypeGet, + WeakMapPrototypeGetOrInsert, + WeakMapPrototypeGetOrInsertComputed, + WeakMapPrototypeHas, + WeakMapPrototypeSet, WeakRef: NativeWeakRef, + WeakRefPrototypeDeref, WeakSet: NativeWeakSet, -} = globalThis; + WeakSetPrototypeAdd, + WeakSetPrototypeDelete, + WeakSetPrototypeHas, +} = primordials; + +// Only an object can be a canary; anything else goes to the native method as is. +function isObject(value) { + return (typeof value === "object" && value !== null) || typeof value === "function"; +} + +// The values the native collections accept as keys and targets. +function canBeHeldWeakly(value) { + return isObject(value) || (typeof value === "symbol" && SymbolKeyFor(value) === undefined); +} // unwrap(value, true) makes the key on first use; lookups pass false so they -// never create state for an object that was never a key. +// never create state for an object that was never a key. A key is made only +// once the native method can no longer reject the call, so a failed call leaves +// no state behind: the receiver is checked first with a method that cannot +// insert anything. +// +// A canary is stored as a key itself only when its object can have no lifetime +// key (a class forbidding associated objects or refusing weak references, a +// -dealloc receiver, a released wrapper that never had one), and unwrap then +// returns the canary too. So every method first tries the value as is and +// unwraps it only when that misses: plain JS keys never reach native code. + +// The lifetime key value stands for, or undefined when that is value itself. +function otherKey(value) { + if (!isObject(value)) { + return undefined; + } + const key = unwrap(value, false); + return key !== value ? key : undefined; +} + +// Unregistering it finds nothing: it is never a token. +const receiverProbe = ObjectFreeze({ __proto__: null }); + class WeakMap extends NativeWeakMap { get(key) { - return super.get(unwrap(key, false)); + const direct = WeakMapPrototypeGet(this, key); + if (direct !== undefined) { + return direct; + } + const other = otherKey(key); + return other === undefined ? undefined : WeakMapPrototypeGet(this, other); } set(key, value) { - super.set(unwrap(key, true), value); + // Also checks the receiver before a key is made. + if (isObject(key) && !WeakMapPrototypeHas(this, key)) { + key = unwrap(key, true); + } + WeakMapPrototypeSet(this, key, value); return this; } has(key) { - return super.has(unwrap(key, false)); + if (WeakMapPrototypeHas(this, key)) { + return true; + } + const other = otherKey(key); + return other !== undefined && WeakMapPrototypeHas(this, other); } delete(key) { - return super.delete(unwrap(key, false)); + if (WeakMapPrototypeDelete(this, key)) { + return true; + } + const other = otherKey(key); + return other !== undefined && WeakMapPrototypeDelete(this, other); } } // V8 ships the upsert proposal; inherited, these would key on the wrapper. -if (typeof NativeWeakMap.prototype.getOrInsert === "function") { +if (WeakMapPrototypeGetOrInsert !== undefined) { ObjectDefineProperty(WeakMap.prototype, "getOrInsert", { value: function getOrInsert(key, value) { - return NativeWeakMap.prototype.getOrInsert.call(this, unwrap(key, true), value); + if (isObject(key) && !WeakMapPrototypeHas(this, key)) { + key = unwrap(key, true); + } + return WeakMapPrototypeGetOrInsert(this, key, value); }, writable: true, configurable: true, }); } -if (typeof NativeWeakMap.prototype.getOrInsertComputed === "function") { +if (WeakMapPrototypeGetOrInsertComputed !== undefined) { ObjectDefineProperty(WeakMap.prototype, "getOrInsertComputed", { value: function getOrInsertComputed(key, callbackfn) { - if (typeof callbackfn !== "function") { - throw new TypeError("WeakMap.prototype.getOrInsertComputed: callback is not a function"); + if (!isObject(key) || typeof callbackfn !== "function") { + return WeakMapPrototypeGetOrInsertComputed(this, key, callbackfn); + } + if (WeakMapPrototypeHas(this, key)) { + return WeakMapPrototypeGet(this, key); } - // The callback sees the key it was given, not the lifetime key. - return NativeWeakMap.prototype.getOrInsertComputed.call(this, unwrap(key, true), () => - callbackfn(key), - ); + const existing = otherKey(key); + if (existing !== undefined && WeakMapPrototypeHas(this, existing)) { + return WeakMapPrototypeGet(this, existing); + } + // Before the key exists, so a callback that throws leaves none; it sees + // the key it was given, not the lifetime key. + const value = callbackfn(key); + WeakMapPrototypeSet(this, unwrap(key, true), value); + return value; }, writable: true, configurable: true, @@ -65,22 +143,34 @@ if (typeof NativeWeakMap.prototype.getOrInsertComputed === "function") { class WeakSet extends NativeWeakSet { add(value) { - super.add(unwrap(value, true)); + // Also checks the receiver before a key is made. + if (isObject(value) && !WeakSetPrototypeHas(this, value)) { + value = unwrap(value, true); + } + WeakSetPrototypeAdd(this, value); return this; } has(value) { - return super.has(unwrap(value, false)); + if (WeakSetPrototypeHas(this, value)) { + return true; + } + const other = otherKey(value); + return other !== undefined && WeakSetPrototypeHas(this, other); } delete(value) { - return super.delete(unwrap(value, false)); + if (WeakSetPrototypeDelete(this, value)) { + return true; + } + const other = otherKey(value); + return other !== undefined && WeakSetPrototypeDelete(this, other); } } class WeakRef extends NativeWeakRef { constructor(target) { - const key = unwrap(target, true); + const key = isObject(target) ? unwrap(target, true) : target; super(key); // The target itself, not only its key, stays alive until the job ends. if (key !== target) { @@ -93,16 +183,21 @@ class WeakRef extends NativeWeakRef { // target, the wrapper it returns stays alive until the job ends, so a second // deref in the same job sees the same object. deref() { - const result = wrap(super.deref()); - if (typeof result === "object" && result !== null) { + const target = WeakRefPrototypeDeref(this); + if (!isObject(target)) { + return target; + } + const result = wrap(target); + // The native deref already keeps a target it returns as is. + if (result !== target && result !== undefined) { new NativeWeakRef(result); } return result; } } -// weak-ref.js gave the native prototype a `get` alias of `deref`; inherited -// as is, it would hand out the lifetime key. +// weak-ref.js gave the native prototype `get` (an alias of `deref`) and a +// deprecated `clear`; inherited as is, `get` would hand out the lifetime key. ObjectDefineProperty(WeakRef.prototype, "get", { value: WeakRef.prototype.deref, writable: true, @@ -114,20 +209,29 @@ class FinalizationRegistry extends NativeFinalizationRegistry { super(cleanupCallback); } - register(target, heldValue, unregisterToken) { - // Checked on the original values: unwrapping would make them differ. - if (target === heldValue) { - throw new TypeError("FinalizationRegistry.prototype.register: target and holdings must not be same"); + // The default keeps `length` at 2, as on the native method. + register(target, heldValue, unregisterToken = undefined) { + // Checked on the original values: unwrapping would make target and + // heldValue differ. A call the native method rejects gets its error. + if (!canBeHeldWeakly(target) || target === heldValue || + (unregisterToken !== undefined && !canBeHeldWeakly(unregisterToken))) { + return FinalizationRegistryPrototypeRegister(this, target, heldValue, unregisterToken); } - super.register( - unwrap(target, true), + FinalizationRegistryPrototypeUnregister(this, receiverProbe); + FinalizationRegistryPrototypeRegister( + this, + isObject(target) ? unwrap(target, true) : target, heldValue, - unregisterToken === undefined ? undefined : unwrap(unregisterToken, true), + isObject(unregisterToken) ? unwrap(unregisterToken, true) : unregisterToken, ); } unregister(unregisterToken) { - return super.unregister(unwrap(unregisterToken, false)); + if (FinalizationRegistryPrototypeUnregister(this, unregisterToken)) { + return true; + } + const other = otherKey(unregisterToken); + return other !== undefined && FinalizationRegistryPrototypeUnregister(this, other); } } diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js index 968eda912..134b7ea57 100644 --- a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -281,6 +281,340 @@ describe("Weak collections of native objects", function () { expect(new WeakRef(key).deref()).toBe(key); }); + it("keys the entries given to the WeakMap and WeakSet constructors by the native object", function (done) { + var owner = NSMutableArray.new(); + var map; + var set; + (function () { + var obj = Extended.new(); + map = new WeakMap([[obj, "constructed"]]); + set = new WeakSet([obj]); + owner.addObject(obj); + })(); + settle(2, function () { + var obj = owner.objectAtIndex(0); + expect(map.get(obj)).toBe("constructed"); + expect(set.has(obj)).toBe(true); + done(); + }); + }); + + it("keys a native object in a subclass of WeakMap", function (done) { + class TaggedMap extends WeakMap { + set(key, value) { + return super.set(key, "tagged:" + value); + } + } + var owner = NSMutableArray.new(); + var map = new TaggedMap(); + (function () { + var obj = Extended.new(); + map.set(obj, "value"); + owner.addObject(obj); + })(); + settle(2, function () { + expect(map.get(owner.objectAtIndex(0))).toBe("tagged:value"); + expect(map instanceof WeakMap).toBe(true); + done(); + }); + }); + + // In canary mode what was keyed on a native object follows the object, not + // a released wrapper of it; elsewhere the standard collections key the + // wrapper itself. Only booleans reach expect(), which could otherwise keep + // a wrapper alive. + function pollCleaned(isCleaned, registry, callback) { + var attempts = 15; + (function poll() { + settle(1, function () { + // A collected registry runs no callbacks. + if ((isCleaned() && registry !== null) || --attempts === 0) { + callback(); + return; + } + poll(); + }); + })(); + } + + it("keeps what was keyed on a natively retained object after a wrapper of it is released", function (done) { + if (!canary) { + done(); + return; + } + var owner = NSMutableArray.new(); + var map = new WeakMap(); + var set = new WeakSet(); + var cleaned = false; + var registry = new FinalizationRegistry(function (held) { + cleaned = held === "released"; + }); + var obj = Extended.new(); + owner.addObject(obj); + map.set(obj, "value"); + set.add(obj); + var ref = new WeakRef(obj); + registry.register(obj, "released"); + __releaseNativeCounterpart(obj); + + expect(map.get(obj)).toBe("value"); + expect(set.has(obj)).toBe(true); + expect(ref.deref() === undefined).toBe(true); + settle(2, function () { + expect(map.get(obj)).toBe("value"); + expect(set.has(obj)).toBe(true); + expect(ref.deref() === undefined).toBe(true); + var later = owner.objectAtIndex(0); + expect(later === obj).toBe(false); + expect(map.get(later)).toBe("value"); + expect(set.has(later)).toBe(true); + expect(ref.deref() === later).toBe(true); + later = null; + obj = null; + settle(3, function () { + expect(cleaned).toBe(false); + owner.removeAllObjects(); + pollCleaned(function () { + return cleaned; + }, registry, function () { + expect(cleaned).toBe(true); + done(); + }); + }); + }); + }); + + it("keeps what was keyed on a native object JS owned alone while its released wrapper lives", function (done) { + if (!canary) { + done(); + return; + } + var map = new WeakMap(); + var cleaned = false; + var registry = new FinalizationRegistry(function (held) { + cleaned = held === "released"; + }); + var obj = Extended.new(); + map.set(obj, "value"); + var ref = new WeakRef(obj); + registry.register(obj, "released"); + __releaseNativeCounterpart(obj); + + expect(ref.deref() === undefined).toBe(true); + expect(map.get(obj)).toBe("value"); + settle(2, function () { + expect(map.get(obj)).toBe("value"); + expect(ref.deref() === undefined).toBe(true); + expect(cleaned).toBe(false); + obj = null; + pollCleaned(function () { + return cleaned; + }, registry, function () { + expect(cleaned).toBe(true); + done(); + }); + }); + }); + + // __releaseNativeCounterpart frees a heavy native object early: in every + // mode, neither its keys nor its released wrapper may keep it alive. The + // weak table sees the object without retaining it. + it("deallocates a keyed native object JS owned alone at its release", function () { + var watch = NSHashTable.weakObjectsHashTable(); + var map = new WeakMap(); + var set = new WeakSet(); + var registry = new FinalizationRegistry(function () {}); + var obj = Extended.new(); + watch.addObject(obj); + map.set(obj, "value"); + set.add(obj); + var ref = new WeakRef(obj); + registry.register(obj, "held"); + __releaseNativeCounterpart(obj); + + expect(watch.allObjects.count).toBe(0); + expect(map.get(obj)).toBe("value"); + expect(set.has(obj)).toBe(true); + expect(ref.deref() === undefined || ref.deref() === obj).toBe(true); + }); + + it("deallocates a released natively retained object once native code lets go, while its wrapper is held and dereferenced", function (done) { + var watch = NSHashTable.weakObjectsHashTable(); + var owner = NSMutableArray.new(); + var map = new WeakMap(); + var set = new WeakSet(); + var obj = Extended.new(); + owner.addObject(obj); + watch.addObject(obj); + map.set(obj, "value"); + set.add(obj); + var ref = new WeakRef(obj); + __releaseNativeCounterpart(obj); + + function poke() { + for (var i = 0; i < 10; i++) { + ref.deref(); + map.get(obj); + set.has(obj); + } + } + poke(); + settle(2, function () { + poke(); + owner.removeAllObjects(); + poke(); + expect(watch.allObjects.count).toBe(0); + expect(ref.deref() === undefined || ref.deref() === obj).toBe(true); + expect(map.get(obj)).toBe("value"); + expect(set.has(obj)).toBe(true); + done(); + }); + }); + + // A canary released before anything keyed its object is a key of its own, + // which nothing keyed on the object can find. + function releasedNeverKeyed() { + var obj = Extended.new(); + __releaseNativeCounterpart(obj); + return obj; + } + + function releasedAccesses() { + return runtime.getWrapperLifetimeStats().releasedAccesses; + } + + it("reports a weak collection operation on a canary released before anything keyed its object", function (done) { + if (!canary) { + done(); + return; + } + var messages = []; + var onReleased = function (e) { + if (/weak collection/.test(e.error.message)) { + messages.push(e.error.message); + } + }; + global.addEventListener("releasednativeaccess", onReleased); + var map = new WeakMap(); + var before = releasedAccesses(); + map.set(releasedNeverKeyed(), 1); + expect(releasedAccesses()).toBe(before + 1); + new WeakRef(releasedNeverKeyed()); + expect(releasedAccesses()).toBe(before + 2); + expect(map.get(releasedNeverKeyed())).toBeUndefined(); + expect(releasedAccesses()).toBe(before + 3); + setTimeout(function () { + global.removeEventListener("releasednativeaccess", onReleased); + expect(messages.length).toBe(3); + for (var i = 0; i < messages.length; i++) { + expect(messages[i]).toMatch(/released by __releaseNativeCounterpart \(weak collection key/); + } + done(); + }); + }); + + it("throws for a weak collection operation on a canary released before anything keyed its object under the throw policy", function () { + if (!canary) { + return; + } + var policy = runtime.getConfig("releasedObjectPolicy"); + runtime.setConfig("releasedObjectPolicy", "throw"); + try { + var map = new WeakMap(); + expect(function () { + map.set(releasedNeverKeyed(), 1); + }).toThrowError(ReferenceError, /weak collection key/); + expect(function () { + new WeakRef(releasedNeverKeyed()); + }).toThrowError(ReferenceError, /weak collection key/); + expect(function () { + map.get(releasedNeverKeyed()); + }).toThrowError(ReferenceError, /weak collection key/); + } finally { + runtime.setConfig("releasedObjectPolicy", policy); + } + }); + + it("does not report a weak collection operation on a released wrapper whose object was keyed", function () { + var policy = runtime.getConfig("releasedObjectPolicy"); + runtime.setConfig("releasedObjectPolicy", "throw"); + try { + var obj = Extended.new(); + var map = new WeakMap(); + map.set(obj, 1); + __releaseNativeCounterpart(obj); + var before = releasedAccesses(); + var other = new WeakMap(); + expect(map.get(obj)).toBe(1); + expect(other.get(obj)).toBeUndefined(); + other.set(obj, 2); + expect(other.get(obj)).toBe(2); + new WeakRef(obj); + new WeakSet().add(obj); + expect(releasedAccesses()).toBe(before); + } finally { + runtime.setConfig("releasedObjectPolicy", policy); + } + }); + + it("makes no key for a call the standard collection rejects", function () { + var obj = Extended.new(); + var registry = new FinalizationRegistry(function () {}); + var map = new WeakMap(); + var states = runtime.getWrapperLifetimeStats().canaryStates; + expect(function () { + WeakMap.prototype.set.call({}, obj, 1); + }).toThrowError(TypeError); + expect(function () { + WeakSet.prototype.add.call({}, obj); + }).toThrowError(TypeError); + expect(function () { + registry.register(obj, "held", 1); + }).toThrowError(TypeError); + expect(function () { + FinalizationRegistry.prototype.register.call({}, obj, "held"); + }).toThrowError(TypeError); + if (typeof map.getOrInsertComputed === "function") { + expect(function () { + WeakMap.prototype.getOrInsert.call({}, obj, 1); + }).toThrowError(TypeError); + expect(function () { + map.getOrInsertComputed(obj, function () { + throw new Error("callback"); + }); + }).toThrowError("callback"); + } + expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(states); + }); + + it("hands getOrInsertComputed's callback the key it was given", function () { + var map = new WeakMap(); + if (typeof map.getOrInsertComputed !== "function") { + return; + } + var obj = Extended.new(); + var seen; + expect(map.getOrInsertComputed(obj, function (key) { + seen = key; + return "computed"; + })).toBe("computed"); + expect(seen === obj).toBe(true); + expect(map.get(obj)).toBe("computed"); + }); + + it("matches the standard FinalizationRegistry.prototype.register signature and errors", function () { + var registry = new FinalizationRegistry(function () {}); + expect(FinalizationRegistry.prototype.register.length).toBe(2); + expect(function () { + registry.register(1, 1); + }).toThrowError(TypeError, /invalid target/); + }); + + it("installs the canary collections only in canary mode", function () { + var native = WeakMap.toString().indexOf("[native code]") !== -1; + expect(native).toBe(!canary); + }); + it("iterates NSArray wrappers through the prototype", function () { var array = NSArray.arrayWithArray([1, 2, 3]); var seen = []; diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index 6fee6ba24..d32a9eeae 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -325,7 +325,17 @@ canary lives at a time, and anything still referencing the old one would have ke An explicit release is different: `__releaseNativeCounterpart` drops the object's `Caches::Instances` entry but not its state, so if the object lives on, the next native return of it builds a new canary with the state's properties while JS can still hold the released -one, and `===` tells them apart. What could notice is whatever keys on the canary without keeping it. The weak collections +one, and `===` tells them apart. What the weak collections keyed on the object follows the +object: before the release, `WrapperLifetimeCanary::KeepWeakCollectionKey` copies the key onto +the released canary (a private), so `unwrap` still returns it there, and marks the record +released, so `wrap` returns the object's live canary only once native code handed the object +back (a new canary adopted the record, which clears the mark) and `undefined` until then; it +never builds a canary for a released record, and never returns the released one. A canary +released with no key to copy gets `null` in that private instead, and `unwrap` reports each +weak-collection operation that reaches it as a released-object access (`ReportReleasedObject`). The key +lives while the state or the released canary does. A record whose token +`objc_removeAssociatedObjects` already retired gets neither: the released canary then keys +nothing, as that limitation describes. What could notice is whatever keys on the canary without keeping it. The weak collections don't: in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, `WeakRef` and `FinalizationRegistry` with subclasses that key an in-scope native object (a canary: not a plain object under `"extended"`, a tagged pointer, a block, or an object of a class diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index 72030cab8..c0c0367a4 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -159,16 +159,16 @@ on every write to a canary: | counter | counts | |---|---| -| `resurrections` | finalizers that kept a wrapper alive because native code still retained its object (JS-extended classes) | +| `resurrections` | finalizers that kept an ObjC wrapper alive because native code still retained its object: a JS-extended class's instance, or a wrapper the runtime keeps strong while native code owns its object | | `collected` | finalizers that released their native object | | `releasedExplicitly` | native objects released through `__releaseNativeCounterpart` | -| `releasedAccesses` | times JS passed a released wrapper to native code, used it as a method receiver, or touched a released pointer, reference or struct | +| `releasedAccesses` | uses of a released wrapper: as a native argument, collection element or return value, as a method receiver or `toString()` receiver, as a weak collection key in `"canary"` mode when it was released before anything keyed its object (see [Weak collections in canary mode](#weak-collections-in-canary-mode)), and reads or writes through a released pointer, reference or struct | | `stateLostCandidates` | collected wrappers carrying JS properties whose object was still retained by something else (only while `wrapperLifetimeWarnings` is on) | | `stateLostConfirmed` | candidates whose object was still alive a runloop turn later, once releases queued for the runtime thread ran; each class is also reported once in the log | | `promotions` | `"rooted"` only: weak wrappers made strong because native code retained their object | | `demotions` | `"rooted"` only: strong wrappers made weak at a major collection because native code no longer retained their object | | `queuedReleases` | `"rooted"` and `"canary"`: collected wrappers whose native object was queued for release on the runtime's thread | -| `canaryStates` | `"canary"` only: state objects created to hold native objects' JS properties | +| `canaryStates` | `"canary"` only: state objects created to hold native objects' JS properties, or only to hold the key a weak collection made for the object (see [Weak collections in canary mode](#weak-collections-in-canary-mode)) | | `canaryNativeDeaths` | `"canary"` only: state objects let go of because their native object was deallocated or, with the default death signal, because `objc_removeAssociatedObjects` released its lifecycle token while the object was still alive | | `canaryInterceptedWrites` | `"canary"` only: writes to canaries the runtime's write handler saw. Other wrappers have no handler | @@ -182,6 +182,43 @@ or return value becomes `nil` (`NSNull` inside a collection) and a value there (an attributes dictionary holding a released `UIColor`, say); the event names the element so the cause can be found. +### Weak collections in canary mode + +In the `"canary"` mode a wrapper dies with its JS references while its native +object lives on, and the next wrapper for that object is a new JS object. So +the runtime replaces `WeakMap`, `WeakSet`, `WeakRef` and +`FinalizationRegistry` with subclasses that key a native object by a key that +lives exactly as long as the object, rather than by its current wrapper. +Entries and targets survive a wrapper change, and `WeakRef.prototype.deref` +returns the live wrapper, a new one, or `undefined` once the object is gone. +Every other value, a Proxy around a native object included, is its own key as +in the standard collections. What follows from that: + +- They are subclasses, and it shows: `Object.getPrototypeOf(WeakMap.prototype)` + is the standard prototype, and `WeakMap.toString()` is not native code. +- `deref` builds a wrapper when the old one died, and that wrapper holds the + native object strongly until it dies in turn. A `WeakRef` dereferenced on + every frame or event may keep its object from ever being released. +- A key lives in the object's state, so the first `set`, `add`, `new WeakRef` + or `register` of an object without JS properties creates a state, counted in + `canaryStates` and `getWrapperClassCounts()`'s `states`. Lookups never do. +- What was keyed on a native object follows the object, not a released + wrapper of it. After `__releaseNativeCounterpart(w)`, `w` still finds the + object's entries while JS holds it, and a wrapper native code hands back + later finds them too. `deref` returns `undefined` until native code hands + the object back, and a `FinalizationRegistry` callback runs only once the + object is gone and `w` is collected. If the release deallocated the object, + the callback runs once `w` is collected. A wrapper released before anything + keyed its object is a plain key from then on: entries made through it don't + reach the object, and `new WeakRef(w).deref()` returns `w`. Each weak + collection operation that has to look such a wrapper up is a released-object + access: it counts in `releasedAccesses` and follows `releasedObjectPolicy` + (a `releasednativeaccess` event, then the plain-key behavior, under + `"report"`; a `ReferenceError` under `"throw"`). A lookup that finds the + wrapper itself as a key does not count. +- A prototype set later with `Object.setPrototypeOf` or `__proto__` is not + state: a later wrapper has its class's prototype again. + ### Leak diagnostics The `"rooted"` and `"canary"` wrapper lifetime modes trade resurrection's early