|
1 | 1 | #include "WrapperLifetimeCanary.h" |
2 | 2 | #import <Foundation/Foundation.h> |
| 3 | +#include <dlfcn.h> |
3 | 4 | #include <objc/message.h> |
4 | 5 | #include <objc/runtime.h> |
5 | 6 | #include <atomic> |
|
22 | 23 | // their class's own -dealloc (InstallDeallocHook) either way; for others: |
23 | 24 | // - ASSOCIATION: a token associated with the object reports it from its |
24 | 25 | // -dealloc, immediately and on any thread. The ObjC runtime aborts the |
25 | | -// process for a class that forbids associated objects (no public query). |
| 26 | +// process for a class that forbids associated objects (Swift actors, for |
| 27 | +// one) and has no public query for it: ForbidsAssociatedObjects reads the |
| 28 | +// runtime's private flag instead. |
26 | 29 | // - WEAK_SWEEP: each record holds a weak reference to the object, checked by a |
27 | | -// sweep after every major collection, so state lingers until then. Classes |
28 | | -// that refuse weak references keep their JS properties on the canary. |
| 30 | +// sweep after every major collection, so state lingers until then. |
| 31 | +// An object the chosen signal cannot watch (a class forbidding associated |
| 32 | +// objects, or refusing weak references) gets no state: its JS properties stay |
| 33 | +// on the canary and the weak collections key its wrapper. |
29 | 34 | #define NS_CANARY_DEATH_ASSOCIATION 1 |
30 | 35 | #define NS_CANARY_DEATH_WEAK_SWEEP 2 |
31 | 36 | #ifndef NS_CANARY_DEATH_SIGNAL |
@@ -222,6 +227,33 @@ bool IsExtendedInstance(id object) { |
222 | 227 | return false; |
223 | 228 | } |
224 | 229 |
|
| 230 | +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION |
| 231 | +// The check objc_setAssociatedObject aborts on: RW_FORBIDS_ASSOCIATED_OBJECTS |
| 232 | +// in the class_rw_t flags of the object's own class, reached through the mask |
| 233 | +// libobjc exports for debuggers. Private layout (objc_class keeps `bits` after |
| 234 | +// isa, superclass and the cache; the flags lead class_rw_t), so anything it |
| 235 | +// cannot read, a missing mask or an unrealized class, counts as forbidding. Not |
| 236 | +// a message, as IsExtendedInstance. |
| 237 | +bool ForbidsAssociatedObjects(id object) { |
| 238 | + static const uintptr_t* rwDataMask = |
| 239 | + static_cast<const uintptr_t*>(dlsym(RTLD_DEFAULT, "objc_debug_class_rw_data_mask")); |
| 240 | + if (rwDataMask == nullptr) { |
| 241 | + return true; |
| 242 | + } |
| 243 | + struct ClassLayout { |
| 244 | + void* isa; |
| 245 | + void* superclass; |
| 246 | + void* cache[2]; |
| 247 | + uintptr_t bits; |
| 248 | + }; |
| 249 | + const auto* cls = static_cast<const ClassLayout*>((__bridge const void*)object_getClass(object)); |
| 250 | + const auto* flags = reinterpret_cast<const uint32_t*>(cls->bits & *rwDataMask); |
| 251 | + constexpr uint32_t kRealized = 1u << 31; |
| 252 | + constexpr uint32_t kForbidsAssociatedObjects = 1u << 20; |
| 253 | + return flags == nullptr || (*flags & kRealized) == 0 || (*flags & kForbidsAssociatedObjects) != 0; |
| 254 | +} |
| 255 | +#endif |
| 256 | + |
225 | 257 | #if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP |
226 | 258 | // Lets go of the state of every object that was deallocated. Runs as an event |
227 | 259 | // loop task (under the Locker, on the runtime's thread), never inside the GC. |
@@ -453,6 +485,11 @@ bool StateForWrite(Isolate* isolate, Local<Object> holder, Local<Object>* out) { |
453 | 485 | bool replacesDied = canary->canaryRecord_ != nullptr; |
454 | 486 | // A JS-extended class's own -dealloc reports its instances' deaths. |
455 | 487 | bool extended = IsExtendedInstance(object); |
| 488 | +#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_ASSOCIATION |
| 489 | + if (!extended && ForbidsAssociatedObjects(object)) { |
| 490 | + return false; |
| 491 | + } |
| 492 | +#endif |
456 | 493 | // Allocated before the lock: a collection this allocation starts can run |
457 | 494 | // callbacks (the weak sweep's, a finalizer's -dealloc) that take it. |
458 | 495 | Local<Object> freshState = Object::New(isolate, Null(isolate), nullptr, nullptr, 0); |
|
0 commit comments