Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 16 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 17 | #include "check_reference_map_visitor.h" |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 18 | #include "jni.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 22 | #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 Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 33 | } while (false); |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 34 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 35 | struct ReferenceMap2Visitor : public CheckReferenceMapVisitor { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 36 | explicit ReferenceMap2Visitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 37 | : CheckReferenceMapVisitor(thread) {} |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 38 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 39 | bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 40 | if (CheckReferenceMapVisitor::VisitFrame()) { |
| 41 | return true; |
| 42 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | ArtMethod* m = GetMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 44 | std::string m_name(m->GetName()); |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 45 | |
| 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 Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 50 | CHECK_REGS_CONTAIN_REFS(0x03U, true, 8); // v8: this |
| 51 | CHECK_REGS_CONTAIN_REFS(0x06U, true, 8, 1); // v8: this, v1: x |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 52 | CHECK_REGS_CONTAIN_REFS(0x0cU, true, 8, 3, 1); // v8: this, v3: y, v1: x |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 53 | CHECK_REGS_CONTAIN_REFS(0x10U, true, 8, 3, 1); // v8: this, v3: y, v1: x |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 54 | // v2 is added because of the instruction at DexPC 0024. Object merges with 0 is Object. See: |
| 55 | // 0024: move-object v3, v2 |
| 56 | // 0025: goto 0013 |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 57 | // Detailed dex instructions for ReferenceMap.java are at the end of this function. |
Brian Carlstrom | 0f055d1 | 2013-07-26 12:46:02 -0700 | [diff] [blame] | 58 | // CHECK_REGS_CONTAIN_REFS(8, 3, 2, 1); // v8: this, v3: y, v2: y, v1: x |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 59 | // We eliminate the non-live registers at a return, so only v3 is live. |
| 60 | // Note that it is OK for a compiler to not have a dex map at this dex PC because |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 61 | // a return is not necessarily a safepoint. |
Sebastien Hertz | c94ff12 | 2016-02-29 16:54:16 +0100 | [diff] [blame] | 62 | CHECK_REGS_CONTAIN_REFS(0x14U, false, 2); // v2: y |
Vladimir Marko | 767c752 | 2015-03-20 12:47:30 +0000 | [diff] [blame] | 63 | // Note that v0: ex can be eliminated because it's a dead merge of two different exceptions. |
| 64 | CHECK_REGS_CONTAIN_REFS(0x18U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex) |
Sebastien Hertz | c94ff12 | 2016-02-29 16:54:16 +0100 | [diff] [blame] | 65 | CHECK_REGS_CONTAIN_REFS(0x22U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex) |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 66 | |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 67 | if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) { |
Nicolas Geoffray | e0395dd | 2015-09-25 11:04:45 +0100 | [diff] [blame] | 68 | CHECK_REGS_CONTAIN_REFS(0x27U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x |
| 69 | } |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 70 | CHECK_REGS_CONTAIN_REFS(0x29U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x |
| 71 | CHECK_REGS_CONTAIN_REFS(0x2cU, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 72 | // Note that it is OK for a compiler to not have a dex map at these two dex PCs because |
| 73 | // a goto is not necessarily a safepoint. |
| 74 | CHECK_REGS_CONTAIN_REFS(0x2fU, false, 8, 4, 3, 2, 1); // v8: this, v4: ex, v3: y, v2: y, v1: x |
| 75 | CHECK_REGS_CONTAIN_REFS(0x32U, false, 8, 3, 2, 1, 0); // v8: this, v3: y, v2: y, v1: x, v0: ex |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 76 | } |
Elliott Hughes | 530fa00 | 2012-03-12 11:44:49 -0700 | [diff] [blame] | 77 | |
| 78 | return true; |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
Sebastien Hertz | c94ff12 | 2016-02-29 16:54:16 +0100 | [diff] [blame] | 82 | // DEX code |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 83 | // |
Sebastien Hertz | c94ff12 | 2016-02-29 16:54:16 +0100 | [diff] [blame] | 84 | // 0000: const/4 v4, #int 2 // #2 |
| 85 | // 0001: const/4 v7, #int 0 // #0 |
| 86 | // 0002: const/4 v6, #int 1 // #1 |
| 87 | // 0003: new-array v1, v4, [Ljava/lang/Object; // type@0007 |
| 88 | // 0005: const/4 v2, #int 0 // #0 |
| 89 | // 0006: new-instance v3, Ljava/lang/Object; // type@0003 |
| 90 | // 0008: invoke-direct {v3}, Ljava/lang/Object;.<init>:()V // method@0004 |
| 91 | // 000b: const/4 v4, #int 2 // #2 |
| 92 | // 000c: aput-object v3, v1, v4 |
| 93 | // 000e: aput-object v3, v1, v6 |
| 94 | // 0010: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 |
| 95 | // 0013: move-object v2, v3 |
| 96 | // 0014: return-object v2 |
| 97 | // 0015: move-exception v0 |
| 98 | // 0016: if-nez v2, 0020 // +000a |
| 99 | // 0018: new-instance v4, Ljava/lang/Object; // type@0003 |
| 100 | // 001a: invoke-direct {v4}, Ljava/lang/Object;.<init>:()V // method@0004 |
| 101 | // 001d: const/4 v5, #int 1 // #1 |
| 102 | // 001e: aput-object v4, v1, v5 |
| 103 | // 0020: aput-object v2, v1, v6 |
| 104 | // 0022: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 |
| 105 | // 0025: goto 0014 // -0011 |
| 106 | // 0026: move-exception v4 |
| 107 | // 0027: aput-object v2, v1, v6 |
| 108 | // 0029: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 |
| 109 | // 002c: throw v4 |
| 110 | // 002d: move-exception v4 |
| 111 | // 002e: move-object v2, v3 |
| 112 | // 002f: goto 0027 // -0008 |
| 113 | // 0030: move-exception v0 |
| 114 | // 0031: move-object v2, v3 |
| 115 | // 0032: goto 0016 // -001c |
| 116 | // catches : 3 |
| 117 | // 0x0006 - 0x000b |
| 118 | // Ljava/lang/Exception; -> 0x0015 |
| 119 | // <any> -> 0x0026 |
| 120 | // 0x000c - 0x000e |
| 121 | // Ljava/lang/Exception; -> 0x0030 |
| 122 | // <any> -> 0x002d |
| 123 | // 0x0018 - 0x0020 |
| 124 | // <any> -> 0x0026 |
| 125 | // positions : |
| 126 | // 0x0003 line=22 |
| 127 | // 0x0005 line=23 |
| 128 | // 0x0006 line=25 |
| 129 | // 0x000b line=26 |
| 130 | // 0x000e line=32 |
| 131 | // 0x0010 line=33 |
| 132 | // 0x0014 line=35 |
| 133 | // 0x0015 line=27 |
| 134 | // 0x0016 line=28 |
| 135 | // 0x0018 line=29 |
| 136 | // 0x0020 line=32 |
| 137 | // 0x0022 line=33 |
| 138 | // 0x0026 line=31 |
| 139 | // 0x0027 line=32 |
| 140 | // 0x0029 line=33 |
| 141 | // 0x002c line=31 |
| 142 | // 0x0030 line=27 |
| 143 | // locals : |
| 144 | // 0x0006 - 0x000b reg=2 y Ljava/lang/Object; |
| 145 | // 0x000b - 0x0014 reg=3 y Ljava/lang/Object; |
| 146 | // 0x0015 - 0x0016 reg=2 y Ljava/lang/Object; |
| 147 | // 0x0016 - 0x0026 reg=0 ex Ljava/lang/Exception; |
| 148 | // 0x002d - 0x002f reg=3 y Ljava/lang/Object; |
| 149 | // 0x002f - 0x0030 reg=2 y Ljava/lang/Object; |
| 150 | // 0x0030 - 0x0032 reg=3 y Ljava/lang/Object; |
| 151 | // 0x0031 - 0x0033 reg=0 ex Ljava/lang/Exception; |
| 152 | // 0x0005 - 0x0033 reg=1 x [Ljava/lang/Object; |
| 153 | // 0x0032 - 0x0033 reg=2 y Ljava/lang/Object; |
| 154 | // 0x0000 - 0x0033 reg=8 this LMain; |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 155 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 156 | extern "C" JNIEXPORT jint JNICALL Java_Main_refmap(JNIEnv*, jobject, jint count) { |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 157 | // Visitor |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 158 | ScopedObjectAccess soa(Thread::Current()); |
| 159 | ReferenceMap2Visitor mapper(soa.Self()); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 160 | mapper.WalkStack(); |
Shih-wei Liao | 63433ba | 2011-10-15 18:40:39 -0700 | [diff] [blame] | 161 | |
| 162 | return count + 1; |
| 163 | } |
| 164 | |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 165 | } // namespace art |