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());
68     StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
69     CodeItemDataAccessor accessor(m->DexInstructionData());
70     uint16_t number_of_dex_registers = accessor.RegistersSize();
71     DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map);
72     DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
73     uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
74     BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
75     for (int i = 0; i < number_of_references; ++i) {
76       int reg = registers[i];
77       CHECK_LT(reg, accessor.RegistersSize());
78       DexRegisterLocation location = dex_register_map[reg];
79       switch (location.GetKind()) {
80         case DexRegisterLocation::Kind::kNone:
81           // Not set, should not be a reference.
82           CHECK(false);
83           break;
84         case DexRegisterLocation::Kind::kInStack:
85           DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
86           CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
87           break;
88         case DexRegisterLocation::Kind::kInRegister:
89         case DexRegisterLocation::Kind::kInRegisterHigh:
90           CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
91           break;
92         case DexRegisterLocation::Kind::kInFpuRegister:
93         case DexRegisterLocation::Kind::kInFpuRegisterHigh:
94           // In Fpu register, should not be a reference.
95           CHECK(false);
96           break;
97         case DexRegisterLocation::Kind::kConstant:
98           CHECK_EQ(location.GetValue(), 0);
99           break;
100         default:
101           LOG(FATAL) << "Unexpected location kind " << location.GetKind();
102       }
103     }
104   }
105 };
106 
107 }  // namespace art
108 
109 #endif  // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_
110