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
|
/*
* Copyright (C) 2014 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 "CopiedSpaceInlines.h"
#include "HeapIterationScope.h"
#include "JSCInlines.h"
#include "JSObject.h"
namespace JSC {
HeapVerifier::HeapVerifier(Heap* heap, unsigned numberOfGCCyclesToRecord)
: m_heap(heap)
, m_currentCycle(0)
, m_numberOfCycles(numberOfGCCyclesToRecord)
{
RELEASE_ASSERT(m_numberOfCycles > 0);
m_cycles = std::make_unique<GCCycle[]>(m_numberOfCycles);
}
const char* HeapVerifier::collectionTypeName(HeapOperation type)
{
switch (type) {
case NoOperation:
return "NoOperation";
case AnyCollection:
return "AnyCollection";
case Allocation:
return "Allocation";
case EdenCollection:
return "EdenCollection";
case FullCollection:
return "FullCollection";
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr; // Silencing a compiler warning.
}
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.
}
static void getButterflyDetails(JSObject* obj, void*& butterflyBase, size_t& butterflyCapacityInBytes, CopiedBlock*& butterflyBlock)
{
Structure* structure = obj->structure();
Butterfly* butterfly = obj->butterfly();
butterflyBase = butterfly->base(structure);
butterflyBlock = CopiedSpace::blockFor(butterflyBase);
size_t propertyCapacity = structure->outOfLineCapacity();
size_t preCapacity;
size_t indexingPayloadSizeInBytes;
bool hasIndexingHeader = obj->hasIndexingHeader();
if (UNLIKELY(hasIndexingHeader)) {
preCapacity = butterfly->indexingHeader()->preCapacity(structure);
indexingPayloadSizeInBytes = butterfly->indexingHeader()->indexingPayloadSizeInBytes(structure);
} else {
preCapacity = 0;
indexingPayloadSizeInBytes = 0;
}
butterflyCapacityInBytes = Butterfly::totalSize(preCapacity, propertyCapacity, hasIndexingHeader, indexingPayloadSizeInBytes);
}
void HeapVerifier::initializeGCCycle()
{
Heap* heap = m_heap;
incrementCycle();
currentCycle().collectionType = heap->operationInProgress();
}
struct GatherLiveObjFunctor : MarkedBlock::CountFunctor {
GatherLiveObjFunctor(LiveObjectList& list)
: m_list(list)
{
ASSERT(!list.liveObjects.size());
}
inline void visit(JSCell* cell)
{
if (!cell->isObject())
return;
LiveObjectData data(asObject(cell));
m_list.liveObjects.append(data);
}
IterationStatus operator()(JSCell* cell)
{
visit(cell);
return IterationStatus::Continue;
}
LiveObjectList& m_list;
};
void HeapVerifier::gatherLiveObjects(HeapVerifier::Phase phase)
{
Heap* heap = m_heap;
LiveObjectList& list = *liveObjectListForGathering(phase);
HeapIterationScope iterationScope(*heap);
list.reset();
GatherLiveObjFunctor functor(list);
heap->m_objectSpace.forEachLiveCell(iterationScope, functor);
}
LiveObjectList* HeapVerifier::liveObjectListForGathering(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 objects during these phases.
break;
}
RELEASE_ASSERT_NOT_REACHED();
return nullptr; // Silencing a compiler warning.
}
static void trimDeadObjectsFromList(HashSet<JSObject*>& knownLiveSet, LiveObjectList& list)
{
if (!list.hasLiveObjects)
return;
size_t liveObjectsFound = 0;
for (size_t i = 0; i < list.liveObjects.size(); i++) {
LiveObjectData& objData = list.liveObjects[i];
if (objData.isConfirmedDead)
continue; // Don't "resurrect" known dead objects.
if (!knownLiveSet.contains(objData.obj)) {
objData.isConfirmedDead = true;
continue;
}
liveObjectsFound++;
}
list.hasLiveObjects = !!liveObjectsFound;
}
void HeapVerifier::trimDeadObjects()
{
HashSet<JSObject*> knownLiveSet;
LiveObjectList& after = currentCycle().after;
for (size_t i = 0; i < after.liveObjects.size(); i++) {
LiveObjectData& objData = after.liveObjects[i];
knownLiveSet.add(objData.obj);
}
trimDeadObjectsFromList(knownLiveSet, currentCycle().before);
for (int i = -1; i > -m_numberOfCycles; i--) {
trimDeadObjectsFromList(knownLiveSet, cycleForIndex(i).before);
trimDeadObjectsFromList(knownLiveSet, cycleForIndex(i).after);
}
}
bool HeapVerifier::verifyButterflyIsInStorageSpace(Phase phase, LiveObjectList& list)
{
auto& liveObjects = list.liveObjects;
CopiedSpace& storageSpace = m_heap->m_storageSpace;
bool listNamePrinted = false;
bool success = true;
for (size_t i = 0; i < liveObjects.size(); i++) {
LiveObjectData& objectData = liveObjects[i];
if (objectData.isConfirmedDead)
continue;
JSObject* obj = objectData.obj;
Butterfly* butterfly = obj->butterfly();
if (butterfly) {
void* butterflyBase;
size_t butterflyCapacityInBytes;
CopiedBlock* butterflyBlock;
getButterflyDetails(obj, butterflyBase, butterflyCapacityInBytes, butterflyBlock);
if (!storageSpace.contains(butterflyBlock)) {
if (!listNamePrinted) {
dataLogF("Verification @ phase %s FAILED in object list '%s' (size %zu)\n",
phaseName(phase), list.name, liveObjects.size());
listNamePrinted = true;
}
Structure* structure = obj->structure();
const char* structureClassName = structure->classInfo()->className;
dataLogF(" butterfly %p (base %p size %zu block %p) NOT in StorageSpace | obj %p type '%s'\n",
butterfly, butterflyBase, butterflyCapacityInBytes, butterflyBlock, obj, structureClassName);
success = false;
}
}
}
return success;
}
void HeapVerifier::verify(HeapVerifier::Phase phase)
{
bool beforeVerified = verifyButterflyIsInStorageSpace(phase, currentCycle().before);
bool afterVerified = verifyButterflyIsInStorageSpace(phase, currentCycle().after);
RELEASE_ASSERT(beforeVerified && afterVerified);
}
void HeapVerifier::reportObject(LiveObjectData& objData, int cycleIndex, HeapVerifier::GCCycle& cycle, LiveObjectList& list)
{
JSObject* obj = objData.obj;
if (objData.isConfirmedDead) {
dataLogF("FOUND dead obj %p in GC[%d] %s list '%s'\n",
obj, cycleIndex, cycle.collectionTypeName(), list.name);
return;
}
Structure* structure = obj->structure();
Butterfly* butterfly = obj->butterfly();
void* butterflyBase;
size_t butterflyCapacityInBytes;
CopiedBlock* butterflyBlock;
getButterflyDetails(obj, butterflyBase, butterflyCapacityInBytes, butterflyBlock);
dataLogF("FOUND obj %p type '%s' butterfly %p (base %p size %zu block %p) in GC[%d] %s list '%s'\n",
obj, structure->classInfo()->className,
butterfly, butterflyBase, butterflyCapacityInBytes, butterflyBlock,
cycleIndex, cycle.collectionTypeName(), list.name);
}
void HeapVerifier::checkIfRecorded(JSObject* obj)
{
bool found = false;
for (int cycleIndex = 0; cycleIndex > -m_numberOfCycles; cycleIndex--) {
GCCycle& cycle = cycleForIndex(cycleIndex);
LiveObjectList& beforeList = cycle.before;
LiveObjectList& afterList = cycle.after;
LiveObjectData* objData;
objData = beforeList.findObject(obj);
if (objData) {
reportObject(*objData, cycleIndex, cycle, beforeList);
found = true;
}
objData = afterList.findObject(obj);
if (objData) {
reportObject(*objData, cycleIndex, cycle, afterList);
found = true;
}
}
if (!found)
dataLogF("obj %p NOT FOUND\n", obj);
}
} // namespace JSC
|