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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* Copyright (C) 2019-2021 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "UnlinkedCodeBlockGenerator.h"
#include "BytecodeRewriter.h"
#include "InstructionStream.h"
#include "JSCJSValueInlines.h"
#include "PreciseJumpTargets.h"
#include "UnlinkedMetadataTableInlines.h"
namespace JSC {
inline void UnlinkedCodeBlockGenerator::getLineAndColumn(const ExpressionRangeInfo& info, unsigned& line, unsigned& column) const
{
switch (info.mode) {
case ExpressionRangeInfo::FatLineMode:
info.decodeFatLineMode(line, column);
break;
case ExpressionRangeInfo::FatColumnMode:
info.decodeFatColumnMode(line, column);
break;
case ExpressionRangeInfo::FatLineAndColumnMode: {
unsigned fatIndex = info.position;
const ExpressionRangeInfo::FatPosition& fatPos = m_expressionInfoFatPositions[fatIndex];
line = fatPos.line;
column = fatPos.column;
break;
}
} // switch
}
void UnlinkedCodeBlockGenerator::addExpressionInfo(unsigned instructionOffset, int divot, int startOffset, int endOffset, unsigned line, unsigned column)
{
if (divot > ExpressionRangeInfo::MaxDivot) {
// Overflow has occurred, we can only give line number info for errors for this region
divot = 0;
startOffset = 0;
endOffset = 0;
} else if (startOffset > ExpressionRangeInfo::MaxOffset) {
// If the start offset is out of bounds we clear both offsets
// so we only get the divot marker. Error message will have to be reduced
// to line and charPosition number.
startOffset = 0;
endOffset = 0;
} else if (endOffset > ExpressionRangeInfo::MaxOffset) {
// The end offset is only used for additional context, and is much more likely
// to overflow (eg. function call arguments) so we are willing to drop it without
// dropping the rest of the range.
endOffset = 0;
}
unsigned positionMode =
(line <= ExpressionRangeInfo::MaxFatLineModeLine && column <= ExpressionRangeInfo::MaxFatLineModeColumn)
? ExpressionRangeInfo::FatLineMode
: (line <= ExpressionRangeInfo::MaxFatColumnModeLine && column <= ExpressionRangeInfo::MaxFatColumnModeColumn)
? ExpressionRangeInfo::FatColumnMode
: ExpressionRangeInfo::FatLineAndColumnMode;
ExpressionRangeInfo info;
info.instructionOffset = instructionOffset;
info.divotPoint = divot;
info.startOffset = startOffset;
info.endOffset = endOffset;
info.mode = positionMode;
switch (positionMode) {
case ExpressionRangeInfo::FatLineMode:
info.encodeFatLineMode(line, column);
break;
case ExpressionRangeInfo::FatColumnMode:
info.encodeFatColumnMode(line, column);
break;
case ExpressionRangeInfo::FatLineAndColumnMode: {
unsigned fatIndex = m_expressionInfoFatPositions.size();
ExpressionRangeInfo::FatPosition fatPos = { line, column };
m_expressionInfoFatPositions.append(fatPos);
info.position = fatIndex;
}
} // switch
m_expressionInfo.append(info);
}
void UnlinkedCodeBlockGenerator::addTypeProfilerExpressionInfo(unsigned instructionOffset, unsigned startDivot, unsigned endDivot)
{
UnlinkedCodeBlock::RareData::TypeProfilerExpressionRange range;
range.m_startDivot = startDivot;
range.m_endDivot = endDivot;
m_typeProfilerInfoMap.set(instructionOffset, range);
}
void UnlinkedCodeBlockGenerator::finalize(std::unique_ptr<JSInstructionStream> instructions)
{
ASSERT(instructions);
{
Locker locker { m_codeBlock->cellLock() };
m_codeBlock->m_instructions = WTFMove(instructions);
m_codeBlock->allocateSharedProfiles(m_numBinaryArithProfiles, m_numUnaryArithProfiles);
m_codeBlock->m_metadata->finalize();
m_codeBlock->m_jumpTargets = WTFMove(m_jumpTargets);
m_codeBlock->m_identifiers = WTFMove(m_identifiers);
m_codeBlock->m_constantRegisters = WTFMove(m_constantRegisters);
m_codeBlock->m_constantsSourceCodeRepresentation = WTFMove(m_constantsSourceCodeRepresentation);
m_codeBlock->m_functionDecls = WTFMove(m_functionDecls);
m_codeBlock->m_functionExprs = WTFMove(m_functionExprs);
m_codeBlock->m_expressionInfo = WTFMove(m_expressionInfo);
m_codeBlock->m_outOfLineJumpTargets = WTFMove(m_outOfLineJumpTargets);
if (!m_codeBlock->m_rareData) {
if (!m_exceptionHandlers.isEmpty()
|| !m_unlinkedSwitchJumpTables.isEmpty()
|| !m_unlinkedStringSwitchJumpTables.isEmpty()
|| !m_expressionInfoFatPositions.isEmpty()
|| !m_typeProfilerInfoMap.isEmpty()
|| !m_opProfileControlFlowBytecodeOffsets.isEmpty()
|| !m_bitVectors.isEmpty()
|| !m_constantIdentifierSets.isEmpty())
m_codeBlock->createRareDataIfNecessary(locker);
}
if (m_codeBlock->m_rareData) {
m_codeBlock->m_rareData->m_exceptionHandlers = WTFMove(m_exceptionHandlers);
m_codeBlock->m_rareData->m_unlinkedSwitchJumpTables = WTFMove(m_unlinkedSwitchJumpTables);
m_codeBlock->m_rareData->m_unlinkedStringSwitchJumpTables = WTFMove(m_unlinkedStringSwitchJumpTables);
m_codeBlock->m_rareData->m_expressionInfoFatPositions = WTFMove(m_expressionInfoFatPositions);
m_codeBlock->m_rareData->m_typeProfilerInfoMap = WTFMove(m_typeProfilerInfoMap);
m_codeBlock->m_rareData->m_opProfileControlFlowBytecodeOffsets = WTFMove(m_opProfileControlFlowBytecodeOffsets);
m_codeBlock->m_rareData->m_bitVectors = WTFMove(m_bitVectors);
m_codeBlock->m_rareData->m_constantIdentifierSets = WTFMove(m_constantIdentifierSets);
}
if (UNLIKELY(Options::returnEarlyFromInfiniteLoopsForFuzzing()))
m_codeBlock->initializeLoopHintExecutionCounter();
}
m_vm.writeBarrier(m_codeBlock.get());
m_vm.heap.reportExtraMemoryAllocated(m_codeBlock->m_instructions->sizeInBytes() + m_codeBlock->m_metadata->sizeInBytes());
}
UnlinkedHandlerInfo* UnlinkedCodeBlockGenerator::handlerForBytecodeIndex(BytecodeIndex bytecodeIndex, RequiredHandler requiredHandler)
{
return handlerForIndex(bytecodeIndex.offset(), requiredHandler);
}
UnlinkedHandlerInfo* UnlinkedCodeBlockGenerator::handlerForIndex(unsigned index, RequiredHandler requiredHandler)
{
return UnlinkedHandlerInfo::handlerForIndex<UnlinkedHandlerInfo>(m_exceptionHandlers, index, requiredHandler);
}
void UnlinkedCodeBlockGenerator::applyModification(BytecodeRewriter& rewriter, JSInstructionStreamWriter& instructions)
{
// Before applying the changes, we adjust the jumps based on the original bytecode offset, the offset to the jump target, and
// the insertion information.
rewriter.adjustJumpTargets();
// Then, exception handlers should be adjusted.
for (UnlinkedHandlerInfo& handler : m_exceptionHandlers) {
handler.target = rewriter.adjustAbsoluteOffset(handler.target);
handler.start = rewriter.adjustAbsoluteOffset(handler.start);
handler.end = rewriter.adjustAbsoluteOffset(handler.end);
}
for (size_t i = 0; i < m_opProfileControlFlowBytecodeOffsets.size(); ++i)
m_opProfileControlFlowBytecodeOffsets[i] = rewriter.adjustAbsoluteOffset(m_opProfileControlFlowBytecodeOffsets[i]);
if (!m_typeProfilerInfoMap.isEmpty()) {
HashMap<unsigned, UnlinkedCodeBlock::RareData::TypeProfilerExpressionRange> adjustedTypeProfilerInfoMap;
for (auto& entry : m_typeProfilerInfoMap)
adjustedTypeProfilerInfoMap.set(rewriter.adjustAbsoluteOffset(entry.key), entry.value);
m_typeProfilerInfoMap.swap(adjustedTypeProfilerInfoMap);
}
for (size_t i = 0; i < m_expressionInfo.size(); ++i)
m_expressionInfo[i].instructionOffset = rewriter.adjustAbsoluteOffset(m_expressionInfo[i].instructionOffset);
// Then, modify the unlinked instructions.
rewriter.applyModification();
// And recompute the jump target based on the modified unlinked instructions.
m_jumpTargets.clear();
recomputePreciseJumpTargets(this, instructions, m_jumpTargets);
}
void UnlinkedCodeBlockGenerator::addOutOfLineJumpTarget(JSInstructionStream::Offset bytecodeOffset, int target)
{
RELEASE_ASSERT(target);
m_outOfLineJumpTargets.set(bytecodeOffset, target);
}
int UnlinkedCodeBlockGenerator::outOfLineJumpOffset(JSInstructionStream::Offset bytecodeOffset)
{
ASSERT(m_outOfLineJumpTargets.contains(bytecodeOffset));
return m_outOfLineJumpTargets.get(bytecodeOffset);
}
void UnlinkedCodeBlockGenerator::dump(PrintStream&) const
{
}
} // namespace JSC
|