| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -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 | |
| 17 | #include "Dalvik.h" |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 18 | #include "alloc/HeapBitmap.h" |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 19 | #include "alloc/HeapSource.h" |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 20 | #include "alloc/Verify.h" |
| Carl Shapiro | adc346f | 2010-06-03 23:01:39 -0700 | [diff] [blame] | 21 | #include "alloc/Visit.h" |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 22 | |
| Carl Shapiro | 97c3ddd | 2011-02-28 18:14:22 -0800 | [diff] [blame] | 23 | /* |
| 24 | * Visitor applied to each reference field when searching for things |
| 25 | * that point to an object. Sets the argument to NULL when a match is |
| 26 | * found. |
| 27 | */ |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 28 | static void dumpReferencesVisitor(void *pObj, void *arg) |
| 29 | { |
| 30 | Object *obj = *(Object **)pObj; |
| 31 | Object *lookingFor = *(Object **)arg; |
| 32 | if (lookingFor != NULL && lookingFor == obj) { |
| 33 | *(Object **)arg = NULL; |
| 34 | } |
| 35 | } |
| 36 | |
| Carl Shapiro | 97c3ddd | 2011-02-28 18:14:22 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Visitor applied to each bitmap element to search for things that |
| 39 | * point to an object. Logs a message when a match is found. |
| 40 | */ |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 41 | static void dumpReferencesCallback(void *ptr, void *arg) |
| 42 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 43 | Object *obj = (Object *)arg; |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 44 | if (ptr == obj) { |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 45 | return; |
| 46 | } |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 47 | dvmVisitObject(dumpReferencesVisitor, (Object *)ptr, &obj); |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 48 | if (obj == NULL) { |
| 49 | LOGD("Found %p in the heap @ %p", arg, ptr); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 50 | dvmDumpObject((Object *)ptr); |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| Carl Shapiro | 97c3ddd | 2011-02-28 18:14:22 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Visitor applied to each root to search for things that point to an |
| 56 | * object. Logs a message when a match is found. |
| 57 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 58 | static void dumpReferencesRootVisitor(void *ptr, u4 threadId, |
| 59 | RootType type, void *arg) |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 60 | { |
| 61 | Object *obj = *(Object **)ptr; |
| 62 | Object *lookingFor = *(Object **)arg; |
| 63 | if (obj == lookingFor) { |
| 64 | LOGD("Found %p in a root @ %p", arg, ptr); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Searches the roots and heap for object references. |
| 70 | */ |
| 71 | static void dumpReferences(const Object *obj) |
| 72 | { |
| 73 | HeapBitmap *bitmap = dvmHeapSourceGetLiveBits(); |
| 74 | void *arg = (void *)obj; |
| 75 | dvmVisitRoots(dumpReferencesRootVisitor, arg); |
| 76 | dvmHeapBitmapWalk(bitmap, dumpReferencesCallback, arg); |
| 77 | } |
| 78 | |
| Carl Shapiro | c85ec00 | 2010-04-05 19:36:37 -0700 | [diff] [blame] | 79 | /* |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 80 | * Checks that the given reference points to a valid object. |
| Carl Shapiro | c85ec00 | 2010-04-05 19:36:37 -0700 | [diff] [blame] | 81 | */ |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 82 | static void verifyReference(void *addr, void *arg) |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 83 | { |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 84 | Object *obj; |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 85 | bool isValid; |
| 86 | |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 87 | assert(addr != NULL); |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 88 | obj = *(Object **)addr; |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 89 | if (obj == NULL) { |
| 90 | isValid = true; |
| 91 | } else { |
| 92 | isValid = dvmIsValidObject(obj); |
| 93 | } |
| 94 | if (!isValid) { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 95 | Object **parent = (Object **)arg; |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 96 | if (*parent != NULL) { |
| 97 | LOGE("Verify of object %p failed", *parent); |
| 98 | dvmDumpObject(*parent); |
| 99 | *parent = NULL; |
| 100 | } |
| 101 | LOGE("Verify of reference %p @ %p failed", obj, addr); |
| 102 | dvmDumpObject(obj); |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 105 | |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 106 | /* |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 107 | * Verifies an object reference. |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 108 | */ |
| 109 | void dvmVerifyObject(const Object *obj) |
| 110 | { |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 111 | Object *arg = (Object *)obj; |
| 112 | dvmVisitObject(verifyReference, (Object *)obj, &arg); |
| 113 | if (arg == NULL) { |
| 114 | dumpReferences(obj); |
| 115 | dvmAbort(); |
| 116 | } |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Helper function to call dvmVerifyObject from a bitmap walker. |
| 121 | */ |
| Carl Shapiro | c44bca6 | 2010-08-27 15:21:13 -0700 | [diff] [blame] | 122 | static void verifyBitmapCallback(void *ptr, void *arg) |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 123 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame] | 124 | dvmVerifyObject((Object *)ptr); |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 128 | * Verifies the object references in a heap bitmap. Assumes the VM is |
| 129 | * suspended. |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 130 | */ |
| 131 | void dvmVerifyBitmap(const HeapBitmap *bitmap) |
| 132 | { |
| Barry Hayes | 962adba | 2010-03-17 12:12:39 -0700 | [diff] [blame] | 133 | dvmHeapBitmapWalk(bitmap, verifyBitmapCallback, NULL); |
| Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 134 | } |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 135 | |
| 136 | /* |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 137 | * Helper function to call verifyReference from the root verifier. |
| 138 | */ |
| 139 | static void verifyRootReference(void *addr, u4 threadId, |
| 140 | RootType type, void *arg) |
| 141 | { |
| 142 | verifyReference(addr, arg); |
| 143 | } |
| 144 | |
| 145 | /* |
| Carl Shapiro | 1fbfcab | 2010-07-22 17:29:23 -0700 | [diff] [blame] | 146 | * Verifies references in the roots. |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 147 | */ |
| 148 | void dvmVerifyRoots(void) |
| 149 | { |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 150 | dvmVisitRoots(verifyRootReference, NULL); |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 151 | } |