1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/**
* This source code is licensed under the terms found in the LICENSE file in
* node-jsc's root directory.
*/
#include "config.h"
#include "ErrorStackTrace.h"
#include <JavaScriptCore/CatchScope.h>
#include <JavaScriptCore/DebuggerPrimitives.h>
#include <JavaScriptCore/Exception.h>
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/ErrorInstance.h>
#include <JavaScriptCore/StackVisitor.h>
#include <wtf/IterationStatus.h>
using namespace JSC;
using namespace WebCore;
namespace Zig {
JSCStackTrace JSCStackTrace::fromExisting(JSC::VM& vm, const WTF::Vector<JSC::StackFrame>& existingFrames)
{
WTF::Vector<JSCStackFrame> newFrames;
size_t frameCount = existingFrames.size();
if (0 == frameCount) {
return JSCStackTrace();
}
newFrames.reserveInitialCapacity(frameCount);
for (size_t i = 0; i < frameCount; i++) {
newFrames.constructAndAppend(vm, existingFrames.at(i));
}
return JSCStackTrace(newFrames);
}
JSCStackTrace JSCStackTrace::captureCurrentJSStackTrace(Zig::GlobalObject* globalObject, JSC::CallFrame* callFrame, size_t frameLimit, JSC::JSValue caller)
{
JSC::VM& vm = globalObject->vm();
if (!callFrame) {
return JSCStackTrace();
}
WTF::Vector<JSCStackFrame> stackFrames;
size_t framesCount = 0;
bool belowCaller = false;
int32_t skipFrames = 0;
WTF::String callerName {};
if (JSC::JSFunction* callerFunction = JSC::jsDynamicCast<JSC::JSFunction*>(caller)) {
callerName = callerFunction->name(vm);
if (!callerFunction->name(vm).isEmpty() || callerFunction->isHostOrBuiltinFunction()) {
callerName = callerFunction->name(vm);
} else {
callerName = callerFunction->jsExecutable()->name().string();
}
}
if (JSC::InternalFunction* callerFunctionInternal = JSC::jsDynamicCast<JSC::InternalFunction*>(caller)) {
callerName = callerFunctionInternal->name();
}
JSC::StackVisitor::visit(callFrame, vm, [&](JSC::StackVisitor& visitor) -> WTF::IterationStatus {
// skip caller frame and all frames above it
if (!callerName.isEmpty()) {
if (!belowCaller) {
if (visitor->functionName() == callerName) {
belowCaller = true;
return WTF::IterationStatus::Continue;
}
skipFrames += 1;
}
}
if (!visitor->isNativeFrame()) {
framesCount++;
}
return WTF::IterationStatus::Continue;
});
framesCount = std::min(frameLimit, framesCount);
// Create the actual stack frames
size_t i = 0;
stackFrames.reserveInitialCapacity(framesCount);
JSC::StackVisitor::visit(callFrame, vm, [&](JSC::StackVisitor& visitor) -> WTF::IterationStatus {
// Skip native frames
if (visitor->isNativeFrame()) {
return WTF::IterationStatus::Continue;
}
// Skip frames if needed
if (skipFrames > 0) {
skipFrames--;
return WTF::IterationStatus::Continue;
}
stackFrames.constructAndAppend(vm, visitor);
i++;
return (i == framesCount) ? WTF::IterationStatus::Done : WTF::IterationStatus::Continue;
});
return JSCStackTrace(stackFrames);
}
JSCStackTrace JSCStackTrace::getStackTraceForThrownValue(JSC::VM& vm, JSC::JSValue thrownValue)
{
const WTF::Vector<JSC::StackFrame>* jscStackTrace = nullptr;
JSC::Exception* currentException = DECLARE_CATCH_SCOPE(vm).exception();
if (currentException && currentException->value() == thrownValue) {
jscStackTrace = ¤tException->stack();
} else {
JSC::ErrorInstance* error = JSC::jsDynamicCast<JSC::ErrorInstance*>(thrownValue);
if (error) {
jscStackTrace = error->stackTrace();
}
}
if (!jscStackTrace) {
return JSCStackTrace();
}
return fromExisting(vm, *jscStackTrace);
}
JSCStackFrame::JSCStackFrame(JSC::VM& vm, JSC::StackVisitor& visitor)
: m_vm(vm)
, m_codeBlock(nullptr)
, m_bytecodeIndex(JSC::BytecodeIndex())
, m_sourceURL(nullptr)
, m_functionName(nullptr)
, m_isWasmFrame(false)
, m_sourcePositionsState(SourcePositionsState::NotCalculated)
{
m_callee = visitor->callee().asCell();
m_callFrame = visitor->callFrame();
// Based on JSC's GetStackTraceFunctor (Interpreter.cpp)
if (visitor->isWasmFrame()) {
m_wasmFunctionIndexOrName = visitor->wasmFunctionIndexOrName();
m_isWasmFrame = true;
} else if (!!visitor->codeBlock() && !visitor->codeBlock()->unlinkedCodeBlock()->isBuiltinFunction()) {
m_codeBlock = visitor->codeBlock();
m_bytecodeIndex = visitor->bytecodeIndex();
}
}
JSCStackFrame::JSCStackFrame(JSC::VM& vm, const JSC::StackFrame& frame)
: m_vm(vm)
, m_callFrame(nullptr)
, m_codeBlock(nullptr)
, m_bytecodeIndex(JSC::BytecodeIndex())
, m_sourceURL(nullptr)
, m_functionName(nullptr)
, m_isWasmFrame(false)
, m_sourcePositionsState(SourcePositionsState::NotCalculated)
{
m_callee = frame.callee();
// Based on JSC's GetStackTraceFunctor (Interpreter.cpp)
if (frame.isWasmFrame()) {
m_wasmFunctionIndexOrName = frame.wasmFunctionIndexOrName();
m_isWasmFrame = true;
} else {
m_codeBlock = frame.codeBlock();
if (frame.hasBytecodeIndex()) {
m_bytecodeIndex = frame.bytecodeIndex();
}
}
}
intptr_t JSCStackFrame::sourceID() const
{
return m_codeBlock ? m_codeBlock->ownerExecutable()->sourceID() : JSC::noSourceID;
}
JSC::JSString* JSCStackFrame::sourceURL()
{
if (!m_sourceURL) {
m_sourceURL = retrieveSourceURL();
}
return m_sourceURL;
}
JSC::JSString* JSCStackFrame::functionName()
{
if (!m_functionName) {
m_functionName = retrieveFunctionName();
}
return m_functionName;
}
JSC::JSString* JSCStackFrame::typeName()
{
if (!m_typeName) {
m_typeName = retrieveTypeName();
}
return m_typeName;
}
JSCStackFrame::SourcePositions* JSCStackFrame::getSourcePositions()
{
if (SourcePositionsState::NotCalculated == m_sourcePositionsState) {
m_sourcePositionsState = calculateSourcePositions() ? SourcePositionsState::Calculated : SourcePositionsState::Failed;
}
return (SourcePositionsState::Calculated == m_sourcePositionsState) ? &m_sourcePositions : nullptr;
}
static auto sourceURLWasmString = MAKE_STATIC_STRING_IMPL("[wasm code]");
static auto sourceURLNativeString = MAKE_STATIC_STRING_IMPL("[native code]");
ALWAYS_INLINE JSC::JSString* JSCStackFrame::retrieveSourceURL()
{
if (m_isWasmFrame) {
return jsOwnedString(m_vm, sourceURLWasmString);
}
if (!m_codeBlock) {
return jsOwnedString(m_vm, sourceURLNativeString);
}
String sourceURL = m_codeBlock->ownerExecutable()->sourceURL();
return sourceURL.isNull() ? m_vm.smallStrings.emptyString() : JSC::jsString(m_vm, sourceURL);
}
static auto functionNameEvalCodeString = MAKE_STATIC_STRING_IMPL("eval code");
static auto functionNameModuleCodeString = MAKE_STATIC_STRING_IMPL("module code");
static auto functionNameGlobalCodeString = MAKE_STATIC_STRING_IMPL("global code");
ALWAYS_INLINE JSC::JSString* JSCStackFrame::retrieveFunctionName()
{
if (m_isWasmFrame) {
return jsString(m_vm, JSC::Wasm::makeString(m_wasmFunctionIndexOrName));
}
if (m_codeBlock) {
switch (m_codeBlock->codeType()) {
case JSC::EvalCode:
return JSC::jsOwnedString(m_vm, functionNameEvalCodeString);
case JSC::ModuleCode:
return JSC::jsOwnedString(m_vm, functionNameModuleCodeString);
case JSC::FunctionCode:
break;
case JSC::GlobalCode:
return JSC::jsOwnedString(m_vm, functionNameGlobalCodeString);
default:
ASSERT_NOT_REACHED();
}
}
if (!m_callee || !m_callee->isObject()) {
return m_vm.smallStrings.emptyString();
}
JSC::JSObject* calleeAsObject = JSC::jsCast<JSC::JSObject*>(m_callee);
// First, try the "displayName" property
JSC::JSValue displayName = calleeAsObject->getDirect(m_vm, m_vm.propertyNames->displayName);
if (displayName && isJSString(displayName)) {
return JSC::asString(displayName);
}
// Our addition - if there's no "dispalyName" property, try the "name" property
JSC::JSValue name = calleeAsObject->getDirect(m_vm, m_vm.propertyNames->name);
if (name && isJSString(name)) {
return JSC::asString(name);
}
/* For functions (either JSFunction or InternalFunction), fallback to their "native" name property.
* Based on JSC::getCalculatedDisplayName, "inlining" the
* JSFunction::calculatedDisplayName\InternalFunction::calculatedDisplayName calls */
if (JSC::JSFunction* function = JSC::jsDynamicCast<JSC::JSFunction*>(calleeAsObject)) {
// Based on JSC::JSFunction::calculatedDisplayName, skipping the "displayName" property check
WTF::String actualName = function->name(m_vm);
if (!actualName.isEmpty() || function->isHostOrBuiltinFunction()) {
return JSC::jsString(m_vm, actualName);
}
return JSC::jsString(m_vm, function->jsExecutable()->name().string());
}
if (JSC::InternalFunction* function = JSC::jsDynamicCast<JSC::InternalFunction*>(calleeAsObject)) {
// Based on JSC::InternalFunction::calculatedDisplayName, skipping the "displayName" property check
return JSC::jsString(m_vm, function->name());
}
return m_vm.smallStrings.emptyString();
}
ALWAYS_INLINE JSC::JSString* JSCStackFrame::retrieveTypeName()
{
JSC::JSObject* calleeObject = JSC::jsCast<JSC::JSObject*>(m_callee);
// return JSC::jsTypeStringForValue(m_globalObjectcalleeObject->toThis()
return jsString(m_vm, makeString(calleeObject->className()));
}
// General flow here is based on JSC's appendSourceToError (ErrorInstance.cpp)
bool JSCStackFrame::calculateSourcePositions()
{
if (!m_codeBlock) {
return false;
}
JSC::BytecodeIndex bytecodeIndex = hasBytecodeIndex() ? m_bytecodeIndex : JSC::BytecodeIndex();
/* Get the "raw" position info.
* Note that we're using m_codeBlock->unlinkedCodeBlock()->expressionRangeForBytecodeOffset rather than m_codeBlock->expressionRangeForBytecodeOffset
* in order get the "raw" offsets and avoid the CodeBlock's expressionRangeForBytecodeOffset modifications to the line and column numbers,
* (we don't need the column number from it, and we'll calculate the line "fixes" ourselves). */
int startOffset = 0;
int endOffset = 0;
int divotPoint = 0;
unsigned line = 0;
unsigned unusedColumn = 0;
m_codeBlock->unlinkedCodeBlock()->expressionRangeForBytecodeIndex(bytecodeIndex, divotPoint, startOffset, endOffset, line, unusedColumn);
divotPoint += m_codeBlock->sourceOffset();
/* On the first line of the source code, it seems that we need to "fix" the column with the starting
* offset. We currently use codeBlock->source()->startPosition().m_column.oneBasedInt() as the
* offset in the first line rather than codeBlock->firstLineColumnOffset(), which seems simpler
* (and what CodeBlock::expressionRangeForBytecodeOffset does). This is because firstLineColumnOffset
* values seems different from what we expect (according to v8's tests) and I haven't dove into the
* relevant parts in JSC (yet) to figure out why. */
unsigned columnOffset = line ? 0 : m_codeBlock->source().startColumn().zeroBasedInt();
// "Fix" the line number
JSC::ScriptExecutable* executable = m_codeBlock->ownerExecutable();
line = executable->overrideLineNumber(m_vm).value_or(line + executable->firstLine());
// Calculate the staring\ending offsets of the entire expression
int expressionStart = divotPoint - startOffset;
int expressionStop = divotPoint + endOffset;
// Make sure the range is valid
StringView sourceString = m_codeBlock->source().provider()->source();
if (!expressionStop || expressionStart > static_cast<int>(sourceString.length())) {
return false;
}
// Search for the beginning of the line
unsigned int lineStart = expressionStart;
while ((lineStart > 0) && ('\n' != sourceString[lineStart - 1])) {
lineStart--;
}
// Search for the end of the line
unsigned int lineStop = expressionStop;
unsigned int sourceLength = sourceString.length();
while ((lineStop < sourceLength) && ('\n' != sourceString[lineStop])) {
lineStop++;
}
/* Finally, store the source "positions" info.
* Notes:
* - The retrieved column seem to point the "end column". To make sure we're current, we'll calculate the
* columns ourselves, since we've already found where the line starts. Note that in v8 it should be 0-based
* here (in contrast the 1-based column number in v8::StackFrame).
* - The static_casts are ugly, but comes from differences between JSC and v8's api, and should be OK
* since no source should be longer than "max int" chars.
*/
m_sourcePositions.expressionStart = WTF::OrdinalNumber::fromZeroBasedInt(expressionStart);
m_sourcePositions.expressionStop = WTF::OrdinalNumber::fromZeroBasedInt(expressionStop);
m_sourcePositions.line = WTF::OrdinalNumber::fromZeroBasedInt(static_cast<int>(line));
m_sourcePositions.startColumn = WTF::OrdinalNumber::fromZeroBasedInt((expressionStart - lineStart) + columnOffset);
m_sourcePositions.endColumn = WTF::OrdinalNumber::fromZeroBasedInt(m_sourcePositions.startColumn.zeroBasedInt() + (expressionStop - expressionStart));
m_sourcePositions.lineStart = WTF::OrdinalNumber::fromZeroBasedInt(static_cast<int>(lineStart));
m_sourcePositions.lineStop = WTF::OrdinalNumber::fromZeroBasedInt(static_cast<int>(lineStop));
return true;
}
}
|