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