12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 1663433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 17ba17f458e20d82b197536d62e1d61c5300da4932Mathieu Chartier#include "art_method-inl.h" 18e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray#include "check_reference_map_visitor.h" 1963433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao#include "jni.h" 2063433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 2163433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liaonamespace art { 2263433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 23524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray#define CHECK_REGS_CONTAIN_REFS(dex_pc, abort_if_not_found, ...) do { \ 24524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray int t[] = {__VA_ARGS__}; \ 25524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray int t_size = sizeof(t) / sizeof(*t); \ 26524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray const OatQuickMethodHeader* method_header = GetCurrentOatQuickMethodHeader(); \ 27524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray uintptr_t native_quick_pc = method_header->ToNativeQuickPc(GetMethod(), \ 28524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray dex_pc, \ 29524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray /* is_catch_handler */ false, \ 30524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray abort_if_not_found); \ 31524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray if (native_quick_pc != UINTPTR_MAX) { \ 32524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray CheckReferences(t, t_size, method_header->NativeQuickPcOffset(native_quick_pc)); \ 33524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray } \ 34e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray} while (false); 3563433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 36e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffraystruct ReferenceMap2Visitor : public CheckReferenceMapVisitor { 3790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier explicit ReferenceMap2Visitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_) 38e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray : CheckReferenceMapVisitor(thread) {} 3963433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 4090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { 41e982f0b8e809cece6f460fa2d8df25873aa69de4Nicolas Geoffray if (CheckReferenceMapVisitor::VisitFrame()) { 42530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 4363433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao } 44e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier ArtMethod* m = GetMethod(); 45bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier std::string m_name(m->GetName()); 4663433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 4763433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao // Given the method name and the number of times the method has been called, 4863433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao // we know the Dex registers with live reference values. Assert that what we 4963433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao // find is what is expected. 5063433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao if (m_name.compare("f") == 0) { 51de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x03U, true, 8); // v8: this 52de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x06U, true, 8, 1); // v8: this, v1: x 53de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x0cU, true, 8, 3, 1); // v8: this, v3: y, v1: x 54de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x10U, true, 8, 3, 1); // v8: this, v3: y, v1: x 55371814fd131d5b7b54a5839d292240f9c4ed4160Shih-wei Liao // v2 is added because of the instruction at DexPC 0024. Object merges with 0 is Object. See: 56371814fd131d5b7b54a5839d292240f9c4ed4160Shih-wei Liao // 0024: move-object v3, v2 57371814fd131d5b7b54a5839d292240f9c4ed4160Shih-wei Liao // 0025: goto 0013 588b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko // Detailed dex instructions for ReferenceMap.java are at the end of this function. 590f055d11096cb02563e9c040cd03c791fd8f69a3Brian Carlstrom // CHECK_REGS_CONTAIN_REFS(8, 3, 2, 1); // v8: this, v3: y, v2: y, v1: x 60de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray // We eliminate the non-live registers at a return, so only v3 is live. 61de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray // Note that it is OK for a compiler to not have a dex map at this dex PC because 628b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko // a return is not necessarily a safepoint. 63c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz CHECK_REGS_CONTAIN_REFS(0x14U, false, 2); // v2: y 64767c752fddc64e280dba507457e4f06002b5f678Vladimir Marko // Note that v0: ex can be eliminated because it's a dead merge of two different exceptions. 65767c752fddc64e280dba507457e4f06002b5f678Vladimir Marko CHECK_REGS_CONTAIN_REFS(0x18U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex) 66c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz CHECK_REGS_CONTAIN_REFS(0x22U, true, 8, 2, 1); // v8: this, v2: y, v1: x (dead v0: ex) 67e0395dd58454e27fc47c0ca273913929fb658e6cNicolas Geoffray 68524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray if (!GetCurrentOatQuickMethodHeader()->IsOptimized()) { 69e0395dd58454e27fc47c0ca273913929fb658e6cNicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x27U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x 70e0395dd58454e27fc47c0ca273913929fb658e6cNicolas Geoffray } 71de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x29U, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x 72de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray CHECK_REGS_CONTAIN_REFS(0x2cU, true, 8, 4, 2, 1); // v8: this, v4: ex, v2: y, v1: x 738b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko // Note that it is OK for a compiler to not have a dex map at these two dex PCs because 748b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko // a goto is not necessarily a safepoint. 758b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko CHECK_REGS_CONTAIN_REFS(0x2fU, false, 8, 4, 3, 2, 1); // v8: this, v4: ex, v3: y, v2: y, v1: x 768b858e16563ebf8e522df026a6ab409f1bd9b3deVladimir Marko CHECK_REGS_CONTAIN_REFS(0x32U, false, 8, 3, 2, 1, 0); // v8: this, v3: y, v2: y, v1: x, v0: ex 7763433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao } 78530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes 79530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes return true; 8063433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao } 8163433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao}; 8263433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 83c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// DEX code 84371814fd131d5b7b54a5839d292240f9c4ed4160Shih-wei Liao// 85c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0000: const/4 v4, #int 2 // #2 86c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0001: const/4 v7, #int 0 // #0 87c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0002: const/4 v6, #int 1 // #1 88c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0003: new-array v1, v4, [Ljava/lang/Object; // type@0007 89c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0005: const/4 v2, #int 0 // #0 90c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0006: new-instance v3, Ljava/lang/Object; // type@0003 91c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0008: invoke-direct {v3}, Ljava/lang/Object;.<init>:()V // method@0004 92c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 000b: const/4 v4, #int 2 // #2 93c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 000c: aput-object v3, v1, v4 94c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 000e: aput-object v3, v1, v6 95c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0010: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 96c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0013: move-object v2, v3 97c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0014: return-object v2 98c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0015: move-exception v0 99c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0016: if-nez v2, 0020 // +000a 100c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0018: new-instance v4, Ljava/lang/Object; // type@0003 101c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 001a: invoke-direct {v4}, Ljava/lang/Object;.<init>:()V // method@0004 102c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 001d: const/4 v5, #int 1 // #1 103c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 001e: aput-object v4, v1, v5 104c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0020: aput-object v2, v1, v6 105c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0022: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 106c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0025: goto 0014 // -0011 107c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0026: move-exception v4 108c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0027: aput-object v2, v1, v6 109c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0029: invoke-virtual {v8, v7}, LMain;.refmap:(I)I // method@0003 110c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 002c: throw v4 111c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 002d: move-exception v4 112c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 002e: move-object v2, v3 113c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 002f: goto 0027 // -0008 114c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0030: move-exception v0 115c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0031: move-object v2, v3 116c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0032: goto 0016 // -001c 117c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// catches : 3 118c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0006 - 0x000b 119c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// Ljava/lang/Exception; -> 0x0015 120c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// <any> -> 0x0026 121c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x000c - 0x000e 122c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// Ljava/lang/Exception; -> 0x0030 123c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// <any> -> 0x002d 124c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0018 - 0x0020 125c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// <any> -> 0x0026 126c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// positions : 127c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0003 line=22 128c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0005 line=23 129c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0006 line=25 130c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x000b line=26 131c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x000e line=32 132c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0010 line=33 133c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0014 line=35 134c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0015 line=27 135c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0016 line=28 136c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0018 line=29 137c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0020 line=32 138c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0022 line=33 139c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0026 line=31 140c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0027 line=32 141c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0029 line=33 142c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x002c line=31 143c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0030 line=27 144c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// locals : 145c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0006 - 0x000b reg=2 y Ljava/lang/Object; 146c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x000b - 0x0014 reg=3 y Ljava/lang/Object; 147c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0015 - 0x0016 reg=2 y Ljava/lang/Object; 148c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0016 - 0x0026 reg=0 ex Ljava/lang/Exception; 149c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x002d - 0x002f reg=3 y Ljava/lang/Object; 150c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x002f - 0x0030 reg=2 y Ljava/lang/Object; 151c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0030 - 0x0032 reg=3 y Ljava/lang/Object; 152c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0031 - 0x0033 reg=0 ex Ljava/lang/Exception; 153c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0005 - 0x0033 reg=1 x [Ljava/lang/Object; 154c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0032 - 0x0033 reg=2 y Ljava/lang/Object; 155c94ff128d770f5126d7685972b95a789c0b55cb0Sebastien Hertz// 0x0000 - 0x0033 reg=8 this LMain; 156371814fd131d5b7b54a5839d292240f9c4ed4160Shih-wei Liao 1571c83cbc4a817acbd7f9abb5b29a2d418a958e6a1Andreas Gampeextern "C" JNIEXPORT jint JNICALL Java_Main_refmap(JNIEnv*, jobject, jint count) { 15863433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao // Visitor 1597a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers ScopedObjectAccess soa(Thread::Current()); 1607a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers ReferenceMap2Visitor mapper(soa.Self()); 1610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers mapper.WalkStack(); 16263433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 16363433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao return count + 1; 16463433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao} 16563433ba79fbe063881c263f8b348fb990c25a83eShih-wei Liao 1660cd7ec2dcd8d7ba30bf3ca420b40dac52849876cBrian Carlstrom} // namespace art 167