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" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 21 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 22 | #include "scoped_thread_state_change.h" |
| 23 | #include "stack_map.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Helper class for tests checking that the compiler keeps track of dex registers |
| 28 | // holding references. |
| 29 | class CheckReferenceMapVisitor : public StackVisitor { |
| 30 | public: |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 31 | explicit CheckReferenceMapVisitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 32 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {} |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 33 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 34 | bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | ArtMethod* m = GetMethod(); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 36 | if (m->IsCalleeSaveMethod() || m->IsNative()) { |
| 37 | CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex); |
| 38 | } |
| 39 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 40 | if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) { |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | |
| 44 | LOG(INFO) << "At " << PrettyMethod(m, false); |
| 45 | |
| 46 | if (m->IsCalleeSaveMethod()) { |
| 47 | LOG(WARNING) << "no PC for " << PrettyMethod(m); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 55 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 56 | CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized()); |
| 57 | CheckOptimizedMethod(registers, number_of_references, native_pc_offset); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
| 61 | void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 62 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 63 | ArtMethod* m = GetMethod(); |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 64 | CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo(); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 65 | CodeInfoEncoding encoding = code_info.ExtractEncoding(); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 66 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding); |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 67 | uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 68 | DexRegisterMap dex_register_map = |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 69 | code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 70 | uint32_t register_mask = stack_map.GetRegisterMask(encoding.stack_map_encoding); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 71 | for (int i = 0; i < number_of_references; ++i) { |
| 72 | int reg = registers[i]; |
| 73 | CHECK(reg < m->GetCodeItem()->registers_size_); |
David Brazdil | f677ebf | 2015-05-29 16:29:43 +0100 | [diff] [blame] | 74 | DexRegisterLocation location = dex_register_map.GetDexRegisterLocation( |
| 75 | reg, number_of_dex_registers, code_info, encoding); |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 76 | switch (location.GetKind()) { |
| 77 | case DexRegisterLocation::Kind::kNone: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 78 | // Not set, should not be a reference. |
| 79 | CHECK(false); |
| 80 | break; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 81 | case DexRegisterLocation::Kind::kInStack: |
| 82 | DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0); |
David Srbecky | 09ed098 | 2016-02-12 21:58:43 +0000 | [diff] [blame] | 83 | CHECK(stack_map.GetStackMaskBit(encoding.stack_map_encoding, |
| 84 | location.GetValue() / kFrameSlotSize)); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 85 | break; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 86 | case DexRegisterLocation::Kind::kInRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 87 | case DexRegisterLocation::Kind::kInRegisterHigh: |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 88 | CHECK_NE(register_mask & (1 << location.GetValue()), 0u); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 89 | break; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 90 | case DexRegisterLocation::Kind::kInFpuRegister: |
David Brazdil | d9cb68e | 2015-08-25 13:52:43 +0100 | [diff] [blame] | 91 | case DexRegisterLocation::Kind::kInFpuRegisterHigh: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 92 | // In Fpu register, 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::kConstant: |
| 96 | CHECK_EQ(location.GetValue(), 0); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 97 | break; |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 98 | default: |
David Srbecky | 7dc1178 | 2016-02-25 13:23:56 +0000 | [diff] [blame] | 99 | LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind(); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace art |
| 106 | |
Nicolas Geoffray | 48a8961 | 2014-09-17 10:19:01 +0100 | [diff] [blame] | 107 | #endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_ |