1 | /* |
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected]) |
3 | * Copyright (C) 2001 Peter Kelly ([email protected]) |
4 | * Copyright (C) 2003-2018 Apple Inc. All rights reserved. |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public License |
17 | * along with this library; see the file COPYING.LIB. If not, write to |
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | * |
21 | */ |
22 | |
23 | #pragma once |
24 | |
25 | #include "AbstractPC.h" |
26 | #include "CalleeBits.h" |
27 | #include "MacroAssemblerCodeRef.h" |
28 | #include "Register.h" |
29 | #include "StackVisitor.h" |
30 | #include "VM.h" |
31 | #include "VMEntryRecord.h" |
32 | |
33 | namespace JSC { |
34 | |
35 | class Arguments; |
36 | class ExecState; |
37 | class Interpreter; |
38 | class JSCallee; |
39 | class JSScope; |
40 | class SourceOrigin; |
41 | |
42 | struct Instruction; |
43 | |
44 | typedef ExecState CallFrame; |
45 | |
46 | class CallSiteIndex { |
47 | public: |
48 | CallSiteIndex() = default; |
49 | |
50 | explicit CallSiteIndex(uint32_t bits) |
51 | : m_bits(bits) |
52 | { } |
53 | #if USE(JSVALUE32_64) |
54 | explicit CallSiteIndex(const Instruction* instruction) |
55 | : m_bits(bitwise_cast<uint32_t>(instruction)) |
56 | { } |
57 | #endif |
58 | |
59 | explicit operator bool() const { return m_bits != UINT_MAX; } |
60 | bool operator==(const CallSiteIndex& other) const { return m_bits == other.m_bits; } |
61 | |
62 | inline uint32_t bits() const { return m_bits; } |
63 | |
64 | private: |
65 | uint32_t m_bits { UINT_MAX }; |
66 | }; |
67 | |
68 | class DisposableCallSiteIndex : public CallSiteIndex { |
69 | public: |
70 | DisposableCallSiteIndex() = default; |
71 | |
72 | explicit DisposableCallSiteIndex(uint32_t bits) |
73 | : CallSiteIndex(bits) |
74 | { |
75 | } |
76 | |
77 | static DisposableCallSiteIndex fromCallSiteIndex(CallSiteIndex callSiteIndex) |
78 | { |
79 | return DisposableCallSiteIndex(callSiteIndex.bits()); |
80 | } |
81 | }; |
82 | |
83 | // arm64_32 expects caller frame and return pc to use 8 bytes |
84 | struct CallerFrameAndPC { |
85 | alignas(CPURegister) CallFrame* callerFrame; |
86 | alignas(CPURegister) const Instruction* returnPC; |
87 | static const int sizeInRegisters = 2 * sizeof(CPURegister) / sizeof(Register); |
88 | }; |
89 | static_assert(CallerFrameAndPC::sizeInRegisters == sizeof(CallerFrameAndPC) / sizeof(Register), "CallerFrameAndPC::sizeInRegisters is incorrect." ); |
90 | |
91 | struct CallFrameSlot { |
92 | static const int codeBlock = CallerFrameAndPC::sizeInRegisters; |
93 | static const int callee = codeBlock + 1; |
94 | static const int argumentCount = callee + 1; |
95 | static const int thisArgument = argumentCount + 1; |
96 | static const int firstArgument = thisArgument + 1; |
97 | }; |
98 | |
99 | // Represents the current state of script execution. |
100 | // Passed as the first argument to most functions. |
101 | class ExecState : private Register { |
102 | public: |
103 | static const int = CallFrameSlot::argumentCount + 1; |
104 | |
105 | // This function should only be called in very specific circumstances |
106 | // when you've guaranteed the callee can't be a Wasm callee, and can |
107 | // be an arbitrary JSValue. This function should basically never be used. |
108 | // Its only use right now is when we are making a call, and we're not |
109 | // yet sure if the callee is a cell. In general, a JS callee is guaranteed |
110 | // to be a cell, however, there is a brief window where we need to check |
111 | // to see if it's a cell, and if it's not, we throw an exception. |
112 | JSValue guaranteedJSValueCallee() const |
113 | { |
114 | ASSERT(!callee().isWasm()); |
115 | return this[CallFrameSlot::callee].jsValue(); |
116 | } |
117 | JSObject* jsCallee() const |
118 | { |
119 | ASSERT(!callee().isWasm()); |
120 | return this[CallFrameSlot::callee].object(); |
121 | } |
122 | CalleeBits callee() const { return CalleeBits(this[CallFrameSlot::callee].pointer()); } |
123 | SUPPRESS_ASAN CalleeBits unsafeCallee() const { return CalleeBits(this[CallFrameSlot::callee].asanUnsafePointer()); } |
124 | CodeBlock* codeBlock() const { return this[CallFrameSlot::codeBlock].Register::codeBlock(); } |
125 | CodeBlock** addressOfCodeBlock() const { return bitwise_cast<CodeBlock**>(this + CallFrameSlot::codeBlock); } |
126 | SUPPRESS_ASAN CodeBlock* unsafeCodeBlock() const { return this[CallFrameSlot::codeBlock].Register::asanUnsafeCodeBlock(); } |
127 | JSScope* scope(int scopeRegisterOffset) const |
128 | { |
129 | ASSERT(this[scopeRegisterOffset].Register::scope()); |
130 | return this[scopeRegisterOffset].Register::scope(); |
131 | } |
132 | |
133 | JSGlobalObject* wasmAwareLexicalGlobalObject(VM&); |
134 | |
135 | bool isAnyWasmCallee(); |
136 | |
137 | // Global object in which the currently executing code was defined. |
138 | // Differs from VM::vmEntryGlobalObject() during function calls across web browser frames. |
139 | JSGlobalObject* lexicalGlobalObject() const; |
140 | |
141 | // Differs from lexicalGlobalObject because this will have DOM window shell rather than |
142 | // the actual DOM window, which can't be "this" for security reasons. |
143 | JSObject* globalThisValue() const; |
144 | |
145 | VM& vm() const; |
146 | |
147 | static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); } |
148 | Register* registers() { return this; } |
149 | const Register* registers() const { return this; } |
150 | |
151 | CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; } |
152 | |
153 | CallFrame* callerFrame() const { return static_cast<CallFrame*>(callerFrameOrEntryFrame()); } |
154 | void* callerFrameOrEntryFrame() const { return callerFrameAndPC().callerFrame; } |
155 | SUPPRESS_ASAN void* unsafeCallerFrameOrEntryFrame() const { return unsafeCallerFrameAndPC().callerFrame; } |
156 | |
157 | CallFrame* unsafeCallerFrame(EntryFrame*&) const; |
158 | JS_EXPORT_PRIVATE CallFrame* callerFrame(EntryFrame*&) const; |
159 | |
160 | JS_EXPORT_PRIVATE SourceOrigin callerSourceOrigin(); |
161 | |
162 | static ptrdiff_t callerFrameOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, callerFrame); } |
163 | |
164 | ReturnAddressPtr returnPC() const { return ReturnAddressPtr(callerFrameAndPC().returnPC); } |
165 | bool hasReturnPC() const { return !!callerFrameAndPC().returnPC; } |
166 | void clearReturnPC() { callerFrameAndPC().returnPC = 0; } |
167 | static ptrdiff_t returnPCOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, returnPC); } |
168 | AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); } |
169 | |
170 | bool callSiteBitsAreBytecodeOffset() const; |
171 | bool callSiteBitsAreCodeOriginIndex() const; |
172 | |
173 | unsigned callSiteAsRawBits() const; |
174 | unsigned unsafeCallSiteAsRawBits() const; |
175 | CallSiteIndex callSiteIndex() const; |
176 | CallSiteIndex unsafeCallSiteIndex() const; |
177 | private: |
178 | unsigned callSiteBitsAsBytecodeOffset() const; |
179 | public: |
180 | |
181 | // This will try to get you the bytecode offset, but you should be aware that |
182 | // this bytecode offset may be bogus in the presence of inlining. This will |
183 | // also return 0 if the call frame has no notion of bytecode offsets (for |
184 | // example if it's native code). |
185 | // https://bugs.webkit.org/show_bug.cgi?id=121754 |
186 | unsigned bytecodeOffset(); |
187 | |
188 | // This will get you a CodeOrigin. It will always succeed. May return |
189 | // CodeOrigin(0) if we're in native code. |
190 | JS_EXPORT_PRIVATE CodeOrigin codeOrigin(); |
191 | |
192 | Register* topOfFrame() |
193 | { |
194 | if (!codeBlock()) |
195 | return registers(); |
196 | return topOfFrameInternal(); |
197 | } |
198 | |
199 | const Instruction* currentVPC() const; // This only makes sense in the LLInt and baseline. |
200 | void setCurrentVPC(const Instruction*); |
201 | |
202 | void setCallerFrame(CallFrame* frame) { callerFrameAndPC().callerFrame = frame; } |
203 | void setScope(int scopeRegisterOffset, JSScope* scope) { static_cast<Register*>(this)[scopeRegisterOffset] = scope; } |
204 | |
205 | static void initGlobalExec(ExecState* globalExec, JSCallee* globalCallee); |
206 | |
207 | // Read a register from the codeframe (or constant from the CodeBlock). |
208 | Register& r(int); |
209 | Register& r(VirtualRegister); |
210 | // Read a register for a non-constant |
211 | Register& uncheckedR(int); |
212 | Register& uncheckedR(VirtualRegister); |
213 | |
214 | // Access to arguments as passed. (After capture, arguments may move to a different location.) |
215 | size_t argumentCount() const { return argumentCountIncludingThis() - 1; } |
216 | size_t argumentCountIncludingThis() const { return this[CallFrameSlot::argumentCount].payload(); } |
217 | static int argumentOffset(int argument) { return (CallFrameSlot::firstArgument + argument); } |
218 | static int argumentOffsetIncludingThis(int argument) { return (CallFrameSlot::thisArgument + argument); } |
219 | |
220 | // In the following (argument() and setArgument()), the 'argument' |
221 | // parameter is the index of the arguments of the target function of |
222 | // this frame. The index starts at 0 for the first arg, 1 for the |
223 | // second, etc. |
224 | // |
225 | // The arguments (in this case) do not include the 'this' value. |
226 | // arguments(0) will not fetch the 'this' value. To get/set 'this', |
227 | // use thisValue() and setThisValue() below. |
228 | |
229 | JSValue* addressOfArgumentsStart() const { return bitwise_cast<JSValue*>(this + argumentOffset(0)); } |
230 | JSValue argument(size_t argument) |
231 | { |
232 | if (argument >= argumentCount()) |
233 | return jsUndefined(); |
234 | return getArgumentUnsafe(argument); |
235 | } |
236 | JSValue uncheckedArgument(size_t argument) |
237 | { |
238 | ASSERT(argument < argumentCount()); |
239 | return getArgumentUnsafe(argument); |
240 | } |
241 | void setArgument(size_t argument, JSValue value) |
242 | { |
243 | this[argumentOffset(argument)] = value; |
244 | } |
245 | |
246 | JSValue getArgumentUnsafe(size_t argIndex) |
247 | { |
248 | // User beware! This method does not verify that there is a valid |
249 | // argument at the specified argIndex. This is used for debugging |
250 | // and verification code only. The caller is expected to know what |
251 | // he/she is doing when calling this method. |
252 | return this[argumentOffset(argIndex)].jsValue(); |
253 | } |
254 | |
255 | static int thisArgumentOffset() { return argumentOffsetIncludingThis(0); } |
256 | JSValue thisValue() { return this[thisArgumentOffset()].jsValue(); } |
257 | void setThisValue(JSValue value) { this[thisArgumentOffset()] = value; } |
258 | |
259 | // Under the constructor implemented in C++, thisValue holds the newTarget instead of the automatically constructed value. |
260 | // The result of this function is only effective under the "construct" context. |
261 | JSValue newTarget() { return thisValue(); } |
262 | |
263 | JSValue argumentAfterCapture(size_t argument); |
264 | |
265 | static int offsetFor(size_t argumentCountIncludingThis) { return argumentCountIncludingThis + CallFrameSlot::thisArgument - 1; } |
266 | |
267 | static CallFrame* noCaller() { return nullptr; } |
268 | bool isGlobalExec() const |
269 | { |
270 | return callerFrameAndPC().callerFrame == noCaller() && callerFrameAndPC().returnPC == nullptr; |
271 | } |
272 | |
273 | void convertToStackOverflowFrame(VM&, CodeBlock* codeBlockToKeepAliveUntilFrameIsUnwound); |
274 | bool isStackOverflowFrame() const; |
275 | bool isWasmFrame() const; |
276 | |
277 | void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[CallFrameSlot::argumentCount].payload() = count; } |
278 | void setCallee(JSObject* callee) { static_cast<Register*>(this)[CallFrameSlot::callee] = callee; } |
279 | void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[CallFrameSlot::codeBlock] = codeBlock; } |
280 | void setReturnPC(void* value) { callerFrameAndPC().returnPC = reinterpret_cast<const Instruction*>(value); } |
281 | |
282 | String friendlyFunctionName(); |
283 | |
284 | // CallFrame::iterate() expects a Functor that implements the following method: |
285 | // StackVisitor::Status operator()(StackVisitor&) const; |
286 | // FIXME: This method is improper. We rely on the fact that we can call it with a null |
287 | // receiver. We should always be using StackVisitor directly. |
288 | // It's only valid to call this from a non-wasm top frame. |
289 | template <StackVisitor::EmptyEntryFrameAction action = StackVisitor::ContinueIfTopEntryFrameIsEmpty, typename Functor> void iterate(const Functor& functor) |
290 | { |
291 | VM* vm; |
292 | void* rawThis = this; |
293 | if (!!rawThis) { |
294 | RELEASE_ASSERT(callee().isCell()); |
295 | vm = &this->vm(); |
296 | } else |
297 | vm = nullptr; |
298 | StackVisitor::visit<action, Functor>(this, vm, functor); |
299 | } |
300 | |
301 | void dump(PrintStream&); |
302 | JS_EXPORT_PRIVATE const char* describeFrame(); |
303 | |
304 | private: |
305 | |
306 | ExecState(); |
307 | ~ExecState(); |
308 | |
309 | Register* topOfFrameInternal(); |
310 | |
311 | // The following are for internal use in debugging and verification |
312 | // code only and not meant as an API for general usage: |
313 | |
314 | size_t argIndexForRegister(Register* reg) |
315 | { |
316 | // The register at 'offset' number of slots from the frame pointer |
317 | // i.e. |
318 | // reg = frame[offset]; |
319 | // ==> reg = frame + offset; |
320 | // ==> offset = reg - frame; |
321 | int offset = reg - this->registers(); |
322 | |
323 | // The offset is defined (based on argumentOffset()) to be: |
324 | // offset = CallFrameSlot::firstArgument - argIndex; |
325 | // Hence: |
326 | // argIndex = CallFrameSlot::firstArgument - offset; |
327 | size_t argIndex = offset - CallFrameSlot::firstArgument; |
328 | return argIndex; |
329 | } |
330 | |
331 | CallerFrameAndPC& callerFrameAndPC() { return *reinterpret_cast<CallerFrameAndPC*>(this); } |
332 | const CallerFrameAndPC& callerFrameAndPC() const { return *reinterpret_cast<const CallerFrameAndPC*>(this); } |
333 | SUPPRESS_ASAN const CallerFrameAndPC& unsafeCallerFrameAndPC() const { return *reinterpret_cast<const CallerFrameAndPC*>(this); } |
334 | }; |
335 | |
336 | } // namespace JSC |
337 | |