1/*
2 * Copyright (C) 2018 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ExecutableToCodeBlockEdge.h"
28
29#include "CodeBlock.h"
30#include "IsoCellSetInlines.h"
31
32namespace JSC {
33
34const ClassInfo ExecutableToCodeBlockEdge::s_info = { "ExecutableToCodeBlockEdge", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(ExecutableToCodeBlockEdge) };
35
36Structure* ExecutableToCodeBlockEdge::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
37{
38 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
39}
40
41ExecutableToCodeBlockEdge* ExecutableToCodeBlockEdge::create(VM& vm, CodeBlock* codeBlock)
42{
43 ExecutableToCodeBlockEdge* result = new (NotNull, allocateCell<ExecutableToCodeBlockEdge>(vm.heap)) ExecutableToCodeBlockEdge(vm, codeBlock);
44 result->finishCreation(vm);
45 return result;
46}
47
48void ExecutableToCodeBlockEdge::finishCreation(VM& vm)
49{
50 Base::finishCreation(vm);
51 ASSERT(!isActive());
52}
53
54void ExecutableToCodeBlockEdge::visitChildren(JSCell* cell, SlotVisitor& visitor)
55{
56 VM& vm = visitor.vm();
57 ExecutableToCodeBlockEdge* edge = jsCast<ExecutableToCodeBlockEdge*>(cell);
58 ASSERT_GC_OBJECT_INHERITS(cell, info());
59 Base::visitChildren(cell, visitor);
60
61 CodeBlock* codeBlock = edge->m_codeBlock.get();
62
63 // It's possible for someone to hold a pointer to the edge after the edge has cleared its weak
64 // reference to the codeBlock. In a conservative GC like ours, that could happen at random for
65 // no good reason and it's Totally OK (TM). See finalizeUnconditionally() for where we clear
66 // m_codeBlock.
67 if (!codeBlock)
68 return;
69
70 if (!edge->isActive()) {
71 visitor.appendUnbarriered(codeBlock);
72 return;
73 }
74
75 ConcurrentJSLocker locker(codeBlock->m_lock);
76
77 if (codeBlock->shouldVisitStrongly(locker))
78 visitor.appendUnbarriered(codeBlock);
79
80 if (!vm.heap.isMarked(codeBlock))
81 vm.executableToCodeBlockEdgesWithFinalizers.add(edge);
82
83 if (JITCode::isOptimizingJIT(codeBlock->jitType())) {
84 // If we jettison ourselves we'll install our alternative, so make sure that it
85 // survives GC even if we don't.
86 visitor.append(codeBlock->m_alternative);
87 }
88
89 // NOTE: There are two sides to this constraint, with different requirements for correctness.
90 // Because everything is ultimately protected with weak references and jettisoning, it's
91 // always "OK" to claim that something is dead prematurely and it's "OK" to keep things alive.
92 // But both choices could lead to bad perf - either recomp cycles or leaks.
93 //
94 // Determining CodeBlock liveness: This part is the most consequential. We want to keep the
95 // output constraint active so long as we think that we may yet prove that the CodeBlock is
96 // live but we haven't done it yet.
97 //
98 // Marking Structures if profitable: It's important that we do a pass of this. Logically, this
99 // seems like it is a constraint of CodeBlock. But we have always first run this as a result
100 // of the edge being marked even before we determine the liveness of the CodeBlock. This
101 // allows a CodeBlock to mark itself by first proving that all of the Structures it weakly
102 // depends on could be strongly marked. (This part is also called propagateTransitions.)
103 //
104 // As a weird caveat, we only fixpoint the constraints so long as the CodeBlock is not live.
105 // This means that we may overlook structure marking opportunities created by other marking
106 // that happens after the CodeBlock is marked. This was an accidental policy decision from a
107 // long time ago, but it is probably OK, since it's only worthwhile to keep fixpointing the
108 // structure marking if we still have unmarked structures after the first round. We almost
109 // never will because we will mark-if-profitable based on the owning global object being
110 // already marked. We mark it just in case that hadn't happened yet. And if the CodeBlock is
111 // not yet marked because it weakly depends on a structure that we did not yet mark, then we
112 // will keep fixpointing until the end.
113 visitor.appendUnbarriered(codeBlock->globalObject());
114 vm.executableToCodeBlockEdgesWithConstraints.add(edge);
115 edge->runConstraint(locker, vm, visitor);
116}
117
118void ExecutableToCodeBlockEdge::visitOutputConstraints(JSCell* cell, SlotVisitor& visitor)
119{
120 VM& vm = visitor.vm();
121 ExecutableToCodeBlockEdge* edge = jsCast<ExecutableToCodeBlockEdge*>(cell);
122
123 edge->runConstraint(NoLockingNecessary, vm, visitor);
124}
125
126void ExecutableToCodeBlockEdge::finalizeUnconditionally(VM& vm)
127{
128 CodeBlock* codeBlock = m_codeBlock.get();
129
130 if (!vm.heap.isMarked(codeBlock)) {
131 if (codeBlock->shouldJettisonDueToWeakReference(vm))
132 codeBlock->jettison(Profiler::JettisonDueToWeakReference);
133 else
134 codeBlock->jettison(Profiler::JettisonDueToOldAge);
135 m_codeBlock.clear();
136 }
137
138 vm.executableToCodeBlockEdgesWithFinalizers.remove(this);
139 vm.executableToCodeBlockEdgesWithConstraints.remove(this);
140}
141
142inline void ExecutableToCodeBlockEdge::activate()
143{
144 setPerCellBit(true);
145}
146
147inline void ExecutableToCodeBlockEdge::deactivate()
148{
149 setPerCellBit(false);
150}
151
152inline bool ExecutableToCodeBlockEdge::isActive() const
153{
154 return perCellBit();
155}
156
157CodeBlock* ExecutableToCodeBlockEdge::deactivateAndUnwrap(ExecutableToCodeBlockEdge* edge)
158{
159 if (!edge)
160 return nullptr;
161 edge->deactivate();
162 return edge->codeBlock();
163}
164
165ExecutableToCodeBlockEdge* ExecutableToCodeBlockEdge::wrap(CodeBlock* codeBlock)
166{
167 if (!codeBlock)
168 return nullptr;
169 return codeBlock->ownerEdge();
170}
171
172ExecutableToCodeBlockEdge* ExecutableToCodeBlockEdge::wrapAndActivate(CodeBlock* codeBlock)
173{
174 if (!codeBlock)
175 return nullptr;
176 ExecutableToCodeBlockEdge* result = codeBlock->ownerEdge();
177 result->activate();
178 return result;
179}
180
181ExecutableToCodeBlockEdge::ExecutableToCodeBlockEdge(VM& vm, CodeBlock* codeBlock)
182 : Base(vm, vm.executableToCodeBlockEdgeStructure.get())
183 , m_codeBlock(vm, this, codeBlock)
184{
185}
186
187void ExecutableToCodeBlockEdge::runConstraint(const ConcurrentJSLocker& locker, VM& vm, SlotVisitor& visitor)
188{
189 CodeBlock* codeBlock = m_codeBlock.get();
190
191 codeBlock->propagateTransitions(locker, visitor);
192 codeBlock->determineLiveness(locker, visitor);
193
194 if (vm.heap.isMarked(codeBlock))
195 vm.executableToCodeBlockEdgesWithConstraints.remove(this);
196}
197
198} // namespace JSC
199
200