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