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 | |
| 20 | #include "gc_map.h" |
| 21 | #include "mirror/art_method-inl.h" |
| 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: |
| 31 | explicit CheckReferenceMapVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 32 | : StackVisitor(thread, nullptr) {} |
| 33 | |
| 34 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 35 | mirror::ArtMethod* m = GetMethod(); |
| 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) |
| 55 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | a7dd038 | 2014-11-20 17:08:58 -0800 | [diff] [blame] | 56 | if (GetMethod()->IsOptimized(sizeof(void*))) { |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 57 | CheckOptimizedMethod(registers, number_of_references, native_pc_offset); |
| 58 | } else { |
| 59 | CheckQuickMethod(registers, number_of_references, native_pc_offset); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset) |
| 65 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 66 | mirror::ArtMethod* m = GetMethod(); |
| 67 | CodeInfo code_info = m->GetOptimizedCodeInfo(); |
| 68 | StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset); |
| 69 | DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map, m->GetCodeItem()->registers_size_); |
| 70 | MemoryRegion stack_mask = stack_map.GetStackMask(); |
| 71 | uint32_t register_mask = stack_map.GetRegisterMask(); |
| 72 | for (int i = 0; i < number_of_references; ++i) { |
| 73 | int reg = registers[i]; |
| 74 | CHECK(reg < m->GetCodeItem()->registers_size_); |
| 75 | DexRegisterMap::LocationKind location = dex_register_map.GetLocationKind(reg); |
| 76 | switch (location) { |
| 77 | case DexRegisterMap::kNone: |
| 78 | // Not set, should not be a reference. |
| 79 | CHECK(false); |
| 80 | break; |
| 81 | case DexRegisterMap::kInStack: |
| 82 | CHECK(stack_mask.LoadBit(dex_register_map.GetValue(reg) >> 2)); |
| 83 | break; |
| 84 | case DexRegisterMap::kInRegister: |
Nicolas Geoffray | 9889396 | 2015-01-21 12:32:32 +0000 | [diff] [blame] | 85 | CHECK_NE(register_mask & (1 << dex_register_map.GetValue(reg)), 0u); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 86 | break; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 87 | case DexRegisterMap::kInFpuRegister: |
| 88 | // In Fpu register, should not be a reference. |
| 89 | CHECK(false); |
| 90 | break; |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 91 | case DexRegisterMap::kConstant: |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 92 | CHECK_EQ(dex_register_map.GetValue(reg), 0); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void CheckQuickMethod(int* registers, int number_of_references, uint32_t native_pc_offset) |
| 99 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 100 | mirror::ArtMethod* m = GetMethod(); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 101 | NativePcOffsetToReferenceMap map(m->GetNativeGcMap(sizeof(void*))); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 102 | const uint8_t* ref_bitmap = map.FindBitMap(native_pc_offset); |
| 103 | CHECK(ref_bitmap); |
| 104 | for (int i = 0; i < number_of_references; ++i) { |
| 105 | int reg = registers[i]; |
| 106 | CHECK(reg < m->GetCodeItem()->registers_size_); |
| 107 | CHECK((*((ref_bitmap) + reg / 8) >> (reg % 8) ) & 0x01) |
| 108 | << "Error: Reg @" << i << " is not in GC map"; |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | } // namespace art |
| 114 | |
Nicolas Geoffray | 48a8961 | 2014-09-17 10:19:01 +0100 | [diff] [blame] | 115 | #endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_ |