blob: b9ea4751498397731b03433634c0e6569ff4db8c [file] [log] [blame]
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001/*
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
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010021#include "gc_map.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010022#include "oat_quick_method_header.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "scoped_thread_state_change.h"
24#include "stack_map.h"
25
26namespace art {
27
28// Helper class for tests checking that the compiler keeps track of dex registers
29// holding references.
30class CheckReferenceMapVisitor : public StackVisitor {
31 public:
Mathieu Chartier90443472015-07-16 20:32:27 -070032 explicit CheckReferenceMapVisitor(Thread* thread) SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010033 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010034
Mathieu Chartier90443472015-07-16 20:32:27 -070035 bool VisitFrame() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070036 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010037 if (m->IsCalleeSaveMethod() || m->IsNative()) {
38 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
39 }
40
Ian Rogerscf7f1912014-10-22 22:06:39 -070041 if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010042 return true;
43 }
44
45 LOG(INFO) << "At " << PrettyMethod(m, false);
46
47 if (m->IsCalleeSaveMethod()) {
48 LOG(WARNING) << "no PC for " << PrettyMethod(m);
49 return true;
50 }
51
52 return false;
53 }
54
55 void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -070056 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010057 if (GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010058 CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
59 } else {
60 CheckQuickMethod(registers, number_of_references, native_pc_offset);
61 }
62 }
63
64 private:
65 void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -070066 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070067 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010068 CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
David Brazdilf677ebf2015-05-29 16:29:43 +010069 StackMapEncoding encoding = code_info.ExtractEncoding();
70 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000071 uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000072 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010073 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
74 MemoryRegion stack_mask = stack_map.GetStackMask(encoding);
75 uint32_t register_mask = stack_map.GetRegisterMask(encoding);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010076 for (int i = 0; i < number_of_references; ++i) {
77 int reg = registers[i];
78 CHECK(reg < m->GetCodeItem()->registers_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +010079 DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
80 reg, number_of_dex_registers, code_info, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000081 switch (location.GetKind()) {
82 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010083 // Not set, should not be a reference.
84 CHECK(false);
85 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000086 case DexRegisterLocation::Kind::kInStack:
87 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
88 CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010089 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000090 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010091 case DexRegisterLocation::Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +000092 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010093 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000094 case DexRegisterLocation::Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010095 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010096 // In Fpu register, should not be a reference.
97 CHECK(false);
98 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000099 case DexRegisterLocation::Kind::kConstant:
100 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100101 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000102 default:
103 LOG(FATAL) << "Unexpected location kind"
104 << DexRegisterLocation::PrettyDescriptor(location.GetInternalKind());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100105 }
106 }
107 }
108
109 void CheckQuickMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700110 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700111 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100112 NativePcOffsetToReferenceMap map(GetCurrentOatQuickMethodHeader()->GetNativeGcMap());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100113 const uint8_t* ref_bitmap = map.FindBitMap(native_pc_offset);
114 CHECK(ref_bitmap);
115 for (int i = 0; i < number_of_references; ++i) {
116 int reg = registers[i];
117 CHECK(reg < m->GetCodeItem()->registers_size_);
118 CHECK((*((ref_bitmap) + reg / 8) >> (reg % 8) ) & 0x01)
119 << "Error: Reg @" << i << " is not in GC map";
120 }
121 }
122};
123
124} // namespace art
125
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100126#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_