1 | /* |
2 | * Copyright (C) 2011-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "HandleTypes.h" |
29 | #include "IterationStatus.h" |
30 | #include "MarkStack.h" |
31 | #include "VisitRaceKey.h" |
32 | #include <wtf/Forward.h> |
33 | #include <wtf/MonotonicTime.h> |
34 | #include <wtf/SharedTask.h> |
35 | #include <wtf/text/CString.h> |
36 | |
37 | namespace JSC { |
38 | |
39 | class ConservativeRoots; |
40 | class GCThreadSharedData; |
41 | class Heap; |
42 | class HeapCell; |
43 | class HeapAnalyzer; |
44 | class MarkedBlock; |
45 | class MarkingConstraint; |
46 | class MarkingConstraintSolver; |
47 | template<typename T> class Weak; |
48 | template<typename T, typename Traits> class WriteBarrierBase; |
49 | |
50 | typedef uint32_t HeapVersion; |
51 | |
52 | class SlotVisitor { |
53 | WTF_MAKE_NONCOPYABLE(SlotVisitor); |
54 | WTF_MAKE_FAST_ALLOCATED; |
55 | |
56 | friend class SetCurrentCellScope; |
57 | friend class Heap; |
58 | |
59 | public: |
60 | enum RootMarkReason { |
61 | None, |
62 | ConservativeScan, |
63 | StrongReferences, |
64 | ProtectedValues, |
65 | MarkListSet, |
66 | VMExceptions, |
67 | StrongHandles, |
68 | Debugger, |
69 | JITStubRoutines, |
70 | WeakSets, |
71 | Output, |
72 | DFGWorkLists, |
73 | CodeBlocks, |
74 | DOMGCOutput, |
75 | }; |
76 | |
77 | SlotVisitor(Heap&, CString codeName); |
78 | ~SlotVisitor(); |
79 | |
80 | MarkStackArray& collectorMarkStack() { return m_collectorStack; } |
81 | MarkStackArray& mutatorMarkStack() { return m_mutatorStack; } |
82 | const MarkStackArray& collectorMarkStack() const { return m_collectorStack; } |
83 | const MarkStackArray& mutatorMarkStack() const { return m_mutatorStack; } |
84 | |
85 | VM& vm(); |
86 | const VM& vm() const; |
87 | Heap* heap() const; |
88 | |
89 | void append(const ConservativeRoots&); |
90 | |
91 | template<typename T, typename Traits> void append(const WriteBarrierBase<T, Traits>&); |
92 | template<typename T, typename Traits> void appendHidden(const WriteBarrierBase<T, Traits>&); |
93 | template<typename Iterator> void append(Iterator begin , Iterator end); |
94 | void appendValues(const WriteBarrierBase<Unknown, DumbValueTraits<Unknown>>*, size_t count); |
95 | void appendValuesHidden(const WriteBarrierBase<Unknown, DumbValueTraits<Unknown>>*, size_t count); |
96 | |
97 | // These don't require you to prove that you have a WriteBarrier<>. That makes sense |
98 | // for: |
99 | // |
100 | // - roots. |
101 | // - sophisticated data structures that barrier through other means (like DFG::Plan and |
102 | // friends). |
103 | // |
104 | // If you are not a root and you don't know what kind of barrier you have, then you |
105 | // shouldn't call these methods. |
106 | void appendUnbarriered(JSValue); |
107 | void appendUnbarriered(JSValue*, size_t); |
108 | void appendUnbarriered(JSCell*); |
109 | |
110 | template<typename T> |
111 | void append(const Weak<T>& weak); |
112 | |
113 | void appendHiddenUnbarriered(JSValue); |
114 | void appendHiddenUnbarriered(JSCell*); |
115 | |
116 | bool addOpaqueRoot(void*); // Returns true if the root was new. |
117 | |
118 | bool containsOpaqueRoot(void*) const; |
119 | |
120 | bool isEmpty() { return m_collectorStack.isEmpty() && m_mutatorStack.isEmpty(); } |
121 | |
122 | bool isFirstVisit() const { return m_isFirstVisit; } |
123 | |
124 | void didStartMarking(); |
125 | void reset(); |
126 | void clearMarkStacks(); |
127 | |
128 | size_t bytesVisited() const { return m_bytesVisited; } |
129 | size_t visitCount() const { return m_visitCount; } |
130 | |
131 | void addToVisitCount(size_t value) { m_visitCount += value; } |
132 | |
133 | void donate(); |
134 | void drain(MonotonicTime timeout = MonotonicTime::infinity()); |
135 | void donateAndDrain(MonotonicTime timeout = MonotonicTime::infinity()); |
136 | |
137 | enum SharedDrainMode { SlaveDrain, MasterDrain }; |
138 | enum class SharedDrainResult { Done, TimedOut }; |
139 | SharedDrainResult drainFromShared(SharedDrainMode, MonotonicTime timeout = MonotonicTime::infinity()); |
140 | |
141 | SharedDrainResult drainInParallel(MonotonicTime timeout = MonotonicTime::infinity()); |
142 | SharedDrainResult drainInParallelPassively(MonotonicTime timeout = MonotonicTime::infinity()); |
143 | |
144 | SharedDrainResult waitForTermination(MonotonicTime timeout = MonotonicTime::infinity()); |
145 | |
146 | // Attempts to perform an increment of draining that involves only walking `bytes` worth of data. This |
147 | // is likely to accidentally walk more or less than that. It will usually mark more than bytes. It may |
148 | // mark less than bytes if we're reaching termination or if the global worklist is empty (which may in |
149 | // rare cases happen temporarily even if we're not reaching termination). |
150 | size_t performIncrementOfDraining(size_t bytes); |
151 | |
152 | // This informs the GC about auxiliary of some size that we are keeping alive. If you don't do |
153 | // this then the space will be freed at end of GC. |
154 | void markAuxiliary(const void* base); |
155 | |
156 | void (size_t); |
157 | #if ENABLE(RESOURCE_USAGE) |
158 | void reportExternalMemoryVisited(size_t); |
159 | #endif |
160 | |
161 | void dump(PrintStream&) const; |
162 | |
163 | bool isAnalyzingHeap() const { return !!m_heapAnalyzer; } |
164 | HeapAnalyzer* heapAnalyzer() const { return m_heapAnalyzer; } |
165 | |
166 | RootMarkReason rootMarkReason() const { return m_rootMarkReason; } |
167 | void setRootMarkReason(RootMarkReason reason) { m_rootMarkReason = reason; } |
168 | |
169 | HeapVersion markingVersion() const { return m_markingVersion; } |
170 | |
171 | bool mutatorIsStopped() const { return m_mutatorIsStopped; } |
172 | |
173 | Lock& rightToRun() { return m_rightToRun; } |
174 | |
175 | void updateMutatorIsStopped(const AbstractLocker&); |
176 | void updateMutatorIsStopped(); |
177 | |
178 | bool hasAcknowledgedThatTheMutatorIsResumed() const; |
179 | bool mutatorIsStoppedIsUpToDate() const; |
180 | |
181 | void optimizeForStoppedMutator(); |
182 | |
183 | void didRace(const VisitRaceKey&); |
184 | void didRace(JSCell* cell, const char* reason) { didRace(VisitRaceKey(cell, reason)); } |
185 | |
186 | void visitAsConstraint(const JSCell*); |
187 | |
188 | bool didReachTermination(); |
189 | |
190 | void setIgnoreNewOpaqueRoots(bool value) { m_ignoreNewOpaqueRoots = value; } |
191 | |
192 | void donateAll(); |
193 | |
194 | const char* codeName() const { return m_codeName.data(); } |
195 | |
196 | JS_EXPORT_PRIVATE void addParallelConstraintTask(RefPtr<SharedTask<void(SlotVisitor&)>>); |
197 | |
198 | private: |
199 | friend class ParallelModeEnabler; |
200 | friend class MarkingConstraintSolver; |
201 | |
202 | void appendJSCellOrAuxiliary(HeapCell*); |
203 | |
204 | JS_EXPORT_PRIVATE void appendSlow(JSCell*, Dependency); |
205 | JS_EXPORT_PRIVATE void appendHiddenSlow(JSCell*, Dependency); |
206 | void appendHiddenSlowImpl(JSCell*, Dependency); |
207 | |
208 | template<typename ContainerType> |
209 | void setMarkedAndAppendToMarkStack(ContainerType&, JSCell*, Dependency); |
210 | |
211 | void appendToMarkStack(JSCell*); |
212 | |
213 | template<typename ContainerType> |
214 | void appendToMarkStack(ContainerType&, JSCell*); |
215 | |
216 | void noteLiveAuxiliaryCell(HeapCell*); |
217 | |
218 | void visitChildren(const JSCell*); |
219 | |
220 | void propagateExternalMemoryVisitedIfNecessary(); |
221 | |
222 | void donateKnownParallel(); |
223 | void donateKnownParallel(MarkStackArray& from, MarkStackArray& to); |
224 | |
225 | void donateAll(const AbstractLocker&); |
226 | |
227 | bool hasWork(const AbstractLocker&); |
228 | bool didReachTermination(const AbstractLocker&); |
229 | |
230 | #if CPU(X86_64) |
231 | NEVER_INLINE NO_RETURN_DUE_TO_CRASH NOT_TAIL_CALLED void reportZappedCellAndCrash(JSCell*); |
232 | #endif |
233 | |
234 | template<typename Func> |
235 | IterationStatus forEachMarkStack(const Func&); |
236 | |
237 | MarkStackArray& correspondingGlobalStack(MarkStackArray&); |
238 | |
239 | MarkStackArray m_collectorStack; |
240 | MarkStackArray m_mutatorStack; |
241 | |
242 | size_t m_bytesVisited; |
243 | size_t m_visitCount; |
244 | size_t m_nonCellVisitCount { 0 }; // Used for incremental draining, ignored otherwise. |
245 | Checked<size_t, RecordOverflow> { 0 }; |
246 | bool m_isInParallelMode; |
247 | bool m_ignoreNewOpaqueRoots { false }; // Useful as a debugging mode. |
248 | |
249 | HeapVersion m_markingVersion; |
250 | |
251 | Heap& m_heap; |
252 | |
253 | HeapAnalyzer* m_heapAnalyzer { nullptr }; |
254 | JSCell* m_currentCell { nullptr }; |
255 | RootMarkReason m_rootMarkReason { RootMarkReason::None }; |
256 | bool m_isFirstVisit { false }; |
257 | bool m_mutatorIsStopped { false }; |
258 | bool m_canOptimizeForStoppedMutator { false }; |
259 | Lock m_rightToRun; |
260 | |
261 | CString m_codeName; |
262 | |
263 | MarkingConstraint* m_currentConstraint { nullptr }; |
264 | MarkingConstraintSolver* m_currentSolver { nullptr }; |
265 | |
266 | // Put padding here to mitigate false sharing between multiple SlotVisitors. |
267 | char padding[64]; |
268 | public: |
269 | #if !ASSERT_DISABLED |
270 | bool m_isCheckingForDefaultMarkViolation; |
271 | bool m_isDraining; |
272 | #endif |
273 | }; |
274 | |
275 | class ParallelModeEnabler { |
276 | public: |
277 | ParallelModeEnabler(SlotVisitor& stack) |
278 | : m_stack(stack) |
279 | { |
280 | ASSERT(!m_stack.m_isInParallelMode); |
281 | m_stack.m_isInParallelMode = true; |
282 | } |
283 | |
284 | ~ParallelModeEnabler() |
285 | { |
286 | ASSERT(m_stack.m_isInParallelMode); |
287 | m_stack.m_isInParallelMode = false; |
288 | } |
289 | |
290 | private: |
291 | SlotVisitor& m_stack; |
292 | }; |
293 | |
294 | class SetRootMarkReasonScope { |
295 | public: |
296 | SetRootMarkReasonScope(SlotVisitor& visitor, SlotVisitor::RootMarkReason reason) |
297 | : m_visitor(visitor) |
298 | , m_previousReason(visitor.rootMarkReason()) |
299 | { |
300 | m_visitor.setRootMarkReason(reason); |
301 | } |
302 | |
303 | ~SetRootMarkReasonScope() |
304 | { |
305 | m_visitor.setRootMarkReason(m_previousReason); |
306 | } |
307 | |
308 | private: |
309 | SlotVisitor& m_visitor; |
310 | SlotVisitor::RootMarkReason m_previousReason; |
311 | }; |
312 | |
313 | } // namespace JSC |
314 | |