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 "oat_quick_method_header.h"
22 #include "scoped_thread_state_change-inl.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:
CheckReferenceMapVisitor(Thread * thread)31   explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
32       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
33 
VisitFrame()34   bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
35     ArtMethod* m = GetMethod();
36     if (m->IsCalleeSaveMethod() || m->IsNative()) {
37       CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
38     }
39 
40     if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
41       return true;
42     }
43 
44     LOG(INFO) << "At " << m->PrettyMethod(false);
45 
46     if (m->IsCalleeSaveMethod()) {
47       LOG(WARNING) << "no PC for " << m->PrettyMethod();
48       return true;
49     }
50 
51     return false;
52   }
53 
CheckReferences(int * registers,int number_of_references,uint32_t native_pc_offset)54   void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
55       REQUIRES_SHARED(Locks::mutator_lock_) {
56     CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
57     CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
58   }
59 
60  private:
CheckOptimizedMethod(int * registers,int number_of_references,uint32_t native_pc_offset)61   void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
62       REQUIRES_SHARED(Locks::mutator_lock_) {
63     ArtMethod* m = GetMethod();
64     CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
65     CodeInfoEncoding encoding = code_info.ExtractEncoding();
66     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
67     uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
68     DexRegisterMap dex_register_map =
69         code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
70     uint32_t register_mask = code_info.GetRegisterMaskOf(encoding, stack_map);
71     BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
72     for (int i = 0; i < number_of_references; ++i) {
73       int reg = registers[i];
74       CHECK(reg < m->GetCodeItem()->registers_size_);
75       DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
76           reg, number_of_dex_registers, code_info, encoding);
77       switch (location.GetKind()) {
78         case DexRegisterLocation::Kind::kNone:
79           // Not set, should not be a reference.
80           CHECK(false);
81           break;
82         case DexRegisterLocation::Kind::kInStack:
83           DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
84           CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
85           break;
86         case DexRegisterLocation::Kind::kInRegister:
87         case DexRegisterLocation::Kind::kInRegisterHigh:
88           CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
89           break;
90         case DexRegisterLocation::Kind::kInFpuRegister:
91         case DexRegisterLocation::Kind::kInFpuRegisterHigh:
92           // In Fpu register, should not be a reference.
93           CHECK(false);
94           break;
95         case DexRegisterLocation::Kind::kConstant:
96           CHECK_EQ(location.GetValue(), 0);
97           break;
98         default:
99           LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind();
100       }
101     }
102   }
103 };
104 
105 }  // namespace art
106 
107 #endif  // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_
108