blob: 0659c0beb5d6a58c08210c07e4a8e827242b8122 [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 Liao63433ba2011-10-15 18:40:39 -070016
Mathieu Chartier48b2b3e2016-05-05 15:31:12 -070017#include "art_method-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010018#include "check_reference_map_visitor.h"
Shih-wei Liao63433ba2011-10-15 18:40:39 -070019#include "jni.h"
20
21namespace art {
22
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010023#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 } \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010038} while (false);
Shih-wei Liao63433ba2011-10-15 18:40:39 -070039
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010040struct ReferenceMap2Visitor : public CheckReferenceMapVisitor {
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 explicit ReferenceMap2Visitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010042 : CheckReferenceMapVisitor(thread) {}
Shih-wei Liao63433ba2011-10-15 18:40:39 -070043
Andreas Gampefa6a1b02018-09-07 08:11:55 -070044 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010045 if (CheckReferenceMapVisitor::VisitFrame()) {
46 return true;
47 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070048 ArtMethod* m = GetMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070049 std::string m_name(m->GetName());
Shih-wei Liao63433ba2011-10-15 18:40:39 -070050
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) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000055 CHECK_REGS_CONTAIN_REFS(0x06U, true, 8, 1); // v8: this, v1: x
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000056 CHECK_REGS_CONTAIN_REFS(0x0cU, true, 8, 3, 1); // v8: this, v3: y, v1: x
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000057 CHECK_REGS_CONTAIN_REFS(0x10U, true, 8, 3, 1); // v8: this, v3: y, v1: x
Shih-wei Liao371814f2011-10-27 16:52:10 -070058 // v2 is added because of the instruction at DexPC 0024. Object merges with 0 is Object. See:
59 // 0024: move-object v3, v2
60 // 0025: goto 0013
Vladimir Marko8b858e12014-11-27 14:52:37 +000061 // Detailed dex instructions for ReferenceMap.java are at the end of this function.
Brian Carlstrom0f055d12013-07-26 12:46:02 -070062 // CHECK_REGS_CONTAIN_REFS(8, 3, 2, 1); // v8: this, v3: y, v2: y, v1: x
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000063 // We eliminate the non-live registers at a return, so only v3 is live.
64 // Note that it is OK for a compiler to not have a dex map at this dex PC because
Vladimir Marko8b858e12014-11-27 14:52:37 +000065 // a return is not necessarily a safepoint.
Igor Murashkind61c3812017-06-21 11:13:16 -070066 CHECK_REGS_CONTAIN_REFS(0x13U, false, 3); // v3: y
Vladimir Marko767c7522015-03-20 12:47:30 +000067 // Note that v0: ex can be eliminated because it's a dead merge of two different exceptions.
68 CHECK_REGS_CONTAIN_REFS(0x18U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
Igor Murashkind61c3812017-06-21 11:13:16 -070069 CHECK_REGS_CONTAIN_REFS(0x21U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010070
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010072 CHECK_REGS_CONTAIN_REFS(0x27U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
73 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000074 CHECK_REGS_CONTAIN_REFS(0x29U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
75 CHECK_REGS_CONTAIN_REFS(0x2cU, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
Vladimir Marko8b858e12014-11-27 14:52:37 +000076 // Note that it is OK for a compiler to not have a dex map at these two dex PCs because
77 // a goto is not necessarily a safepoint.
78 CHECK_REGS_CONTAIN_REFS(0x2fU, false, 8, 4, 3, 2, 1); // v8: this, v4: ex, v3: y, v2: y, v1: x
79 CHECK_REGS_CONTAIN_REFS(0x32U, false, 8, 3, 2, 1, 0); // v8: this, v3: y, v2: y, v1: x, v0: ex
Shih-wei Liao63433ba2011-10-15 18:40:39 -070080 }
Elliott Hughes530fa002012-03-12 11:44:49 -070081
82 return true;
Shih-wei Liao63433ba2011-10-15 18:40:39 -070083 }
84};
85
Igor Murashkind61c3812017-06-21 11:13:16 -070086// Dex instructions for the function 'f' in ReferenceMap.java
87// Virtual methods -
88// #0 : (in LReferenceMap;)
89// name : 'f'
90// type : '()Ljava/lang/Object;'
91// access : 0x0000 ()
92// code -
93// registers : 9
94// ins : 1
95// outs : 2
96// insns size : 51 16-bit code units
97// |[0001e8] ReferenceMap.f:()Ljava/lang/Object;
98// |0000: const/4 v4, #int 2 // #2
99// |0001: const/4 v7, #int 0 // #0
100// |0002: const/4 v6, #int 1 // #1
Shih-wei Liao371814f2011-10-27 16:52:10 -0700101//
Igor Murashkind61c3812017-06-21 11:13:16 -0700102// 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],
103// |0003: new-array v1, v4, [Ljava/lang/Object; // type@0007
104// |0005: const/4 v2, #int 0 // #0
105
106// 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],
107// |0006: new-instance v3, Ljava/lang/Object; // type@0003
108
109// [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],
110// |0008: +invoke-object-init/range {}, Ljava/lang/Object;.<init>:()V // method@0005
111// |000b: const/4 v4, #int 2 // #2
112
113// 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],
114// |000c: aput-object v3, v1, v4
115
116// 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],
117// |000e: aput-object v3, v1, v6
118
119// 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],
120// |0010: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
121
122// 0:[Conflict],1:[Conflict],2:[Conflict],3:[Reference: java.lang.Object],4:[Conflict],5:[Conflict],6:[Conflict],7:[Conflict],8:[Conflict],
123// |0013: return-object v3
124// |0014: move-exception v0
125
126// 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],
127// |0015: if-nez v2, 001f // +000a
128// |0017: const/4 v4, #int 1 // #1
129
130// 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],
131// |0018: new-instance v5, Ljava/lang/Object; // type@0003
132
133// 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],
134// |001a: +invoke-object-init/range {}, Ljava/lang/Object;.<init>:()V // method@0005
135
136// 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],
137// |001d: aput-object v5, v1, v4
138
139// 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],
140// |001f: aput-object v2, v1, v6
141
142// 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],
143// |0021: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
144// |0024: move-object v3, v2
145
146// 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],
147// |0025: goto 0013 // -0012
148// |0026: move-exception v4
149
150// 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],
151// |0027: aput-object v2, v1, v6
152
153// 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],
154// |0029: +invoke-virtual-quick {v8, v7}, [000c] // vtable #000c
155
156// 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],
157// |002c: throw v4
158// |002d: move-exception v4
159// |002e: move-object v2, v3
160
161// 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],
162// |002f: goto 0027 // -0008
163// |0030: move-exception v0
164// |0031: move-object v2, v3
165
166// 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],
167// |0032: goto 0015 // -001d
168// catches : 3
169// 0x0006 - 0x000b
170// Ljava/lang/Exception; -> 0x0014
171// <any> -> 0x0026
172// 0x000c - 0x000e
173// Ljava/lang/Exception; -> 0x0030
174// <any> -> 0x002d
175// 0x0018 - 0x001f
176// <any> -> 0x0026
177// positions :
178// 0x0003 line=8
179// 0x0005 line=9
180// 0x0006 line=11
181// 0x000b line=12
182// 0x000e line=18
183// 0x0010 line=19
184// 0x0013 line=21
185// 0x0014 line=13
186// 0x0015 line=14
187// 0x0017 line=15
188// 0x001f line=18
189// 0x0021 line=19
190// 0x0025 line=20
191// 0x0026 line=18
192// 0x0029 line=19
193// 0x002d line=18
194// 0x0030 line=13
195// locals :
196// 0x0006 - 0x000b reg=2 y Ljava/lang/Object;
197// 0x000b - 0x0013 reg=3 y Ljava/lang/Object;
198// 0x0014 - 0x0015 reg=2 y Ljava/lang/Object;
199// 0x0015 - 0x0026 reg=0 ex Ljava/lang/Exception;
200// 0x002d - 0x0032 reg=3 y Ljava/lang/Object;
201// 0x0005 - 0x0033 reg=1 x [Ljava/lang/Object;
202// 0x0032 - 0x0033 reg=2 y Ljava/lang/Object;
203// 0x0000 - 0x0033 reg=8 this LReferenceMap;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700204
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700205extern "C" JNIEXPORT jint JNICALL Java_Main_refmap(JNIEnv*, jobject, jint count) {
Shih-wei Liao63433ba2011-10-15 18:40:39 -0700206 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800207 ScopedObjectAccess soa(Thread::Current());
208 ReferenceMap2Visitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700209 mapper.WalkStack();
Shih-wei Liao63433ba2011-10-15 18:40:39 -0700210
211 return count + 1;
212}
213
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700214} // namespace art