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