1 /*
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 */
16
17 #include "art_method-inl.h"
18 #include "check_reference_map_visitor.h"
19 #include "jni.h"
20
21 namespace art {
22
23 #define CHECK_REGS_CONTAIN_REFS(dex_pc, abort_if_not_found, ...) do { \
24 int t[] = {__VA_ARGS__}; \
25 int t_size = sizeof(t) / sizeof(*t); \
26 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); \
27 uintptr_t native_quick_pc = method_header->ToNativeQuickPc(GetMethod(), \
28 dex_pc, \
29 /* is_catch_handler */ false, \
30 abort_if_not_found); \
31 if (native_quick_pc != UINTPTR_MAX) { \
32 CheckReferences(t, \
33 t_size, \
34 dex_pc, \
35 method_header->NativeQuickPcOffset(native_quick_pc), \
36 /* search_for_valid_stack_map= */ true); \
37 } \
38 } while (false);
39
40 struct ReferenceMap2Visitor : public CheckReferenceMapVisitor {
REQUIRES_SHAREDart::ReferenceMap2Visitor41 explicit ReferenceMap2Visitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
42 : CheckReferenceMapVisitor(thread) {}
43
VisitFrameart::ReferenceMap2Visitor44 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
45 if (CheckReferenceMapVisitor::VisitFrame()) {
46 return true;
47 }
48 ArtMethod* m = GetMethod();
49 std::string m_name(m->GetName());
50
51 // Given the method name and the number of times the method has been called,
52 // we know the Dex registers with live reference values. Assert that what we
53 // find is what is expected.
54 if (m_name.compare("f") == 0) {
55 CHECK_REGS_CONTAIN_REFS(0x03U, true, 8); // v8: this
56 CHECK_REGS_CONTAIN_REFS(0x06U, true, 8, 1); // v8: this, v1: x
57 CHECK_REGS_CONTAIN_REFS(0x0cU, true, 8, 3, 1); // v8: this, v3: y, v1: x
58 CHECK_REGS_CONTAIN_REFS(0x10U, true, 8, 3, 1); // v8: this, v3: y, v1: x
59 // v2 is added because of the instruction at DexPC 0024. Object merges with 0 is Object. See:
60 // 0024: move-object v3, v2
61 // 0025: goto 0013
62 // Detailed dex instructions for ReferenceMap.java are at the end of this function.
63 // CHECK_REGS_CONTAIN_REFS(8, 3, 2, 1); // v8: this, v3: y, v2: y, v1: x
64 // We eliminate the non-live registers at a return, so only v3 is live.
65 // Note that it is OK for a compiler to not have a dex map at this dex PC because
66 // a return is not necessarily a safepoint.
67 CHECK_REGS_CONTAIN_REFS(0x13U, false, 3); // v3: y
68 // Note that v0: ex can be eliminated because it's a dead merge of two different exceptions.
69 CHECK_REGS_CONTAIN_REFS(0x18U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
70 CHECK_REGS_CONTAIN_REFS(0x21U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
71
72 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
73 CHECK_REGS_CONTAIN_REFS(0x27U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
74 }
75 CHECK_REGS_CONTAIN_REFS(0x29U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
76 CHECK_REGS_CONTAIN_REFS(0x2cU, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
77 // Note that it is OK for a compiler to not have a dex map at these two dex PCs because
78 // a goto is not necessarily a safepoint.
79 CHECK_REGS_CONTAIN_REFS(0x2fU, false, 8, 4, 3, 2, 1); // v8: this, v4: ex, v3: y, v2: y, v1: x
80 CHECK_REGS_CONTAIN_REFS(0x32U, false, 8, 3, 2, 1, 0); // v8: this, v3: y, v2: y, v1: x, v0: ex
81 }
82
83 return true;
84 }
85 };
86
87 // Dex instructions for the function 'f' in ReferenceMap.java
88 // Virtual methods -
89 // #0 : (in LReferenceMap;)
90 // name : 'f'
91 // type : '()Ljava/lang/Object;'
92 // access : 0x0000 ()
93 // code -
94 // registers : 9
95 // ins : 1
96 // outs : 2
97 // insns size : 51 16-bit code units
98 // |[0001e8] ReferenceMap.f:()Ljava/lang/Object;
99 // |0000: const/4 v4, #int 2 // #2
100 // |0001: const/4 v7, #int 0 // #0
101 // |0002: const/4 v6, #int 1 // #1
102 //
103 // 0:[Unknown],1:[Unknown],2:[Unknown],3:[Unknown],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
104 // |0003: new-array v1, v4, [Ljava/lang/Object; // type@0007
105 // |0005: const/4 v2, #int 0 // #0
106
107 // 0:[Unknown],1:[Reference: java.lang.Object[]],2:[Zero],3:[Unknown],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
108 // |0006: new-instance v3, Ljava/lang/Object; // type@0003
109
110 // [Unknown],1:[Reference: java.lang.Object[]],2:[Zero],3:[Uninitialized Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
111 // |0008: +invoke-object-init/range {}, Ljava/lang/Object;.<init>:()V // method@0005
112 // |000b: const/4 v4, #int 2 // #2
113
114 // 0:[Unknown],1:[Reference: java.lang.Object[]],2:[Zero],3:[Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
115 // |000c: aput-object v3, v1, v4
116
117 // 0:[Unknown],1:[Reference: java.lang.Object[]],2:[Zero],3:[Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
118 // |000e: aput-object v3, v1, v6
119
120 // 0:[Unknown],1:[Reference: java.lang.Object[]],2:[Zero],3:[Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
121 // |0010: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
122
123 // 0:[Conflict],1:[Conflict],2:[Conflict],3:[Reference: java.lang.Object],4:[Conflict],5:[Conflict],6:[Conflict],7:[Conflict],8:[Conflict],
124 // |0013: return-object v3
125 // |0014: move-exception v0
126
127 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
128 // |0015: if-nez v2, 001f // +000a
129 // |0017: const/4 v4, #int 1 // #1
130
131 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 1],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
132 // |0018: new-instance v5, Ljava/lang/Object; // type@0003
133
134 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 1],5:[Uninitialized Reference: java.lang.Object],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
135 // |001a: +invoke-object-init/range {}, Ljava/lang/Object;.<init>:()V // method@0005
136
137 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 1],5:[Reference: java.lang.Object],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
138 // |001d: aput-object v5, v1, v4
139
140 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 2],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
141 // |001f: aput-object v2, v1, v6
142
143 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[32-bit Constant: 2],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
144 // |0021: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
145 // |0024: move-object v3, v2
146
147 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
148 // |0025: goto 0013 // -0012
149 // |0026: move-exception v4
150
151 // 0:[Conflict],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[Reference: java.lang.Throwable],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
152 // |0027: aput-object v2, v1, v6
153
154 // 0:[Conflict],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[Reference: java.lang.Throwable],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
155 // |0029: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
156
157 // 0:[Conflict],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Conflict],4:[Reference: java.lang.Throwable],5:[Conflict],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
158 // |002c: throw v4
159 // |002d: move-exception v4
160 // |002e: move-object v2, v3
161
162 // 0:[Unknown],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Reference: java.lang.Object],4:[Reference: java.lang.Throwable],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
163 // |002f: goto 0027 // -0008
164 // |0030: move-exception v0
165 // |0031: move-object v2, v3
166
167 // 0:[Reference: java.lang.Exception],1:[Reference: java.lang.Object[]],2:[Reference: java.lang.Object],3:[Reference: java.lang.Object],4:[32-bit Constant: 2],5:[Unknown],6:[32-bit Constant: 1],7:[Zero],8:[Reference: ReferenceMap],
168 // |0032: goto 0015 // -001d
169 // catches : 3
170 // 0x0006 - 0x000b
171 // Ljava/lang/Exception; -> 0x0014
172 // <any> -> 0x0026
173 // 0x000c - 0x000e
174 // Ljava/lang/Exception; -> 0x0030
175 // <any> -> 0x002d
176 // 0x0018 - 0x001f
177 // <any> -> 0x0026
178 // positions :
179 // 0x0003 line=8
180 // 0x0005 line=9
181 // 0x0006 line=11
182 // 0x000b line=12
183 // 0x000e line=18
184 // 0x0010 line=19
185 // 0x0013 line=21
186 // 0x0014 line=13
187 // 0x0015 line=14
188 // 0x0017 line=15
189 // 0x001f line=18
190 // 0x0021 line=19
191 // 0x0025 line=20
192 // 0x0026 line=18
193 // 0x0029 line=19
194 // 0x002d line=18
195 // 0x0030 line=13
196 // locals :
197 // 0x0006 - 0x000b reg=2 y Ljava/lang/Object;
198 // 0x000b - 0x0013 reg=3 y Ljava/lang/Object;
199 // 0x0014 - 0x0015 reg=2 y Ljava/lang/Object;
200 // 0x0015 - 0x0026 reg=0 ex Ljava/lang/Exception;
201 // 0x002d - 0x0032 reg=3 y Ljava/lang/Object;
202 // 0x0005 - 0x0033 reg=1 x [Ljava/lang/Object;
203 // 0x0032 - 0x0033 reg=2 y Ljava/lang/Object;
204 // 0x0000 - 0x0033 reg=8 this LReferenceMap;
205
Java_Main_refmap(JNIEnv *,jobject,jint count)206 extern "C" JNIEXPORT jint JNICALL Java_Main_refmap(JNIEnv*, jobject, jint count) {
207 // Visitor
208 ScopedObjectAccess soa(Thread::Current());
209 ReferenceMap2Visitor mapper(soa.Self());
210 mapper.WalkStack();
211
212 return count + 1;
213 }
214
215 } // namespace art
216