blob: f6c8fa96594ea3685a46a70e73c79b2d8f6791e1 [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"
Andreas Gampe513061a2017-06-01 09:17:34 -070023#include "stack.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#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:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070032 explicit CheckReferenceMapVisitor(Thread* thread) REQUIRES_SHARED(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
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070035 bool VisitFrame() REQUIRES_SHARED(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
David Sehr709b0702016-10-13 09:12:37 -070045 LOG(INFO) << "At " << m->PrettyMethod(false);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010046
47 if (m->IsCalleeSaveMethod()) {
David Sehr709b0702016-10-13 09:12:37 -070048 LOG(WARNING) << "no PC for " << m->PrettyMethod();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010049 return true;
50 }
51
52 return false;
53 }
54
55 void CheckReferences(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070056 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010057 CHECK(GetCurrentOatQuickMethodHeader()->IsOptimized());
58 CheckOptimizedMethod(registers, number_of_references, native_pc_offset);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010059 }
60
61 private:
62 void CheckOptimizedMethod(int* registers, int number_of_references, uint32_t native_pc_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070064 ArtMethod* m = GetMethod();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010065 CodeInfo code_info = GetCurrentOatQuickMethodHeader()->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +000066 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +010067 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000068 uint16_t number_of_dex_registers = m->GetCodeItem()->registers_size_;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000069 DexRegisterMap dex_register_map =
David Brazdilf677ebf2015-05-29 16:29:43 +010070 code_info.GetDexRegisterMapOf(stack_map, encoding, number_of_dex_registers);
Mathieu Chartier1a20b682017-01-31 14:25:16 -080071 uint32_t register_mask = code_info.GetRegisterMaskOf(encoding, stack_map);
David Srbecky45aa5982016-03-18 02:15:09 +000072 BitMemoryRegion stack_mask = code_info.GetStackMaskOf(encoding, stack_map);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010073 for (int i = 0; i < number_of_references; ++i) {
74 int reg = registers[i];
75 CHECK(reg < m->GetCodeItem()->registers_size_);
David Brazdilf677ebf2015-05-29 16:29:43 +010076 DexRegisterLocation location = dex_register_map.GetDexRegisterLocation(
77 reg, number_of_dex_registers, code_info, encoding);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000078 switch (location.GetKind()) {
79 case DexRegisterLocation::Kind::kNone:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010080 // Not set, should not be a reference.
81 CHECK(false);
82 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000083 case DexRegisterLocation::Kind::kInStack:
84 DCHECK_EQ(location.GetValue() % kFrameSlotSize, 0);
David Srbecky45aa5982016-03-18 02:15:09 +000085 CHECK(stack_mask.LoadBit(location.GetValue() / kFrameSlotSize));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010086 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000087 case DexRegisterLocation::Kind::kInRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010088 case DexRegisterLocation::Kind::kInRegisterHigh:
Roland Levillaina2d8ec62015-03-12 15:25:29 +000089 CHECK_NE(register_mask & (1 << location.GetValue()), 0u);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010090 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000091 case DexRegisterLocation::Kind::kInFpuRegister:
David Brazdild9cb68e2015-08-25 13:52:43 +010092 case DexRegisterLocation::Kind::kInFpuRegisterHigh:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010093 // In Fpu register, should not be a reference.
94 CHECK(false);
95 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000096 case DexRegisterLocation::Kind::kConstant:
97 CHECK_EQ(location.GetValue(), 0);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010098 break;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000099 default:
David Srbecky7dc11782016-02-25 13:23:56 +0000100 LOG(FATAL) << "Unexpected location kind " << location.GetInternalKind();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100101 }
102 }
103 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100104};
105
106} // namespace art
107
Nicolas Geoffray48a89612014-09-17 10:19:01 +0100108#endif // ART_RUNTIME_CHECK_REFERENCE_MAP_VISITOR_H_