Repository navigation
Expand file tree
/
Copy pathObjectManager.mm
More file actions
1010 lines (912 loc) · 36.7 KB
/
Copy pathObjectManager.mm
File metadata and controls
1010 lines (912 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ObjectManager.h"
#include <Block.h>
#include <CoreFoundation/CoreFoundation.h>
#include <objc/message.h>
#include <objc/runtime.h>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Caches.h"
#include "ClassBuilder.h"
#include "Constants.h"
#include "DataWrapper.h"
#include "EventLoop.h"
#include "FFICall.h"
#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);
extern "C" id objc_loadWeakRetained(id* location);
extern "C" void objc_destroyWeak(id* location);
using namespace v8;
using namespace std;
namespace tns {
static Class NSTimerClass = objc_getClass("NSTimer");
namespace {
// Wrappers whose handle ObjectManager::ManageOwnership took over, keyed by the
// finalizer state their handle carries.
struct OwnershipTracking {
std::unordered_map<ObjectWeakCallbackState*, ObjCDataWrapper*> managed;
// ObjectManager::NativeRetainCountImp per class.
std::unordered_map<Class, IMP> retainCountImps;
};
void ForgetOwnership(Isolate* isolate, ObjectWeakCallbackState* state) {
if (OwnershipTracking* tracking = Caches::StateFor<OwnershipTracking>(isolate)) {
tracking->managed.erase(state);
}
}
void ApplyOwnership(ObjectWeakCallbackState* state, bool nativelyOwned) {
bool weak = state->target_->IsWeak();
if (nativelyOwned && weak) {
state->target_->ClearWeak<ObjectWeakCallbackState>();
} else if (!nativelyOwned && !weak) {
state->target_->SetWeak(state, ObjectManager::FinalizerCallback, WeakCallbackType::kFinalizer);
}
}
// For callers that may not enter JS (the GC prologue, the finalizer drain), so
// only a native -retainCount is read. Objects without one read as 0.
NSUInteger RetainCountWithoutJS(OwnershipTracking* tracking, id obj) {
Class cls = object_getClass(obj);
IMP imp;
if (tracking != nullptr) {
auto found = tracking->retainCountImps.find(cls);
if (found == tracking->retainCountImps.end()) {
found =
tracking->retainCountImps.emplace(cls, ObjectManager::NativeRetainCountImp(cls)).first;
}
imp = found->second;
} else {
imp = ObjectManager::NativeRetainCountImp(cls);
}
return imp != nullptr ? ((NSUInteger (*)(id, SEL))imp)(obj, @selector(retainCount)) : 0;
}
bool NativelyOwnedWithoutJS(OwnershipTracking* tracking, id obj) {
return RetainCountWithoutJS(tracking, obj) > 1;
}
bool NativelyOwnedWithoutJS(Isolate* isolate, id obj) {
return NativelyOwnedWithoutJS(Caches::ExistingStateFor<OwnershipTracking>(isolate), obj);
}
void ReconcileOwnershipBeforeGC(Isolate* isolate, GCType, GCCallbackFlags) {
// The teardown walk deletes registrations without forgetting them, so
// `managed` can hold freed states from then on.
if (Caches::IsTearingDown(isolate)) {
return;
}
OwnershipTracking* tracking = Caches::StateFor<OwnershipTracking>(isolate);
if (tracking == nullptr) {
return;
}
for (auto& [state, wrapper] : tracking->managed) {
if (state->disposing_ || state->target_ == nullptr || state->target_->IsEmpty()) {
continue;
}
ApplyOwnership(state, NativelyOwnedWithoutJS(tracking, wrapper->Data()));
}
}
// A finalized wrapper that carried JS properties while something other than
// the wrapper still retained its object. Confirmed on a later internal task,
// once the releases a collection on another thread queued for the runtime
// thread have run, so a co-collected owner does not produce a warning.
struct StateLostCandidate {
id* weakSlot;
std::string description;
std::string className;
std::string properties;
};
struct WrapperLifetimeState {
ObjectManager::LifetimeStats stats;
std::vector<StateLostCandidate> candidates;
bool confirmPosted = false;
// Times the pending confirmation stepped behind queued releases.
int confirmDeferrals = 0;
std::unordered_set<std::string> warnedClasses;
~WrapperLifetimeState() {
for (StateLostCandidate& candidate : candidates) {
objc_destroyWeak(candidate.weakSlot);
delete candidate.weakSlot;
}
}
};
bool ReleasesPendingOnRuntimeThread(Isolate* isolate);
bool PostStateLostConfirmation(Isolate* isolate);
void ConfirmStateLost(Isolate* isolate) {
auto* state = Caches::StateFor<WrapperLifetimeState>(isolate);
if (state == nullptr) {
return;
}
// The internal lane is FIFO, so posting again runs after the queued drain.
// Bounded, in case collections elsewhere keep queueing releases.
if (ReleasesPendingOnRuntimeThread(isolate) && state->confirmDeferrals < 4 &&
PostStateLostConfirmation(isolate)) {
state->confirmDeferrals++;
return;
}
state->confirmPosted = false;
state->confirmDeferrals = 0;
std::vector<StateLostCandidate> candidates;
candidates.swap(state->candidates);
for (StateLostCandidate& candidate : candidates) {
id obj = objc_loadWeakRetained(candidate.weakSlot);
if (obj != nil) {
[obj release];
state->stats.stateLostConfirmed++;
if (state->warnedClasses.insert(candidate.className).second) {
Log("NativeScript: JS properties (%s) set on %s were lost when its JavaScript wrapper was "
"garbage collected, but the native object is still alive. If native code hands it "
"back, it gets a new wrapper without them. Keep a JS reference to the object while "
"its JS state is needed. Reported once per class; disable with \"wrapperLifetime\": "
"{ \"warnings\": false }.",
candidate.properties.c_str(), candidate.description.c_str());
}
}
objc_destroyWeak(candidate.weakSlot);
delete candidate.weakSlot;
}
}
// Called from the finalizer drain just before the wrapper releases `target`.
void NoteStateLostCandidate(Isolate* isolate, Local<Object> obj, id target) {
if (IsTaggedPointer(target)) {
return;
}
NSUInteger count =
RetainCountWithoutJS(Caches::ExistingStateFor<OwnershipTracking>(isolate), target);
if (count <= 1 || count >= (NSUIntegerMax >> 1)) {
return;
}
SEL allowsWeak = sel_registerName("allowsWeakReference");
if ([target respondsToSelector:allowsWeak] &&
!reinterpret_cast<BOOL (*)(id, SEL)>(objc_msgSend)(target, allowsWeak)) {
return;
}
// The V8 calls below can run a nested collection whose JS releases target,
// so it is not touched after them.
const char* className = object_getClassName(target);
char description[256];
snprintf(description, sizeof(description), "<%s: %p>", className, (void*)target);
id* slot = new id(nil);
objc_initWeak(slot, target);
auto discardSlot = [slot]() {
objc_destroyWeak(slot);
delete slot;
};
HandleScope handleScope(isolate);
TryCatch tryCatch(isolate);
Local<Context> context = tns::GetCreationContextOrCurrent(isolate, obj);
Local<v8::Array> names;
if (context.IsEmpty() ||
!obj->GetOwnPropertyNames(context, PropertyFilter::SKIP_SYMBOLS).ToLocal(&names) ||
names->Length() == 0) {
discardSlot();
return;
}
auto* state =
Caches::IsTearingDown(isolate) ? nullptr : Caches::StateFor<WrapperLifetimeState>(isolate);
if (state == nullptr) {
discardSlot();
return;
}
state->stats.stateLostCandidates++;
std::string properties;
uint32_t shown = std::min<uint32_t>(names->Length(), 5);
for (uint32_t i = 0; i < shown; i++) {
Local<Value> name;
if (!names->Get(context, i).ToLocal(&name)) {
continue;
}
if (!properties.empty()) {
properties += ", ";
}
properties += tns::ToString(isolate, name);
}
if (names->Length() > shown) {
properties += ", ...";
}
state->candidates.push_back({slot, description, className, properties});
if (!state->confirmPosted) {
state->confirmPosted = PostStateLostConfirmation(isolate);
}
}
} // namespace
namespace {
void LinkRegistered(v8::Isolate* isolate, ObjectWeakCallbackState* state) {
std::shared_ptr<Caches> cache = Caches::Get(isolate);
if (cache == nullptr) {
return;
}
state->head_ = &cache->ObjectManagedValues;
state->next_ = *state->head_;
if (state->next_ != nullptr) {
state->next_->prev_ = state;
}
*state->head_ = state;
}
void UnlinkRegistered(ObjectWeakCallbackState* state);
} // namespace
void ObjectManager::Unlink(ObjectWeakCallbackState* state) { UnlinkRegistered(state); }
namespace {
void UnlinkRegistered(ObjectWeakCallbackState* state) {
if (state->head_ == nullptr) {
return;
}
if (state->prev_ != nullptr) {
state->prev_->next_ = state->next_;
} else if (*state->head_ == state) {
*state->head_ = state->next_;
}
if (state->next_ != nullptr) {
state->next_->prev_ = state->prev_;
}
state->head_ = nullptr;
state->prev_ = nullptr;
state->next_ = nullptr;
}
// The native release of an object that first reached JS on its runtime's
// thread belongs on that thread. JS, and with it a collection and its
// finalizers, can run on any thread that holds the isolate's Locker (a JS
// callback dispatched to a background queue), and a -dealloc that tears down a
// view, a CA transaction or a block capturing one crashes off the main thread.
// Objects first wrapped on another thread are still released where the
// collection runs.
//
// Every access holds the isolate's Locker: finalizers,
// __releaseNativeCounterpart and wrapper creation on any thread, and on the
// runtime's thread the drain task and teardown. So no other synchronization.
struct DeferredReleases {
// Object -> releases it is owed. An object stays alive while it is owed one,
// so its address cannot be reused by another object meanwhile.
std::unordered_map<void*, unsigned> objects;
std::vector<const void*> blocks;
bool drainPosted = false;
};
// A release can run JS (a -dealloc implemented in JS) that wraps an object
// whose own release still waits, or that reaches this drain again. So one
// release leaves the queue at a time, the rest stay there for
// TakeDeferredRelease, and nothing is held across a release.
void DrainDeferredReleases(DeferredReleases* pending) {
pending->drainPosted = false;
while (!pending->objects.empty()) {
auto it = pending->objects.begin();
id obj = (__bridge id)it->first;
if (--it->second == 0) {
pending->objects.erase(it);
}
[obj release];
}
while (!pending->blocks.empty()) {
const void* block = pending->blocks.back();
pending->blocks.pop_back();
Block_release(block);
}
}
// The queue to hand a release to, or null when the caller releases inline.
// Created by Init, so a collection on another thread never creates it. A
// collection can still run on another thread once teardown has begun, which
// drains the queue.
DeferredReleases* DeferralQueue(Isolate* isolate, bool bornOnRuntimeThread) {
if (!bornOnRuntimeThread || OnRuntimeThreadOf(isolate)) {
return nullptr;
}
return Caches::ExistingStateFor<DeferredReleases>(isolate);
}
// One event loop task per batch. If the loop no longer takes work, teardown is
// under way and drains the queue itself.
void PostDrain(Isolate* isolate, DeferredReleases* pending) {
if (pending->drainPosted) {
return;
}
Runtime* runtime = Runtime::GetRuntime(isolate);
std::shared_ptr<EventLoop> loop = runtime != nullptr ? runtime->GetEventLoop() : nullptr;
pending->drainPosted = loop != nullptr && loop->PostInternal([isolate]() {
if (DeferredReleases* queued = Caches::ExistingStateFor<DeferredReleases>(isolate)) {
DrainDeferredReleases(queued);
}
});
}
bool ReleasesPendingOnRuntimeThread(Isolate* isolate) {
DeferredReleases* pending = Caches::ExistingStateFor<DeferredReleases>(isolate);
return pending != nullptr && pending->drainPosted;
}
bool PostStateLostConfirmation(Isolate* isolate) {
Runtime* runtime = Runtime::GetRuntime(isolate);
std::shared_ptr<EventLoop> loop = runtime != nullptr ? runtime->GetEventLoop() : nullptr;
return loop != nullptr && loop->PostInternal([isolate]() { ConfirmStateLost(isolate); });
}
void ReleaseOnRuntimeThread(Isolate* isolate, id obj, bool bornOnRuntimeThread) {
DeferredReleases* pending = DeferralQueue(isolate, bornOnRuntimeThread);
if (pending == nullptr) {
[obj release];
return;
}
pending->objects[(__bridge void*)obj]++;
PostDrain(isolate, pending);
}
void BlockReleaseOnRuntimeThread(Isolate* isolate, const void* block, bool bornOnRuntimeThread) {
DeferredReleases* pending = DeferralQueue(isolate, bornOnRuntimeThread);
if (pending == nullptr) {
Block_release(block);
return;
}
pending->blocks.push_back(block);
PostDrain(isolate, pending);
}
} // namespace
void ObjectManager::Init(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
globalTemplate->Set(tns::ToV8String(isolate, "__releaseNativeCounterpart"),
FunctionTemplate::New(isolate, ReleaseNativeCounterpartCallback));
isolate->AddGCPrologueCallback(
ReconcileOwnershipBeforeGC,
static_cast<GCType>(kGCTypeMarkSweepCompact | kGCTypeIncrementalMarking));
Caches::StateFor<DeferredReleases>(isolate);
// Created here so a collection on another thread never allocates them, and
// the warnings flag is read from the app config before any finalizer asks.
tns::InitReleasedMarkers(isolate);
Caches::StateFor<WrapperLifetimeState>(isolate);
tns::WrapperLifetimeWarningsEnabled();
WrapperLifetime::Init(isolate);
}
std::shared_ptr<Persistent<Value>> ObjectManager::Register(Local<Context> context,
const Local<Value> obj) {
Isolate* isolate = v8::Isolate::GetCurrent();
std::shared_ptr<Persistent<Value>> objectHandle =
std::make_shared<Persistent<Value>>(isolate, obj);
objectHandle->SetWrapperClassId(Constants::ClassTypes::ObjectManagedValue);
ObjectWeakCallbackState* state = new ObjectWeakCallbackState(objectHandle);
objectHandle->SetWeak(state, FinalizerCallback, WeakCallbackType::kFinalizer);
LinkRegistered(isolate, state);
return objectHandle;
}
namespace {
// The DataWrapper-tagged handles used to be reached through
// Isolate::VisitHandlesWithClassIds; they all live in Caches, so walk those
// directly instead.
template <typename Map>
void DisposeHandleMap(v8::Isolate* isolate, Map& map) {
for (auto& entry : map) {
if (entry.second == nullptr || entry.second->IsEmpty()) {
continue;
}
ObjectManager::DisposeValue(isolate, entry.second->Get(isolate), true);
}
}
void DisposeHandle(v8::Isolate* isolate,
const std::unique_ptr<v8::Persistent<v8::Function>>& handle) {
if (handle == nullptr || handle->IsEmpty()) {
return;
}
ObjectManager::DisposeValue(isolate, handle->Get(isolate), true);
}
} // namespace
void ObjectManager::DisposeAllRegistered(Isolate* isolate) {
std::shared_ptr<Caches> cache = Caches::Get(isolate);
if (cache == nullptr) {
return;
}
// Runs from ~Runtime, which holds a Locker but has not entered the isolate;
// creating handles below requires it to be entered.
Isolate::Scope isolateScope(isolate);
HandleScope scope(isolate);
// The event loop has stopped taking work, so nothing else would drain.
DeferredReleases* deferred = Caches::ExistingStateFor<DeferredReleases>(isolate);
if (deferred != nullptr) {
DrainDeferredReleases(deferred);
}
// Detach the whole list first so disposal can't walk into freed entries, and
// claim every state up front: disposing one entry can re-enter JS from
// -dealloc and reach __releaseNativeCounterpart for any other entry this
// walk still owns.
// Wrappers that died before teardown are already off the list, waiting in
// the release queue.
WrapperLifetime::DrainForTeardown(isolate);
ObjectWeakCallbackState* state = cache->ObjectManagedValues;
cache->ObjectManagedValues = nullptr;
for (ObjectWeakCallbackState* claimed = state; claimed != nullptr; claimed = claimed->next_) {
claimed->disposing_ = true;
}
while (state != nullptr) {
ObjectWeakCallbackState* next = state->next_;
std::shared_ptr<Persistent<Value>> handle = state->target_;
bool released = false;
WrapperLifetime::DropRetainBeforeDisposal(state);
if (handle != nullptr && !handle->IsEmpty()) {
ObjectManager::DisposeValue(isolate, handle->Get(isolate), true);
released = true;
if (handle->IsWeak()) {
handle->ClearWeak<ObjectWeakCallbackState>();
}
handle->Reset();
}
WrapperLifetime::Retire(isolate, state, released);
delete state;
state = next;
}
// A walk that runs off the runtime's thread queues its own releases.
if (deferred != nullptr) {
DrainDeferredReleases(deferred);
}
DisposeHandleMap(isolate, cache->CtorFuncs);
DisposeHandleMap(isolate, cache->ProtocolCtorFuncs);
DisposeHandleMap(isolate, cache->CFunctions);
DisposeHandleMap(isolate, cache->PrimitiveInteropTypes);
DisposeHandle(isolate, cache->InteropReferenceCtorFunc);
DisposeHandle(isolate, cache->PointerCtorFunc);
DisposeHandle(isolate, cache->FunctionReferenceCtorFunc);
}
void ObjectManager::ManageOwnership(Isolate* isolate, ObjCDataWrapper* wrapper,
ObjectWeakCallbackState* state) {
if (Caches::IsTearingDown(isolate)) {
return;
}
OwnershipTracking* tracking = Caches::StateFor<OwnershipTracking>(isolate);
if (tracking == nullptr || state == nullptr) {
return;
}
wrapper->SetOwnershipState(state);
tracking->managed[state] = wrapper;
ObjectManager::ReconcileOwnership(isolate, wrapper);
}
void ObjectManager::ReconcileOwnership(Isolate* isolate, ObjCDataWrapper* wrapper) {
ObjectWeakCallbackState* state = wrapper->OwnershipState();
if (state == nullptr || state->disposing_ || state->target_ == nullptr ||
state->target_->IsEmpty()) {
return;
}
// The handle's strength carries the ownership; a GcProtect from the retain
// swizzle would keep a weak handle from ever being disposed.
wrapper->GcUnprotect();
ApplyOwnership(state, NativelyOwnedWithoutJS(isolate, wrapper->Data()));
}
void ObjectManager::FinalizerCallback(const WeakCallbackInfo<ObjectWeakCallbackState>& data) {
ObjectWeakCallbackState* state = data.GetParameter();
Isolate* isolate = data.GetIsolate();
if (state->disposing_) {
// Another frame owns this state's teardown — the DisposeAllRegistered
// walk, whose pre-claimed entries a nested collection can still condemn.
// Re-arm to satisfy the finalizer contract on this node and leave the
// clear/reset/delete to the owner.
state->target_->ClearWeak<void>();
state->target_->SetWeak(state, FinalizerCallback, WeakCallbackType::kFinalizer);
return;
}
state->disposing_ = true;
Local<Value> value = state->target_->Get(isolate);
bool disposed = ObjectManager::DisposeValue(isolate, value);
state->disposing_ = false;
// Disposal releases the native counterpart, and a -dealloc reached that way
// can reset this very handle (the collection adapters reset the persistent
// they were built from). An empty handle means the node is already freed, so
// there is nothing left to reset or re-arm -- ClearWeak/SetWeak would write
// through a dead slot -- and the registration must be retired even when
// disposal was refused.
if (disposed || state->target_->IsEmpty()) {
state->target_->Reset();
UnlinkRegistered(state);
ForgetOwnership(isolate, state);
delete state;
return;
}
state->target_->ClearWeak<void>();
state->target_->SetWeak(state, FinalizerCallback, WeakCallbackType::kFinalizer);
// Refused because native code took the object back: keep the handle strong
// rather than resurrecting it at every collection.
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
if (objcWrapper->OwnershipState() == state) {
ApplyOwnership(state, NativelyOwnedWithoutJS(isolate, objcWrapper->Data()));
}
}
}
bool ObjectManager::TakeDeferredRelease(Isolate* isolate, id obj) {
DeferredReleases* pending = Caches::ExistingStateFor<DeferredReleases>(isolate);
if (pending == nullptr || pending->objects.empty()) {
return false;
}
auto it = pending->objects.find((__bridge void*)obj);
if (it == pending->objects.end()) {
return false;
}
if (--it->second == 0) {
pending->objects.erase(it);
}
return true;
}
void ObjectManager::RunDeferredReleases(Isolate* isolate) {
if (!OnRuntimeThreadOf(isolate)) {
return;
}
if (DeferredReleases* pending = Caches::ExistingStateFor<DeferredReleases>(isolate)) {
DrainDeferredReleases(pending);
}
}
void ObjectManager::ProtectIfNativelyRetained(Isolate* isolate, id obj, ObjCDataWrapper* wrapper) {
// Only a wrapper the class's -release unprotects again may be protected.
if (ClassBuilder::TracksNativeRetains(isolate, object_getClass(obj)) && [obj retainCount] > 1) {
wrapper->GcProtect();
}
}
bool ObjectManager::DisposeValue(Isolate* isolate, Local<Value> value, bool isFinalDisposal) {
if (value.IsEmpty() || value->IsNullOrUndefined() || !value->IsObject()) {
return true;
}
Local<Object> obj = value.As<Object>();
if (obj->InternalFieldCount() > 1) {
Local<Value> superValue = obj->GetInternalField(1).As<v8::Value>();
if (!superValue.IsEmpty() && superValue->IsString()) {
// A "super" alias owns no wrapper; the one GetValue finds through it is
// its receiver's.
return true;
}
}
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
// NSLog(@"dispose %p", wrapper);
if (wrapper == nullptr) {
if (tns::GetReleasedReason(obj) == tns::ReleasedReason::kNone) {
tns::SetValue(isolate, obj, nullptr);
}
return true;
}
const WrapperType type = wrapper->Type();
if (wrapper->IsGcProtected() && !isFinalDisposal) {
if (type == WrapperType::ObjCObject) {
if (LifetimeStats* stats = GetLifetimeStats(isolate)) {
stats->resurrections++;
}
}
return false;
}
// A handle ReconcileOwnership keeps is weak while the Instances entry looked
// like the only owner; a retain from another thread can still be on its way
// to making it strong again.
if (!isFinalDisposal && wrapper->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
if (objcWrapper->OwnershipState() != nullptr &&
NativelyOwnedWithoutJS(isolate, objcWrapper->Data())) {
if (LifetimeStats* stats = GetLifetimeStats(isolate)) {
stats->resurrections++;
}
return false;
}
}
std::shared_ptr<Caches> cache = Caches::Get(isolate);
switch (type) {
case WrapperType::Struct:
// Lives in the struct object's ArrayBuffer storage, which the GC frees.
return true;
case WrapperType::ObjCObject: {
ObjCDataWrapper* objCObjectWrapper = static_cast<ObjCDataWrapper*>(wrapper);
id target = objCObjectWrapper->Data();
if (target != nil) {
// Instances is keyed on the raw address, so an entry rebuilt for a
// later object living there must survive this wrapper going away —
// only the entry that still points back at this object is ours.
auto it = cache->Instances.find(target);
if (it != cache->Instances.end()) {
Local<Value> cached = it->second->Get(isolate);
if (cached.IsEmpty() || cached == value) {
cache->Instances.erase(it);
}
}
if (!isFinalDisposal) {
if (LifetimeStats* stats = GetLifetimeStats(isolate)) {
stats->collected++;
}
if (tns::WrapperLifetimeWarningsEnabled()) {
NoteStateLostCandidate(isolate, obj, target);
// It allocates, so a nested collection may have run finalizers
// whose JS released this object explicitly.
if (tns::GetValue(isolate, obj) != wrapper) {
return true;
}
}
}
if (objCObjectWrapper->IsAdapterClaim()) {
// The adapter's -dealloc retires this wrapper; it must run first.
[target release];
} else {
ReleaseOnRuntimeThread(isolate, target, objCObjectWrapper->BornOnRuntimeThread());
}
}
break;
}
case WrapperType::Block: {
BlockWrapper* blockWrapper = static_cast<BlockWrapper*>(wrapper);
if (!blockWrapper->OwnsBlock()) {
// A block created from a JS callback is owned by the native code it
// was handed to (e.g. NSNotificationCenter), and it owns this wrapper
// (see Interop::JSBlock): its dispose helper frees both once the last
// native reference goes, possibly after this isolate is gone.
return true;
}
// Balance the Block_copy taken when a native block was wrapped for JS
// (see Interop::GetResult). Block_release is the correct counterpart to
// Block_copy and runs the block's dispose helper once we drop the last
// reference. (Using CFRelease here over-released stack blocks that were
// never promoted to the heap, crashing in objc_release during GC.)
BlockReleaseOnRuntimeThread(isolate, blockWrapper->Block(),
blockWrapper->BornOnRuntimeThread());
break;
}
case WrapperType::Reference: {
ReferenceWrapper* referenceWrapper = static_cast<ReferenceWrapper*>(wrapper);
if (referenceWrapper->Data() != nullptr) {
referenceWrapper->SetData(nullptr);
referenceWrapper->SetEncoding(nullptr);
}
break;
}
case WrapperType::Pointer: {
PointerWrapper* pointerWrapper = static_cast<PointerWrapper*>(wrapper);
if (pointerWrapper->Data() != nullptr) {
cache->PointerInstances.erase(pointerWrapper->Data());
if (pointerWrapper->IsAdopted()) {
std::free(pointerWrapper->Data());
pointerWrapper->SetData(nullptr);
}
}
break;
}
case WrapperType::FunctionReference: {
FunctionReferenceWrapper* funcWrapper = static_cast<FunctionReferenceWrapper*>(wrapper);
std::shared_ptr<Persistent<Value>> func = funcWrapper->Function();
if (func != nullptr) {
func->Reset();
}
break;
}
case WrapperType::AnonymousFunction: {
break;
}
case WrapperType::ExtVector: {
ExtVectorWrapper* extVectorWrapper = static_cast<ExtVectorWrapper*>(wrapper);
FFICall::DisposeFFIType(extVectorWrapper->FFIType(), extVectorWrapper->TypeEncoding());
void* data = extVectorWrapper->Data();
if (data) {
std::free(data);
}
break;
}
case WrapperType::Worker: {
WorkerWrapper* worker = static_cast<WorkerWrapper*>(wrapper);
// Final disposal lets go of the wrapper for good, and only deletes it
// when the worker thread is done with it; otherwise that thread deletes
// it when it ends.
//
// A weak callback deletes the wrapper only when the worker thread is
// done with it, and changes nothing otherwise. A running worker's Worker
// object is rooted (WorkerWrapper::RootWorkerObject), so a weak callback
// should not reach a live worker at all. The refusal stays as the floor
// under that: re-arming keeps the wrapper alive for another cycle, which
// is safe, whereas freeing it while the thread still posts through it is
// not. Reaching it is not free either -- a re-armed handle that is also a
// weak-collection key can corrupt the collector's ephemeron bookkeeping
// -- so it is a fallback, not a mechanism to rely on.
bool lastHolder = isFinalDisposal ? worker->ReleaseFromParent() : worker->HeldByParentOnly();
if (!lastHolder) {
return false;
}
break;
}
default:
break;
}
// A branch above can run arbitrary code -- [target release] reaching an ObjC
// -dealloc is the reachable one -- and that code may detach this wrapper or
// attach a different one. The object's internal field is the wrapper's owner,
// so only a wrapper still sitting in it is ours to free.
if (tns::GetValue(isolate, obj) == wrapper) {
delete wrapper;
if (type == WrapperType::ObjCObject) {
tns::MarkReleased(
isolate, obj,
isFinalDisposal ? tns::ReleasedReason::kTeardown : tns::ReleasedReason::kCollected);
} else {
tns::DeleteValue(isolate, obj);
}
}
return true;
}
void ObjectManager::ReleaseNativeCounterpartCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (info.Length() != 1) {
std::ostringstream errorStream;
errorStream << "Actual arguments count: \"" << info.Length() << "\". Expected: \"1\".";
std::string errorMessage = errorStream.str();
Local<Value> error = Exception::Error(tns::ToV8String(isolate, errorMessage));
isolate->ThrowException(error);
return;
}
// The lookup, the Instances key and the final SetValue must all address the
// same object, so a Proxy is resolved once here, and a "super" alias to the
// receiver whose wrapper it reads.
Local<Value> value = tns::UnwrapProxy(info[0]);
if (!value.IsEmpty() && value->IsObject()) {
Local<Object> receiver = tns::SuperAliasReceiver(value.As<Object>());
if (!receiver.IsEmpty()) {
value = receiver;
}
}
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper == nullptr) {
std::string arg0 = tns::ToString(isolate, info[0]);
std::ostringstream errorStream;
errorStream << arg0 << " is an object which is not a native wrapper.";
std::string errorMessage = errorStream.str();
Local<Value> error = Exception::Error(tns::ToV8String(isolate, errorMessage));
isolate->ThrowException(error);
return;
}
if (wrapper->Type() != WrapperType::ObjCObject) {
return;
}
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
if (objcWrapper->IsBorrowed()) {
if (tns::GetValue(isolate, value) == wrapper) {
tns::MarkReleased(isolate, value.As<Object>(), tns::ReleasedReason::kExplicit);
delete wrapper;
}
return;
}
id data = objcWrapper->Data();
if (data != nil) {
std::shared_ptr<Caches> cache = Caches::Get(isolate);
auto it = cache->Instances.find(data);
bool canary = false;
if (ObjectWeakCallbackState* managed = objcWrapper->OwnershipState()) {
// The handle ReconcileOwnership manages is this wrapper's own, whatever
// Instances maps the object to now.
if (managed->disposing_) {
return;
}
if (it != cache->Instances.end() && it->second == managed->target_) {
cache->Instances.erase(it);
}
managed->target_->Reset();
UnlinkRegistered(managed);
ForgetOwnership(isolate, managed);
delete managed;
objcWrapper->SetOwnershipState(nullptr);
} else if (ObjectWeakCallbackState* adopted = objcWrapper->LifetimeState()) {
// An adopted wrapper (WrapperLifetime) owns its registration whether or not
// the Instances entry still names it, and its handle may be strong.
if (adopted->disposing_) {
// Another frame owns this state and releases the object: the teardown
// walk, or an outer call still writing the canary's key below.
return;
}
canary = adopted->lifetimeCanary_;
if (canary) {
// Before the release below, which can run the object's -dealloc. The
// write can allocate, and the finalizers of the collection that
// triggers can run JS that calls this builtin on the same wrapper.
adopted->disposing_ = true;
WrapperLifetimeCanary::KeepWeakCollectionKey(isolate, value.As<Object>());
// The wrapper first: a release that got through would have freed it.
if (tns::GetValue(isolate, value) != wrapper || objcWrapper->LifetimeState() != adopted) {
return;
}
// Those finalizers can also have changed Instances.
it = cache->Instances.find(data);
}
if (it != cache->Instances.end() && it->second == adopted->target_) {
cache->Instances.erase(it);
}
adopted->target_->Reset();
WrapperLifetime::Forget(isolate, adopted);
objcWrapper->SetLifetimeState(nullptr);
UnlinkRegistered(adopted);
std::shared_ptr<CanaryRecord> record = std::move(adopted->canaryRecord_);
delete adopted;
WrapperLifetimeCanary::DropRecord(isolate, std::move(record));
} else if (it != cache->Instances.end()) {
std::shared_ptr<Persistent<Value>> handle = it->second;
bool wasWeak = handle->IsWeak();
ObjectWeakCallbackState* state =
wasWeak ? handle->ClearWeak<ObjectWeakCallbackState>() : nullptr;
if (state != nullptr && state->disposing_) {
// Reached from a -dealloc running inside this handle's own finalizer
// or the teardown walk: that frame already released the native
// counterpart and owns the state, so restore the weakness and let it
// finish.
handle->SetWeak(state, FinalizerCallback, WeakCallbackType::kFinalizer);
return;
}
cache->Instances.erase(it);
if (state != nullptr) {
// Reset before deleting the state: it frees the node, which clears the
// pending-finalizer bit and guarantees no callback can reach the freed
// parameter.
handle->Reset();
UnlinkRegistered(state);
ForgetOwnership(isolate, state);
delete state;
}
}
if (!canary && !objcWrapper->IsAdapterClaim()) {
// Before the release below, which can run the object's -dealloc. The
// write can allocate, and the finalizers of the collection that
// triggers can run JS that calls this builtin on the same wrapper: a
// release that got through freed the wrapper.
WrapperLifetimeCanary::KeepPlainWeakCollectionKey(isolate, value.As<Object>(), data);
if (tns::GetValue(isolate, value) != wrapper) {
return;
}
}
// Release the runtime's strong reference (taken when the object was first
// wrapped or adopted from an alloc/new/copy return). For instances solely
// owned by JS this deallocates them, on the runtime's thread (at once when
// called there); for shared natives (e.g. an NSNotificationCenter observer
// token) the remaining owners keep it alive.
// Calling [data dealloc] here, as this used to do, destroyed objects that
// were still referenced elsewhere and caused use-after-free crashes.
// Read before the release below: an adapter claim's -dealloc frees the
// wrapper, so it must not be touched afterwards.
bool adapterClaim = objcWrapper->IsAdapterClaim();
if (adapterClaim) {
[data release];
} else {
ReleaseOnRuntimeThread(isolate, data, objcWrapper->BornOnRuntimeThread());
}
// The release above can run a -dealloc that detaches this wrapper; the
// internal field owns it, so free only what is still attached. An
// adapter's claim is exempt: the adapter owns it exclusively and deletes
// it from its own -dealloc even after isolate teardown, so retiring it
// here would leave the adapter holding a stale pointer it later frees
// again.
if (!adapterClaim && tns::GetValue(isolate, value) == wrapper) {
delete wrapper;
tns::MarkReleased(isolate, value.As<Object>(), tns::ReleasedReason::kExplicit);
}
if (LifetimeStats* stats = GetLifetimeStats(isolate)) {
stats->releasedExplicitly++;
}
}
}
bool ObjectManager::IsInstanceOf(id obj, Class clazz) { return [obj isKindOfClass:clazz]; }
ObjectManager::LifetimeStats* ObjectManager::GetLifetimeStats(Isolate* isolate) {
auto* state = Caches::StateFor<WrapperLifetimeState>(isolate);
return state != nullptr ? &state->stats : nullptr;
}
long ObjectManager::GetRetainCount(id obj) {
if (!obj) {
return 0;
}
if (ObjectManager::IsInstanceOf(obj, NSTimerClass)) {
return 0;
}
return CFGetRetainCount(obj);
}
IMP ObjectManager::NativeRetainCountImp(Class cls) {
static Class timerPlaceholder = object_getClass([NSTimer alloc]);
if (cls == timerPlaceholder) {
return nullptr;
}
SEL selector = @selector(retainCount);
static SEL original = sel_registerName((Constants::SwizzledPrefix + "retainCount").c_str());
// Each class's own method list, rather than class_getMethodImplementation,
// so an alias inherited from a replaced ancestor never hides a native
// override below it.
for (Class c = ClassBuilder::FindNativeClass(cls); c != nil; c = class_getSuperclass(c)) {
unsigned int count = 0;
Method* methods = class_copyMethodList(c, &count);
IMP own = nullptr;
IMP kept = nullptr;
for (unsigned int i = 0; i < count; i++) {
SEL name = method_getName(methods[i]);
if (name == original) {
kept = method_getImplementation(methods[i]);
} else if (name == selector) {
own = method_getImplementation(methods[i]);
}