blob: 37731981ff2bff58330101bda1319e0f943cbf74 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Shih-wei Liao9407c602011-09-16 10:36:43 -070016
17#include <stdio.h>
18
19#include "UniquePtr.h"
20#include "class_linker.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070021#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080022#include "object_utils.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070023#include "jni.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070024#include "verifier/gc_map.h"
Shih-wei Liao9407c602011-09-16 10:36:43 -070025
26namespace art {
27
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080028#define REG(mh, reg_bitmap, reg) \
29 ( ((reg) < mh.GetCodeItem()->registers_size_) && \
Ian Rogersd81871c2011-10-03 13:57:23 -070030 (( *((reg_bitmap) + (reg)/8) >> ((reg) % 8) ) & 0x01) )
Shih-wei Liao9407c602011-09-16 10:36:43 -070031
Ian Rogers0399dde2012-06-06 17:09:28 -070032#define CHECK_REGS(...) if (!IsShadowFrame()) { \
33 int t[] = {__VA_ARGS__}; \
34 int t_size = sizeof(t) / sizeof(*t); \
35 for (int i = 0; i < t_size; ++i) \
36 CHECK(REG(mh, reg_bitmap, t[i])) << "Error: Reg " << i << " is not in RegisterMap"; \
37 }
Shih-wei Liao9407c602011-09-16 10:36:43 -070038
39static int gJava_StackWalk_refmap_calls = 0;
40
Ian Rogers0399dde2012-06-06 17:09:28 -070041struct TestReferenceMapVisitor : public StackVisitor {
42 explicit TestReferenceMapVisitor(const ManagedStack* stack,
43 const std::vector<TraceStackFrame>* trace_stack) :
44 StackVisitor(stack, trace_stack) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070045 }
46
Ian Rogers0399dde2012-06-06 17:09:28 -070047 bool VisitFrame() {
48 Method* m = GetMethod();
Ian Rogersd81871c2011-10-03 13:57:23 -070049 CHECK(m != NULL);
Shih-wei Liao9407c602011-09-16 10:36:43 -070050 LOG(INFO) << "At " << PrettyMethod(m, false);
51
Ian Rogersd81871c2011-10-03 13:57:23 -070052 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Shih-wei Liao9407c602011-09-16 10:36:43 -070053 LOG(WARNING) << "no PC for " << PrettyMethod(m);
Ian Rogers0399dde2012-06-06 17:09:28 -070054 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
Elliott Hughes530fa002012-03-12 11:44:49 -070055 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070056 }
Ian Rogers0399dde2012-06-06 17:09:28 -070057 const uint8_t* reg_bitmap = NULL;
58 if (!IsShadowFrame()) {
59 verifier::PcToReferenceMap map(m->GetGcMap(), m->GetGcMapLength());
60 reg_bitmap = map.FindBitMap(GetDexPc());
61 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080062 MethodHelper mh(m);
63 StringPiece m_name(mh.GetName());
Shih-wei Liao9407c602011-09-16 10:36:43 -070064
65 // Given the method name and the number of times the method has been called,
66 // we know the Dex registers with live reference values. Assert that what we
67 // find is what is expected.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080068 if (m_name == "f") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070069 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070070 CHECK_EQ(1U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070071 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070072 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070073 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070074 CHECK_EQ(5U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070075 CHECK_REGS(1);
Shih-wei Liao9407c602011-09-16 10:36:43 -070076 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080077 } else if (m_name == "g") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070078 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070079 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070080 CHECK_REGS(0, 2); // Note that v1 is not in the minimal root set
Shih-wei Liao9407c602011-09-16 10:36:43 -070081 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070082 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070083 CHECK_EQ(0xcU, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070084 CHECK_REGS(0, 2);
Shih-wei Liao9407c602011-09-16 10:36:43 -070085 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080086 } else if (m_name == "shlemiel") {
Shih-wei Liao9407c602011-09-16 10:36:43 -070087 if (gJava_StackWalk_refmap_calls == 1) {
Ian Rogers0399dde2012-06-06 17:09:28 -070088 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070089 CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
Shih-wei Liao9407c602011-09-16 10:36:43 -070090 } else {
Elliott Hughesf5a7a472011-10-07 14:31:02 -070091 CHECK_EQ(gJava_StackWalk_refmap_calls, 2);
Ian Rogers0399dde2012-06-06 17:09:28 -070092 CHECK_EQ(0x380U, GetDexPc());
Shih-wei Liao0839bdb2011-10-12 10:50:44 -070093 CHECK_REGS(2, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 21, 25);
Shih-wei Liao9407c602011-09-16 10:36:43 -070094 }
95 }
Elliott Hughes91250e02011-12-13 22:30:35 -080096 LOG(INFO) << reinterpret_cast<const void*>(reg_bitmap);
Elliott Hughes530fa002012-03-12 11:44:49 -070097
98 return true;
Shih-wei Liao9407c602011-09-16 10:36:43 -070099 }
100};
101
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700102extern "C" JNIEXPORT jint JNICALL Java_StackWalk_refmap(JNIEnv*, jobject, jint count) {
Shih-wei Liao0839bdb2011-10-12 10:50:44 -0700103 CHECK_EQ(count, 0);
Shih-wei Liao9407c602011-09-16 10:36:43 -0700104 gJava_StackWalk_refmap_calls++;
105
106 // Visitor
Ian Rogers0399dde2012-06-06 17:09:28 -0700107 TestReferenceMapVisitor mapper(Thread::Current()->GetManagedStack(),
108 Thread::Current()->GetTraceStack());
109 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700110
111 return count + 1;
112}
113
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700114extern "C" JNIEXPORT jint JNICALL Java_StackWalk2_refmap2(JNIEnv*, jobject, jint count) {
Shih-wei Liao9407c602011-09-16 10:36:43 -0700115 gJava_StackWalk_refmap_calls++;
116
117 // Visitor
Ian Rogers0399dde2012-06-06 17:09:28 -0700118 TestReferenceMapVisitor mapper(Thread::Current()->GetManagedStack(),
119 Thread::Current()->GetTraceStack());
120 mapper.WalkStack();
Shih-wei Liao9407c602011-09-16 10:36:43 -0700121
122 return count + 1;
123}
124
125}