| 1 | /* |
| 2 | * Copyright (C) 2014, 2016 Apple Inc. All rights reserved. |
| 3 | * Copyright (c) 2010 Google Inc. All rights reserved. |
| 4 | * Copyright (C) 2012 Research In Motion Limited. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include "config.h" |
| 34 | #include "ScriptCallStackFactory.h" |
| 35 | |
| 36 | #include "CallFrame.h" |
| 37 | #include "CatchScope.h" |
| 38 | #include "CodeBlock.h" |
| 39 | #include "Exception.h" |
| 40 | #include "JSCJSValue.h" |
| 41 | #include "JSCInlines.h" |
| 42 | #include "ScriptArguments.h" |
| 43 | #include "ScriptCallFrame.h" |
| 44 | #include "StackVisitor.h" |
| 45 | #include "StrongInlines.h" |
| 46 | #include <wtf/text/WTFString.h> |
| 47 | |
| 48 | using namespace JSC; |
| 49 | |
| 50 | namespace Inspector { |
| 51 | |
| 52 | class CreateScriptCallStackFunctor { |
| 53 | public: |
| 54 | CreateScriptCallStackFunctor(bool needToSkipAFrame, Vector<ScriptCallFrame>& frames, size_t remainingCapacity) |
| 55 | : m_needToSkipAFrame(needToSkipAFrame) |
| 56 | , m_frames(frames) |
| 57 | , m_remainingCapacityForFrameCapture(remainingCapacity) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | StackVisitor::Status operator()(StackVisitor& visitor) const |
| 62 | { |
| 63 | if (m_needToSkipAFrame) { |
| 64 | m_needToSkipAFrame = false; |
| 65 | return StackVisitor::Continue; |
| 66 | } |
| 67 | |
| 68 | if (m_remainingCapacityForFrameCapture) { |
| 69 | unsigned line; |
| 70 | unsigned column; |
| 71 | visitor->computeLineAndColumn(line, column); |
| 72 | m_frames.append(ScriptCallFrame(visitor->functionName(), visitor->sourceURL(), static_cast<SourceID>(visitor->sourceID()), line, column)); |
| 73 | |
| 74 | m_remainingCapacityForFrameCapture--; |
| 75 | return StackVisitor::Continue; |
| 76 | } |
| 77 | |
| 78 | return StackVisitor::Done; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | mutable bool m_needToSkipAFrame; |
| 83 | Vector<ScriptCallFrame>& m_frames; |
| 84 | mutable size_t m_remainingCapacityForFrameCapture; |
| 85 | }; |
| 86 | |
| 87 | Ref<ScriptCallStack> createScriptCallStack(JSC::ExecState* exec, size_t maxStackSize) |
| 88 | { |
| 89 | if (!exec) |
| 90 | return ScriptCallStack::create(); |
| 91 | |
| 92 | JSLockHolder locker(exec); |
| 93 | Vector<ScriptCallFrame> frames; |
| 94 | |
| 95 | CallFrame* frame = exec->vm().topCallFrame; |
| 96 | if (!frame) |
| 97 | return ScriptCallStack::create(); |
| 98 | CreateScriptCallStackFunctor functor(false, frames, maxStackSize); |
| 99 | frame->iterate(functor); |
| 100 | |
| 101 | return ScriptCallStack::create(frames); |
| 102 | } |
| 103 | |
| 104 | Ref<ScriptCallStack> createScriptCallStackForConsole(JSC::ExecState* exec, size_t maxStackSize) |
| 105 | { |
| 106 | if (!exec) |
| 107 | return ScriptCallStack::create(); |
| 108 | |
| 109 | JSLockHolder locker(exec); |
| 110 | Vector<ScriptCallFrame> frames; |
| 111 | |
| 112 | CallFrame* frame = exec->vm().topCallFrame; |
| 113 | if (!frame) |
| 114 | return ScriptCallStack::create(); |
| 115 | CreateScriptCallStackFunctor functor(true, frames, maxStackSize); |
| 116 | frame->iterate(functor); |
| 117 | |
| 118 | if (frames.isEmpty()) { |
| 119 | CreateScriptCallStackFunctor functor(false, frames, maxStackSize); |
| 120 | frame->iterate(functor); |
| 121 | } |
| 122 | |
| 123 | return ScriptCallStack::create(frames); |
| 124 | } |
| 125 | |
| 126 | static bool (JSC::ExecState* exec, JSObject* exceptionObject, int* lineNumber, int* columnNumber, String* sourceURL) |
| 127 | { |
| 128 | VM& vm = exec->vm(); |
| 129 | auto scope = DECLARE_CATCH_SCOPE(vm); |
| 130 | |
| 131 | // FIXME: <http://webkit.org/b/115087> Web Inspector: Should not need to evaluate JavaScript handling exceptions |
| 132 | JSValue lineValue = exceptionObject->getDirect(vm, Identifier::fromString(exec, "line" )); |
| 133 | JSValue columnValue = exceptionObject->getDirect(vm, Identifier::fromString(exec, "column" )); |
| 134 | JSValue sourceURLValue = exceptionObject->getDirect(vm, Identifier::fromString(exec, "sourceURL" )); |
| 135 | |
| 136 | bool result = false; |
| 137 | if (lineValue && lineValue.isNumber() |
| 138 | && sourceURLValue && sourceURLValue.isString()) { |
| 139 | *lineNumber = int(lineValue.toNumber(exec)); |
| 140 | *columnNumber = columnValue && columnValue.isNumber() ? int(columnValue.toNumber(exec)) : 0; |
| 141 | *sourceURL = sourceURLValue.toWTFString(exec); |
| 142 | result = true; |
| 143 | } else if (ErrorInstance* error = jsDynamicCast<ErrorInstance*>(vm, exceptionObject)) { |
| 144 | unsigned unsignedLine; |
| 145 | unsigned unsignedColumn; |
| 146 | result = getLineColumnAndSource(error->stackTrace(), unsignedLine, unsignedColumn, *sourceURL); |
| 147 | *lineNumber = static_cast<int>(unsignedLine); |
| 148 | *columnNumber = static_cast<int>(unsignedColumn); |
| 149 | } |
| 150 | |
| 151 | if (sourceURL->isEmpty()) |
| 152 | *sourceURL = "undefined"_s ; |
| 153 | |
| 154 | scope.clearException(); |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | Ref<ScriptCallStack> createScriptCallStackFromException(JSC::ExecState* exec, JSC::Exception* exception, size_t maxStackSize) |
| 159 | { |
| 160 | Vector<ScriptCallFrame> frames; |
| 161 | auto& stackTrace = exception->stack(); |
| 162 | VM& vm = exec->vm(); |
| 163 | for (size_t i = 0; i < stackTrace.size() && i < maxStackSize; i++) { |
| 164 | unsigned line; |
| 165 | unsigned column; |
| 166 | stackTrace[i].computeLineAndColumn(line, column); |
| 167 | String functionName = stackTrace[i].functionName(vm); |
| 168 | frames.append(ScriptCallFrame(functionName, stackTrace[i].sourceURL(), static_cast<SourceID>(stackTrace[i].sourceID()), line, column)); |
| 169 | } |
| 170 | |
| 171 | // Fallback to getting at least the line and sourceURL from the exception object if it has values and the exceptionStack doesn't. |
| 172 | if (exception->value().isObject()) { |
| 173 | JSObject* exceptionObject = exception->value().toObject(exec); |
| 174 | ASSERT(exceptionObject); |
| 175 | int lineNumber; |
| 176 | int columnNumber; |
| 177 | String exceptionSourceURL; |
| 178 | if (!frames.size()) { |
| 179 | if (extractSourceInformationFromException(exec, exceptionObject, &lineNumber, &columnNumber, &exceptionSourceURL)) |
| 180 | frames.append(ScriptCallFrame(String(), exceptionSourceURL, noSourceID, lineNumber, columnNumber)); |
| 181 | } else { |
| 182 | // FIXME: The typical stack trace will have a native frame at the top, and consumers of |
| 183 | // this code already know this (see JSDOMExceptionHandling.cpp's reportException, for |
| 184 | // example - it uses firstNonNativeCallFrame). This looks like it splats something else |
| 185 | // over it. That something else is probably already at stackTrace[1]. |
| 186 | // https://bugs.webkit.org/show_bug.cgi?id=176663 |
| 187 | if (!stackTrace[0].hasLineAndColumnInfo() || stackTrace[0].sourceURL().isEmpty()) { |
| 188 | const ScriptCallFrame& firstCallFrame = frames.first(); |
| 189 | if (extractSourceInformationFromException(exec, exceptionObject, &lineNumber, &columnNumber, &exceptionSourceURL)) |
| 190 | frames[0] = ScriptCallFrame(firstCallFrame.functionName(), exceptionSourceURL, stackTrace[0].sourceID(), lineNumber, columnNumber); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return ScriptCallStack::create(frames); |
| 196 | } |
| 197 | |
| 198 | Ref<ScriptArguments> createScriptArguments(JSC::ExecState* state, unsigned skipArgumentCount) |
| 199 | { |
| 200 | VM& vm = state->vm(); |
| 201 | Vector<JSC::Strong<JSC::Unknown>> arguments; |
| 202 | size_t argumentCount = state->argumentCount(); |
| 203 | for (size_t i = skipArgumentCount; i < argumentCount; ++i) |
| 204 | arguments.append({ vm, state->uncheckedArgument(i) }); |
| 205 | return ScriptArguments::create(*state, WTFMove(arguments)); |
| 206 | } |
| 207 | |
| 208 | } // namespace Inspector |
| 209 | |