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
|
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003-2023 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "JSCell.h"
#include "IntegrityInlines.h"
#include "IsoSubspaceInlines.h"
#include "JSBigInt.h"
#include "JSCInlines.h"
#include "MarkedBlockInlines.h"
#include "SubspaceInlines.h"
#include "Symbol.h"
#include <wtf/LockAlgorithmInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
const ASCIILiteral SymbolCoercionError { "Cannot convert a symbol to a string"_s };
static_assert(sizeof(JSCell) == sizeof(uint64_t), "jscell is eight bytes");
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSCell);
void JSCell::destroy(JSCell* cell)
{
cell->JSCell::~JSCell();
}
void JSCell::dump(PrintStream& out) const
{
methodTable()->dumpToStream(this, out);
}
void JSCell::dumpToStream(const JSCell* cell, PrintStream& out)
{
out.printf("<%p, %s>", cell, cell->className().characters());
}
size_t JSCell::estimatedSizeInBytes(VM& vm) const
{
return methodTable()->estimatedSize(const_cast<JSCell*>(this), vm);
}
size_t JSCell::estimatedSize(JSCell* cell, VM&)
{
return cell->cellSize();
}
void JSCell::analyzeHeap(JSCell*, HeapAnalyzer&)
{
}
bool JSCell::getString(JSGlobalObject* globalObject, String& stringValue) const
{
if (!isString())
return false;
stringValue = static_cast<const JSString*>(this)->value(globalObject);
return true;
}
String JSCell::getString(JSGlobalObject* globalObject) const
{
return isString() ? static_cast<const JSString*>(this)->value(globalObject) : String();
}
JSObject* JSCell::getObject()
{
return isObject() ? asObject(this) : nullptr;
}
const JSObject* JSCell::getObject() const
{
return isObject() ? static_cast<const JSObject*>(this) : nullptr;
}
CallData JSCell::getCallData(JSCell*)
{
return { };
}
CallData JSCell::getConstructData(JSCell*)
{
return { };
}
bool JSCell::isValidCallee() const
{
return isObject() && asObject(this)->globalObject();
}
bool JSCell::put(JSCell* cell, JSGlobalObject* globalObject, PropertyName identifier, JSValue value, PutPropertySlot& slot)
{
if (cell->isString() || cell->isSymbol() || cell->isHeapBigInt())
return JSValue(cell).putToPrimitive(globalObject, identifier, value, slot);
JSObject* thisObject = cell->toObject(globalObject);
return thisObject->methodTable()->put(thisObject, globalObject, identifier, value, slot);
}
bool JSCell::putByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned identifier, JSValue value, bool shouldThrow)
{
VM& vm = globalObject->vm();
if (cell->isString() || cell->isSymbol() || cell->isHeapBigInt()) {
PutPropertySlot slot(cell, shouldThrow);
return JSValue(cell).putToPrimitive(globalObject, Identifier::from(vm, identifier), value, slot);
}
JSObject* thisObject = cell->toObject(globalObject);
return thisObject->methodTable()->putByIndex(thisObject, globalObject, identifier, value, shouldThrow);
}
bool JSCell::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName identifier, DeletePropertySlot& slot)
{
JSObject* thisObject = cell->toObject(globalObject);
return thisObject->methodTable()->deleteProperty(thisObject, globalObject, identifier, slot);
}
bool JSCell::deleteProperty(JSCell* cell, JSGlobalObject* globalObject, PropertyName identifier)
{
JSObject* thisObject = cell->toObject(globalObject);
DeletePropertySlot slot;
return thisObject->methodTable()->deleteProperty(thisObject, globalObject, identifier, slot);
}
bool JSCell::deletePropertyByIndex(JSCell* cell, JSGlobalObject* globalObject, unsigned identifier)
{
JSObject* thisObject = cell->toObject(globalObject);
return thisObject->methodTable()->deletePropertyByIndex(thisObject, globalObject, identifier);
}
JSValue JSCell::toPrimitive(JSGlobalObject* globalObject, PreferredPrimitiveType preferredType) const
{
if (const auto* string = jsDynamicCast<const JSString*>(this))
return string->toPrimitive(globalObject, preferredType);
if (const auto* symbol = jsDynamicCast<const Symbol*>(this))
return symbol->toPrimitive(globalObject, preferredType);
if (const auto* bigInt = jsDynamicCast<const JSBigInt*>(this))
return bigInt->toPrimitive(globalObject, preferredType);
return jsSecureCast<const JSObject*>(this)->toPrimitive(globalObject, preferredType);
}
double JSCell::toNumber(JSGlobalObject* globalObject) const
{
if (const auto* string = jsDynamicCast<const JSString*>(this))
return string->toNumber(globalObject);
if (const auto* symbol = jsDynamicCast<const Symbol*>(this))
return symbol->toNumber(globalObject);
if (const auto* bigInt = jsDynamicCast<const JSBigInt*>(this))
return bigInt->toNumber(globalObject);
return jsSecureCast<const JSObject*>(this)->toNumber(globalObject);
}
JSObject* JSCell::toObjectSlow(JSGlobalObject* globalObject) const
{
Integrity::auditStructureID(structureID());
ASSERT(!isObject());
if (const auto* string = jsDynamicCast<const JSString*>(this))
return string->toObject(globalObject);
if (const auto* bigInt = jsDynamicCast<const JSBigInt*>(this))
return bigInt->toObject(globalObject);
return jsSecureCast<const Symbol*>(this)->toObject(globalObject);
}
void slowValidateCell(JSCell* cell)
{
ASSERT_GC_OBJECT_LOOKS_VALID(cell);
}
bool JSCell::getOwnPropertySlot(JSObject*, JSGlobalObject*, PropertyName, PropertySlot&)
{
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool JSCell::getOwnPropertySlotByIndex(JSObject*, JSGlobalObject*, unsigned, PropertySlot&)
{
RELEASE_ASSERT_NOT_REACHED();
return false;
}
void JSCell::getOwnPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, DontEnumPropertiesMode)
{
RELEASE_ASSERT_NOT_REACHED();
}
void JSCell::getOwnSpecialPropertyNames(JSObject*, JSGlobalObject*, PropertyNameArray&, DontEnumPropertiesMode)
{
RELEASE_ASSERT_NOT_REACHED();
}
ASCIILiteral JSCell::className() const
{
return classInfo()->className;
}
bool JSCell::customHasInstance(JSObject*, JSGlobalObject*, JSValue)
{
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool JSCell::defineOwnProperty(JSObject*, JSGlobalObject*, PropertyName, const PropertyDescriptor&, bool)
{
RELEASE_ASSERT_NOT_REACHED();
return false;
}
bool JSCell::preventExtensions(JSObject*, JSGlobalObject*)
{
RELEASE_ASSERT_NOT_REACHED();
}
bool JSCell::isExtensible(JSObject*, JSGlobalObject*)
{
RELEASE_ASSERT_NOT_REACHED();
}
bool JSCell::setPrototype(JSObject*, JSGlobalObject*, JSValue, bool)
{
RELEASE_ASSERT_NOT_REACHED();
}
JSValue JSCell::getPrototype(JSObject*, JSGlobalObject*)
{
RELEASE_ASSERT_NOT_REACHED();
}
JSString* JSCell::toStringSlowCase(JSGlobalObject* globalObject) const
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ASSERT(isSymbol() || isHeapBigInt());
auto* emptyString = jsEmptyString(vm);
if (auto* bigInt = jsDynamicCast<JSBigInt*>(const_cast<JSCell*>(this))) {
// FIXME: we should rather have two cases here: one-character string vs jsNonTrivialString for everything else.
auto string = bigInt->toString(globalObject, 10);
RETURN_IF_EXCEPTION(scope, emptyString);
JSString* returnString = JSString::create(vm, string.releaseImpl().releaseNonNull());
RETURN_IF_EXCEPTION(scope, emptyString);
return returnString;
}
ASSERT(isSymbol());
throwTypeError(globalObject, scope, SymbolCoercionError);
return emptyString;
}
void JSCellLock::lockSlow()
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
IndexingTypeLockAlgorithm::lockSlow(*lock);
}
void JSCellLock::unlockSlow()
{
Atomic<IndexingType>* lock = std::bit_cast<Atomic<IndexingType>*>(&m_indexingTypeAndMisc);
IndexingTypeLockAlgorithm::unlockSlow(*lock);
}
#if CPU(X86_64)
NEVER_INLINE NO_RETURN_DUE_TO_CRASH NOT_TAIL_CALLED void reportZappedCellAndCrash(Heap& heap, const JSCell* cell)
{
MarkedBlock::Handle* foundBlockHandle = nullptr;
uint64_t* cellWords = std::bit_cast<uint64_t*>(cell);
uintptr_t cellAddress = std::bit_cast<uintptr_t>(cell);
uint64_t headerWord = cellWords[0];
uint64_t zapReasonAndMore = cellWords[1];
unsigned subspaceHash = 0;
size_t cellSize = 0;
heap.objectSpace().forEachBlock([&](MarkedBlock::Handle* blockHandle) {
if (blockHandle->contains(std::bit_cast<JSCell*>(cell))) {
foundBlockHandle = blockHandle;
return IterationStatus::Done;
}
return IterationStatus::Continue;
});
uint64_t variousState = 0;
MarkedBlock* foundBlock = nullptr;
if (foundBlockHandle) {
foundBlock = &foundBlockHandle->block();
subspaceHash = SuperFastHash::computeHash(foundBlockHandle->subspace()->name());
cellSize = foundBlockHandle->cellSize();
variousState |= static_cast<uint64_t>(foundBlockHandle->isFreeListed()) << 0;
variousState |= static_cast<uint64_t>(foundBlockHandle->isAllocated()) << 1;
variousState |= static_cast<uint64_t>(foundBlockHandle->isEmpty()) << 2;
variousState |= static_cast<uint64_t>(foundBlockHandle->needsDestruction()) << 3;
variousState |= static_cast<uint64_t>(foundBlock->isNewlyAllocated(cell)) << 4;
ptrdiff_t cellOffset = cellAddress - std::bit_cast<uintptr_t>(foundBlockHandle->start());
bool cellIsProperlyAligned = !(cellOffset % cellSize);
variousState |= static_cast<uint64_t>(cellIsProperlyAligned) << 5;
} else {
bool isFreeListed = false;
PreciseAllocation* foundPreciseAllocation = nullptr;
heap.objectSpace().forEachSubspace([&](Subspace& subspace) {
subspace.forEachPreciseAllocation([&](PreciseAllocation* allocation) {
if (allocation->contains(cell))
foundPreciseAllocation = allocation;
});
if (foundPreciseAllocation)
return IterationStatus::Done;
if (subspace.isIsoSubspace()) {
static_cast<IsoSubspace&>(subspace).forEachLowerTierPreciseFreeListedPreciseAllocation([&](PreciseAllocation* allocation) {
if (allocation->contains(cell)) {
foundPreciseAllocation = allocation;
isFreeListed = true;
}
});
}
if (foundPreciseAllocation)
return IterationStatus::Done;
return IterationStatus::Continue;
});
if (foundPreciseAllocation) {
subspaceHash = SuperFastHash::computeHash(foundPreciseAllocation->subspace()->name());
cellSize = foundPreciseAllocation->cellSize();
variousState |= static_cast<uint64_t>(isFreeListed) << 0;
variousState |= static_cast<uint64_t>(!isFreeListed) << 1;
variousState |= static_cast<uint64_t>(foundPreciseAllocation->subspace()->attributes().destruction == NeedsDestruction) << 3;
if (!isFreeListed) {
variousState |= static_cast<uint64_t>(foundPreciseAllocation->isEmpty()) << 2;
variousState |= static_cast<uint64_t>(foundPreciseAllocation->isNewlyAllocated()) << 4;
}
bool cellIsProperlyAligned = foundPreciseAllocation->cell() == cell;
variousState |= static_cast<uint64_t>(cellIsProperlyAligned) << 5;
}
}
CRASH_WITH_INFO(cellAddress, headerWord, zapReasonAndMore, subspaceHash, cellSize, foundBlock, variousState);
}
#endif // CPU(X86_64)
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|