summaryrefslogtreecommitdiffstats
path: root/vm/alloc/Verify.cpp
blob: 93a090ab0c6e4d518c072fcd8d36bf36b0dc8c32 (plain)
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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Dalvik.h"
#include "alloc/HeapBitmap.h"
#include "alloc/HeapSource.h"
#include "alloc/Verify.h"
#include "alloc/Visit.h"

/*
 * Visitor applied to each reference field when searching for things
 * that point to an object.  Sets the argument to NULL when a match is
 * found.
 */
static void dumpReferencesVisitor(void *pObj, void *arg)
{
    Object *obj = *(Object **)pObj;
    Object *lookingFor = *(Object **)arg;
    if (lookingFor != NULL && lookingFor == obj) {
        *(Object **)arg = NULL;
    }
}

/*
 * Visitor applied to each bitmap element to search for things that
 * point to an object.  Logs a message when a match is found.
 */
static void dumpReferencesCallback(Object *obj, void *arg)
{
    if (obj == (Object *)arg) {
        return;
    }
    dvmVisitObject(dumpReferencesVisitor, obj, &arg);
    if (arg == NULL) {
        ALOGD("Found %p in the heap @ %p", arg, obj);
        dvmDumpObject(obj);
    }
}

/*
 * Visitor applied to each root to search for things that point to an
 * object.  Logs a message when a match is found.
 */
static void dumpReferencesRootVisitor(void *ptr, u4 threadId,
                                      RootType type, void *arg)
{
    Object *obj = *(Object **)ptr;
    Object *lookingFor = *(Object **)arg;
    if (obj == lookingFor) {
        ALOGD("Found %p in a root @ %p", arg, ptr);
    }
}

/*
 * Searches the roots and heap for object references.
 */
static void dumpReferences(const Object *obj)
{
    HeapBitmap *bitmap = dvmHeapSourceGetLiveBits();
    void *arg = (void *)obj;
    dvmVisitRoots(dumpReferencesRootVisitor, arg);
    dvmHeapBitmapWalk(bitmap, dumpReferencesCallback, arg);
}

/*
 * Checks that the given reference points to a valid object.
 */
static void verifyReference(void *addr, void *arg)
{
    Object *obj;
    bool isValid;

    assert(addr != NULL);
    obj = *(Object **)addr;
    if (obj == NULL) {
        isValid = true;
    } else {
        isValid = dvmIsValidObject(obj);
    }
    if (!isValid) {
        Object **parent = (Object **)arg;
        if (*parent != NULL) {
            ALOGE("Verify of object %p failed", *parent);
            dvmDumpObject(*parent);
            *parent = NULL;
        }
        ALOGE("Verify of reference %p @ %p failed", obj, addr);
        dvmDumpObject(obj);
    }
}

/*
 * Verifies an object reference.
 */
void dvmVerifyObject(const Object *obj)
{
    Object *arg = const_cast<Object*>(obj);
    dvmVisitObject(verifyReference, arg, &arg);
    if (arg == NULL) {
        dumpReferences(obj);
        dvmAbort();
    }
}

/*
 * Helper function to call dvmVerifyObject from a bitmap walker.
 */
static void verifyBitmapCallback(Object *obj, void *arg)
{
    dvmVerifyObject(obj);
}

/*
 * Verifies the object references in a heap bitmap. Assumes the VM is
 * suspended.
 */
void dvmVerifyBitmap(const HeapBitmap *bitmap)
{
    dvmHeapBitmapWalk(bitmap, verifyBitmapCallback, NULL);
}

/*
 * Helper function to call verifyReference from the root verifier.
 */
static void verifyRootReference(void *addr, u4 threadId,
                                RootType type, void *arg)
{
    verifyReference(addr, arg);
}

/*
 * Verifies references in the roots.
 */
void dvmVerifyRoots()
{
    dvmVisitRoots(verifyRootReference, NULL);
}