| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [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 |  */ | 
 | 16 |  | 
 | 17 | #ifndef ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_ | 
 | 18 | #define ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_ | 
 | 19 |  | 
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" | 
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 21 | #include "dex/code_item_accessors-inl.h" | 
 | 22 | #include "dex/dex_file_types.h" | 
| Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 23 | #include "oat_quick_method_header.h" | 
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" | 
| Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 25 | #include "stack.h" | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 26 | #include "stack_map.h" | 
 | 27 |  | 
 | 28 | namespace art { | 
 | 29 |  | 
 | 30 | // Helper class for tests checking that the compiler keeps track of dex registers | 
 | 31 | // holding references. | 
 | 32 | class CheckReferenceMapVisitor : public StackVisitor { | 
 | 33 |  public: | 
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 34 |   explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) | 
| Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 35 |       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 36 |  | 
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 37 |   bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) { | 
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 |     ArtMethod* m = GetMethod(); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 39 |     if (m->IsCalleeSaveMethod() || m->IsNative()) { | 
| Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 40 |       CHECK_EQ(GetDexPc(), dex::kDexNoIndex); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 41 |     } | 
 | 42 |  | 
| Nicolas Geoffray | 013d1ee | 2019-12-04 16:18:15 +0000 | [diff] [blame] | 43 |     // If the method is not compiled, continue the stack walk. | 
 | 44 |     if (m == nullptr || | 
 | 45 |         m->IsNative() || | 
 | 46 |         m->IsRuntimeMethod() || | 
 | 47 |         IsShadowFrame() || | 
 | 48 |         !GetCurrentOatQuickMethodHeader()->IsOptimized()) { | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 49 |       return true; | 
 | 50 |     } | 
 | 51 |  | 
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 52 |     LOG(INFO) << "At " << m->PrettyMethod(false); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 53 |  | 
 | 54 |     if (m->IsCalleeSaveMethod()) { | 
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 55 |       LOG(WARNING) << "no PC for " << m->PrettyMethod(); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 56 |       return true; | 
 | 57 |     } | 
 | 58 |  | 
 | 59 |     return false; | 
 | 60 |   } | 
 | 61 |  | 
 | 62 |   void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset) | 
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 |       REQUIRES_SHARED(Locks::mutator_lock_) { | 
| Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 64 |     CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized()); | 
 | 65 |     CheckOptimizedMethod(registers, number_of_references, native_pc_offset); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 66 |   } | 
 | 67 |  | 
 | 68 |  private: | 
 | 69 |   void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset) | 
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 |       REQUIRES_SHARED(Locks::mutator_lock_) { | 
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 71 |     ArtMethod* m = GetMethod(); | 
| David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 72 |     CodeInfo code_info(GetCurrentOatQuickMethodHeader()); | 
 | 73 |     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset); | 
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 74 |     CodeItemDataAccessor accessor(m->DexInstructionData()); | 
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 75 |     uint16_t number_of_dex_registers = accessor.RegistersSize(); | 
| Artem Serov | 2808be8 | 2018-12-20 19:15:11 +0000 | [diff] [blame] | 76 |  | 
 | 77 |     if (!Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) { | 
 | 78 |       // We can only guarantee dex register info presence for debuggable methods. | 
 | 79 |       return; | 
 | 80 |     } | 
 | 81 |  | 
| David Srbecky | fd89b07 | 2018-06-03 12:00:22 +0100 | [diff] [blame] | 82 |     DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map); | 
 | 83 |     DCHECK_EQ(dex_register_map.size(), number_of_dex_registers); | 
| David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 84 |     uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map); | 
 | 85 |     BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 86 |     for (int i = 0; i < number_of_references; ++i) { | 
 | 87 |       int reg = registers[i]; | 
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 88 |       CHECK_LT(reg, accessor.RegistersSize()); | 
| David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 89 |       DexRegisterLocation location = dex_register_map[reg]; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 90 |       switch (location.GetKind()) { | 
 | 91 |         case DexRegisterLocation::Kind::kNone: | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 92 |           // Not set, should not be a reference. | 
 | 93 |           CHECK(false); | 
 | 94 |           break; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 95 |         case DexRegisterLocation::Kind::kInStack: | 
 | 96 |           DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0); | 
| David Srbecky | 45aa598 | 2016-03-18 02:15:09 +0000 | [diff] [blame] | 97 |           CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize)); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 98 |           break; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 99 |         case DexRegisterLocation::Kind::kInRegister: | 
| David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 100 |         case DexRegisterLocation::Kind::kInRegisterHigh: | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 101 |           CHECK_NE(register_mask & (1 << location.GetValue()), 0u); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 102 |           break; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 103 |         case DexRegisterLocation::Kind::kInFpuRegister: | 
| David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 104 |         case DexRegisterLocation::Kind::kInFpuRegisterHigh: | 
| Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 105 |           // In Fpu register, should not be a reference. | 
 | 106 |           CHECK(false); | 
 | 107 |           break; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 108 |         case DexRegisterLocation::Kind::kConstant: | 
 | 109 |           CHECK_EQ(location.GetValue(), 0); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 110 |           break; | 
| Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 111 |         default: | 
| David Srbecky | e140212 | 2018-06-13 18:20:45 +0100 | [diff] [blame] | 112 |           LOG(FATAL) << "Unexpected location kind " << location.GetKind(); | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 113 |       } | 
 | 114 |     } | 
 | 115 |   } | 
| Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 116 | }; | 
 | 117 |  | 
 | 118 | }  // namespace art | 
 | 119 |  | 
| Nicolas Geoffray | 48a8961 | 2014-09-17 10:19:01 +0100 | [diff] [blame] | 120 | #endif  // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_ |