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 "base/macros.h"
22 #include "dex/code_item_accessors-inl.h"
23 #include "dex/dex_file_types.h"
24 #include "oat/oat_quick_method_header.h"
25 #include "oat/stack_map.h"
26 #include "scoped_thread_state_change-inl.h"
27 #include "stack.h"
28 
29 namespace art HIDDEN {
30 
31 // Helper class for tests checking that the compiler keeps track of dex registers
32 // holding references.
33 class CheckReferenceMapVisitor : public StackVisitor {
34  public:
CheckReferenceMapVisitor(Thread * thread)35   explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
36       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
37 
VisitFrame()38   bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
39     ArtMethod* m = GetMethod();
40     if (m->IsCalleeSaveMethod() || m->IsNative()) {
41       CHECK_EQ(GetDexPc(), dex::kDexNoIndex);
42     }
43 
44     // If the method is not compiled, continue the stack walk.
45     if (m == nullptr ||
46         m->IsNative() ||
47         m->IsRuntimeMethod() ||
48         IsShadowFrame() ||
49         !GetCurrentOatQuickMethodHeader()->IsOptimized()) {
50       return true;
51     }
52 
53     LOG(INFO) << "At " << m->PrettyMethod(false);
54 
55     if (m->IsCalleeSaveMethod()) {
56       LOG(WARNING) << "no PC for " << m->PrettyMethod();
57       return true;
58     }
59 
60     return false;
61   }
62 
CheckReferences(int * registers,int number_of_references,uint32_t dex_pc,uint32_t native_pc_offset,bool search_for_valid_stack_map)63   void CheckReferences(int* registers,
64                        int number_of_references,
65                        uint32_t dex_pc,
66                        uint32_t native_pc_offset,
67                        bool search_for_valid_stack_map)
68       REQUIRES_SHARED(Locks::mutator_lock_) {
69     CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
70     CheckOptimizedMethod(
71         registers, number_of_references, dex_pc, native_pc_offset, search_for_valid_stack_map);
72   }
73 
74  private:
CheckOptimizedMethod(int * registers,int number_of_references,uint32_t dex_pc,uint32_t native_pc_offset,bool search_for_valid_stack_map)75   void CheckOptimizedMethod(int* registers,
76                             int number_of_references,
77                             uint32_t dex_pc,
78                             uint32_t native_pc_offset,
79                             bool search_for_valid_stack_map)
80       REQUIRES_SHARED(Locks::mutator_lock_) {
81     ArtMethod* m = GetMethod();
82     CodeInfo code_info(GetCurrentOatQuickMethodHeader());
83     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
84     if (search_for_valid_stack_map && !code_info.GetStackMaskOf(stack_map).IsValid()) {
85       for (StackMap map : code_info.GetStackMaps()) {
86         if (map.GetDexPc() == dex_pc && code_info.GetStackMaskOf(map).IsValid()) {
87           stack_map = map;
88           break;
89         }
90       }
91     }
92     CodeItemDataAccessor accessor(m->DexInstructionData());
93     uint16_t number_of_dex_registers = accessor.RegistersSize();
94 
95     if (!Runtime::Current()->IsAsyncDeoptimizeable(GetOuterMethod(), GetCurrentQuickFramePc())) {
96       // We can only guarantee dex register info presence for debuggable methods.
97       return;
98     }
99 
100     DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map);
101     DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
102     uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
103     BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
104     for (int i = 0; i < number_of_references; ++i) {
105       int reg = registers[i];
106       CHECK_LT(reg, accessor.RegistersSize());
107       DexRegisterLocation location = dex_register_map[reg];
108       switch (location.GetKind()) {
109         case DexRegisterLocation::Kind::kNone:
110           // Not set, should not be a reference.
111           CHECK(false);
112           break;
113         case DexRegisterLocation::Kind::kInStack:
114           CHECK(stack_mask.IsValid());
115           DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
116           CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
117           break;
118         case DexRegisterLocation::Kind::kInRegister:
119         case DexRegisterLocation::Kind::kInRegisterHigh:
120           CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
121           break;
122         case DexRegisterLocation::Kind::kInFpuRegister:
123         case DexRegisterLocation::Kind::kInFpuRegisterHigh:
124           // In Fpu register, should not be a reference.
125           CHECK(false);
126           break;
127         case DexRegisterLocation::Kind::kConstant:
128           CHECK_EQ(location.GetValue(), 0);
129           break;
130         default:
131           LOG(FATAL) << "Unexpected location kind " << location.GetKind();
132       }
133     }
134   }
135 };
136 
137 }  // namespace art
138 
139 #endif  // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_
140