|
31 | 31 | static_cast<int64_t>(kUint64AllBitsSet << 53) + 1; // -9007199254740991 (-(2^53-1)) |
32 | 32 | static constexpr int64_t kMaxSafeInteger = -kMinSafeInteger; // 9007199254740991 (2^53-1) |
33 | 33 |
|
| 34 | +namespace { |
| 35 | + |
| 36 | +void DisposeJSBlockWrapper(MethodCallbackWrapper* wrapper, BlockWrapper* blockWrapper) { |
| 37 | + if (wrapper->isolateWrapper_.HandOffTeardown( |
| 38 | + [wrapper, blockWrapper]() { DisposeJSBlockWrapper(wrapper, blockWrapper); })) { |
| 39 | + return; |
| 40 | + } |
| 41 | + { |
| 42 | + IsolatePin pin = wrapper->isolateWrapper_.Pin(); |
| 43 | + if (pin) { |
| 44 | + Isolate* isolate = wrapper->isolateWrapper_.Isolate(); |
| 45 | + v8::Locker locker(isolate); |
| 46 | + if (!wrapper->isolateWrapper_.IsTornDown()) { |
| 47 | + Isolate::Scope isolate_scope(isolate); |
| 48 | + HandleScope handle_scope(isolate); |
| 49 | + Local<Value> callback = wrapper->callback_->Get(isolate); |
| 50 | + if (!callback.IsEmpty() && callback->IsObject() && |
| 51 | + tns::GetValue(isolate, callback) == blockWrapper) { |
| 52 | + tns::DeleteValue(isolate, callback); |
| 53 | + } |
| 54 | + wrapper->callback_->Reset(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + delete blockWrapper; |
| 59 | + delete wrapper; |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace |
| 63 | + |
34 | 64 | Interop::JSBlock::JSBlockDescriptor Interop::JSBlock::kJSBlockDescriptor = { |
35 | 65 | .reserved = 0, |
36 | 66 | .size = sizeof(JSBlock), |
|
40 | 70 | if (block->descriptor == &JSBlock::kJSBlockDescriptor) { |
41 | 71 | MethodCallbackWrapper* wrapper = static_cast<MethodCallbackWrapper*>(block->userData); |
42 | 72 | BlockWrapper* blockWrapper = block->blockWrapper; |
43 | | - // Runs on whatever thread drops the last native reference. That is |
44 | | - // safe inline: callback_ is a strong, unregistered persistent, so |
45 | | - // resetting it never touches the finalizer drain's bookkeeping, |
46 | | - // and a foreign-thread Locker into the block's own isolate is |
47 | | - // legitimate now that extended class names are worker-scoped. |
48 | | - // A thread holding another isolate's Locker is the exception: it |
49 | | - // gets here when that isolate was handed this block, and it |
50 | | - // deadlocks if this isolate is waiting for that Locker in turn. |
51 | | - // Blocks shared between isolates carry that risk. |
52 | | - // |
53 | | - // Validity is not the test here: the slot stays reachable after |
54 | | - // the isolate is invalidated, by JS still running under the Locker |
55 | | - // and by the teardown's own walk of registered objects. Until the |
56 | | - // gate closes, the wrapper is only freed once the slot is cleared. |
57 | | - { |
58 | | - IsolatePin pin = wrapper->isolateWrapper_.Pin(); |
59 | | - if (pin) { |
60 | | - Isolate* isolate = wrapper->isolateWrapper_.Isolate(); |
61 | | - v8::Locker locker(isolate); |
62 | | - if (!wrapper->isolateWrapper_.IsTornDown()) { |
63 | | - Isolate::Scope isolate_scope(isolate); |
64 | | - HandleScope handle_scope(isolate); |
65 | | - Local<Value> callback = wrapper->callback_->Get(isolate); |
66 | | - if (!callback.IsEmpty() && callback->IsObject()) { |
67 | | - // The slot may hold another wrapper by now; only our own is |
68 | | - // cleared from it. |
69 | | - if (tns::GetValue(isolate, callback) == blockWrapper) { |
70 | | - tns::DeleteValue(isolate, callback); |
71 | | - } |
72 | | - } |
73 | | - // Unconditional: an already-detached callback still owns its |
74 | | - // node, and dropping the persistent without a reset would leave |
75 | | - // that node rooted forever. |
76 | | - wrapper->callback_->Reset(); |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - // Outside the gate: once the teardown is done the cache slot is |
81 | | - // unreachable and nothing else can free the wrapper. |
82 | | - delete blockWrapper; |
83 | | - delete wrapper; |
| 73 | + // libclosure frees the block as soon as this helper returns. |
| 74 | + // Invalidate its cached address before deferring any V8 cleanup. |
| 75 | + blockWrapper->ClearBlock(); |
84 | 76 | ffi_closure_free(block->ffiClosure); |
85 | 77 | block->~JSBlock(); |
| 78 | + DisposeJSBlockWrapper(wrapper, blockWrapper); |
86 | 79 | } |
87 | 80 | }}; |
88 | 81 |
|
|
113 | 106 | return wrapper->callback_->Get(isolate); |
114 | 107 | } |
115 | 108 |
|
116 | | -// libclosure's increment ignores the deallocating bit, so Block_copy would hand |
117 | | -// out a block whose dispose is already waiting for the isolate's Locker and |
118 | | -// which libclosure frees right after. This CASes the same word libclosure does. |
| 109 | +// libclosure's increment ignores the deallocating bit. The wrapper's mutex |
| 110 | +// keeps the allocation alive here; this CAS also rejects a last release that |
| 111 | +// won before dispose acquired that mutex. |
119 | 112 | bool Interop::TryRetainJSBlock(JSBlock* block) { |
120 | 113 | volatile int32_t* flags = &block->flags; |
121 | 114 | int32_t old = __atomic_load_n(flags, __ATOMIC_RELAXED); |
|
133 | 126 | } |
134 | 127 | } |
135 | 128 |
|
| 129 | +void* BlockWrapper::RetainBlock() { |
| 130 | + if (ownsBlock_) { |
| 131 | + return Block_copy(block_); |
| 132 | + } |
| 133 | + std::lock_guard<std::mutex> lock(blockMutex_); |
| 134 | + if (block_ == nullptr || !Interop::TryRetainJSBlock(static_cast<Interop::JSBlock*>(block_))) { |
| 135 | + return nullptr; |
| 136 | + } |
| 137 | + return block_; |
| 138 | +} |
| 139 | + |
136 | 140 | std::pair<IMP, ffi_closure*> Interop::CreateMethodInternal(const uint8_t initialParamIndex, |
137 | 141 | const uint8_t argsCount, |
138 | 142 | const TypeEncoding* typeEncoding, |
@@ -683,16 +687,8 @@ static bool SnapshotArrayLike(Isolate* isolate, Local<Object> object, Local<v8:: |
683 | 687 | // so the reference that keeps it alive across the call must be balanced: |
684 | 688 | // the JSBlock dispose helper owns the ffi closure and the callback wrapper |
685 | 689 | // and only runs once the last reference goes away. |
686 | | - if (wrapper->OwnsBlock()) { |
687 | | - // A native block, or another isolate's JS block; the wrapper's own |
688 | | - // Block_copy keeps it alive. |
689 | | - blockPtr = CFAutorelease(Block_copy(wrapper->Block())); |
690 | | - } else if (TryRetainJSBlock(static_cast<JSBlock*>(wrapper->Block()))) { |
691 | | - // Reading the block is safe even when its last release raced ahead: |
692 | | - // until the isolate's teardown closes its gate, dispose clears this |
693 | | - // slot under the Locker this thread holds, before libclosure frees |
694 | | - // the block. |
695 | | - blockPtr = CFAutorelease(wrapper->Block()); |
| 690 | + if (void* block = wrapper->RetainBlock()) { |
| 691 | + blockPtr = CFAutorelease(block); |
696 | 692 | } |
697 | 693 | } |
698 | 694 | if (blockPtr == nullptr) { |
@@ -808,7 +804,7 @@ static bool SnapshotArrayLike(Isolate* isolate, Local<Object> object, Local<v8:: |
808 | 804 | } |
809 | 805 | } else if (argHelper.isObject()) { |
810 | 806 | Local<Object> obj = arg.As<Object>(); |
811 | | - BaseDataWrapper* wrapper = tns::GetValue(isolate, obj); |
| 807 | + BaseDataWrapper* wrapper = tns::GetValueForMarshalling(isolate, obj); |
812 | 808 |
|
813 | 809 | if (wrapper != nullptr) { |
814 | 810 | if (wrapper->Type() == WrapperType::Enum) { |
@@ -932,7 +928,7 @@ static bool SnapshotArrayLike(Isolate* isolate, Local<Object> object, Local<v8:: |
932 | 928 | ArrayAdapter* adapter = [[ArrayAdapter alloc] initWithJSObject:array isolate:isolate]; |
933 | 929 | return adapter; |
934 | 930 | } else if (arg->IsObject()) { |
935 | | - if (BaseDataWrapper* wrapper = tns::GetValue(isolate, arg)) { |
| 931 | + if (BaseDataWrapper* wrapper = tns::GetValueForMarshalling(isolate, arg)) { |
936 | 932 | switch (wrapper->Type()) { |
937 | 933 | case WrapperType::ObjCObject: { |
938 | 934 | ObjCDataWrapper* wr = static_cast<ObjCDataWrapper*>(wrapper); |
|
0 commit comments