blob: a7c3e45ae7b4c70dc875f2d98f5f07186f8e4291 [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"
David Sehr9e734c72018-01-04 17:56:19 -080021#include "dex/code_item_accessors-inl.h"
22#include "dex/dex_file_types.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010023#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070025#include "stack.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010026#include "stack_map.h"
27
28namespace art {
29
30// Helper class for tests checking that the compiler keeps track of dex registers
31// holding references.
32class CheckReferenceMapVisitor : public StackVisitor {
33 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070034 explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010035 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010036
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010039 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Andreas Gampee2abbc62017-09-15 11:59:26 -070040 CHECK_EQ(GetDexPc(), dex::kDexNoIndex);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010041 }
42
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000043 // If the method is not compiled, continue the stack walk.
44 if (m == nullptr ||
45 m->IsNative() ||
46 m->IsRuntimeMethod() ||
47 IsShadowFrame() ||
48 !GetCurrentOatQuickMethodHeader()->IsOptimized()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010049 return true;
50 }
51
David Sehr709b0702016-10-13 09:12:37 -070052 LOG(INFO) << "At " << m->PrettyMethod(false);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010053
54 if (m->IsCalleeSaveMethod()) {
David Sehr709b0702016-10-13 09:12:37 -070055 LOG(WARNING) << "no PC for " << m->PrettyMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010056 return true;
57 }
58
59 return false;
60 }
61
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010062 void CheckReferences(int* registers,
63 int number_of_references,
64 uint32_t dex_pc,
65 uint32_t native_pc_offset,
66 bool search_for_valid_stack_map)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010068 CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010069 CheckOptimizedMethod(
70 registers, number_of_references, dex_pc, native_pc_offset, search_for_valid_stack_map);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010071 }
72
73 private:
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010074 void CheckOptimizedMethod(int* registers,
75 int number_of_references,
76 uint32_t dex_pc,
77 uint32_t native_pc_offset,
78 bool search_for_valid_stack_map)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070079 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 ArtMethod* m = GetMethod();
David Srbecky052f8ca2018-04-26 15:42:54 +010081 CodeInfo code_info(GetCurrentOatQuickMethodHeader());
82 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +010083 if (search_for_valid_stack_map && !code_info.GetStackMaskOf(stack_map).IsValid()) {
84 for (StackMap map : code_info.GetStackMaps()) {
85 if (map.GetDexPc() == dex_pc && code_info.GetStackMaskOf(map).IsValid()) {
86 stack_map = map;
87 break;
88 }
89 }
90 }
David Sehr0225f8e2018-01-31 08:52:24 +000091 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -080092 uint16_t number_of_dex_registers = accessor.RegistersSize();
Artem Serov2808be82018-12-20 19:15:11 +000093
94 if (!Runtime::Current()->IsAsyncDeoptimizeable(GetCurrentQuickFramePc())) {
95 // We can only guarantee dex register info presence for debuggable methods.
96 return;
97 }
98
David Srbeckyfd89b072018-06-03 12:00:22 +010099 DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(stack_map);
100 DCHECK_EQ(dex_register_map.size(), number_of_dex_registers);
David Srbecky052f8ca2018-04-26 15:42:54 +0100101 uint32_t register_mask = code_info.GetRegisterMaskOf(stack_map);
102 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(stack_map);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100103 for (int i = 0; i < number_of_references; ++i) {
104 int reg = registers[i];
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800105 CHECK_LT(reg, accessor.RegistersSize());
David Srbeckye1402122018-06-13 18:20:45 +0100106 DexRegisterLocation location = dex_register_map[reg];
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000107 switch (location.GetKind()) {
108 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100109 // Not set, should not be a reference.
110 CHECK(false);
111 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000112 case DexRegisterLocation::Kind::kInStack:
Nicolas Geoffrayb0bf9e22020-09-10 09:54:05 +0100113 CHECK(stack_mask.IsValid());
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000114 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
David Srbecky45aa5982016-03-18 02:15:09 +0000115 CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100116 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000117 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100118 case DexRegisterLocation::Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000119 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100120 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000121 case DexRegisterLocation::Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +0100122 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100123 // In Fpu register, should not be a reference.
124 CHECK(false);
125 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000126 case DexRegisterLocation::Kind::kConstant:
127 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100128 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000129 default:
David Srbeckye1402122018-06-13 18:20:45 +0100130 LOG(FATAL) << "Unexpected location kind " << location.GetKind();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100131 }
132 }
133 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100134};
135
136} // namespace art
137
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100138#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_