blob: 93fdaa616158f0b6e1d06fd62f78e8c143ea3a14 [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 Geoffray524e7ea2015-10-16 17:13:34 +010021#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070022#include "scoped_thread_state_change-inl.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#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:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070031 explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffray8e5bd182015-05-06 11:34:34 +010032 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010033
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070034 bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070035 ArtMethod* m = GetMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010036 if (m->IsCalleeSaveMethod() || m->IsNative()) {
37 CHECK_EQ(GetDexPc(), DexFile::kDexNoIndex);
38 }
39
Ian Rogerscf7f1912014-10-22 22:06:39 -070040 if (m == nullptr || m->IsNative() || m->IsRuntimeMethod() || IsShadowFrame()) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010041 return true;
42 }
43
David Sehr709b0702016-10-13 09:12:37 -070044 LOG(INFO) << "At " << m->PrettyMethod(false);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010045
46 if (m->IsCalleeSaveMethod()) {
David Sehr709b0702016-10-13 09:12:37 -070047 LOG(WARNING) << "no PC for " << m->PrettyMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010048 return true;
49 }
50
51 return false;
52 }
53
54 void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010056 CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
57 CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010058 }
59
60 private:
61 void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070062 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010064 CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000065 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010066 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000067 uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000068 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010069 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
David Srbecky09ed0982016-02-12 21:58:43 +000070 uint32_t register_mask = stack_map.GetRegisterMask(encoding.stack_map_encoding);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010071 for (int i = 0; i < number_of_references; ++i) {
72 int reg = registers[i];
73 CHECK(reg < m->GetCodeItem()->registers_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +010074 DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
75 reg, number_of_dex_registers, code_info, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000076 switch (location.GetKind()) {
77 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010078 // Not set, should not be a reference.
79 CHECK(false);
80 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000081 case DexRegisterLocation::Kind::kInStack:
82 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
David Srbecky09ed0982016-02-12 21:58:43 +000083 CHECK(stack_map.GetStackMaskBit(encoding.stack_map_encoding,
84 location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010085 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000086 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010087 case DexRegisterLocation::Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +000088 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010089 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000090 case DexRegisterLocation::Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010091 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010092 // In Fpu register, should not be a reference.
93 CHECK(false);
94 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000095 case DexRegisterLocation::Kind::kConstant:
96 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010097 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000098 default:
David Srbecky7dc11782016-02-25 13:23:56 +000099 LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100100 }
101 }
102 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100103};
104
105} // namespace art
106
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100107#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_