blob: 504b7536f6dfb811c38527352bbcc943e32f1d23 [file] [log] [blame]
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001/*
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 Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010021#include "gc_map.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010022#include "scoped_thread_state_change.h"
23#include "stack_map.h"
24
25namespace art {
26
27// Helper class for tests checking that the compiler keeps track of dex registers
28// holding references.
29class CheckReferenceMapVisitor : public StackVisitor {
30 public:
31 explicit CheckReferenceMapVisitor(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010032 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010033
34 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070035 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010036 if (m->IsCalleeSaveMethod() || m->IsNative()) {
37 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
38 }
39
Ian Rogerscf7f1912014-10-22 22:06:39 -070040 if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010041 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 Chartiera7dd0382014-11-20 17:08:58 -080056 if (GetMethod()->IsOptimized(sizeof(void*))) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010057 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_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010067 CodeInfo code_info = m->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +010068 StackMapEncoding encoding = code_info.ExtractEncoding();
69 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000070 uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000071 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010072 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
73 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
74 uint32_t register_mask = stack_map.GetRegisterMask(encoding);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010075 for (int i = 0; i < number_of_references; ++i) {
76 int reg = registers[i];
77 CHECK(reg < m->GetCodeItem()->registers_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +010078 DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
79 reg, number_of_dex_registers, code_info, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000080 switch (location.GetKind()) {
81 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010082 // Not set, should not be a reference.
83 CHECK(false);
84 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000085 case DexRegisterLocation::Kind::kInStack:
86 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
87 CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010088 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000089 case DexRegisterLocation::Kind::kInRegister:
90 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010091 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000092 case DexRegisterLocation::Kind::kInFpuRegister:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010093 // In Fpu register, should not be a reference.
94 CHECK(false);
95 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000096 case DexRegisterLocation::Kind::kConstant:
97 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010098 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000099 default:
100 LOG(FATAL) << "Unexpected location kind"
101 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100102 }
103 }
104 }
105
106 void CheckQuickMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 ArtMethod* m = GetMethod();
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800109 NativePcOffsetToReferenceMap map(m->GetNativeGcMap(sizeof(void*)));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100110 const uint8_t* ref_bitmap = map.FindBitMap(native_pc_offset);
111 CHECK(ref_bitmap);
112 for (int i = 0; i < number_of_references; ++i) {
113 int reg = registers[i];
114 CHECK(reg < m->GetCodeItem()->registers_size_);
115 CHECK((*((ref_bitmap) + reg / 8) >> (reg % 8) ) & 0x01)
116 << "Error: Reg @" << i << " is not in GC map";
117 }
118 }
119};
120
121} // namespace art
122
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100123#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_