Skip to content

Commit

Permalink
[JSC] Use enum for machine code copy function
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=267217
rdar://120625991

Reviewed by Justin Michaud.

This makes code much simpler. We can remove many weird workaround for GCC, like memcpyWrapper.

* Source/JavaScriptCore/assembler/ARM64Assembler.h:
* Source/JavaScriptCore/assembler/ARMv7Assembler.h:
(JSC::ARMv7Assembler::fillNops):
(JSC::ARMv7Assembler::replaceWithNops):
(JSC::ARMv7Assembler::linkJumpT1):
(JSC::ARMv7Assembler::linkJumpT2):
(JSC::ARMv7Assembler::linkJumpT3):
(JSC::ARMv7Assembler::linkJumpT4):
(JSC::ARMv7Assembler::linkConditionalJumpT4):
(JSC::ARMv7Assembler::linkBX):
(JSC::ARMv7Assembler::linkConditionalBX):
* Source/JavaScriptCore/assembler/AbstractMacroAssembler.h:
(JSC::AbstractMacroAssembler::emitNops):
* Source/JavaScriptCore/assembler/AssemblerCommon.h:
(JSC::machineCodeCopy):
* Source/JavaScriptCore/assembler/LinkBuffer.cpp:
(JSC::LinkBuffer::copyCompactAndLinkCode):
* Source/JavaScriptCore/assembler/MIPSAssembler.h:
(JSC::MIPSAssembler::nop):
* Source/JavaScriptCore/assembler/MacroAssemblerARM64.h:
* Source/JavaScriptCore/assembler/MacroAssemblerARMv7.h:
* Source/JavaScriptCore/assembler/RISCV64Assembler.h:
(JSC::RISCV64Assembler::replaceWithNops):
(JSC::RISCV64Assembler::fillNops):
* Source/JavaScriptCore/assembler/X86Assembler.h:
(JSC::X86Assembler::replaceWithNops):
(JSC::X86Assembler::memcpyWrapper): Deleted.
* Source/JavaScriptCore/jit/ExecutableAllocator.cpp:
* Source/JavaScriptCore/jit/ExecutableAllocator.h:
(JSC::memcpyWrapper): Deleted.

Canonical link: https://commits.webkit.org/272768@main
  • Loading branch information
Constellation committed Jan 8, 2024
1 parent 8e11338 commit bbfda64
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 89 deletions.
43 changes: 21 additions & 22 deletions Source/JavaScriptCore/assembler/ARM64Assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2284,9 +2284,8 @@ class ARM64Assembler {
}

enum BranchTargetType { DirectBranch, IndirectBranch };
using CopyFunction = void*(&)(void*, const void*, size_t);

template <CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void fillNops(void* base, size_t size)
{
RELEASE_ASSERT(!(size % sizeof(int32_t)));
Expand All @@ -2295,11 +2294,11 @@ class ARM64Assembler {
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(ptr) == ptr);
for (; n--;) {
int insn = nopPseudo();
copy(ptr++, &insn, sizeof(int));
machineCodeCopy<copy>(ptr++, &insn, sizeof(int));
}
}

template <CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void fillNearTailCall(void* from, void* to)
{
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(from) == from);
Expand All @@ -2308,7 +2307,7 @@ class ARM64Assembler {
ASSERT(isInt<26>(offset));
constexpr bool isCall = false;
int insn = unconditionalBranchImmediate(isCall, static_cast<int>(offset));
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
cacheFlush(from, sizeof(int));
}

Expand Down Expand Up @@ -3539,7 +3538,7 @@ class ARM64Assembler {

static void replaceWithNops(void* where, size_t memoryToFillWithNopsInBytes)
{
fillNops<performJITMemcpy>(where, memoryToFillWithNopsInBytes);
fillNops<MachineCodeCopyMode::JITMemcpy>(where, memoryToFillWithNopsInBytes);
cacheFlush(where, memoryToFillWithNopsInBytes);
}

Expand Down Expand Up @@ -3782,7 +3781,7 @@ class ARM64Assembler {
return m_jumpsToLink;
}

template<CopyFunction copy>
template<MachineCodeCopyMode copy>
static void ALWAYS_INLINE link(LinkRecord& record, uint8_t* from, const uint8_t* fromInstruction8, uint8_t* to)
{
const int* fromInstruction = reinterpret_cast<const int*>(fromInstruction8);
Expand Down Expand Up @@ -3867,7 +3866,7 @@ class ARM64Assembler {
setPointer(address, valuePtr, rd, flush);
}

template<BranchType type, CopyFunction copy = performJITMemcpy>
template<BranchType type, MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkJumpOrCall(int* from, const int* fromInstruction, void* to)
{
static_assert(type == BranchType_JMP || type == BranchType_CALL);
Expand All @@ -3888,7 +3887,7 @@ class ARM64Assembler {

#if ENABLE(JUMP_ISLANDS)
if (!isInt<26>(offset)) {
if (copy == performJITMemcpy)
if constexpr (copy == MachineCodeCopyMode::JITMemcpy)
to = ExecutableAllocator::singleton().getJumpIslandToUsingJITMemcpy(bitwise_cast<void*>(fromInstruction), to);
else
to = ExecutableAllocator::singleton().getJumpIslandToUsingMemcpy(bitwise_cast<void*>(fromInstruction), to);
Expand All @@ -3899,10 +3898,10 @@ class ARM64Assembler {

int insn = unconditionalBranchImmediate(isCall, static_cast<int>(offset));
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(from) == from);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
}

template<BranchTargetType type, CopyFunction copy = performJITMemcpy>
template<BranchTargetType type, MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkCompareAndBranch(Condition condition, bool is64Bit, RegisterID rt, int* from, const int* fromInstruction, void* to)
{
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(from) == from);
Expand All @@ -3916,19 +3915,19 @@ class ARM64Assembler {
if (useDirect || type == DirectBranch) {
ASSERT(isInt<19>(offset));
int insn = compareAndBranchImmediate(is64Bit ? Datasize_64 : Datasize_32, condition == ConditionNE, static_cast<int>(offset), rt);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
if (type == IndirectBranch) {
insn = nopPseudo();
copy(from + 1, &insn, sizeof(int));
machineCodeCopy<copy>(from + 1, &insn, sizeof(int));
}
} else {
int insn = compareAndBranchImmediate(is64Bit ? Datasize_64 : Datasize_32, invert(condition) == ConditionNE, 2, rt);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
linkJumpOrCall<BranchType_JMP, copy>(from + 1, fromInstruction + 1, to);
}
}

template<BranchTargetType type, CopyFunction copy = performJITMemcpy>
template<BranchTargetType type, MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkConditionalBranch(Condition condition, int* from, const int* fromInstruction, void* to)
{
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(from) == from);
Expand All @@ -3942,19 +3941,19 @@ class ARM64Assembler {
if (useDirect || type == DirectBranch) {
ASSERT(isInt<19>(offset));
int insn = conditionalBranchImmediate(static_cast<int>(offset), condition);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
if (type == IndirectBranch) {
insn = nopPseudo();
copy(from + 1, &insn, sizeof(int));
machineCodeCopy<copy>(from + 1, &insn, sizeof(int));
}
} else {
int insn = conditionalBranchImmediate(2, invert(condition));
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
linkJumpOrCall<BranchType_JMP, copy>(from + 1, fromInstruction + 1, to);
}
}

template<BranchTargetType type, CopyFunction copy = performJITMemcpy>
template<BranchTargetType type, MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkTestAndBranch(Condition condition, unsigned bitNumber, RegisterID rt, int* from, const int* fromInstruction, void* to)
{
RELEASE_ASSERT(roundUpToMultipleOf<instructionSize>(from) == from);
Expand All @@ -3969,14 +3968,14 @@ class ARM64Assembler {
if (useDirect || type == DirectBranch) {
ASSERT(isInt<14>(offset));
int insn = testAndBranchImmediate(condition == ConditionNE, static_cast<int>(bitNumber), static_cast<int>(offset), rt);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
if (type == IndirectBranch) {
insn = nopPseudo();
copy(from + 1, &insn, sizeof(int));
machineCodeCopy<copy>(from + 1, &insn, sizeof(int));
}
} else {
int insn = testAndBranchImmediate(invert(condition) == ConditionNE, static_cast<int>(bitNumber), 2, rt);
copy(from, &insn, sizeof(int));
machineCodeCopy<copy>(from, &insn, sizeof(int));
linkJumpOrCall<BranchType_JMP, copy>(from + 1, fromInstruction + 1, to);
}
}
Expand Down
42 changes: 20 additions & 22 deletions Source/JavaScriptCore/assembler/ARMv7Assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2423,9 +2423,7 @@ class ARMv7Assembler {
return OP_NOP_T2a | (OP_NOP_T2b << 16);
}

using CopyFunction = void*(&)(void*, const void*, size_t);

template <CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void fillNops(void* base, size_t size)
{
RELEASE_ASSERT(!(size % sizeof(int16_t)));
Expand All @@ -2434,7 +2432,7 @@ class ARMv7Assembler {
const size_t num32s = size / sizeof(int32_t);
for (size_t i = 0; i < num32s; i++) {
const int32_t insn = nopPseudo32();
copy(ptr, &insn, sizeof(int32_t));
machineCodeCopy<copy>(ptr, &insn, sizeof(int32_t));
ptr += sizeof(int32_t);
}

Expand All @@ -2443,11 +2441,11 @@ class ARMv7Assembler {
ASSERT(num16s * sizeof(int16_t) + num32s * sizeof(int32_t) == size);
if (num16s) {
const int16_t insn = nopPseudo16();
copy(ptr, &insn, sizeof(int16_t));
machineCodeCopy<copy>(ptr, &insn, sizeof(int16_t));
}
}

template <CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void fillNearTailCall(void* from, void* to)
{
uint16_t* ptr = reinterpret_cast<uint16_t*>(from) + 2;
Expand Down Expand Up @@ -2589,7 +2587,7 @@ class ARMv7Assembler {
return m_jumpsToLink;
}

template<CopyFunction copy>
template<MachineCodeCopyMode copy>
static void ALWAYS_INLINE link(LinkRecord& record, uint8_t* from, const uint8_t* fromInstruction8, uint8_t* to)
{
const uint16_t* fromInstruction = reinterpret_cast_ptr<const uint16_t*>(fromInstruction8);
Expand Down Expand Up @@ -2774,7 +2772,7 @@ class ARMv7Assembler {

static void replaceWithNops(void* instructionStart, size_t memoryToFillWithNopsInBytes)
{
fillNops<performJITMemcpy>(instructionStart, memoryToFillWithNopsInBytes);
fillNops<MachineCodeCopyMode::JITMemcpy>(instructionStart, memoryToFillWithNopsInBytes);
cacheFlush(instructionStart, memoryToFillWithNopsInBytes);
}

Expand Down Expand Up @@ -3052,7 +3050,7 @@ class ARMv7Assembler {
return ((relative << 7) >> 7) == relative;
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkJumpT1(Condition cond, uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3069,10 +3067,10 @@ class ARMv7Assembler {
// All branch offsets should be an even distance.
ASSERT(!(relative & 1));
uint16_t newInstruction = OP_B_T1 | ((cond & 0xf) << 8) | ((relative & 0x1fe) >> 1);
copy(writeTarget - 1, &newInstruction, sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 1, &newInstruction, sizeof(uint16_t));
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkJumpT2(uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3089,10 +3087,10 @@ class ARMv7Assembler {
// All branch offsets should be an even distance.
ASSERT(!(relative & 1));
uint16_t newInstruction = OP_B_T2 | ((relative & 0xffe) >> 1);
copy(writeTarget - 1, &newInstruction, sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 1, &newInstruction, sizeof(uint16_t));
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkJumpT3(Condition cond, uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3107,10 +3105,10 @@ class ARMv7Assembler {
uint16_t instructions[2];
instructions[0] = OP_B_T3a | ((relative & 0x100000) >> 10) | ((cond & 0xf) << 6) | ((relative & 0x3f000) >> 12);
instructions[1] = OP_B_T3b | ((relative & 0x80000) >> 8) | ((relative & 0x40000) >> 5) | ((relative & 0xffe) >> 1);
copy(writeTarget - 2, instructions, 2 * sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 2, instructions, 2 * sizeof(uint16_t));
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkJumpT4(uint16_t* writeTarget, const uint16_t* instruction, void* target, BranchWithLink link)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3128,22 +3126,22 @@ class ARMv7Assembler {
uint16_t instructions[2];
instructions[0] = OP_B_T4a | ((relative & 0x1000000) >> 14) | ((relative & 0x3ff000) >> 12);
instructions[1] = OP_B_T4b | (static_cast<uint16_t>(link) << 14) | ((relative & 0x800000) >> 10) | ((relative & 0x400000) >> 11) | ((relative & 0xffe) >> 1);
copy(writeTarget - 2, instructions, 2 * sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 2, instructions, 2 * sizeof(uint16_t));
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkConditionalJumpT4(Condition cond, uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));

uint16_t newInstruction = ifThenElse(cond) | OP_IT;
copy(writeTarget - 3, &newInstruction, sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 3, &newInstruction, sizeof(uint16_t));
linkJumpT4<copy>(writeTarget, instruction, target, BranchWithLink::No);
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkBX(uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3161,10 +3159,10 @@ class ARMv7Assembler {
instructions[3] = twoWordOp5i6Imm4Reg4EncodedImmSecond(JUMP_TEMPORARY_REGISTER, hi16);
instructions[4] = OP_BX | (JUMP_TEMPORARY_REGISTER << 3);

copy(writeTarget - 5, instructions, 5 * sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 5, instructions, 5 * sizeof(uint16_t));
}

template<CopyFunction copy = performJITMemcpy>
template<MachineCodeCopyMode copy = MachineCodeCopyMode::JITMemcpy>
static void linkConditionalBX(Condition cond, uint16_t* writeTarget, const uint16_t* instruction, void* target)
{
// FIMXE: this should be up in the MacroAssembler layer. :-(
Expand All @@ -3173,7 +3171,7 @@ class ARMv7Assembler {

linkBX(writeTarget, instruction, target);
uint16_t newInstruction = ifThenElse(cond, true, true) | OP_IT;
copy(writeTarget - 6, &newInstruction, sizeof(uint16_t));
machineCodeCopy<copy>(writeTarget - 6, &newInstruction, sizeof(uint16_t));
}

static void linkJumpAbsolute(uint16_t* writeTarget, const uint16_t* instruction, void* target)
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/assembler/AbstractMacroAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ class AbstractMacroAssembler : public AbstractMacroAssemblerBase {
size_t startCodeSize = buffer.codeSize();
size_t targetCodeSize = startCodeSize + memoryToFillWithNopsInBytes;
buffer.ensureSpace(memoryToFillWithNopsInBytes);
AssemblerType::template fillNops<memcpy>(static_cast<char*>(buffer.data()) + startCodeSize, memoryToFillWithNopsInBytes);
AssemblerType::template fillNops<MachineCodeCopyMode::Memcpy>(static_cast<char*>(buffer.data()) + startCodeSize, memoryToFillWithNopsInBytes);
buffer.setCodeSize(targetCodeSize);
#endif
}
Expand Down
16 changes: 16 additions & 0 deletions Source/JavaScriptCore/assembler/AssemblerCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,20 @@ ALWAYS_INLINE bool isValidARMThumb2Immediate(int64_t value)
return false;
}

enum class MachineCodeCopyMode : uint8_t {
Memcpy,
JITMemcpy,
};

static void* performJITMemcpy(void *dst, const void *src, size_t n);

template<MachineCodeCopyMode copy>
ALWAYS_INLINE void* machineCodeCopy(void *dst, const void *src, size_t n)
{
if constexpr (copy == MachineCodeCopyMode::Memcpy)
return memcpy(dst, src, n);
else
return performJITMemcpy(dst, src, n);
}

} // namespace JSC.
8 changes: 4 additions & 4 deletions Source/JavaScriptCore/assembler/LinkBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,19 +353,19 @@ void LinkBuffer::copyCompactAndLinkCode(MacroAssembler& macroAssembler, JITCompi
else
target = codeOutData + to - executableOffsetFor(to);
if (g_jscConfig.useFastJITPermissions)
MacroAssembler::link<memcpyWrapper>(linkRecord, outData + linkRecord.from(), location, target);
MacroAssembler::link<MachineCodeCopyMode::Memcpy>(linkRecord, outData + linkRecord.from(), location, target);
else
MacroAssembler::link<performJITMemcpy>(linkRecord, outData + linkRecord.from(), location, target);
MacroAssembler::link<MachineCodeCopyMode::JITMemcpy>(linkRecord, outData + linkRecord.from(), location, target);
}

size_t compactSize = writePtr + initialSize - readPtr;
if (!m_executableMemory) {
size_t nopSizeInBytes = initialSize - compactSize;

if (g_jscConfig.useFastJITPermissions)
Assembler::fillNops<memcpyWrapper>(outData + compactSize, nopSizeInBytes);
Assembler::fillNops<MachineCodeCopyMode::Memcpy>(outData + compactSize, nopSizeInBytes);
else
Assembler::fillNops<performJITMemcpy>(outData + compactSize, nopSizeInBytes);
Assembler::fillNops<MachineCodeCopyMode::JITMemcpy>(outData + compactSize, nopSizeInBytes);
}

if (g_jscConfig.useFastJITPermissions)
Expand Down
4 changes: 1 addition & 3 deletions Source/JavaScriptCore/assembler/MIPSAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ class MIPSAssembler {
{
emitInst(0x00000000);
}

using CopyFunction = void*(&)(void*, const void*, size_t);

template <CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void fillNops(void* base, size_t size)
{
UNUSED_PARAM(copy);
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/assembler/MacroAssemblerARM64.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MacroAssemblerARM64 : public AbstractMacroAssembler<Assembler> {
static JumpLinkType computeJumpType(LinkRecord& record, const uint8_t* from, const uint8_t* to) { return Assembler::computeJumpType(record, from, to); }
static int jumpSizeDelta(JumpType jumpType, JumpLinkType jumpLinkType) { return Assembler::jumpSizeDelta(jumpType, jumpLinkType); }

template <Assembler::CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void link(LinkRecord& record, uint8_t* from, const uint8_t* fromInstruction, uint8_t* to) { return Assembler::link<copy>(record, from, fromInstruction, to); }

static bool isCompactPtrAlignedAddressOffset(ptrdiff_t value)
Expand Down
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/assembler/MacroAssemblerARMv7.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MacroAssemblerARMv7 : public AbstractMacroAssembler<Assembler> {
static JumpLinkType computeJumpType(LinkRecord& record, const uint8_t* from, const uint8_t* to) { return ARMv7Assembler::computeJumpType(record, from, to); }
static int jumpSizeDelta(JumpType jumpType, JumpLinkType jumpLinkType) { return ARMv7Assembler::jumpSizeDelta(jumpType, jumpLinkType); }

template <Assembler::CopyFunction copy>
template<MachineCodeCopyMode copy>
ALWAYS_INLINE static void link(LinkRecord& record, uint8_t* from, const uint8_t* fromInstruction, uint8_t* to) { return ARMv7Assembler::link<copy>(record, from, fromInstruction, to); }

struct ArmAddress {
Expand Down
Loading

0 comments on commit bbfda64

Please sign in to comment.