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.h"
23#include "stack_map.h"
24
25namespace art {
26
27// Helper class for tests checking that the compiler keeps track of dex registers
28// holding references.
29class CheckReferenceMapVisitor : public StackVisitor {
30 public:
31  explicit CheckReferenceMapVisitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_)
32      : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
33
34  bool VisitFrame() SHARED_REQUIRES(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 " << PrettyMethod(m, false);
45
46    if (m->IsCalleeSaveMethod()) {
47      LOG(WARNING) << "no PC for " << PrettyMethod(m);
48      return true;
49    }
50
51    return false;
52  }
53
54  void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
55      SHARED_REQUIRES(Locks::mutator_lock_) {
56    CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
57    CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
58  }
59
60 private:
61  void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
62      SHARED_REQUIRES(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 = stack_map.GetRegisterMask(encoding.stack_map_encoding);
71    for (int i = 0; i < number_of_references; ++i) {
72      int reg = registers[i];
73      CHECK(reg < m->GetCodeItem()->registers_size_);
74      DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
75          reg, number_of_dex_registers, code_info, encoding);
76      switch (location.GetKind()) {
77        case DexRegisterLocation::Kind::kNone:
78          // Not set, should not be a reference.
79          CHECK(false);
80          break;
81        case DexRegisterLocation::Kind::kInStack:
82          DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
83          CHECK(stack_map.GetStackMaskBit(encoding.stack_map_encoding,
84                                          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