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