blob: 2d26fa1ac9456662bbe3747df23585c0ac0500cc [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
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010017#include "check_reference_map_visitor.h"
Shih-wei Liao63433ba2011-10-15 18:40:39 -070018#include "jni.h"
19
20namespace art {
21
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010022#define CHECK_REGS_CONTAIN_REFS(dex_pc, abort_if_not_found, ...) do { \
23 int t[] = {__VA_ARGS__}; \
24 int t_size = sizeof(t) / sizeof(*t); \
25 const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); \
26 uintptr_t native_quick_pc = method_header->ToNativeQuickPc(GetMethod(), \
27 dex_pc, \
28 /* is_catch_handler */ false, \
29 abort_if_not_found); \
30 if (native_quick_pc != UINTPTR_MAX) { \
31 CheckReferences(t, t_size, method_header->NativeQuickPcOffset(native_quick_pc)); \
32 } \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010033} while (false);
Shih-wei Liao63433ba2011-10-15 18:40:39 -070034
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010035struct ReferenceMap2Visitor : public CheckReferenceMapVisitor {
Mathieu Chartier90443472015-07-16 20:32:27 -070036 explicit ReferenceMap2Visitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010037 : CheckReferenceMapVisitor(thread) {}
Shih-wei Liao63433ba2011-10-15 18:40:39 -070038
Mathieu Chartier90443472015-07-16 20:32:27 -070039 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010040 if (CheckReferenceMapVisitor::VisitFrame()) {
41 return true;
42 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* m = GetMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -070044 std::string m_name(m->GetName());
Shih-wei Liao63433ba2011-10-15 18:40:39 -070045
46 // Given the method name and the number of times the method has been called,
47 // we know the Dex registers with live reference values. Assert that what we
48 // find is what is expected.
49 if (m_name.compare("f") == 0) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000050 CHECK_REGS_CONTAIN_REFS(0x03U, true, 8); // v8: this
51 CHECK_REGS_CONTAIN_REFS(0x06U, true, 8, 1); // v8: this, v1: x
David Brazdil95177982015-10-30 12:56:58 -050052 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
53 CHECK_REGS_CONTAIN_REFS(0x08U, true, 8, 3, 1); // v8: this, v3: y, v1: x
54 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000055 CHECK_REGS_CONTAIN_REFS(0x0cU, true, 8, 3, 1); // v8: this, v3: y, v1: x
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010056 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010057 CHECK_REGS_CONTAIN_REFS(0x0eU, true, 8, 3, 1); // v8: this, v3: y, v1: x
58 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000059 CHECK_REGS_CONTAIN_REFS(0x10U, true, 8, 3, 1); // v8: this, v3: y, v1: x
Shih-wei Liao371814f2011-10-27 16:52:10 -070060 // v2 is added because of the instruction at DexPC 0024. Object merges with 0 is Object. See:
61 // 0024: move-object v3, v2
62 // 0025: goto 0013
Vladimir Marko8b858e12014-11-27 14:52:37 +000063 // Detailed dex instructions for ReferenceMap.java are at the end of this function.
Brian Carlstrom0f055d12013-07-26 12:46:02 -070064 // CHECK_REGS_CONTAIN_REFS(8, 3, 2, 1); // v8: this, v3: y, v2: y, v1: x
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000065 // We eliminate the non-live registers at a return, so only v3 is live.
66 // 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 +000067 // a return is not necessarily a safepoint.
Sebastien Hertzc94ff122016-02-29 16:54:16 +010068 CHECK_REGS_CONTAIN_REFS(0x14U, false, 2); // v2: y
Vladimir Marko767c7522015-03-20 12:47:30 +000069 // Note that v0: ex can be eliminated because it's a dead merge of two different exceptions.
70 CHECK_REGS_CONTAIN_REFS(0x18U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010071 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Sebastien Hertzc94ff122016-02-29 16:54:16 +010072 // v8: this, v4: x[1], v2: y, v1: x (dead v0: ex)
73 CHECK_REGS_CONTAIN_REFS(0x1aU, true, 8, 4, 2, 1);
74 // v8: this, v4: x[1], v2: y, v1: x (dead v0: ex)
75 CHECK_REGS_CONTAIN_REFS(0x1eU, true, 8, 4, 2, 1);
76 // v4 is removed from the root set because there is a "merge" operation.
77 // See 0016: if-nez v2, 0020.
78 CHECK_REGS_CONTAIN_REFS(0x20U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010079 }
Sebastien Hertzc94ff122016-02-29 16:54:16 +010080 CHECK_REGS_CONTAIN_REFS(0x22U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex)
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010081
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010082 if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +010083 CHECK_REGS_CONTAIN_REFS(0x27U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
84 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +000085 CHECK_REGS_CONTAIN_REFS(0x29U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x
86 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 +000087 // Note that it is OK for a compiler to not have a dex map at these two dex PCs because
88 // a goto is not necessarily a safepoint.
89 CHECK_REGS_CONTAIN_REFS(0x2fU, false, 8, 4, 3, 2, 1); // v8: this, v4: ex, v3: y, v2: y, v1: x
90 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 -070091 }
Elliott Hughes530fa002012-03-12 11:44:49 -070092
93 return true;
Shih-wei Liao63433ba2011-10-15 18:40:39 -070094 }
95};
96
Sebastien Hertzc94ff122016-02-29 16:54:16 +010097// DEX code
Shih-wei Liao371814f2011-10-27 16:52:10 -070098//
Sebastien Hertzc94ff122016-02-29 16:54:16 +010099// 0000: const/4 v4, #int 2 // #2
100// 0001: const/4 v7, #int 0 // #0
101// 0002: const/4 v6, #int 1 // #1
102// 0003: new-array v1, v4, [Ljava/lang/Object; // type@0007
103// 0005: const/4 v2, #int 0 // #0
104// 0006: new-instance v3, Ljava/lang/Object; // type@0003
105// 0008: invoke-direct {v3}, Ljava/lang/Object;.<init>:()V // method@0004
106// 000b: const/4 v4, #int 2 // #2
107// 000c: aput-object v3, v1, v4
108// 000e: aput-object v3, v1, v6
109// 0010: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003
110// 0013: move-object v2, v3
111// 0014: return-object v2
112// 0015: move-exception v0
113// 0016: if-nez v2, 0020 // +000a
114// 0018: new-instance v4, Ljava/lang/Object; // type@0003
115// 001a: invoke-direct {v4}, Ljava/lang/Object;.<init>:()V // method@0004
116// 001d: const/4 v5, #int 1 // #1
117// 001e: aput-object v4, v1, v5
118// 0020: aput-object v2, v1, v6
119// 0022: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003
120// 0025: goto 0014 // -0011
121// 0026: move-exception v4
122// 0027: aput-object v2, v1, v6
123// 0029: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003
124// 002c: throw v4
125// 002d: move-exception v4
126// 002e: move-object v2, v3
127// 002f: goto 0027 // -0008
128// 0030: move-exception v0
129// 0031: move-object v2, v3
130// 0032: goto 0016 // -001c
131// catches : 3
132// 0x0006 - 0x000b
133// Ljava/lang/Exception; -> 0x0015
134// <any> -> 0x0026
135// 0x000c - 0x000e
136// Ljava/lang/Exception; -> 0x0030
137// <any> -> 0x002d
138// 0x0018 - 0x0020
139// <any> -> 0x0026
140// positions :
141// 0x0003 line=22
142// 0x0005 line=23
143// 0x0006 line=25
144// 0x000b line=26
145// 0x000e line=32
146// 0x0010 line=33
147// 0x0014 line=35
148// 0x0015 line=27
149// 0x0016 line=28
150// 0x0018 line=29
151// 0x0020 line=32
152// 0x0022 line=33
153// 0x0026 line=31
154// 0x0027 line=32
155// 0x0029 line=33
156// 0x002c line=31
157// 0x0030 line=27
158// locals :
159// 0x0006 - 0x000b reg=2 y Ljava/lang/Object;
160// 0x000b - 0x0014 reg=3 y Ljava/lang/Object;
161// 0x0015 - 0x0016 reg=2 y Ljava/lang/Object;
162// 0x0016 - 0x0026 reg=0 ex Ljava/lang/Exception;
163// 0x002d - 0x002f reg=3 y Ljava/lang/Object;
164// 0x002f - 0x0030 reg=2 y Ljava/lang/Object;
165// 0x0030 - 0x0032 reg=3 y Ljava/lang/Object;
166// 0x0031 - 0x0033 reg=0 ex Ljava/lang/Exception;
167// 0x0005 - 0x0033 reg=1 x [Ljava/lang/Object;
168// 0x0032 - 0x0033 reg=2 y Ljava/lang/Object;
169// 0x0000 - 0x0033 reg=8 this LMain;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700170
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700171extern "C" JNIEXPORT jint JNICALL Java_Main_refmap(JNIEnv*, jobject, jint count) {
Shih-wei Liao63433ba2011-10-15 18:40:39 -0700172 // Visitor
Ian Rogers7a22fa62013-01-23 12:16:16 -0800173 ScopedObjectAccess soa(Thread::Current());
174 ReferenceMap2Visitor mapper(soa.Self());
Ian Rogers0399dde2012-06-06 17:09:28 -0700175 mapper.WalkStack();
Shih-wei Liao63433ba2011-10-15 18:40:39 -0700176
177 return count + 1;
178}
179
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700180} // namespace art