| 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" |
| 20 | #include "alloc/HeapSource.h" |
| 21 | #include "alloc/Visit.h" |
| 22 | |
| 23 | /* |
| 24 | * Maintain a card table from the the write barrier. All writes of |
| 25 | * non-NULL values to heap addresses should go through an entry in |
| 26 | * WriteBarrier, and from there to here. |
| 27 | * |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 28 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 29 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 30 | * data per card, to be used by the GC. The value of the byte will be |
| 31 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 32 | * |
| 33 | * After any store of a non-NULL object pointer into a heap object, |
| 34 | * code is obliged to mark the card dirty. The setters in |
| 35 | * ObjectInlines.h [such as dvmSetFieldObject] do this for you. The |
| 36 | * JIT and fast interpreters also contain code to mark cards as dirty. |
| 37 | * |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 38 | * The card table's base [the "biased card table"] gets set to a |
| 39 | * rather strange value. In order to keep the JIT from having to |
| 40 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 41 | * biased base is within the mmap allocation at a point where it's low |
| 42 | * byte is equal to GC_DIRTY_CARD. See dvmCardTableStartup for details. |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * Initializes the card table; must be called before any other |
| 47 | * dvmCardTable*() functions. |
| 48 | */ |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame^] | 49 | bool dvmCardTableStartup(size_t heapMaximumSize) |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 50 | { |
| 51 | size_t length; |
| 52 | void *allocBase; |
| 53 | u1 *biasedBase; |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 54 | GcHeap *gcHeap = gDvm.gcHeap; |
| 55 | void *heapBase = dvmHeapSourceGetBase(); |
| 56 | assert(gcHeap != NULL); |
| 57 | assert(heapBase != NULL); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 58 | |
| 59 | /* Set up the card table */ |
| Carl Shapiro | df9f08b | 2011-01-18 17:59:30 -0800 | [diff] [blame^] | 60 | length = heapMaximumSize / GC_CARD_SIZE; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 61 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
| 62 | allocBase = dvmAllocRegion(length + 0x100, PROT_READ | PROT_WRITE, |
| 63 | "dalvik-card-table"); |
| 64 | if (allocBase == NULL) { |
| 65 | return false; |
| 66 | } |
| 67 | gcHeap->cardTableBase = allocBase; |
| 68 | gcHeap->cardTableLength = length; |
| 69 | /* All zeros is the correct initial value; all clean. */ |
| 70 | assert(GC_CARD_CLEAN == 0); |
| 71 | |
| 72 | biasedBase = (u1 *)((uintptr_t)allocBase - |
| 73 | ((uintptr_t)heapBase >> GC_CARD_SHIFT)); |
| 74 | if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) { |
| Carl Shapiro | 5d81aa3 | 2010-08-20 11:04:25 -0700 | [diff] [blame] | 75 | int offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 76 | biasedBase += offset + (offset < 0 ? 0x100 : 0); |
| 77 | } |
| 78 | assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY); |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 79 | gDvm.biasedCardTableBase = biasedBase; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Tears down the entire CardTable. |
| 86 | */ |
| 87 | void dvmCardTableShutdown() |
| 88 | { |
| Barry Hayes | b874ab9 | 2010-07-14 08:13:18 -0700 | [diff] [blame] | 89 | gDvm.biasedCardTableBase = NULL; |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 90 | munmap(gDvm.gcHeap->cardTableBase, gDvm.gcHeap->cardTableLength); |
| 91 | } |
| 92 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 93 | void dvmClearCardTable(void) |
| 94 | { |
| 95 | assert(gDvm.gcHeap->cardTableBase != NULL); |
| 96 | memset(gDvm.gcHeap->cardTableBase, GC_CARD_CLEAN, gDvm.gcHeap->cardTableLength); |
| 97 | } |
| 98 | |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 99 | /* |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 100 | * Returns true iff the address is within the bounds of the card table. |
| 101 | */ |
| 102 | bool dvmIsValidCard(const u1 *cardAddr) |
| 103 | { |
| 104 | GcHeap *h = gDvm.gcHeap; |
| 105 | return cardAddr >= h->cardTableBase && |
| 106 | cardAddr < &h->cardTableBase[h->cardTableLength]; |
| 107 | } |
| 108 | |
| 109 | /* |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 110 | * Returns the address of the relevent byte in the card table, given |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 111 | * an address on the heap. |
| 112 | */ |
| 113 | u1 *dvmCardFromAddr(const void *addr) |
| 114 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 115 | u1 *biasedBase = gDvm.biasedCardTableBase; |
| 116 | u1 *cardAddr = biasedBase + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 117 | assert(dvmIsValidCard(cardAddr)); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 118 | return cardAddr; |
| 119 | } |
| 120 | |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Returns the first address in the heap which maps to this card. |
| 123 | */ |
| 124 | void *dvmAddrFromCard(const u1 *cardAddr) |
| 125 | { |
| Barry Hayes | 4496ed9 | 2010-07-12 09:52:20 -0700 | [diff] [blame] | 126 | assert(dvmIsValidCard(cardAddr)); |
| 127 | uintptr_t offset = cardAddr - gDvm.biasedCardTableBase; |
| Barry Hayes | 8f921a7 | 2010-07-09 12:53:49 -0700 | [diff] [blame] | 128 | return (void *)(offset << GC_CARD_SHIFT); |
| Barry Hayes | 6e5cf60 | 2010-06-22 12:32:59 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Dirties the card for the given address. |
| 133 | */ |
| 134 | void dvmMarkCard(const void *addr) |
| 135 | { |
| 136 | u1 *cardAddr = dvmCardFromAddr(addr); |
| 137 | *cardAddr = GC_CARD_DIRTY; |
| 138 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 141 | * Returns true if the object is on a dirty card. |
| 142 | */ |
| 143 | static bool isObjectDirty(const Object *obj) |
| 144 | { |
| 145 | assert(obj != NULL); |
| 146 | assert(dvmIsValidObject(obj)); |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 147 | u1 *card = dvmCardFromAddr(obj); |
| 148 | return *card == GC_CARD_DIRTY; |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Context structure for verifying the card table. |
| 153 | */ |
| 154 | typedef struct { |
| 155 | HeapBitmap *markBits; |
| 156 | size_t whiteRefs; |
| 157 | } WhiteReferenceCounter; |
| 158 | |
| 159 | /* |
| 160 | * Visitor that counts white referents. |
| 161 | */ |
| 162 | static void countWhiteReferenceVisitor(void *addr, void *arg) |
| 163 | { |
| 164 | WhiteReferenceCounter *ctx; |
| 165 | Object *obj; |
| 166 | |
| 167 | assert(addr != NULL); |
| 168 | assert(arg != NULL); |
| 169 | obj = *(Object **)addr; |
| Carl Shapiro | 8e14bc7 | 2010-10-18 16:27:29 -0700 | [diff] [blame] | 170 | if (obj == NULL) { |
| 171 | return; |
| 172 | } |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 173 | assert(dvmIsValidObject(obj)); |
| 174 | ctx = arg; |
| Carl Shapiro | 8e14bc7 | 2010-10-18 16:27:29 -0700 | [diff] [blame] | 175 | if (dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | ctx->whiteRefs += 1; |
| 179 | } |
| 180 | |
| 181 | /* |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 182 | * Visitor that logs white references. |
| 183 | */ |
| 184 | static void dumpWhiteReferenceVisitor(void *addr, void *arg) |
| 185 | { |
| 186 | WhiteReferenceCounter *ctx; |
| 187 | Object *obj; |
| 188 | |
| 189 | assert(addr != NULL); |
| 190 | assert(arg != NULL); |
| 191 | obj = *(Object **)addr; |
| 192 | if (obj == NULL) { |
| 193 | return; |
| 194 | } |
| 195 | assert(dvmIsValidObject(obj)); |
| 196 | ctx = arg; |
| 197 | if (dvmHeapBitmapIsObjectBitSet(ctx->markBits, obj)) { |
| 198 | return; |
| 199 | } |
| 200 | LOGE("object %p is white", obj); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Visitor that signals the caller when a matching reference is found. |
| 205 | */ |
| 206 | static void dumpReferencesVisitor(void *pObj, void *arg) |
| 207 | { |
| 208 | Object *obj = *(Object **)pObj; |
| 209 | Object *lookingFor = *(Object **)arg; |
| 210 | if (lookingFor != NULL && lookingFor == obj) { |
| 211 | *(Object **)arg = NULL; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void dumpReferencesCallback(void *ptr, void *arg) |
| 216 | { |
| 217 | Object *obj = arg; |
| 218 | if (ptr == obj) { |
| 219 | return; |
| 220 | } |
| 221 | dvmVisitObject(dumpReferencesVisitor, ptr, &obj); |
| 222 | if (obj == NULL) { |
| 223 | LOGD("Found %p in the heap @ %p", arg, ptr); |
| 224 | dvmDumpObject(ptr); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Root visitor that looks for matching references. |
| 230 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 231 | static void dumpReferencesRootVisitor(void *ptr, u4 threadId, |
| 232 | RootType type, void *arg) |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 233 | { |
| 234 | Object *obj = *(Object **)ptr; |
| 235 | Object *lookingFor = *(Object **)arg; |
| 236 | if (obj == lookingFor) { |
| 237 | LOGD("Found %p in a root @ %p", arg, ptr); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Invokes visitors to search for references to an object. |
| 243 | */ |
| 244 | static void dumpReferences(const Object *obj) |
| 245 | { |
| 246 | HeapBitmap *bitmap = dvmHeapSourceGetLiveBits(); |
| 247 | void *arg = (void *)obj; |
| 248 | dvmVisitRoots(dumpReferencesRootVisitor, arg); |
| 249 | dvmHeapBitmapWalk(bitmap, dumpReferencesCallback, arg); |
| 250 | } |
| 251 | |
| 252 | /* |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 253 | * Returns true if the given object is a reference object and the |
| 254 | * just the referent is unmarked. |
| 255 | */ |
| 256 | static bool isReferentUnmarked(const Object *obj, |
| 257 | const WhiteReferenceCounter* ctx) |
| 258 | { |
| 259 | assert(obj != NULL); |
| 260 | assert(obj->clazz != NULL); |
| 261 | assert(ctx != NULL); |
| 262 | if (ctx->whiteRefs != 1) { |
| 263 | return false; |
| 264 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) { |
| 265 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 266 | const Object *referent = dvmGetFieldObject(obj, offset); |
| 267 | return !dvmHeapBitmapIsObjectBitSet(ctx->markBits, referent); |
| 268 | } else { |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Returns true if the given object is a string and has been interned |
| 275 | * by the user. |
| 276 | */ |
| 277 | static bool isWeakInternedString(const Object *obj) |
| 278 | { |
| 279 | assert(obj != NULL); |
| 280 | if (obj->clazz == gDvm.classJavaLangString) { |
| 281 | return dvmIsWeakInternedString((StringObject *)obj); |
| 282 | } else { |
| 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 288 | * Returns true if the given object has been pushed on the mark stack |
| 289 | * by root marking. |
| 290 | */ |
| 291 | static bool isPushedOnMarkStack(const Object *obj) |
| 292 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 293 | GcMarkStack *stack = &gDvm.gcHeap->markContext.stack; |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 294 | const Object **ptr; |
| 295 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 296 | for (ptr = stack->base; ptr < stack->top; ++ptr) { |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 297 | if (*ptr == obj) { |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Callback applied to marked objects. If the object is gray and on |
| 306 | * an unmarked card an error is logged and the VM is aborted. Card |
| 307 | * table verification occurs between root marking and weak reference |
| 308 | * processing. We treat objects marked from the roots and weak |
| 309 | * references specially as it is permissible for these objects to be |
| 310 | * gray and on an unmarked card. |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 311 | */ |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 312 | static void verifyCardTableCallback(void *ptr, void *arg) |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 313 | { |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 314 | Object *obj = ptr; |
| 315 | WhiteReferenceCounter ctx = { arg, 0 }; |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 316 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 317 | dvmVisitObject(countWhiteReferenceVisitor, obj, &ctx); |
| 318 | if (ctx.whiteRefs == 0) { |
| 319 | return; |
| 320 | } else if (isObjectDirty(obj)) { |
| 321 | return; |
| 322 | } else if (isReferentUnmarked(obj, &ctx)) { |
| 323 | return; |
| 324 | } else if (isWeakInternedString(obj)) { |
| 325 | return; |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 326 | } else if (isPushedOnMarkStack(obj)) { |
| 327 | return; |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 328 | } else { |
| Carl Shapiro | 71ce7a9 | 2010-09-29 01:09:11 -0700 | [diff] [blame] | 329 | 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] | 330 | dvmDumpObject(obj); |
| Carl Shapiro | 7f43c03 | 2010-10-18 17:26:46 -0700 | [diff] [blame] | 331 | dvmVisitObject(dumpWhiteReferenceVisitor, obj, &ctx); |
| 332 | dumpReferences(obj); |
| Carl Shapiro | 5ba3937 | 2010-08-06 17:07:53 -0700 | [diff] [blame] | 333 | dvmAbort(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Verifies that gray objects are on a dirty card. |
| 339 | */ |
| 340 | void dvmVerifyCardTable(void) |
| 341 | { |
| 342 | HeapBitmap *markBits = gDvm.gcHeap->markContext.bitmap; |
| 343 | dvmHeapBitmapWalk(markBits, verifyCardTableCallback, markBits); |
| 344 | } |