1 | /*
|
---|
2 | * Copyright (C) 2016-2020 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 "CCallHelpers.h"
|
---|
28 |
|
---|
29 | #if ENABLE(JIT)
|
---|
30 |
|
---|
31 | #include "LinkBuffer.h"
|
---|
32 | #include "MaxFrameExtentForSlowPathCall.h"
|
---|
33 | #include "ShadowChicken.h"
|
---|
34 |
|
---|
35 | namespace JSC {
|
---|
36 |
|
---|
37 | void CCallHelpers::logShadowChickenProloguePacket(GPRReg shadowPacket, GPRReg scratch1, GPRReg scope)
|
---|
38 | {
|
---|
39 | storePtr(GPRInfo::callFrameRegister, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, frame)));
|
---|
40 | loadPtr(Address(GPRInfo::callFrameRegister, OBJECT_OFFSETOF(CallerFrameAndPC, callerFrame)), scratch1);
|
---|
41 | storePtr(scratch1, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, callerFrame)));
|
---|
42 | loadPtr(addressFor(CallFrameSlot::callee), scratch1);
|
---|
43 | storePtr(scratch1, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, callee)));
|
---|
44 | storePtr(scope, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, scope)));
|
---|
45 | }
|
---|
46 |
|
---|
47 | void CCallHelpers::ensureShadowChickenPacket(VM& vm, GPRReg shadowPacket, GPRReg scratch1NonArgGPR, GPRReg scratch2)
|
---|
48 | {
|
---|
49 | ShadowChicken* shadowChicken = vm.shadowChicken();
|
---|
50 | RELEASE_ASSERT(shadowChicken);
|
---|
51 | ASSERT(!RegisterSet::argumentGPRS().get(scratch1NonArgGPR));
|
---|
52 | move(TrustedImmPtr(shadowChicken->addressOfLogCursor()), scratch1NonArgGPR);
|
---|
53 | loadPtr(Address(scratch1NonArgGPR), shadowPacket);
|
---|
54 | Jump ok = branchPtr(Below, shadowPacket, TrustedImmPtr(shadowChicken->logEnd()));
|
---|
55 | setupArguments<decltype(operationProcessShadowChickenLog)>(TrustedImmPtr(&vm));
|
---|
56 | prepareCallOperation(vm);
|
---|
57 | move(TrustedImmPtr(tagCFunction<OperationPtrTag>(operationProcessShadowChickenLog)), scratch1NonArgGPR);
|
---|
58 | call(scratch1NonArgGPR, OperationPtrTag);
|
---|
59 | move(TrustedImmPtr(shadowChicken->addressOfLogCursor()), scratch1NonArgGPR);
|
---|
60 | loadPtr(Address(scratch1NonArgGPR), shadowPacket);
|
---|
61 | ok.link(this);
|
---|
62 | addPtr(TrustedImm32(sizeof(ShadowChicken::Packet)), shadowPacket, scratch2);
|
---|
63 | storePtr(scratch2, Address(scratch1NonArgGPR));
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | template <typename CodeBlockType>
|
---|
68 | void CCallHelpers::logShadowChickenTailPacketImpl(GPRReg shadowPacket, JSValueRegs thisRegs, GPRReg scope, CodeBlockType codeBlock, CallSiteIndex callSiteIndex)
|
---|
69 | {
|
---|
70 | storePtr(GPRInfo::callFrameRegister, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, frame)));
|
---|
71 | storePtr(TrustedImmPtr(ShadowChicken::Packet::tailMarker()), Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, callee)));
|
---|
72 | storeValue(thisRegs, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, thisValue)));
|
---|
73 | storePtr(scope, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, scope)));
|
---|
74 | storePtr(codeBlock, Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, codeBlock)));
|
---|
75 | store32(TrustedImm32(callSiteIndex.bits()), Address(shadowPacket, OBJECT_OFFSETOF(ShadowChicken::Packet, callSiteIndex)));
|
---|
76 | }
|
---|
77 |
|
---|
78 | void CCallHelpers::logShadowChickenTailPacket(GPRReg shadowPacket, JSValueRegs thisRegs, GPRReg scope, GPRReg codeBlock, CallSiteIndex callSiteIndex)
|
---|
79 | {
|
---|
80 | logShadowChickenTailPacketImpl(shadowPacket, thisRegs, scope, codeBlock, callSiteIndex);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void CCallHelpers::emitJITCodeOver(MacroAssemblerCodePtr<JSInternalPtrTag> where, ScopedLambda<void(CCallHelpers&)> emitCode, const char* description)
|
---|
84 | {
|
---|
85 | CCallHelpers jit;
|
---|
86 | emitCode(jit);
|
---|
87 |
|
---|
88 | constexpr bool needsBranchCompaction = false;
|
---|
89 | LinkBuffer linkBuffer(jit, where, jit.m_assembler.buffer().codeSize(), LinkBuffer::Profile::InlineCache, JITCompilationMustSucceed, needsBranchCompaction);
|
---|
90 | FINALIZE_CODE(linkBuffer, NoPtrTag, description);
|
---|
91 | }
|
---|
92 |
|
---|
93 | static_assert(!((maxFrameExtentForSlowPathCall + 2 * sizeof(CPURegister)) % 16), "Stack must be aligned after CTI thunk entry");
|
---|
94 |
|
---|
95 | void CCallHelpers::emitCTIThunkPrologue(bool returnAddressAlreadyTagged)
|
---|
96 | {
|
---|
97 | // Stash frame pointer and return address
|
---|
98 | if (!returnAddressAlreadyTagged)
|
---|
99 | tagReturnAddress();
|
---|
100 | #if CPU(X86_64)
|
---|
101 | push(X86Registers::ebp); // return address pushed by the call instruction
|
---|
102 | #elif CPU(ARM64) || CPU(ARM_THUMB2) || CPU(RISCV64)
|
---|
103 | pushPair(framePointerRegister, linkRegister);
|
---|
104 | #elif CPU(MIPS)
|
---|
105 | pushPair(framePointerRegister, returnAddressRegister);
|
---|
106 | #else
|
---|
107 | # error "Not implemented on platform"
|
---|
108 | #endif
|
---|
109 | // Make enough space on the stack to pass arguments in a call
|
---|
110 | if constexpr (maxFrameExtentForSlowPathCall)
|
---|
111 | subPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void CCallHelpers::emitCTIThunkEpilogue()
|
---|
115 | {
|
---|
116 | // Reset stack
|
---|
117 | if constexpr (maxFrameExtentForSlowPathCall)
|
---|
118 | addPtr(TrustedImm32(maxFrameExtentForSlowPathCall), stackPointerRegister);
|
---|
119 | // Restore frame pointer and return address
|
---|
120 | #if CPU(X86_64)
|
---|
121 | pop(X86Registers::ebp); // Return address left on stack
|
---|
122 | #elif CPU(ARM64) || CPU(ARM_THUMB2) || CPU(RISCV64)
|
---|
123 | popPair(framePointerRegister, linkRegister);
|
---|
124 | #elif CPU(MIPS)
|
---|
125 | popPair(framePointerRegister, returnAddressRegister);
|
---|
126 | #else
|
---|
127 | # error "Not implemented on platform"
|
---|
128 | #endif
|
---|
129 | }
|
---|
130 |
|
---|
131 | } // namespace JSC
|
---|
132 |
|
---|
133 | #endif // ENABLE(JIT)
|
---|