blob: e2ad7fd83f0c91422643ccaead5f4978f070fa8f [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
Ian Rogerscf7f1912014-10-22 22:06:39 -070043 if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010044 return true;
45 }
46
David Sehr709b0702016-10-13 09:12:37 -070047 LOG(INFO) << "At " << m->PrettyMethod(false);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010048
49 if (m->IsCalleeSaveMethod()) {
David Sehr709b0702016-10-13 09:12:37 -070050 LOG(WARNING) << "no PC for " << m->PrettyMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010051 return true;
52 }
53
54 return false;
55 }
56
57 void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010059 CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
60 CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010061 }
62
63 private:
64 void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070066 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010067 CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000068 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010069 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
David Sehr0225f8e2018-01-31 08:52:24 +000070 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -080071 uint16_t number_of_dex_registers = accessor.RegistersSize();
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);
Mathieu Chartier1a20b682017-01-31 14:25:16 -080074 uint32_t register_mask = code_info.GetRegisterMaskOf(encoding, stack_map);
David Srbecky45aa5982016-03-18 02:15:09 +000075 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010076 for (int i = 0; i < number_of_references; ++i) {
77 int reg = registers[i];
Mathieu Chartier808c7a52017-12-15 11:19:33 -080078 CHECK_LT(reg, accessor.RegistersSize());
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);
David Srbecky45aa5982016-03-18 02:15:09 +000088 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:
David Srbecky7dc11782016-02-25 13:23:56 +0000103 LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100104 }
105 }
106 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100107};
108
109} // namespace art
110
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100111#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_