| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 17 | #include <sys/mman.h> /* for PROT_* */ |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 18 | |
| 19 | #include "Dalvik.h" |
| Carl Shapiro | c38b7ed | 2011-02-08 17:43:43 -0800 | [diff] [blame] | 20 | #include "alloc/HeapBitmap.h" |
| 21 | #include "alloc/HeapBitmapInlines.h" |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 22 | #include "alloc/HeapSource.h" |
| 23 | #include "alloc/Visit.h" |
| 24 | |
| 25 | /* |
| 26 | * Maintain a card table from the the write barrier. All writes of |
| 27 | * non-NULL values to heap addresses should go through an entry in |
| 28 | * WriteBarrier, and from there to here. |
| 29 | * |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 30 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 31 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 32 | * data per card, to be used by the GC. The value of the byte will be |
| 33 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 34 | * |
| 35 | * After any store of a non-NULL object pointer into a heap object, |
| 36 | * code is obliged to mark the card dirty. The setters in |
| 37 | * ObjectInlines.h [such as dvmSetFieldObject] do this for you. The |
| 38 | * JIT and fast interpreters also contain code to mark cards as dirty. |
| 39 | * |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 40 | * The card table's base [the "biased card table"] gets set to a |
| 41 | * rather strange value. In order to keep the JIT from having to |
| 42 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 43 | * biased base is within the mmap allocation at a point where it's low |
| 44 | * byte is equal to GC_DIRTY_CARD. See dvmCardTableStartup for details. |
| 45 | */ |
| 46 | |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 47 | static bool allocCardTable(size_t heapMaximumSize) |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 48 | { |
| 49 | size_t length; |
| 50 | void *allocBase; |
| 51 | u1 *biasedBase; |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 52 | GcHeap *gcHeap = gDvm.gcHeap; |
| 53 | void *heapBase = dvmHeapSourceGetBase(); |
| 54 | assert(gcHeap != NULL); |
| 55 | assert(heapBase != NULL); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 56 | |
| 57 | /* Set up the card table */ |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame] | 58 | length = heapMaximumSize / GC_CARD_SIZE; |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 59 | assert(length * GC_CARD_SIZE == heapMaximumSize); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 60 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
| 61 | allocBase = dvmAllocRegion(length + 0x100, PROT_READ | PROT_WRITE, |
| 62 | "dalvik-card-table"); |
| 63 | if (allocBase == NULL) { |
| 64 | return false; |
| 65 | } |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 66 | gcHeap->cardTableBase = (u1*)allocBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 67 | gcHeap->cardTableLength = length; |
| 68 | /* All zeros is the correct initial value; all clean. */ |
| 69 | assert(GC_CARD_CLEAN == 0); |
| 70 | |
| 71 | biasedBase = (u1 *)((uintptr_t)allocBase - |
| 72 | ((uintptr_t)heapBase >> GC_CARD_SHIFT)); |
| 73 | if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) { |
| Carl Shapiro | 5d81aa3 | 2010-08-20 11:04:25 -0700 | [diff] [blame] | 74 | int offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 75 | biasedBase += offset + (offset < 0 ? 0x100 : 0); |
| 76 | } |
| 77 | assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY); |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 78 | gDvm.biasedCardTableBase = biasedBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| Brian Carlstrom | 4a7db9c | 2011-04-04 12:06:15 -0700 | [diff] [blame] | 83 | static bool allocModUnionTable(size_t heapSize) |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 84 | { |
| Brian Carlstrom | 4a7db9c | 2011-04-04 12:06:15 -0700 | [diff] [blame] | 85 | size_t cardsPerHeap = heapSize / GC_CARD_SIZE; |
| 86 | size_t byteLength = cardsPerHeap / CHAR_BIT; |
| 87 | assert(byteLength * GC_CARD_SIZE * CHAR_BIT == heapSize); |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 88 | int prot = PROT_READ | PROT_WRITE; |
| Brian Carlstrom | 4a7db9c | 2011-04-04 12:06:15 -0700 | [diff] [blame] | 89 | void *allocBase = dvmAllocRegion(byteLength, prot, "dalvik-modunion-table"); |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 90 | if (allocBase == NULL) { |
| 91 | return false; |
| 92 | } |
| 93 | GcHeap *gcHeap = gDvm.gcHeap; |
| 94 | gcHeap->modUnionTableBase = (u1*)allocBase; |
| Brian Carlstrom | 4a7db9c | 2011-04-04 12:06:15 -0700 | [diff] [blame] | 95 | gcHeap->modUnionTableLength = byteLength; |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Initializes the card table; must be called before any other |
| 101 | * dvmCardTable*() functions. |
| 102 | */ |
| 103 | bool dvmCardTableStartup(size_t heapMaximumSize) |
| 104 | { |
| 105 | return allocCardTable(heapMaximumSize) && allocModUnionTable(heapMaximumSize); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Releases storage for the card table and clears its globals. |
| 110 | */ |
| 111 | static void freeCardTable() |
| 112 | { |
| 113 | if (gDvm.biasedCardTableBase == NULL) { |
| 114 | return; |
| 115 | } |
| 116 | gDvm.biasedCardTableBase = NULL; |
| 117 | munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength); |
| 118 | gDvm.gcHeap->cardTableBase = NULL; |
| 119 | gDvm.gcHeap->cardTableLength = 0; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Releases storage for the mod union table and clears its globals. |
| 124 | */ |
| 125 | static void freeModUnionTable() |
| 126 | { |
| 127 | if (gDvm.gcHeap->modUnionTableBase == NULL) { |
| 128 | return; |
| 129 | } |
| 130 | munmap(gDvm.gcHeap->modUnionTableBase, gDvm.gcHeap->modUnionTableLength); |
| 131 | gDvm.gcHeap->modUnionTableBase = NULL; |
| 132 | gDvm.gcHeap->modUnionTableLength = 0; |
| 133 | } |
| 134 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 135 | /* |
| 136 | * Tears down the entire CardTable. |
| 137 | */ |
| 138 | void dvmCardTableShutdown() |
| 139 | { |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 140 | freeCardTable(); |
| 141 | freeModUnionTable(); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Set a bit in the mod union table for each dirty byte in the card |
| 146 | * table. Clears the corresponding byte in the card table. |
| 147 | */ |
| 148 | static void moveCardsToModUnion(u1 *base, u1 *limit) |
| 149 | { |
| 150 | GcHeap *h = gDvm.gcHeap; |
| 151 | u1 *baseCard = dvmCardFromAddr(base); |
| 152 | u1 *limitCard = dvmCardFromAddr(limit); |
| 153 | u4 *bits = (u4*)h->modUnionTableBase; |
| 154 | u1 *heapBase = (u1*)dvmHeapSourceGetBase(); |
| 155 | u1 *card; |
| 156 | for (card = baseCard; card < limitCard; ++card) { |
| 157 | if (*card == GC_CARD_CLEAN) { |
| 158 | continue; |
| 159 | } |
| 160 | u1 *addr = (u1*)dvmAddrFromCard(card); |
| 161 | u1 *biased = (u1*)((uintptr_t)addr - (uintptr_t)heapBase); |
| 162 | size_t offset = (uintptr_t)biased / GC_CARD_SIZE / HB_BITS_PER_WORD; |
| 163 | u4 bit = 1 << (((uintptr_t)biased / GC_CARD_SIZE) % HB_BITS_PER_WORD); |
| 164 | assert((u1*)&bits[offset] >= h->modUnionTableBase); |
| 165 | assert((u1*)&bits[offset] < h->modUnionTableBase+h->modUnionTableLength); |
| 166 | bits[offset] |= bit; |
| 167 | *card = GC_CARD_CLEAN; |
| 168 | } |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 171 | void dvmClearCardTable(void) |
| 172 | { |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 173 | uintptr_t base[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| 174 | uintptr_t limit[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| 175 | size_t numHeaps = dvmHeapSourceGetNumHeaps(); |
| 176 | dvmHeapSourceGetRegions(base, NULL, limit, numHeaps); |
| 177 | size_t i; |
| 178 | for (i = 0; i < numHeaps; ++i) { |
| 179 | if (i != 0) { |
| 180 | moveCardsToModUnion((u1*)base[i], (u1*)limit[i]); |
| 181 | } else { |
| 182 | u1 *baseCard = dvmCardFromAddr((u1*)base[i]); |
| Carl Shapiro | 20d4d04 | 2011-03-08 13:46:10 -0800 | [diff] [blame] | 183 | size_t length = (limit[i] - base[i]) >> GC_CARD_SHIFT; |
| 184 | memset(baseCard, GC_CARD_CLEAN, length); |
| Carl Shapiro | 6c355e5 | 2011-03-02 15:43:39 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 189 | /* |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 190 | * Returns true iff the address is within the bounds of the card table. |
| 191 | */ |
| 192 | bool dvmIsValidCard(const u1 *cardAddr) |
| 193 | { |
| 194 | GcHeap *h = gDvm.gcHeap; |
| 195 | return cardAddr >= h->cardTableBase && |
| 196 | cardAddr < &h->cardTableBase[h->cardTableLength]; |
| 197 | } |
| 198 | |
| 199 | /* |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 200 | * Returns the address of the relevent byte in the card table, given |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 201 | * an address on the heap. |
| 202 | */ |
| 203 | u1 *dvmCardFromAddr(const void *addr) |
| 204 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 205 | u1 *biasedBase = gDvm.biasedCardTableBase; |
| 206 | u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 207 | assert(dvmIsValidCard(cardAddr)); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 208 | return cardAddr; |
| 209 | } |
| 210 | |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 211 | /* |
| 212 | * Returns the first address in the heap which maps to this card. |
| 213 | */ |
| 214 | void *dvmAddrFromCard(const u1 *cardAddr) |
| 215 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 216 | assert(dvmIsValidCard(cardAddr)); |
| 217 | uintptr_t offset = cardAddr - gDvm.biasedCardTableBase; |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 218 | return (void *)(offset << GC_CARD_SHIFT); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Dirties the card for the given address. |
| 223 | */ |
| 224 | void dvmMarkCard(const void *addr) |
| 225 | { |
| 226 | u1 *cardAddr = dvmCardFromAddr(addr); |
| 227 | *cardAddr = GC_CARD_DIRTY; |
| 228 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 229 | |
| 230 | /* |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 231 | * Returns true if the object is on a dirty card. |
| 232 | */ |
| 233 | static bool isObjectDirty(const Object *obj) |
| 234 | { |
| 235 | assert(obj != NULL); |
| 236 | assert(dvmIsValidObject(obj)); |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 237 | u1 *card = dvmCardFromAddr(obj); |
| 238 | return *card == GC_CARD_DIRTY; |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Context structure for verifying the card table. |
| 243 | */ |
| 244 | typedef struct { |
| 245 | HeapBitmap *markBits; |
| 246 | size_t whiteRefs; |
| 247 | } WhiteReferenceCounter; |
| 248 | |
| 249 | /* |
| 250 | * Visitor that counts white referents. |
| 251 | */ |
| 252 | static void countWhiteReferenceVisitor(void *addr, void *arg) |
| 253 | { |
| 254 | WhiteReferenceCounter *ctx; |
| 255 | Object *obj; |
| 256 | |
| 257 | assert(addr != NULL); |
| 258 | assert(arg != NULL); |
| 259 | obj = *(Object **)addr; |
| Carl Shapiro | 8e14bc7 | 2010-10-18 16:27:29 -0700 | [diff] [blame] | 260 | if (obj == NULL) { |
| 261 | return; |
| 262 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 263 | assert(dvmIsValidObject(obj)); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 264 | ctx = (WhiteReferenceCounter *)arg; |
| Carl Shapiro | 8e14bc7 | 2010-10-18 16:27:29 -0700 | [diff] [blame] | 265 | if (dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | ctx->whiteRefs += 1; |
| 269 | } |
| 270 | |
| 271 | /* |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 272 | * Visitor that logs white references. |
| 273 | */ |
| 274 | static void dumpWhiteReferenceVisitor(void *addr, void *arg) |
| 275 | { |
| 276 | WhiteReferenceCounter *ctx; |
| 277 | Object *obj; |
| 278 | |
| 279 | assert(addr != NULL); |
| 280 | assert(arg != NULL); |
| 281 | obj = *(Object **)addr; |
| 282 | if (obj == NULL) { |
| 283 | return; |
| 284 | } |
| 285 | assert(dvmIsValidObject(obj)); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 286 | ctx = (WhiteReferenceCounter*)arg; |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 287 | if (dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| 288 | return; |
| 289 | } |
| 290 | LOGE("object %p is white", obj); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * Visitor that signals the caller when a matching reference is found. |
| 295 | */ |
| 296 | static void dumpReferencesVisitor(void *pObj, void *arg) |
| 297 | { |
| 298 | Object *obj = *(Object **)pObj; |
| 299 | Object *lookingFor = *(Object **)arg; |
| 300 | if (lookingFor != NULL && lookingFor == obj) { |
| 301 | *(Object **)arg = NULL; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void dumpReferencesCallback(void *ptr, void *arg) |
| 306 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 307 | Object *obj = (Object *)arg; |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 308 | if (ptr == obj) { |
| 309 | return; |
| 310 | } |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 311 | dvmVisitObject(dumpReferencesVisitor, (Object *)ptr, &obj); |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 312 | if (obj == NULL) { |
| 313 | LOGD("Found %p in the heap @ %p", arg, ptr); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 314 | dvmDumpObject((Object *)ptr); |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Root visitor that looks for matching references. |
| 320 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 321 | static void dumpReferencesRootVisitor(void *ptr, u4 threadId, |
| 322 | RootType type, void *arg) |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 323 | { |
| 324 | Object *obj = *(Object **)ptr; |
| 325 | Object *lookingFor = *(Object **)arg; |
| 326 | if (obj == lookingFor) { |
| 327 | LOGD("Found %p in a root @ %p", arg, ptr); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Invokes visitors to search for references to an object. |
| 333 | */ |
| 334 | static void dumpReferences(const Object *obj) |
| 335 | { |
| 336 | HeapBitmap *bitmap = dvmHeapSourceGetLiveBits(); |
| 337 | void *arg = (void *)obj; |
| 338 | dvmVisitRoots(dumpReferencesRootVisitor, arg); |
| 339 | dvmHeapBitmapWalk(bitmap, dumpReferencesCallback, arg); |
| 340 | } |
| 341 | |
| 342 | /* |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 343 | * Returns true if the given object is a reference object and the |
| 344 | * just the referent is unmarked. |
| 345 | */ |
| 346 | static bool isReferentUnmarked(const Object *obj, |
| 347 | const WhiteReferenceCounter* ctx) |
| 348 | { |
| 349 | assert(obj != NULL); |
| 350 | assert(obj->clazz != NULL); |
| 351 | assert(ctx != NULL); |
| 352 | if (ctx->whiteRefs != 1) { |
| 353 | return false; |
| 354 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) { |
| 355 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 356 | const Object *referent = dvmGetFieldObject(obj, offset); |
| 357 | return !dvmHeapBitmapIsObjectBitSet(ctx->markBits, referent); |
| 358 | } else { |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Returns true if the given object is a string and has been interned |
| 365 | * by the user. |
| 366 | */ |
| 367 | static bool isWeakInternedString(const Object *obj) |
| 368 | { |
| 369 | assert(obj != NULL); |
| 370 | if (obj->clazz == gDvm.classJavaLangString) { |
| 371 | return dvmIsWeakInternedString((StringObject *)obj); |
| 372 | } else { |
| 373 | return false; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 378 | * Returns true if the given object has been pushed on the mark stack |
| 379 | * by root marking. |
| 380 | */ |
| 381 | static bool isPushedOnMarkStack(const Object *obj) |
| 382 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 383 | GcMarkStack *stack = &gDvm.gcHeap->markContext.stack; |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 384 | const Object **ptr; |
| 385 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 386 | for (ptr = stack->base; ptr < stack->top; ++ptr) { |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 387 | if (*ptr == obj) { |
| 388 | return true; |
| 389 | } |
| 390 | } |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Callback applied to marked objects. If the object is gray and on |
| 396 | * an unmarked card an error is logged and the VM is aborted. Card |
| 397 | * table verification occurs between root marking and weak reference |
| 398 | * processing. We treat objects marked from the roots and weak |
| 399 | * references specially as it is permissible for these objects to be |
| 400 | * gray and on an unmarked card. |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 401 | */ |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 402 | static void verifyCardTableCallback(void *ptr, void *arg) |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 403 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 404 | Object *obj = (Object *)ptr; |
| 405 | WhiteReferenceCounter ctx = { (HeapBitmap *)arg, 0 }; |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 406 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 407 | dvmVisitObject(countWhiteReferenceVisitor, obj, &ctx); |
| 408 | if (ctx.whiteRefs == 0) { |
| 409 | return; |
| 410 | } else if (isObjectDirty(obj)) { |
| 411 | return; |
| 412 | } else if (isReferentUnmarked(obj, &ctx)) { |
| 413 | return; |
| 414 | } else if (isWeakInternedString(obj)) { |
| 415 | return; |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 416 | } else if (isPushedOnMarkStack(obj)) { |
| 417 | return; |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 418 | } else { |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 419 | LOGE("Verify failed, object %p is gray and on an unmarked card", obj); |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 420 | dvmDumpObject(obj); |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 421 | dvmVisitObject(dumpWhiteReferenceVisitor, obj, &ctx); |
| 422 | dumpReferences(obj); |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 423 | dvmAbort(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Verifies that gray objects are on a dirty card. |
| 429 | */ |
| 430 | void dvmVerifyCardTable(void) |
| 431 | { |
| 432 | HeapBitmap *markBits = gDvm.gcHeap->markContext.bitmap; |
| 433 | dvmHeapBitmapWalk(markBits, verifyCardTableCallback, markBits); |
| 434 | } |