| 1 | /* |
| 2 | * Copyright (C) 2016 Yusuke Suzuki <utatane.tea@gmail.com> |
| 3 | * Copyright (C) 2016-2017 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "BytecodeRewriter.h" |
| 29 | |
| 30 | #include "JSCInlines.h" |
| 31 | #include "PreciseJumpTargetsInlines.h" |
| 32 | #include <wtf/BubbleSort.h> |
| 33 | |
| 34 | namespace JSC { |
| 35 | |
| 36 | void BytecodeRewriter::applyModification() |
| 37 | { |
| 38 | for (size_t insertionIndex = m_insertions.size(); insertionIndex--;) { |
| 39 | Insertion& insertion = m_insertions[insertionIndex]; |
| 40 | if (insertion.type == Insertion::Type::Remove) |
| 41 | m_writer.m_instructions.remove(insertion.index.bytecodeOffset, insertion.length()); |
| 42 | else { |
| 43 | if (insertion.includeBranch == IncludeBranch::Yes) { |
| 44 | int finalOffset = insertion.index.bytecodeOffset + calculateDifference(m_insertions.begin(), m_insertions.begin() + insertionIndex); |
| 45 | adjustJumpTargetsInFragment(finalOffset, insertion); |
| 46 | } |
| 47 | m_writer.m_instructions.insertVector(insertion.index.bytecodeOffset, insertion.instructions.m_instructions); |
| 48 | } |
| 49 | } |
| 50 | m_insertions.clear(); |
| 51 | } |
| 52 | |
| 53 | void BytecodeRewriter::execute() |
| 54 | { |
| 55 | WTF::bubbleSort(m_insertions.begin(), m_insertions.end(), [] (const Insertion& lhs, const Insertion& rhs) { |
| 56 | return lhs.index < rhs.index; |
| 57 | }); |
| 58 | |
| 59 | m_codeBlock->applyModification(*this, m_writer); |
| 60 | } |
| 61 | |
| 62 | void BytecodeRewriter::adjustJumpTargetsInFragment(unsigned finalOffset, Insertion& insertion) |
| 63 | { |
| 64 | for (auto& instruction : insertion.instructions) { |
| 65 | if (isBranch(instruction->opcodeID())) { |
| 66 | unsigned bytecodeOffset = finalOffset + instruction.offset(); |
| 67 | updateStoredJumpTargetsForInstruction(m_codeBlock, finalOffset, instruction, [&](int32_t label) { |
| 68 | int absoluteOffset = adjustAbsoluteOffset(label); |
| 69 | return absoluteOffset - static_cast<int>(bytecodeOffset); |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void BytecodeRewriter::insertImpl(InsertionPoint insertionPoint, IncludeBranch includeBranch, InstructionStreamWriter&& writer) |
| 76 | { |
| 77 | ASSERT(insertionPoint.position == Position::Before || insertionPoint.position == Position::After); |
| 78 | m_insertions.append(Insertion { |
| 79 | insertionPoint, |
| 80 | Insertion::Type::Insert, |
| 81 | includeBranch, |
| 82 | 0, |
| 83 | WTFMove(writer) |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | int32_t BytecodeRewriter::adjustJumpTarget(InsertionPoint startPoint, InsertionPoint jumpTargetPoint) |
| 88 | { |
| 89 | if (startPoint < jumpTargetPoint) { |
| 90 | int jumpTarget = jumpTargetPoint.bytecodeOffset; |
| 91 | auto start = std::lower_bound(m_insertions.begin(), m_insertions.end(), startPoint, [&] (const Insertion& insertion, InsertionPoint startPoint) { |
| 92 | return insertion.index < startPoint; |
| 93 | }); |
| 94 | if (start != m_insertions.end()) { |
| 95 | auto end = std::lower_bound(m_insertions.begin(), m_insertions.end(), jumpTargetPoint, [&] (const Insertion& insertion, InsertionPoint jumpTargetPoint) { |
| 96 | return insertion.index < jumpTargetPoint; |
| 97 | }); |
| 98 | jumpTarget += calculateDifference(start, end); |
| 99 | } |
| 100 | return jumpTarget - startPoint.bytecodeOffset; |
| 101 | } |
| 102 | |
| 103 | if (startPoint == jumpTargetPoint) |
| 104 | return 0; |
| 105 | |
| 106 | return -adjustJumpTarget(jumpTargetPoint, startPoint); |
| 107 | } |
| 108 | |
| 109 | // FIXME: unit test the logic in this method |
| 110 | // https://bugs.webkit.org/show_bug.cgi?id=190950 |
| 111 | void BytecodeRewriter::adjustJumpTargets() |
| 112 | { |
| 113 | auto currentInsertion = m_insertions.begin(); |
| 114 | auto outOfLineJumpTargets = m_codeBlock->replaceOutOfLineJumpTargets(); |
| 115 | |
| 116 | int offset = 0; |
| 117 | for (InstructionStream::Offset i = 0; i < m_writer.size();) { |
| 118 | int before = 0; |
| 119 | int after = 0; |
| 120 | int remove = 0; |
| 121 | while (currentInsertion != m_insertions.end() && static_cast<InstructionStream::Offset>(currentInsertion->index.bytecodeOffset) == i) { |
| 122 | auto size = currentInsertion->length(); |
| 123 | if (currentInsertion->type == Insertion::Type::Remove) |
| 124 | remove += size; |
| 125 | else if (currentInsertion->index.position == Position::Before) |
| 126 | before += size; |
| 127 | else if (currentInsertion->index.position == Position::After) |
| 128 | after += size; |
| 129 | ++currentInsertion; |
| 130 | } |
| 131 | |
| 132 | offset += before; |
| 133 | |
| 134 | if (!remove) { |
| 135 | auto instruction = m_writer.ref(i); |
| 136 | updateStoredJumpTargetsForInstruction(m_codeBlock, offset, instruction, [&](int32_t relativeOffset) { |
| 137 | return adjustJumpTarget(instruction.offset(), instruction.offset() + relativeOffset); |
| 138 | }, outOfLineJumpTargets); |
| 139 | i += instruction->size(); |
| 140 | } else { |
| 141 | offset -= remove; |
| 142 | i += remove; |
| 143 | } |
| 144 | |
| 145 | offset += after; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // namespace JSC |
| 150 | |