diff --git a/NativeScript/runtime/ClassBuilder.mm b/NativeScript/runtime/ClassBuilder.mm index 411a24d95..153794a1e 100644 --- a/NativeScript/runtime/ClassBuilder.mm +++ b/NativeScript/runtime/ClassBuilder.mm @@ -762,8 +762,15 @@ void ScopeClassNameToIsolate(std::string& name, int isolateId) { Local propertyNames; + // An inherited iterator only when no superclass enumerates natively: NSArray's + // prototype iterator, or a JS superclass's, already backs that enumeration. Local symbolIterator; + bool ownsIterator = + implementationObject->HasOwnProperty(context, Symbol::GetIterator(isolate)).FromMaybe(false); + bool inheritsEnumeration = class_respondsToSelector( + class_getSuperclass(extendedClass), @selector(countByEnumeratingWithState:objects:count:)); bool success = + (!ownsIterator && inheritsEnumeration) || implementationObject->Get(context, Symbol::GetIterator(isolate)).ToLocal(&symbolIterator); tns::Assert(success, isolate); diff --git a/NativeScript/runtime/Interop.mm b/NativeScript/runtime/Interop.mm index fc4cf2414..9036ede51 100644 --- a/NativeScript/runtime/Interop.mm +++ b/NativeScript/runtime/Interop.mm @@ -17,7 +17,6 @@ #include "Reference.h" #include "Runtime.h" #include "RuntimeConfig.h" -#include "SymbolIterator.h" #include "SymbolLoader.h" #include "UnmanagedType.h" #include "WrapperLifetime.h" @@ -1497,11 +1496,6 @@ static bool SnapshotArrayLike(Isolate* isolate, Local object, LocalPrototypeTemplate()->Set( tns::ToV8String(isolate, "toString"), FunctionTemplate::New(isolate, MetadataBuilder::ToStringFunctionCallback)); + if (meta->type() == MetaType::Interface && strcmp(meta->name(), "NSArray") == 0) { + SymbolIterator::Install(isolate, ctorFuncTemplate->PrototypeTemplate()); + } if (meta->type() == MetaType::Interface) { const InterfaceMeta* interfaceMeta = static_cast(meta); diff --git a/NativeScript/runtime/SymbolIterator.h b/NativeScript/runtime/SymbolIterator.h index 6cf66fc06..392f35412 100644 --- a/NativeScript/runtime/SymbolIterator.h +++ b/NativeScript/runtime/SymbolIterator.h @@ -7,7 +7,10 @@ namespace tns { class SymbolIterator { public: - static void Set(v8::Local context, v8::Local object); + // On NSArray's prototype template, so every array wrapper inherits it. + static void Install(v8::Isolate* isolate, + v8::Local prototypeTemplate); + private: static v8::Local CreateIteratorObject(v8::Local context, v8::Local object); static void NextCallback(const v8::FunctionCallbackInfo& args); diff --git a/NativeScript/runtime/SymbolIterator.mm b/NativeScript/runtime/SymbolIterator.mm index 72477f605..46797c7da 100644 --- a/NativeScript/runtime/SymbolIterator.mm +++ b/NativeScript/runtime/SymbolIterator.mm @@ -2,40 +2,27 @@ #include #include "ArgConverter.h" #include "Helpers.h" -#include "WrapperLifetime.h" using namespace v8; namespace tns { -void SymbolIterator::Set(Local context, Local object) { - Isolate* isolate = v8::Isolate::GetCurrent(); - - // In the canary wrapper lifetime mode it iterates its receiver: this - // property lives on the object's state there, which a reference back to the - // object would keep alive until native death. Elsewhere it keeps `object`, so - // an unbound call still iterates it. - Local captured = - GetWrapperLifetimeMode() == WrapperLifetimeMode::kCanary ? Local() : object; - Local iterator; - bool success = v8::Function::New( - context, - [](const FunctionCallbackInfo& args) { - Isolate* isolate = args.GetIsolate(); - Local context = isolate->GetCurrentContext(); - Local target = args.Data(); - if (target.IsEmpty() || target->IsUndefined()) { - target = args.This(); - } - Local result = CreateIteratorObject(context, target); - args.GetReturnValue().Set(result); - }, - captured) - .ToLocal(&iterator); - tns::Assert(success, isolate); - - Local symbolIteratorKey = Symbol::GetIterator(isolate); - success = object.As()->Set(context, symbolIteratorKey, iterator).FromMaybe(false); +void SymbolIterator::Install(Isolate* isolate, Local prototypeTemplate) { + prototypeTemplate->Set( + Symbol::GetIterator(isolate), + FunctionTemplate::New(isolate, [](const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + // Like Array.prototype[Symbol.iterator], it needs its array as `this`. + BaseDataWrapper* wrapper = tns::GetValue(isolate, args.This()); + if (wrapper == nullptr || wrapper->Type() != WrapperType::ObjCObject || + ![static_cast(wrapper)->Data() isKindOfClass:[NSArray class]]) { + isolate->ThrowException(Exception::TypeError(tns::ToV8String( + isolate, "NSArray.prototype[Symbol.iterator] called on a non-NSArray"))); + return; + } + Local context = isolate->GetCurrentContext(); + args.GetReturnValue().Set(CreateIteratorObject(context, args.This())); + })); } Local SymbolIterator::CreateIteratorObject(Local context, Local object) { diff --git a/NativeScript/runtime/WeakRef.cpp b/NativeScript/runtime/WeakRef.cpp index de04d3acc..5523247a7 100644 --- a/NativeScript/runtime/WeakRef.cpp +++ b/NativeScript/runtime/WeakRef.cpp @@ -2,6 +2,8 @@ #include "BuiltinLoader.h" #include "Helpers.h" +#include "WrapperLifetime.h" +#include "WrapperLifetimeCanary.h" using namespace v8; @@ -18,6 +20,19 @@ void WeakRef::Init(Local context) { tns::LogError(isolate, tc); } tns::Assert(success, isolate); + + if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { + return; + } + Local binding = Object::New(isolate); + success = + WrapperLifetimeCanary::RegisterWrapFunctions(context, binding) && + BuiltinLoader::RunBuiltin(context, BuiltinId::kWeakCollections, binding) + .ToLocal(&result); + if (!success && tc.HasCaught()) { + tns::LogError(isolate, tc); + } + tns::Assert(success, isolate); } } // namespace tns diff --git a/NativeScript/runtime/WrapperLifetimeCanary.h b/NativeScript/runtime/WrapperLifetimeCanary.h index d41f3191e..d71242185 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.h +++ b/NativeScript/runtime/WrapperLifetimeCanary.h @@ -71,6 +71,12 @@ class WrapperLifetimeCanary { // returned instead), which ArgConverter::ConstructObject wraps anew. static bool NativeInstancesIntercept(); + // Adds `unwrap` (a canary's native object -> its weak-collection key) and + // `wrap` (key -> live or new canary, or undefined once the object is gone) + // to `binding`, for the weak-collections builtin. Canary mode only. + static bool RegisterWrapFunctions(v8::Local context, + v8::Local binding); + // The record of an object that already has state in this isolate, for a new // canary to share. Null otherwise. static std::shared_ptr Find(v8::Isolate* isolate, id object); diff --git a/NativeScript/runtime/WrapperLifetimeCanary.mm b/NativeScript/runtime/WrapperLifetimeCanary.mm index 99aeac1f5..880e0fe02 100644 --- a/NativeScript/runtime/WrapperLifetimeCanary.mm +++ b/NativeScript/runtime/WrapperLifetimeCanary.mm @@ -1,5 +1,6 @@ #include "WrapperLifetimeCanary.h" #import +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include "ArgConverter.h" #include "Caches.h" #include "DataWrapper.h" #include "EventLoop.h" @@ -21,19 +23,22 @@ // their class's own -dealloc (InstallDeallocHook) either way; for others: // - ASSOCIATION: a token associated with the object reports it from its // -dealloc, immediately and on any thread. The ObjC runtime aborts the -// process for a class that forbids associated objects (no public query). +// process for a class that forbids associated objects (Swift actors, for +// one) and has no public query for it: ForbidsAssociatedObjects reads the +// runtime's private flag instead. // - WEAK_SWEEP: each record holds a weak reference to the object, checked by a -// sweep after every major collection, so state lingers until then. Classes -// that refuse weak references keep their JS properties on the canary. +// sweep after every major collection, so state lingers until then. +// An object the chosen signal cannot watch (a class forbidding associated +// objects, or refusing weak references) gets no state: its JS properties stay +// on the canary and the weak collections key its wrapper. #define NS_CANARY_DEATH_ASSOCIATION 1 #define NS_CANARY_DEATH_WEAK_SWEEP 2 #ifndef NS_CANARY_DEATH_SIGNAL #define NS_CANARY_DEATH_SIGNAL NS_CANARY_DEATH_ASSOCIATION #endif -#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP extern "C" id objc_initWeakOrNil(id* location, id value); -#endif +extern "C" id objc_loadWeakRetained(id* location); extern "C" void objc_destroyWeak(id* location); using namespace v8; @@ -57,8 +62,10 @@ // tells, when a sweep reads it. bool reportsDeath = false; // An ObjC weak reference to the object, zeroed by the runtime when it is - // deallocated, on any thread. Set for records nothing reports. + // deallocated, on any thread. Set for records nothing reports, and for any + // record whose object became a weak-collection key (Wrap reads it). id object = nil; + bool weakSet = false; ~CanaryRecord() { objc_destroyWeak(&object); } @@ -96,6 +103,10 @@ 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. + Global keyOfState; + Global objectOfKey; }; void DrainDead(const std::shared_ptr& registry) { @@ -215,6 +226,33 @@ bool IsExtendedInstance(id object) { return false; } +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION +// The check objc_setAssociatedObject aborts on: RW_FORBIDS_ASSOCIATED_OBJECTS +// in the class_rw_t flags of the object's own class, reached through the mask +// libobjc exports for debuggers. Private layout (objc_class keeps `bits` after +// isa, superclass and the cache; the flags lead class_rw_t), so anything it +// cannot read, a missing mask or an unrealized class, counts as forbidding. Not +// a message, as IsExtendedInstance. +bool ForbidsAssociatedObjects(id object) { + static const uintptr_t* rwDataMask = + static_cast(dlsym(RTLD_DEFAULT, "objc_debug_class_rw_data_mask")); + if (rwDataMask == nullptr) { + return true; + } + struct ClassLayout { + void* isa; + void* superclass; + void* cache[2]; + uintptr_t bits; + }; + const auto* cls = static_cast((__bridge const void*)object_getClass(object)); + const auto* flags = reinterpret_cast(cls->bits & *rwDataMask); + constexpr uint32_t kRealized = 1u << 31; + constexpr uint32_t kForbidsAssociatedObjects = 1u << 20; + return flags == nullptr || (*flags & kRealized) == 0 || (*flags & kForbidsAssociatedObjects) != 0; +} +#endif + #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP // Lets go of the state of every object that was deallocated. Runs as an event // loop task (under the Locker, on the runtime's thread), never inside the GC. @@ -446,6 +484,11 @@ bool StateForWrite(Isolate* isolate, Local holder, Local* out) { bool replacesDied = canary->canaryRecord_ != nullptr; // A JS-extended class's own -dealloc reports its instances' deaths. bool extended = IsExtendedInstance(object); +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION + if (!extended && ForbidsAssociatedObjects(object)) { + return false; + } +#endif // Allocated before the lock: a collection this allocation starts can run // callbacks (the weak sweep's, a finalizer's -dealloc) that take it. Local freshState = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); @@ -465,10 +508,13 @@ bool StateForWrite(Isolate* isolate, Local holder, Local* out) { auto record = std::make_shared(); record->reportsDeath = extended || NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION; #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP - if (!record->reportsDeath && objc_initWeakOrNil(&record->object, object) == nil) { - // The class refuses weak references. - registry->records.erase((__bridge void*)object); - return false; + if (!record->reportsDeath) { + if (objc_initWeakOrNil(&record->object, object) == nil) { + // The class refuses weak references. + registry->records.erase((__bridge void*)object); + return false; + } + record->weakSet = true; } #endif record->state.Reset(isolate, freshState); @@ -651,6 +697,121 @@ 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 +// `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). +void UnwrapCallback(const FunctionCallbackInfo& info) { + Isolate* isolate = info.GetIsolate(); + Local value = info[0]; + bool create = info[1]->IsTrue(); + info.GetReturnValue().Set(value); + CanaryState* canaryState = Caches::StateFor(isolate); + Local state; + if (canaryState == nullptr || !value->IsObject() || value->IsProxy()) { + return; + } + if (create ? !StateForWrite(isolate, value.As(), &state) + : !ExistingState(isolate, value.As(), &state)) { + 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; + } + if (!create) { + return; + } + ObjectWeakCallbackState* canary = CanaryOf(isolate, value.As()); + if (canary == nullptr) { + // A receiver lent to JS from -dealloc: its object is going away. + return; + } + CanaryRecord* record = canary->canaryRecord_.get(); + if (!record->weakSet) { + if (objc_initWeakOrNil(&record->object, canary->lifetimeTarget_) == nil) { + return; + } + record->weakSet = true; + } + Local newKey = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); + uint64_t address = reinterpret_cast((__bridge void*)canary->lifetimeTarget_); + if (!newKey + ->SetPrivate(context, canaryState->objectOfKey.Get(isolate), + BigInt::NewFromUnsigned(isolate, address)) + .FromMaybe(false) || + !newKey->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromMaybe(false) || + !state->SetPrivate(context, keyOfState, newKey).FromMaybe(false)) { + return; + } + info.GetReturnValue().Set(newKey); +} + +// The object a key stands for: its live canary or a new one, or undefined once +// the native object is gone. Anything that is not a key passes through. +void WrapCallback(const FunctionCallbackInfo& info) { + Isolate* isolate = info.GetIsolate(); + Local value = info[0]; + info.GetReturnValue().Set(value); + CanaryState* canaryState = Caches::StateFor(isolate); + if (canaryState == nullptr || !value->IsObject()) { + return; + } + Local context = isolate->GetCurrentContext(); + Local key = value.As(); + Local address; + if (!key->GetPrivate(context, canaryState->objectOfKey.Get(isolate)).ToLocal(&address) || + !address->IsBigInt()) { + return; + } + info.GetReturnValue().SetUndefined(); + std::shared_ptr record; + { + std::lock_guard lock(canaryState->registry->mutex); + auto it = canaryState->registry->records.find( + reinterpret_cast(static_cast(address.As()->Uint64Value()))); + if (it == canaryState->registry->records.end()) { + return; + } + record = it->second; + } + if (!Usable(record) || !record->weakSet) { + return; + } + // A record at that address may belong to a later object: it must be the + // key's own. + Local recordKey; + Local state = Local::New(isolate, record->state); + if (!state->GetPrivate(context, canaryState->keyOfState.Get(isolate)).ToLocal(&recordKey) || + !recordKey->StrictEquals(key)) { + return; + } + id object = objc_loadWeakRetained(&record->object); + if (object == nil) { + return; + } + // A live wrapper is handed back as is: building one through ConvertArgument + // would reset a protocol-typed wrapper's prototype to its class's. + std::shared_ptr cache = Caches::Get(isolate); + std::shared_ptr> cached = + ArgConverter::FindCachedInstance(isolate, cache, object); + if (cached != nullptr && !cached->IsEmpty()) { + [object release]; + info.GetReturnValue().Set(cached->Get(isolate)); + return; + } + ObjCDataWrapper* wrapper = new ObjCDataWrapper(object); + Local result = ArgConverter::ConvertArgument(context, wrapper); + tns::DeleteWrapperIfUnused(isolate, result, wrapper); + [object release]; + info.GetReturnValue().Set(result); +} + } // namespace void WrapperLifetimeCanary::Init(Isolate* isolate) { @@ -662,6 +823,8 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { return; } state->registry->isolate = isolate; + state->keyOfState.Reset(isolate, Private::New(isolate)); + state->objectOfKey.Reset(isolate, Private::New(isolate)); if (Runtime* runtime = Runtime::GetRuntime(isolate)) { state->registry->loop = runtime->GetEventLoop(); } @@ -674,6 +837,16 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) { #endif } +bool WrapperLifetimeCanary::RegisterWrapFunctions(Local context, Local binding) { + Isolate* isolate = v8::Isolate::GetCurrent(); + Local unwrap; + Local wrap; + return v8::Function::New(context, UnwrapCallback).ToLocal(&unwrap) && + v8::Function::New(context, WrapCallback).ToLocal(&wrap) && + binding->Set(context, tns::ToV8String(isolate, "unwrap"), unwrap).FromMaybe(false) && + binding->Set(context, tns::ToV8String(isolate, "wrap"), wrap).FromMaybe(false); +} + void WrapperLifetimeCanary::InstallDeallocHook(Class extendedClass) { if (GetWrapperLifetimeMode() != WrapperLifetimeMode::kCanary) { return; diff --git a/NativeScript/runtime/js/weak-collections.js b/NativeScript/runtime/js/weak-collections.js new file mode 100644 index 000000000..27d57b761 --- /dev/null +++ b/NativeScript/runtime/js/weak-collections.js @@ -0,0 +1,146 @@ +"use strict"; + +// Canary wrapper lifetime mode only (run by WeakRef.cpp): WeakMap, WeakSet, +// WeakRef and FinalizationRegistry key a native object by its lifetime key +// instead of its wrapper. The wrapper is a canary that dies with its JS +// 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. + +const { unwrap, wrap } = binding; +const { ObjectDefineProperty, TypeError } = primordials; +const { + FinalizationRegistry: NativeFinalizationRegistry, + WeakMap: NativeWeakMap, + WeakRef: NativeWeakRef, + WeakSet: NativeWeakSet, +} = globalThis; + +// 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. +class WeakMap extends NativeWeakMap { + get(key) { + return super.get(unwrap(key, false)); + } + + set(key, value) { + super.set(unwrap(key, true), value); + return this; + } + + has(key) { + return super.has(unwrap(key, false)); + } + + delete(key) { + return super.delete(unwrap(key, false)); + } +} + +// V8 ships the upsert proposal; inherited, these would key on the wrapper. +if (typeof NativeWeakMap.prototype.getOrInsert === "function") { + ObjectDefineProperty(WeakMap.prototype, "getOrInsert", { + value: function getOrInsert(key, value) { + return NativeWeakMap.prototype.getOrInsert.call(this, unwrap(key, true), value); + }, + writable: true, + configurable: true, + }); +} +if (typeof NativeWeakMap.prototype.getOrInsertComputed === "function") { + ObjectDefineProperty(WeakMap.prototype, "getOrInsertComputed", { + value: function getOrInsertComputed(key, callbackfn) { + if (typeof callbackfn !== "function") { + throw new TypeError("WeakMap.prototype.getOrInsertComputed: callback is not a function"); + } + // The callback sees the key it was given, not the lifetime key. + return NativeWeakMap.prototype.getOrInsertComputed.call(this, unwrap(key, true), () => + callbackfn(key), + ); + }, + writable: true, + configurable: true, + }); +} + +class WeakSet extends NativeWeakSet { + add(value) { + super.add(unwrap(value, true)); + return this; + } + + has(value) { + return super.has(unwrap(value, false)); + } + + delete(value) { + return super.delete(unwrap(value, false)); + } +} + +class WeakRef extends NativeWeakRef { + constructor(target) { + const key = unwrap(target, true); + super(key); + // The target itself, not only its key, stays alive until the job ends. + if (key !== target) { + new NativeWeakRef(target); + } + } + + // The live wrapper, a new one if the old one died while the native object + // lived, or undefined once the native object is gone. Like a native WeakRef + // 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) { + 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. +ObjectDefineProperty(WeakRef.prototype, "get", { + value: WeakRef.prototype.deref, + writable: true, + configurable: true, +}); + +class FinalizationRegistry extends NativeFinalizationRegistry { + constructor(cleanupCallback) { + 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"); + } + super.register( + unwrap(target, true), + heldValue, + unregisterToken === undefined ? undefined : unwrap(unregisterToken, true), + ); + } + + unregister(unregisterToken) { + return super.unregister(unwrap(unregisterToken, false)); + } +} + +for (const [name, value] of [ + ["WeakMap", WeakMap], + ["WeakSet", WeakSet], + ["WeakRef", WeakRef], + ["FinalizationRegistry", FinalizationRegistry], +]) { + ObjectDefineProperty(globalThis, name, { + value, + writable: true, + configurable: true, + enumerable: false, + }); +} diff --git a/TestFixtures/Marshalling/TNSAllocLog.h b/TestFixtures/Marshalling/TNSAllocLog.h index e9e035362..9fdcda2ef 100644 --- a/TestFixtures/Marshalling/TNSAllocLog.h +++ b/TestFixtures/Marshalling/TNSAllocLog.h @@ -248,4 +248,15 @@ id TNSOwnedPrimitivesObjectAt(TNSOwnedPrimitives* holder, NSUInteger index); @end +// Hands out instances of a class made at runtime and flagged the way the ObjC +// runtime flags Swift actors: objc_setAssociatedObject on one aborts the +// process. +@interface TNSAssociationForbidder : NSObject + ++ (id)forbiddingInstance; +// Whether the class of `object` carries that flag. ++ (BOOL)forbidsAssociatedObjects:(id)object; + +@end + #endif /* TNSAllocLog_h */ diff --git a/TestFixtures/Marshalling/TNSAllocLog.m b/TestFixtures/Marshalling/TNSAllocLog.m index 190f365a2..b409e2244 100644 --- a/TestFixtures/Marshalling/TNSAllocLog.m +++ b/TestFixtures/Marshalling/TNSAllocLog.m @@ -1,5 +1,6 @@ #import "TNSAllocLog.h" #import +#import #import "../TNSTestCommon.h" @implementation TNSAllocLog @@ -488,3 +489,33 @@ + (void)hookPlaceholder { } @end + +extern uintptr_t objc_debug_class_rw_data_mask; + +// RW_FORBIDS_ASSOCIATED_OBJECTS, in the class_rw_t flags objc_class reaches +// through `bits`, after isa, superclass and the cache. +static const uint32_t TNSForbidsAssociatedObjectsFlag = 1u << 20; + +static uint32_t* TNSClassRWFlags(Class cls) { + uintptr_t bits = ((const uintptr_t*)(__bridge const void*)cls)[4]; + return (uint32_t*)(bits & objc_debug_class_rw_data_mask); +} + +@implementation TNSAssociationForbidder + ++ (id)forbiddingInstance { + static Class forbidding; + static dispatch_once_t once; + dispatch_once(&once, ^{ + forbidding = objc_allocateClassPair([NSObject class], "TNSAssociationForbiddingObject", 0); + *TNSClassRWFlags(forbidding) |= TNSForbidsAssociatedObjectsFlag; + objc_registerClassPair(forbidding); + }); + return [[forbidding alloc] init]; +} + ++ (BOOL)forbidsAssociatedObjects:(id)object { + return (*TNSClassRWFlags(object_getClass(object)) & TNSForbidsAssociatedObjectsFlag) != 0; +} + +@end diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index faa701756..197721051 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -34,6 +34,10 @@ describe("Wrapper lifetime modes", function () { return table.allObjects.count; } + // The engine's WeakRef: in canary mode the global one keys a native object + // by its lifetime, so it outlives the wrapper it was given. + var WrapperWeakRef = canary ? Object.getPrototypeOf(WeakRef) : WeakRef; + function deallocs() { return (TNSGetOutput().match(/TNSAllocLog dealloc/g) || []).length; } @@ -63,7 +67,7 @@ describe("Wrapper lifetime modes", function () { (function () { var obj = make(); owner.addObject(obj); - first = new WeakRef(obj); + first = new WrapperWeakRef(obj); })(); settle(2, function () { if (canary) { @@ -136,7 +140,7 @@ describe("Wrapper lifetime modes", function () { }); }); - it("keeps WeakMap entries and WeakRef targets of natively owned objects when every wrapper is rooted", function (done) { + it("keeps WeakMap entries and WeakRef targets of natively owned objects when every wrapper is in scope", function (done) { var owner = NSMutableArray.new(); var map = new WeakMap(); var ref; @@ -148,7 +152,7 @@ describe("Wrapper lifetime modes", function () { })(); settle(2, function () { var obj = owner.objectAtIndex(0); - if (rooted && scope === "all") { + if (keepsState && scope === "all") { expect(map.get(obj)).toBe("value"); expect(ref.deref()).toBe(obj); } @@ -493,15 +497,21 @@ describe("Wrapper lifetime modes", function () { it("hands back a native object's JS properties on a new wrapper after the old one died", function (done) { var owner = NSMutableArray.new(); var first; + var firstWrapper; (function () { var obj = Extended.new(); obj.tag = "kept"; owner.addObject(obj); first = new WeakRef(obj); + firstWrapper = new WrapperWeakRef(obj); })(); settle(2, function () { if (canary) { - expect(first.deref()).toBeUndefined(); + expect(WrapperWeakRef).not.toBe(WeakRef); + expect(firstWrapper.deref()).toBeUndefined(); + // The global WeakRef keys the native object, so it hands back + // the live one on a new wrapper. + expect(first.deref().tag).toBe("kept"); } var obj = owner.objectAtIndex(0); if (keepsState) { @@ -788,18 +798,15 @@ describe("Wrapper lifetime modes", function () { }); }); - it("iterates an NSArray through an unbound Symbol.iterator outside canary mode", function () { - if (canary) { - return; - } + // Inherited from the NSArray prototype, so like Array's it needs `this`. + it("iterates an NSArray through its inherited Symbol.iterator and rejects an unbound call", function () { var array = NSArray.arrayWithArray(["a", "b"]); + expect(Object.prototype.hasOwnProperty.call(array, Symbol.iterator)).toBe(false); + expect(Array.from(array)).toEqual(["a", "b"]); var iterate = array[Symbol.iterator]; - var iterator = iterate(); - var values = []; - for (var step = iterator.next(); !step.done; step = iterator.next()) { - values.push(step.value); - } - expect(values).toEqual(["a", "b"]); + expect(function () { + iterate(); + }).toThrowError(TypeError, /non-NSArray/); }); it("leaves plain objects outside the extended scope without forwarding", function (done) { @@ -1481,14 +1488,16 @@ describe("Wrapper lifetime modes", function () { obj.tag = "kept"; owner.addObject(obj); handle = interop.handleof(obj); - first = new WeakRef(obj); + first = new WrapperWeakRef(obj); })(); settle(2, function () { + if (canary) { + expect(first.deref()).toBeUndefined(); + } var before = stats(); var again = new TNSAllocLog(handle); expect(again instanceof TNSAllocLog).toBe(true); if (canary) { - expect(first.deref()).toBeUndefined(); expect(Object.getPrototypeOf(again)).toBe(TNSAllocLog.prototype); } if (keepsState) { diff --git a/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js new file mode 100644 index 000000000..968eda912 --- /dev/null +++ b/TestRunner/app/tests/WrapperLifetimeWeakCollectionsTests.js @@ -0,0 +1,336 @@ +// WeakMap, WeakSet, WeakRef and FinalizationRegistry keyed on native objects. +// The expectations hold in every wrapper lifetime mode; under "canary" they +// depend on the weak collections keying a native object by its lifetime key +// rather than by its current wrapper. +describe("Weak collections of native objects", function () { + var runtime = require("ns:runtime"); + var canary = runtime.getConfig("wrapperLifetimeMode") === "canary"; + // The engine's WeakRef, which keys the wrapper itself. + var WrapperWeakRef = canary ? Object.getPrototypeOf(WeakRef) : WeakRef; + + var Extended = (function (_super) { + __extends(Extended, _super); + function Extended() { + return (_super !== null && _super.apply(this, arguments)) || this; + } + return Extended; + })(TNSAllocLog); + + function scrub(n) { + return n > 0 ? scrub(n - 1) + n : 0; + } + + function settle(rounds, callback) { + scrub(300); + __collect(); + __collect(); + setTimeout(function () { + NSObject.new(); + if (rounds > 1) { + settle(rounds - 1, callback); + } else { + callback(); + } + }, 10); + } + + it("keeps a WeakMap entry for a native object across a wrapper change", function (done) { + var owner = NSMutableArray.new(); + var map = new WeakMap(); + var first; + (function () { + var obj = Extended.new(); + map.set(obj, "value"); + owner.addObject(obj); + first = new WrapperWeakRef(obj); + })(); + settle(2, function () { + if (canary) { + // The first wrapper died; the entry belongs to the native object. + expect(first.deref()).toBeUndefined(); + } + var obj = owner.objectAtIndex(0); + expect(map.get(obj)).toBe("value"); + expect(map.has(obj)).toBe(true); + expect(map.delete(obj)).toBe(true); + expect(map.has(obj)).toBe(false); + done(); + }); + }); + + it("keeps a WeakSet member for a native object across a wrapper change", function (done) { + var owner = NSMutableArray.new(); + var set = new WeakSet(); + (function () { + var obj = Extended.new(); + set.add(obj); + owner.addObject(obj); + })(); + settle(2, function () { + expect(set.has(owner.objectAtIndex(0))).toBe(true); + done(); + }); + }); + + it("dereferences a WeakRef to a natively owned object while the object lives", function (done) { + var owner = NSMutableArray.new(); + var ref; + (function () { + var obj = Extended.new(); + obj.tag = "kept"; + ref = new WeakRef(obj); + owner.addObject(obj); + })(); + settle(2, function () { + var target = ref.deref(); + expect(target).toBe(owner.objectAtIndex(0)); + expect(target.tag).toBe("kept"); + expect(ref.get()).toBe(target); + done(); + }); + }); + + it("clears a WeakRef once the native object is gone", function (done) { + var owner = NSMutableArray.new(); + var ref; + (function () { + var obj = Extended.new(); + ref = new WeakRef(obj); + owner.addObject(obj); + })(); + settle(2, function () { + owner.removeAllObjects(); + var attempts = 10; + (function poll() { + settle(1, function () { + if (ref.deref() === undefined || --attempts === 0) { + expect(ref.deref()).toBeUndefined(); + done(); + return; + } + poll(); + }); + })(); + }); + }); + + it("runs a FinalizationRegistry callback once the native object is gone", function (done) { + var owner = NSMutableArray.new(); + var held = []; + var registry = new FinalizationRegistry(function (value) { + held.push(value); + }); + (function () { + var obj = Extended.new(); + registry.register(obj, "held"); + owner.addObject(obj); + })(); + settle(2, function () { + expect(held).toEqual([]); + owner.removeAllObjects(); + var attempts = 15; + (function poll() { + settle(1, function () { + if (held.length > 0 || --attempts === 0) { + expect(held).toEqual(["held"]); + done(); + return; + } + poll(); + }); + })(); + }); + }); + + it("keeps a getOrInsert entry for a native object across a wrapper change", function (done) { + if (typeof WeakMap.prototype.getOrInsert !== "function") { + done(); + return; + } + var owner = NSMutableArray.new(); + var map = new WeakMap(); + var seenKey; + (function () { + var obj = Extended.new(); + expect(map.getOrInsert(obj, "value")).toBe("value"); + var other = Extended.new(); + expect(map.getOrInsertComputed(other, function (key) { + seenKey = key; + return "computed"; + })).toBe("computed"); + expect(seenKey).toBe(other); + owner.addObject(obj); + owner.addObject(other); + })(); + settle(2, function () { + expect(map.get(owner.objectAtIndex(0))).toBe("value"); + expect(map.get(owner.objectAtIndex(1))).toBe("computed"); + done(); + }); + }); + + it("unregisters a FinalizationRegistry target through a later wrapper of its token", function (done) { + var owner = NSMutableArray.new(); + var registry = new FinalizationRegistry(function () {}); + var target = {}; + (function () { + var obj = Extended.new(); + registry.register(target, "held", obj); + owner.addObject(obj); + })(); + settle(2, function () { + expect(registry.unregister(owner.objectAtIndex(0))).toBe(true); + target = null; + expect(FinalizationRegistry.length).toBe(1); + done(); + }); + }); + + it("keys a Proxy around a native object by the Proxy", function () { + var obj = Extended.new(); + var proxy = new Proxy(obj, {}); + var map = new WeakMap(); + map.set(obj, "target"); + expect(map.has(proxy)).toBe(false); + map.set(proxy, "proxy"); + expect(map.get(obj)).toBe("target"); + expect(map.get(proxy)).toBe("proxy"); + expect(new WeakRef(proxy).deref()).toBe(proxy); + }); + + it("does not give state to a native object a lookup only probes", function () { + var before = runtime.getWrapperLifetimeStats(); + var map = new WeakMap(); + var set = new WeakSet(); + for (var i = 0; i < 8; i++) { + var obj = Extended.new(); + map.has(obj); + map.get(obj); + map.delete(obj); + set.has(obj); + } + if (canary) { + expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(before.canaryStates); + } + }); + + it("rejects a FinalizationRegistry registration whose target is its held value", function () { + var registry = new FinalizationRegistry(function () {}); + var obj = Extended.new(); + expect(function () { + registry.register(obj, obj); + }).toThrowError(TypeError); + }); + + it("lets a JS dealloc key a weak collection on its receiver", function (done) { + var map = new WeakMap(); + var ran = 0; + var Keyed = TNSAllocLog.extend({ + dealloc: function () { + map.set(this, "dying"); + ran++; + this.super.dealloc(); + } + }, { name: "WrapperLifetimeWeakCollectionsDeallocKey" }); + var owner = NSMutableArray.new(); + (function () { + var obj = Keyed.new(); + obj.tag = "state"; + owner.addObject(obj); + })(); + settle(2, function () { + owner.removeAllObjects(); + settle(2, function () { + expect(ran).toBe(1); + done(); + }); + }); + }); + + // Under canary mode such an object gets no state: the token that would + // report its deallocation cannot be associated with it. + it("keys an object whose class forbids associated objects by its wrapper", function () { + var obj = TNSAssociationForbidder.forbiddingInstance(); + expect(TNSAssociationForbidder.forbidsAssociatedObjects(obj)).toBe(true); + var before = runtime.getWrapperLifetimeStats(); + var map = new WeakMap(); + map.set(obj, "value"); + expect(map.get(obj)).toBe("value"); + var set = new WeakSet(); + set.add(obj); + expect(set.has(obj)).toBe(true); + expect(new WeakRef(obj).deref()).toBe(obj); + var registry = new FinalizationRegistry(function () {}); + registry.register(obj, "held", obj); + expect(registry.unregister(obj)).toBe(true); + obj.tag = "kept"; + Object.defineProperty(obj, "defined", { value: 1, configurable: true }); + expect(obj.tag).toBe("kept"); + expect(obj.defined).toBe(1); + if (canary) { + expect(runtime.getWrapperLifetimeStats().canaryStates).toBe(before.canaryStates); + } + }); + + it("keeps working as the standard collections for other keys", function () { + var key = {}; + var map = new WeakMap([[key, 1]]); + expect(map.get(key)).toBe(1); + expect(map instanceof WeakMap).toBe(true); + expect(new WeakSet([key]).has(key)).toBe(true); + expect(new WeakRef(key).deref()).toBe(key); + }); + + it("iterates NSArray wrappers through the prototype", function () { + var array = NSArray.arrayWithArray([1, 2, 3]); + var seen = []; + for (var value of array) { + seen.push(value); + } + expect(seen).toEqual([1, 2, 3]); + expect([].concat(Array.from(NSMutableArray.arrayWithArray(["a"])))).toEqual(["a"]); + expect(Object.getOwnPropertySymbols(array).length).toBe(0); + }); + + // A JS class gets a fast-enumeration override only for an iterator it + // defines itself, not for the one it inherits from NSArray. + it("enumerates natively a JS subclass of NSArray that inherits the iterator", function () { + var Pair = (function (_super) { + __extends(Pair, _super); + function Pair() { + return (_super !== null && _super.apply(this, arguments)) || this; + } + Pair.prototype.objectAtIndex = function (index) { + return index === 0 ? "first" : "second"; + }; + Object.defineProperty(Pair.prototype, "count", { + get: function () { + return 2; + }, + enumerable: true, + configurable: true + }); + return Pair; + })(NSArray); + var pair = Pair.alloc().init(); + TNSClearOutput(); + TNSObjCTypes.alloc().init().methodWithNSArray(pair); + expect(TNSGetOutput()).toBe("firstsecond"); + TNSClearOutput(); + }); + + // With no native enumeration to inherit, an inherited JS iterator still + // backs native enumeration. + it("enumerates natively a JS class whose iterator comes from its implementation's prototype", function () { + var Iterated = NSObject.extend(Object.create({ + [Symbol.iterator]: function* () { + yield "only"; + } + }), { name: "WrapperLifetimeWeakCollectionsInheritedIterator" }); + var obj = Iterated.alloc().init(); + TNSClearOutput(); + TNSObjCTypes.alloc().init().methodWithNSArray(obj); + expect(TNSGetOutput()).toBe("only"); + TNSClearOutput(); + }); +}); diff --git a/TestRunner/app/tests/index.js b/TestRunner/app/tests/index.js index a3ddf8ddb..6f3bbed63 100644 --- a/TestRunner/app/tests/index.js +++ b/TestRunner/app/tests/index.js @@ -142,6 +142,7 @@ require("./JSAccessorEncodingTests"); require("./JSAccessorReceiverTests"); require("./WrapperLifetimeTests"); require("./WrapperLifetimeModesTests"); +require("./WrapperLifetimeWeakCollectionsTests"); require("./BlockCacheRaceTests"); require("./WorkerConcurrentStartupTests"); require("./WorkerOptionsTests"); diff --git a/docs/knowledge/v8-resurrecting-finalizers.md b/docs/knowledge/v8-resurrecting-finalizers.md index 60ff2e150..9e05926e4 100644 --- a/docs/knowledge/v8-resurrecting-finalizers.md +++ b/docs/knowledge/v8-resurrecting-finalizers.md @@ -288,23 +288,42 @@ inherits, whose receiver still reads the state), and then retires them. For othe a compile-time choice (`NS_CANARY_DEATH_SIGNAL` in `WrapperLifetimeCanary.mm`): by default a token associated with the object reports it from its `-dealloc`, immediately and on any thread, but the ObjC runtime aborts -the process for a class that forbids associated objects, and there is no public query for that. -Code that calls `objc_removeAssociatedObjects` on a live object releases the token too, which +the process for a class that forbids associated objects (Swift actors, for one), and there is no +public query for that. The runtime checks the flag `objc_setAssociatedObject` aborts on +(`RW_FORBIDS_ASSOCIATED_OBJECTS` in the object's class's `class_rw_t`, reached through the +`objc_debug_class_rw_data_mask` libobjc exports for debuggers) before it attaches a token; an +object it flags, or whose class it cannot read, gets no state: its JS properties stay on the +canary and are gone after a replacement, and the weak collections key its wrapper. Code that +calls `objc_removeAssociatedObjects` on a live object releases the token too, which retires the state as if the object had died: the next write through the canary starts a new state from the canary's properties, but if the canary dies before such a write, a canary built -later starts without them. The alternative keeps an ObjC weak reference in each record +later starts without them. The weak collections' key for the object hangs off that state, so +what they keyed on the object is lost with it: `WeakMap` and `WeakSet` lookups miss, +`WeakRef.prototype.deref` returns `undefined`, and once the old key is collected the +`FinalizationRegistry` cleanup for it runs although the object lives. A JS write through the +canary starts a new state but no key; the next `set`, `add`, `new WeakRef` or `register` on the +object makes a new key, and entries keyed on the old one never come back. This stays a +limitation: `objc_removeAssociatedObjects` removes every client's associations, and Apple +documents it as not for general use. The alternative keeps an ObjC weak reference in each record (`objc_initWeakOrNil`, which declines instead of aborting for classes that refuse weak references) and sweeps the records after every major collection, so state lingers until then. An instance of a class that refuses weak references and is not JS-extended gets no state -under it: its properties live on the canary alone and are gone after a replacement. With the -default, `"scope": "all"` is experimental because of that abort; `"extended"` attaches no -tokens at all. It cannot be +under it, in the same way. With the default, `"scope": "all"` stays experimental because that +check reads private runtime layout; `"extended"` attaches no tokens at all. It cannot be rooted at canary death instead, because a state reachable only through the canary is garbage in the same collection. The cost: state that references its own object (a closure over `this`) keeps it alive. A canary built after a death is a new object, but `===` cannot tell: only one canary lives at a time, and anything still referencing the old one would have kept it alive. -What does notice is whatever keys on the canary without keeping it: `WeakMap` keys and `WeakRef`s -of the old canary, and native `#private` fields, which are branded on it. Other +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 +refusing weak references or forbidding associated objects) by an opaque, frozen key held by its state, so entries and targets live +exactly as long as the native object, and `WeakRef.prototype.deref` returns the live canary, a new +one, or `undefined` once the object is gone. Lookups never create that key, and a Proxy around a +native object stays a key of its own. The cost is the other side of the same rooting: a `WeakMap` +value that references its native key keeps the key, its state and so the native object alive for +as long as the map lives, where in the other modes the entry would be collectable. Native `#private` fields are branded on the canary and do not carry over. Other limits: `Object.freeze`, `seal` and `preventExtensions` throw on a canary (V8 refuses them on objects with interceptors); index-keyed properties are not mirrored and die with the canary; a write through an object whose prototype is a canary lands on that object, since diff --git a/tools/js2c-inputs.xcfilelist b/tools/js2c-inputs.xcfilelist index dd339c974..ca3913c1d 100644 --- a/tools/js2c-inputs.xcfilelist +++ b/tools/js2c-inputs.xcfilelist @@ -30,5 +30,6 @@ $(SRCROOT)/NativeScript/runtime/js/require-factory.js $(SRCROOT)/NativeScript/runtime/js/structured-clone.js $(SRCROOT)/NativeScript/runtime/js/text-encoding.js $(SRCROOT)/NativeScript/runtime/js/ts-helpers.js +$(SRCROOT)/NativeScript/runtime/js/weak-collections.js $(SRCROOT)/NativeScript/runtime/js/weak-ref.js $(SRCROOT)/NativeScript/runtime/js/worker-events.js diff --git a/types/ns-runtime.d.ts b/types/ns-runtime.d.ts index 56e920d8c..ba86f6d78 100644 --- a/types/ns-runtime.d.ts +++ b/types/ns-runtime.d.ts @@ -57,9 +57,12 @@ declare module "ns:runtime" { * also copied to a state object kept until the native object is * deallocated, and a wrapper built for the object later gets them back * with their attributes. Native property writes still reach the native - * setter. Writes cost more than in the other modes; reads do not. Weak - * references to the old wrapper (WeakMap keys, WeakRef), native - * `#private` fields and index-keyed properties do not carry over. + * setter. Writes cost more than in the other modes; reads do not. + * WeakMap, WeakSet, WeakRef and FinalizationRegistry key such native + * objects by their lifetime in this mode, so they carry over too; a + * WeakMap value that references its own native key keeps that object + * alive while the map lives. Native `#private` fields and index-keyed + * properties do not. * State that references its own object keeps that object alive. */ wrapperLifetimeMode: "resurrecting" | "rooted" | "canary";