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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/*
* Copyright (C) 2014-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "HeapVerifier.h"
#include "ButterflyInlines.h"
#include "CodeBlockInlines.h"
#include "JSObject.h"
#include "MarkedSpaceInlines.h"
#include "VMInspector.h"
#include "ValueProfile.h"
#include <wtf/ProcessID.h>
#include <wtf/TZoneMallocInlines.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
WTF_MAKE_TZONE_ALLOCATED_IMPL(HeapVerifier);
HeapVerifier::HeapVerifier(Heap* heap, unsigned numberOfGCCyclesToRecord)
: m_heap(heap)
, m_currentCycle(0)
, m_numberOfCycles(numberOfGCCyclesToRecord)
{
RELEASE_ASSERT(m_numberOfCycles > 0);
m_cycles = makeUniqueArray<GCCycle>(m_numberOfCycles);
}
const char* HeapVerifier::phaseName(HeapVerifier::Phase phase)
{
switch (phase) {
case Phase::BeforeGC:
return "BeforeGC";
case Phase::BeforeMarking:
return "BeforeMarking";
case Phase::AfterMarking:
return "AfterMarking";
case Phase::AfterGC:
return "AfterGC";
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr; // Silencing a compiler warning.
}
void HeapVerifier::startGC()
{
Heap* heap = m_heap;
incrementCycle();
currentCycle().reset();
currentCycle().scope = *heap->collectionScope();
currentCycle().timestamp = MonotonicTime::now();
ASSERT(!m_didPrintLogs);
}
void HeapVerifier::endGC()
{
if (m_didPrintLogs) {
dataLog("END ");
printVerificationHeader();
dataLog("\n\n");
m_didPrintLogs = false;
}
}
void HeapVerifier::gatherLiveCells(HeapVerifier::Phase phase)
{
Heap* heap = m_heap;
CellList& list = *cellListForGathering(phase);
list.reset();
heap->m_objectSpace.forEachLiveCell([&list] (HeapCell* cell, HeapCell::Kind kind) {
list.add({ cell, kind, CellProfile::Live });
return IterationStatus::Continue;
});
}
CellList* HeapVerifier::cellListForGathering(HeapVerifier::Phase phase)
{
switch (phase) {
case Phase::BeforeMarking:
return ¤tCycle().before;
case Phase::AfterMarking:
return ¤tCycle().after;
case Phase::BeforeGC:
case Phase::AfterGC:
// We should not be gathering live cells during these phases.
break;
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr; // Silencing a compiler warning.
}
static void trimDeadCellsFromList(CellList& knownLiveSet, CellList& list)
{
if (!list.size())
return;
for (auto& cellProfile : list.cells()) {
if (cellProfile.isDead())
continue; // Don't "resurrect" known dead cells.
if (!knownLiveSet.find(cellProfile.cell())) {
cellProfile.setIsDead();
continue;
}
cellProfile.setIsLive();
}
}
void HeapVerifier::trimDeadCells()
{
CellList& knownLiveSet = currentCycle().after;
trimDeadCellsFromList(knownLiveSet, currentCycle().before);
for (int i = -1; i > -m_numberOfCycles; i--) {
trimDeadCellsFromList(knownLiveSet, cycleForIndex(i).before);
trimDeadCellsFromList(knownLiveSet, cycleForIndex(i).after);
}
}
void HeapVerifier::printVerificationHeader()
{
RELEASE_ASSERT(m_heap->collectionScope());
CollectionScope scope = currentCycle().scope;
MonotonicTime gcCycleTimestamp = currentCycle().timestamp;
dataLog("Verifying heap in [p", getCurrentProcessID(), ", ", Thread::current(), "] vm ",
RawPointer(&m_heap->vm()), " on ", scope, " GC @ ", gcCycleTimestamp, "\n");
}
bool HeapVerifier::verifyCellList(Phase phase, CellList& list)
{
VM& vm = m_heap->vm();
auto& liveCells = list.cells();
bool listNamePrinted = false;
auto printHeaderIfNeeded = scopedLambda<void()>([&] () {
if (listNamePrinted)
return;
printVerificationHeader();
dataLog(" @ phase ", phaseName(phase), ": FAILED in cell list '", list.name(), "' (size ", liveCells.size(), ")\n");
listNamePrinted = true;
m_didPrintLogs = true;
});
bool success = true;
for (size_t i = 0; i < liveCells.size(); i++) {
CellProfile& profile = liveCells[i];
if (!profile.isLive())
continue;
if (!profile.isJSCell())
continue;
JSCell* cell = profile.jsCell();
if (!validateJSCell(&vm, cell, &profile, &list, printHeaderIfNeeded, " "))
success = false;
}
return success;
}
bool HeapVerifier::validateCell(HeapCell* cell, VM* expectedVM)
{
auto printNothing = scopedLambda<void()>([] () { });
if (cell->isZapped()) {
dataLog(" cell ", RawPointer(cell), " is ZAPPED\n");
return false;
}
if (!isJSCellKind(cell->cellKind()))
return true; // Nothing more to validate.
JSCell* jsCell = static_cast<JSCell*>(cell);
return validateJSCell(expectedVM, jsCell, nullptr, nullptr, printNothing);
}
bool HeapVerifier::validateJSCell(VM* expectedVM, JSCell* cell, CellProfile* profile, CellList* list, const ScopedLambda<void()>& printHeaderIfNeeded, const char* prefix)
{
auto printHeaderAndCell = [cell, profile, &printHeaderIfNeeded, prefix] () {
printHeaderIfNeeded();
dataLog(prefix, "cell ", RawPointer(cell));
if (profile)
dataLog(" [", profile->className(), "]");
};
// 1. Validate the cell.
if (cell->isZapped()) {
printHeaderAndCell();
dataLog(" is zapped\n");
return false;
}
StructureID structureID = cell->structureID();
if (!structureID) {
printHeaderAndCell();
dataLog(" has NULL structureID\n");
return false;
}
if (expectedVM) {
VM* cellVM = &cell->vm();
if (cellVM != expectedVM) {
printHeaderAndCell();
dataLog(" is from a different VM: expected:", RawPointer(expectedVM), " actual:", RawPointer(cellVM), "\n");
return false;
}
// 2. Validate the cell's structure
Structure* structure = structureID.decode();
if (!structure) {
printHeaderAndCell();
uint32_t structureIDAsUint32 = structureID.bits();
dataLog(" with structureID ", structureIDAsUint32, " maps to a NULL Structure pointer\n");
return false;
}
if (structure->isZapped()) {
printHeaderAndCell();
dataLog(" has ZAPPED structure ", RawPointer(structure), "\n");
return false;
}
if (!structure->structureID()) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structureID is NULL\n");
return false;
}
VM* structureVM = &structure->vm();
if (structureVM != expectedVM) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " from a different VM: expected:", RawPointer(expectedVM), " actual:", RawPointer(structureVM), "\n");
return false;
}
if (list) {
auto* structureProfile = list->find(structure);
if (!structureProfile) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " NOT found in the live cell list\n");
return false;
}
if (!structureProfile->isLive()) {
printHeaderAndCell();
dataLog(" has DEAD structure ", RawPointer(structure), "\n");
return false;
}
}
StructureID structureStructureID = structure->structureID();
if (!structureStructureID) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " with a NULL structureID\n");
return false;
}
// 3. Validate the cell's structure's structure.
Structure* structureStructure = structureStructureID.decode();
if (!structureStructure) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure is NULL\n");
return false;
}
if (structureStructure->isZapped()) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure ", RawPointer(structureStructure), " is ZAPPED\n");
return false;
}
if (!structureStructure->structureID()) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure ", RawPointer(structureStructure), " has a NULL structureID\n");
return false;
}
VM* structureStructureVM = &structureStructure->vm();
if (structureStructureVM != expectedVM) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure ", RawPointer(structureStructure), " is from a different VM: expected:", RawPointer(expectedVM), " actual:", RawPointer(structureStructureVM), "\n");
return false;
}
if (list) {
auto* structureStructureProfile = list->find(structureStructure);
if (!structureStructureProfile) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure ", RawPointer(structureStructure), " is NOT found in the live cell list\n");
return false;
}
if (!structureStructureProfile->isLive()) {
printHeaderAndCell();
dataLog(" has structure ", RawPointer(structure), " whose structure ", RawPointer(structureStructure), " is DEAD\n");
return false;
}
}
CodeBlock* codeBlock = jsDynamicCast<CodeBlock*>(cell);
if (UNLIKELY(codeBlock)) {
bool success = true;
codeBlock->forEachValueProfile([&](auto& valueProfile, bool) {
for (unsigned i = 0; i < std::remove_reference_t<decltype(valueProfile)>::totalNumberOfBuckets; ++i) {
JSValue value = JSValue::decode(valueProfile.m_buckets[i]);
if (!value)
continue;
if (!value.isCell())
continue;
JSCell* valueCell = value.asCell();
if (valueCell->isZapped()) {
printHeaderIfNeeded();
dataLog(prefix, "CodeBlock ", RawPointer(codeBlock), " has ZAPPED ValueProfile cell ", RawPointer(valueCell), "\n");
success = false;
continue;
}
}
});
if (!success)
return false;
}
}
return true;
}
void HeapVerifier::verify(HeapVerifier::Phase phase)
{
if (phase == Phase::AfterGC) {
bool verified = verifyCellList(phase, currentCycle().after);
RELEASE_ASSERT(verified);
}
}
void HeapVerifier::reportCell(CellProfile& profile, int cycleIndex, HeapVerifier::GCCycle& cycle, CellList& list, const char* prefix)
{
HeapCell* cell = profile.cell();
VM& vm = m_heap->vm();
if (prefix)
dataLog(prefix);
dataLog("FOUND");
if (profile.isLive())
dataLog(" LIVE");
else if (profile.isDead())
dataLog(" DEAD");
if (!profile.isJSCell())
dataLog(" HeapCell ");
else
dataLog(" JSCell ");
dataLog(RawPointer(cell));
if (profile.className())
dataLog(" [", profile.className(), "]");
if (profile.isLive() && profile.isJSCell()) {
JSCell* jsCell = profile.jsCell();
Structure* structure = jsCell->structure();
dataLog(" structure:", RawPointer(structure));
if (jsCell->isObject()) {
JSObject* obj = static_cast<JSObject*>(cell);
Butterfly* butterfly = obj->butterfly();
void* butterflyBase = butterfly->base(structure);
dataLog(" butterfly:", RawPointer(butterfly), " (base:", RawPointer(butterflyBase), ")");
}
}
dataLog(" in ", cycle.scope, " GC[", cycleIndex, "] in '", list.name(), "' list in VM ",
RawPointer(&vm), " recorded at time ", profile.timestamp(), "\n");
if (profile.stackTrace())
dataLog(*profile.stackTrace());
}
void HeapVerifier::checkIfRecorded(HeapCell* cell)
{
bool found = false;
const char* const prefix = " ";
static constexpr bool verbose = true;
for (int cycleIndex = 0; cycleIndex > -m_numberOfCycles; cycleIndex--) {
GCCycle& cycle = cycleForIndex(cycleIndex);
CellList* lists[] = { &cycle.before, &cycle.after };
if (verbose)
dataLog("Checking ", cycle.scope, " GC<", cycle.timestamp, ">, cycle [", cycleIndex, "]:\n");
const char* resultPrefix = " ";
for (auto* list : lists) {
if (verbose)
dataLog(prefix, "Cycle [", cycleIndex, "] '", list->name(), "' list: ");
CellProfile* profile = list->find(cell);
if (profile) {
reportCell(*profile, cycleIndex, cycle, *list, resultPrefix);
found = true;
} else if (verbose)
dataLog(resultPrefix, "cell NOT found\n");
}
}
if (!found)
dataLog(prefix, "cell ", RawPointer(cell), " NOT FOUND\n");
}
// The following are slower but more robust versions of the corresponding functions of the same name.
// These robust versions are designed so that we can call them interactively from a C++ debugger
// to query if a candidate is recorded cell.
void HeapVerifier::checkIfRecorded(uintptr_t candidateCell)
{
HeapCell* candidateHeapCell = reinterpret_cast<HeapCell*>(candidateCell);
auto& inspector = VMInspector::singleton();
if (!inspector.getLock().tryLockWithTimeout(2_s)) {
dataLog("ERROR: Timed out while waiting to iterate VMs.");
return;
}
Locker locker { AdoptLock, inspector.getLock() };
inspector.iterate([&] (VM& vm) {
if (!vm.isInService())
return IterationStatus::Continue;
if (!vm.heap.m_verifier)
return IterationStatus::Continue;
auto* verifier = vm.heap.m_verifier.get();
dataLog("Search for cell ", RawPointer(candidateHeapCell), " in VM ", RawPointer(&vm), ":\n");
verifier->checkIfRecorded(candidateHeapCell);
return IterationStatus::Continue;
});
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|