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 "art_method-inl.h"
21 #include "dex/code_item_accessors-inl.h"
22 #include "dex/dex_file_types.h"
23 #include "oat_quick_method_header.h"
24 #include "scoped_thread_state_change-inl.h"
25 #include "stack.h"
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:
CheckReferenceMapVisitor(Thread * thread)34   explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
35       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
36 
VisitFrame()37   bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
38     ArtMethod* m = GetMethod();
39     if (m->IsCalleeSaveMethod() || m->IsNative()) {
40       CHECK_EQ(GetDexPc(), dex::kDexNoIndex);
41     }
42 
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()) {
49       return true;
50     }
51 
52     LOG(INFO) << "At " << m->PrettyMethod(false);
53 
54     if (m->IsCalleeSaveMethod()) {
55       LOG(WARNING) << "no PC for " << m->PrettyMethod();
56       return true;
57     }
58 
59     return false;
60   }
61 
CheckReferences(int * registers,int number_of_references,uint32_t native_pc_offset)62   void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
63       REQUIRES_SHARED(Locks::mutator_lock_) {
64     CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
65     CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
66   }
67 
68  private:
CheckOptimizedMethod(int * registers,int number_of_references,uint32_t native_pc_offset)69   void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
70       REQUIRES_SHARED(Locks::mutator_lock_) {
71     ArtMethod* m = GetMethod();
72     CodeInfo code_info(GetCurrentOatQuickMethodHeader());
73     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
74     CodeItemDataAccessor accessor(m->DexInstructionData());
75     uint16_t number_of_dex_registers = accessor.RegistersSize();
76 
77     if (!Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) {
78       // We can only guarantee dex register info presence for debuggable methods.
79       return;
80     }
81 
82     DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map);
83     DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
84     uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
85     BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
86     for (int i = 0; i < number_of_references; ++i) {
87       int reg = registers[i];
88       CHECK_LT(reg, accessor.RegistersSize());
89       DexRegisterLocation location = dex_register_map[reg];
90       switch (location.GetKind()) {
91         case DexRegisterLocation::Kind::kNone:
92           // Not set, should not be a reference.
93           CHECK(false);
94           break;
95         case DexRegisterLocation::Kind::kInStack:
96           DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
97           CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
98           break;
99         case DexRegisterLocation::Kind::kInRegister:
100         case DexRegisterLocation::Kind::kInRegisterHigh:
101           CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
102           break;
103         case DexRegisterLocation::Kind::kInFpuRegister:
104         case DexRegisterLocation::Kind::kInFpuRegisterHigh:
105           // In Fpu register, should not be a reference.
106           CHECK(false);
107           break;
108         case DexRegisterLocation::Kind::kConstant:
109           CHECK_EQ(location.GetValue(), 0);
110           break;
111         default:
112           LOG(FATAL) << "Unexpected location kind " << location.GetKind();
113       }
114     }
115   }
116 };
117 
118 }  // namespace art
119 
120 #endif  // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_
121