Repository navigation
Expand file tree
/
Copy pathObjectManager.h
More file actions
172 lines (155 loc) · 8.51 KB
/
Copy pathObjectManager.h
File metadata and controls
172 lines (155 loc) · 8.51 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
#ifndef ObjectManager_h
#define ObjectManager_h
#include <objc/objc.h>
#include <atomic>
#include <cstdint>
#include "Common.h"
namespace tns {
struct CanaryRecord;
class ObjectManager;
class ObjCDataWrapper;
// Parameter of the kFinalizer weak callback armed on target_.
//
// Ownership: created by ObjectManager::Register and deleted by exactly two
// sites -- ObjectManager::FinalizerCallback's disposed branch and
// DisposeAllRegistered. Retiring a registration from anywhere else (the
// __releaseNativeCounterpart builtin is the only one) must Reset target_
// first: resetting frees the V8 node, which clears its pending-finalizer bit
// and guarantees no further callback, so the state is unreachable afterwards
// and safe to unlink and delete. Dropping the weakness without resetting
// leaves the node rooted forever with parameter() pointing at the freed state.
struct ObjectWeakCallbackState {
ObjectWeakCallbackState(std::shared_ptr<v8::Persistent<v8::Value>> target)
: target_(target) {}
std::shared_ptr<v8::Persistent<v8::Value>> target_;
// Links in the per-isolate registry (Caches::ObjectManagedValues) that
// teardown walks. Intrusive so that a wrapper unlinks itself in O(1) when it
// is disposed, rather than leaving an entry for a later sweep to find.
// head_ points at the owning cache's head slot so unlinking -- which happens
// in a GC finalizer -- needs no cache lookup.
ObjectWeakCallbackState** head_ = nullptr;
ObjectWeakCallbackState* prev_ = nullptr;
ObjectWeakCallbackState* next_ = nullptr;
// Set while one of the two owning sites is disposing this handle's value,
// or while __releaseNativeCounterpart writes a canary's weak collection key
// before retiring the registration it adopted. Disposal releases the native
// counterpart, whose -dealloc can re-enter JS and reach
// __releaseNativeCounterpart for this very handle, and the key write can
// run a collection whose finalizers do; retiring it there would free the
// state under the frame that owns it. Retirement observes the flag and
// leaves the handle to that frame.
bool disposing_ = false;
// Set when WrapperLifetime adopted this registration (an ObjC object wrapper
// under a phantom-callback mode). The JS object is unreadable after its
// phantom callback, so the wrapper and object are cached here.
ObjCDataWrapper* lifetimeWrapper_ = nullptr;
id lifetimeTarget_ = nullptr;
// WrapperLifetime's cross-isolate retain counter for lifetimeTarget_.
std::atomic<uint64_t>* runtimeRetains_ = nullptr;
// May be rooted; scanned when a major collection starts.
bool lifetimeTracked_ = false;
// The handle is currently strong.
bool lifetimeStrong_ = false;
// Canary mode: the wrapper mirrors JS properties into a state object that
// outlives it (WrapperLifetimeCanary), found through this record once the
// object has state.
bool lifetimeCanary_ = false;
std::shared_ptr<CanaryRecord> canaryRecord_;
};
class ObjectManager {
public:
// Per-isolate wrapper lifetime counters. Kept in every build: plain integers
// bumped on cold paths (finalizers, released-object reports) and by canary
// mode's write interceptor, exposed through ns:runtime's
// getWrapperLifetimeStats().
struct LifetimeStats {
// An ObjC wrapper's finalizer refused disposal because native code still
// retains its object (IsGcProtected, or an OwnershipState wrapper whose
// object is natively owned), keeping the wrapper alive.
uint64_t resurrections = 0;
// An ObjC wrapper's finalizer released its native object.
uint64_t collected = 0;
// __releaseNativeCounterpart released a native object.
uint64_t releasedExplicitly = 0;
// A released wrapper was used: as a native argument, collection element or
// return value, as a receiver (toString included), or through a released
// pointer, reference or struct.
uint64_t releasedAccesses = 0;
// A finalized ObjC wrapper carried JS properties while something other
// than the wrapper still retained its object, and whether that object was
// still alive a runloop turn later (see WrapperLifetimeWarningsEnabled).
uint64_t stateLostCandidates = 0;
uint64_t stateLostConfirmed = 0;
// Rooted mode (WrapperLifetime): wrappers made strong because native code
// owns their object, made weak because it no longer does, and dead
// wrappers whose object was queued for release.
uint64_t promotions = 0;
uint64_t demotions = 0;
uint64_t queuedReleases = 0;
// Canary mode: state objects created for native objects, and state
// objects let go of because their native object was deallocated.
uint64_t canaryStates = 0;
uint64_t canaryNativeDeaths = 0;
// Canary mode: writes the canary handlers saw.
uint64_t canaryInterceptedWrites = 0;
// Canary mode: of those, native property writes whose setter the write
// handler found in its cache rather than along the prototype chain.
uint64_t canaryCachedNativeWrites = 0;
};
static LifetimeStats* GetLifetimeStats(v8::Isolate* isolate);
static void Init(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> globalTemplate);
static std::shared_ptr<v8::Persistent<v8::Value>> Register(
v8::Local<v8::Context> context, const v8::Local<v8::Value> obj);
static void FinalizerCallback(
const v8::WeakCallbackInfo<ObjectWeakCallbackState>& data);
static bool DisposeValue(v8::Isolate* isolate, v8::Local<v8::Value> value,
bool isFinalDisposal = false);
// Disposes every handle from Register() that is still alive. Replaces the
// old Isolate::VisitHandlesWithClassIds walk, which V8 removed.
static void DisposeAllRegistered(v8::Isolate* isolate);
// Takes over the handle `state` belongs to (the wrapper's Instances entry):
// from now on it is strong while something besides that entry retains the
// object and weak otherwise, re-checked whenever ReconcileOwnership runs and
// when a major collection starts.
static void ManageOwnership(v8::Isolate* isolate, ObjCDataWrapper* wrapper,
ObjectWeakCallbackState* state);
// Requires the isolate locked and entered, on any thread.
static void ReconcileOwnership(v8::Isolate* isolate,
ObjCDataWrapper* wrapper);
// Removes a registration from the teardown list.
static void Unlink(ObjectWeakCallbackState* state);
// A collection can run on any thread that holds the isolate's Locker, but
// the native releases it causes are handed to the runtime's thread. A new
// Instances entry for an object whose release is still waiting takes that
// release over as its own retain instead of retaining again; returns whether
// it did. Once the entry holds its reference, call ProtectIfNativelyRetained.
static bool TakeDeferredRelease(v8::Isolate* isolate, id obj);
// Makes the releases waiting for the runtime's thread now, rather than from
// the event loop task posted for them. Only where JS may run: a release can
// reach a JS-implemented -dealloc. Off the runtime's thread, does nothing.
static void RunDeferredReleases(v8::Isolate* isolate);
// A class that tracks native retains (ClassBuilder::TracksNativeRetains)
// protects its wrapper when native code first retains the object past the
// runtime's own retain. A native retain made while a release waited found no
// wrapper to protect; this catches up. A surplus reference the caller drops
// afterwards goes through -release, which unprotects again.
static void ProtectIfNativelyRetained(v8::Isolate* isolate, id obj, ObjCDataWrapper* wrapper);
// The -retainCount instances of `cls` answer with natively, never one JS
// implements, so it can be called where JS may not run, such as a GC
// prologue. JS subclasses are skipped, and a native class whose -retainCount
// JS replaced answers with the original that the replacement keeps under the
// swizzled-prefixed alias. Null for NSTimer.alloc()'s placeholder, whose
// -retainCount does not return, and for classes without a -retainCount.
// Walks method lists, so callers cache the result per class.
// Unsupported: a native override that messages [super retainCount] still
// reaches a JS replacement on an ancestor, which then runs where JS may not.
static IMP NativeRetainCountImp(Class cls);
private:
static void ReleaseNativeCounterpartCallback(
const v8::FunctionCallbackInfo<v8::Value>& info);
static long GetRetainCount(id obj);
static bool IsInstanceOf(id obj, Class clazz);
};
} // namespace tns
#endif /* ObjectManager_h */