blob: d9e89159f55e7ba9148d23732937924de3cd54ce [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"
Andreas Gampee2abbc62017-09-15 11:59:26 -070021#include "dex_file_types.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010022#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070023#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070024#include "stack.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010025#include "stack_map.h"
26
27namespace art {
28
29// Helper class for tests checking that the compiler keeps track of dex registers
30// holding references.
31class CheckReferenceMapVisitor : public StackVisitor {
32 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070033 explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010034 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010035
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070036 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070037 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010038 if (m->IsCalleeSaveMethod() || m->IsNative()) {
Andreas Gampee2abbc62017-09-15 11:59:26 -070039 CHECK_EQ(GetDexPc(), dex::kDexNoIndex);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010040 }
41
Ian Rogerscf7f1912014-10-22 22:06:39 -070042 if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010043 return true;
44 }
45
David Sehr709b0702016-10-13 09:12:37 -070046 LOG(INFO) << "At " << m->PrettyMethod(false);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010047
48 if (m->IsCalleeSaveMethod()) {
David Sehr709b0702016-10-13 09:12:37 -070049 LOG(WARNING) << "no PC for " << m->PrettyMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010050 return true;
51 }
52
53 return false;
54 }
55
56 void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070057 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010058 CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
59 CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010060 }
61
62 private:
63 void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010066 CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000067 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010068 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000069 uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000070 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010071 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Mathieu Chartier1a20b682017-01-31 14:25:16 -080072 uint32_t register_mask = code_info.GetRegisterMaskOf(encoding, stack_map);
David Srbecky45aa5982016-03-18 02:15:09 +000073 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010074 for (int i = 0; i < number_of_references; ++i) {
75 int reg = registers[i];
76 CHECK(reg < m->GetCodeItem()->registers_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +010077 DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
78 reg, number_of_dex_registers, code_info, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000079 switch (location.GetKind()) {
80 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010081 // Not set, should not be a reference.
82 CHECK(false);
83 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000084 case DexRegisterLocation::Kind::kInStack:
85 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
David Srbecky45aa5982016-03-18 02:15:09 +000086 CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010087 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000088 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010089 case DexRegisterLocation::Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +000090 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010091 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000092 case DexRegisterLocation::Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010093 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010094 // In Fpu register, should not be a reference.
95 CHECK(false);
96 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000097 case DexRegisterLocation::Kind::kConstant:
98 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010099 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000100 default:
David Srbecky7dc11782016-02-25 13:23:56 +0000101 LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100102 }
103 }
104 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100105};
106
107} // namespace art
108
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100109#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_