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
|
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003-2024 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "ErrorInstance.h"
#include "CodeBlock.h"
#include "InlineCallFrame.h"
#include "IntegrityInlines.h"
#include "Interpreter.h"
#include "JSCInlines.h"
#include "ParseInt.h"
#include "StackFrame.h"
#include <wtf/text/MakeString.h>
namespace JSC {
const ClassInfo ErrorInstance::s_info = { "Error"_s, &JSNonFinalObject::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ErrorInstance) };
ErrorInstance::ErrorInstance(VM& vm, Structure* structure, ErrorType errorType)
: Base(vm, structure)
, m_errorType(errorType)
, m_stackOverflowError(false)
, m_outOfMemoryError(false)
, m_errorInfoMaterialized(false)
, m_nativeGetterTypeError(false)
#if ENABLE(WEBASSEMBLY)
, m_catchableFromWasm(true)
#endif // ENABLE(WEBASSEMBLY)
{
}
ErrorInstance* ErrorInstance::create(JSGlobalObject* globalObject, String&& message, ErrorType errorType, LineColumn lineColumn, String&& sourceURL, String&& stackString)
{
VM& vm = globalObject->vm();
Structure* structure = globalObject->errorStructure(errorType);
ErrorInstance* instance = new (NotNull, allocateCell<ErrorInstance>(vm)) ErrorInstance(vm, structure, errorType);
instance->finishCreation(vm, WTFMove(message), lineColumn, WTFMove(sourceURL), WTFMove(stackString));
return instance;
}
ErrorInstance* ErrorInstance::create(JSGlobalObject* globalObject, Structure* structure, JSValue message, JSValue options, SourceAppender appender, RuntimeType type, ErrorType errorType, bool useCurrentFrame)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
String messageString = message.isUndefined() ? String() : message.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, nullptr);
JSValue cause;
if (options.isObject()) {
// Since `throw undefined;` is valid, we need to distinguish the case where `cause` is an explicit undefined.
cause = asObject(options)->getIfPropertyExists(globalObject, vm.propertyNames->cause);
RETURN_IF_EXCEPTION(scope, nullptr);
}
return create(vm, structure, messageString, cause, appender, type, errorType, useCurrentFrame);
}
String appendSourceToErrorMessage(CodeBlock* codeBlock, BytecodeIndex bytecodeIndex, const String& message, RuntimeType type, ErrorInstance::SourceAppender appender)
{
if (!codeBlock->hasExpressionInfo() || message.isNull())
return message;
auto info = codeBlock->expressionInfoForBytecodeIndex(bytecodeIndex);
int expressionStart = info.divot - info.startOffset;
int expressionStop = info.divot + info.endOffset;
StringView sourceString = codeBlock->source().provider()->source();
if (!expressionStop || expressionStart > static_cast<int>(sourceString.length()))
return message;
if (expressionStart < expressionStop)
return appender(message, codeBlock->source().provider()->getRange(expressionStart, expressionStop), type, ErrorInstance::FoundExactSource);
// No range information, so give a few characters of context.
int dataLength = sourceString.length();
int start = expressionStart;
int stop = expressionStart;
// Get up to 20 characters of context to the left and right of the divot, clamping to the line.
// Then strip whitespace.
while (start > 0 && (expressionStart - start < 20) && sourceString[start - 1] != '\n')
start--;
while (start < (expressionStart - 1) && isStrWhiteSpace(sourceString[start]))
start++;
while (stop < dataLength && (stop - expressionStart < 20) && sourceString[stop] != '\n')
stop++;
while (stop > expressionStart && isStrWhiteSpace(sourceString[stop - 1]))
stop--;
return appender(message, codeBlock->source().provider()->getRange(start, stop), type, ErrorInstance::FoundApproximateSource);
}
void ErrorInstance::finishCreation(VM& vm, const String& message, JSValue cause, SourceAppender appender, RuntimeType type, bool useCurrentFrame)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
m_sourceAppender = appender;
m_runtimeTypeForCause = type;
std::unique_ptr<Vector<StackFrame>> stackTrace = getStackTrace(vm, this, useCurrentFrame);
{
Locker locker { cellLock() };
m_stackTrace = WTFMove(stackTrace);
}
vm.writeBarrier(this);
String messageWithSource = message;
if (m_stackTrace && !m_stackTrace->isEmpty() && hasSourceAppender()) {
auto [codeBlock, bytecodeIndex] = getBytecodeIndex(vm, vm.topCallFrame);
if (codeBlock) {
ErrorInstance::SourceAppender appender = sourceAppender();
clearSourceAppender();
RuntimeType type = runtimeTypeForCause();
clearRuntimeTypeForCause();
messageWithSource = appendSourceToErrorMessage(codeBlock, bytecodeIndex, message, type, appender);
}
}
if (!messageWithSource.isNull())
putDirect(vm, vm.propertyNames->message, jsString(vm, WTFMove(messageWithSource)), static_cast<unsigned>(PropertyAttribute::DontEnum));
if (!cause.isEmpty())
putDirect(vm, vm.propertyNames->cause, cause, static_cast<unsigned>(PropertyAttribute::DontEnum));
}
void ErrorInstance::finishCreation(VM& vm, const String& message, JSValue cause, JSCell* owner, CallLinkInfo* callLinkInfo)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
std::unique_ptr<Vector<StackFrame>> stackTrace = getStackTrace(vm, this, /* useCurrentFrame */ true, owner, callLinkInfo);
{
Locker locker { cellLock() };
m_stackTrace = WTFMove(stackTrace);
}
vm.writeBarrier(this);
if (!message.isNull())
putDirect(vm, vm.propertyNames->message, jsString(vm, message), static_cast<unsigned>(PropertyAttribute::DontEnum));
if (!cause.isEmpty())
putDirect(vm, vm.propertyNames->cause, cause, static_cast<unsigned>(PropertyAttribute::DontEnum));
}
void ErrorInstance::finishCreation(VM& vm, String&& message, LineColumn lineColumn, String&& sourceURL, String&& stackString)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
m_lineColumn = lineColumn;
m_sourceURL = WTFMove(sourceURL);
m_stackString = WTFMove(stackString);
if (!message.isNull())
putDirect(vm, vm.propertyNames->message, jsString(vm, WTFMove(message)), static_cast<unsigned>(PropertyAttribute::DontEnum));
}
// Based on ErrorPrototype's errorProtoFuncToString(), but is modified to
// have no observable side effects to the user (i.e. does not call proxies,
// and getters).
String ErrorInstance::sanitizedMessageString(JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Integrity::auditStructureID(structureID());
JSValue messageValue;
auto messagePropertName = vm.propertyNames->message;
PropertySlot messageSlot(this, PropertySlot::InternalMethodType::VMInquiry, &vm);
if (JSObject::getOwnPropertySlot(this, globalObject, messagePropertName, messageSlot) && messageSlot.isValue())
messageValue = messageSlot.getValue(globalObject, messagePropertName);
RETURN_IF_EXCEPTION(scope, { });
if (!messageValue || !messageValue.isPrimitive())
return { };
RELEASE_AND_RETURN(scope, messageValue.toWTFString(globalObject));
}
String ErrorInstance::sanitizedNameString(JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Integrity::auditStructureID(structureID());
JSValue nameValue;
auto namePropertName = vm.propertyNames->name;
PropertySlot nameSlot(this, PropertySlot::InternalMethodType::VMInquiry, &vm);
JSValue currentObj = this;
unsigned prototypeDepth = 0;
// We only check the current object and its prototype (2 levels) because normal
// Error objects may have a name property, and if not, its prototype should have
// a name property for the type of error e.g. "SyntaxError".
while (currentObj.isCell() && prototypeDepth++ < 2) {
JSObject* obj = jsCast<JSObject*>(currentObj);
if (JSObject::getOwnPropertySlot(obj, globalObject, namePropertName, nameSlot) && nameSlot.isValue()) {
nameValue = nameSlot.getValue(globalObject, namePropertName);
break;
}
currentObj = obj->getPrototypeDirect();
}
RETURN_IF_EXCEPTION(scope, { });
if (!nameValue || !nameValue.isPrimitive())
return "Error"_s;
RELEASE_AND_RETURN(scope, nameValue.toWTFString(globalObject));
}
String ErrorInstance::sanitizedToString(JSGlobalObject* globalObject)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
Integrity::auditStructureID(structureID());
String nameString = sanitizedNameString(globalObject);
RETURN_IF_EXCEPTION(scope, String());
String messageString = sanitizedMessageString(globalObject);
RETURN_IF_EXCEPTION(scope, String());
return makeString(nameString, nameString.isEmpty() || messageString.isEmpty() ? ""_s : ": "_s, messageString);
}
String ErrorInstance::tryGetMessageForDebugging()
{
VM& vm = this->vm();
JSValue messageValue;
auto messagePropertName = vm.propertyNames->message;
PropertySlot messageSlot(this, PropertySlot::InternalMethodType::VMInquiry, &vm);
if (JSObject::getOwnNonIndexPropertySlot(vm, structure(), messagePropertName, messageSlot))
messageValue = messageSlot.getPureResult();
if (JSString* string = jsDynamicCast<JSString*>(messageValue))
return string->tryGetValue();
return emptyString();
}
void ErrorInstance::finalizeUnconditionally(VM& vm, CollectionScope)
{
if (!m_stackTrace)
return;
// We don't want to keep our stack traces alive forever if the user doesn't access the stack trace.
// If we did, we might end up keeping functions (and their global objects) alive that happened to
// get caught in a trace.
for (const auto& frame : *m_stackTrace.get()) {
if (!frame.isMarked(vm)) {
computeErrorInfo(vm);
return;
}
}
}
void ErrorInstance::computeErrorInfo(VM& vm)
{
ASSERT(!m_errorInfoMaterialized);
// Here we use DeferGCForAWhile instead of DeferGC since GC's Heap::runEndPhase can trigger this function. In
// that case, DeferGC's destructor might trigger another GC cycle which is unexpected.
DeferGCForAWhile deferGC(vm);
if (m_stackTrace && !m_stackTrace->isEmpty()) {
getLineColumnAndSource(vm, m_stackTrace.get(), m_lineColumn, m_sourceURL);
m_stackString = Interpreter::stackTraceAsString(vm, *m_stackTrace.get());
m_stackTrace = nullptr;
}
}
bool ErrorInstance::materializeErrorInfoIfNeeded(VM& vm)
{
if (m_errorInfoMaterialized)
return false;
computeErrorInfo(vm);
if (!m_stackString.isNull()) {
auto attributes = static_cast<unsigned>(PropertyAttribute::DontEnum);
putDirect(vm, vm.propertyNames->line, jsNumber(m_lineColumn.line), attributes);
putDirect(vm, vm.propertyNames->column, jsNumber(m_lineColumn.column), attributes);
if (!m_sourceURL.isEmpty())
putDirect(vm, vm.propertyNames->sourceURL, jsString(vm, WTFMove(m_sourceURL)), attributes);
putDirect(vm, vm.propertyNames->stack, jsString(vm, WTFMove(m_stackString)), attributes);
}
m_errorInfoMaterialized = true;
return true;
}
bool ErrorInstance::materializeErrorInfoIfNeeded(VM& vm, PropertyName propertyName)
{
if (propertyName == vm.propertyNames->line
|| propertyName == vm.propertyNames->column
|| propertyName == vm.propertyNames->sourceURL
|| propertyName == vm.propertyNames->stack)
return materializeErrorInfoIfNeeded(vm);
return false;
}
bool ErrorInstance::getOwnPropertySlot(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
{
VM& vm = globalObject->vm();
ErrorInstance* thisObject = jsCast<ErrorInstance*>(object);
thisObject->materializeErrorInfoIfNeeded(vm, propertyName);
return Base::getOwnPropertySlot(thisObject, globalObject, propertyName, slot);
}
void ErrorInstance::getOwnSpecialPropertyNames(JSObject* object, JSGlobalObject* globalObject, PropertyNameArray&, DontEnumPropertiesMode mode)
{
VM& vm = globalObject->vm();
ErrorInstance* thisObject = jsCast<ErrorInstance*>(object);
if (mode == DontEnumPropertiesMode::Include)
thisObject->materializeErrorInfoIfNeeded(vm);
}
bool ErrorInstance::defineOwnProperty(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, const PropertyDescriptor& descriptor, bool shouldThrow)
{
VM& vm = globalObject->vm();
ErrorInstance* thisObject = jsCast<ErrorInstance*>(object);
thisObject->materializeErrorInfoIfNeeded(vm, propertyName);
return Base::defineOwnProperty(thisObject, globalObject, propertyName, descriptor, shouldThrow);
}
bool ErrorInstance::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
{
VM& vm = globalObject->vm();
ErrorInstance* thisObject = jsCast<ErrorInstance*>(cell);
bool materializedProperties = thisObject->materializeErrorInfoIfNeeded(vm, propertyName);
if (materializedProperties)
slot.disableCaching();
return Base::put(thisObject, globalObject, propertyName, value, slot);
}
bool ErrorInstance::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName propertyName, DeletePropertySlot& slot)
{
VM& vm = globalObject->vm();
ErrorInstance* thisObject = jsCast<ErrorInstance*>(cell);
bool materializedProperties = thisObject->materializeErrorInfoIfNeeded(vm, propertyName);
if (materializedProperties)
slot.disableCaching();
return Base::deleteProperty(thisObject, globalObject, propertyName, slot);
}
} // namespace JSC
|