blob: 382314393f7c5afc94d2d4861cba868923f363be [file] [log] [blame]
Ian Rogers39ebcb82013-05-30 16:57:23 -07001/*
2 * Copyright (C) 2013 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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_INL_H_
18#define ART_RUNTIME_VERIFIER_REGISTER_LINE_INL_H_
Ian Rogers39ebcb82013-05-30 16:57:23 -070019
20#include "register_line.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070021
Ian Rogers39ebcb82013-05-30 16:57:23 -070022#include "method_verifier.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070023#include "reg_type_cache-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070024
25namespace art {
26namespace verifier {
27
Andreas Gampea727e372015-08-25 09:22:37 -070028// Should we dump a warning on failures to verify balanced locking? That would be an indication to
29// developers that their code will be slow.
30static constexpr bool kDumpLockFailures = true;
31
Ian Rogers7b078e82014-09-10 14:44:24 -070032inline const RegType& RegisterLine::GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const {
Ian Rogers39ebcb82013-05-30 16:57:23 -070033 // The register index was validated during the static pass, so we don't need to check it here.
34 DCHECK_LT(vsrc, num_regs_);
Ian Rogers7b078e82014-09-10 14:44:24 -070035 return verifier->GetRegTypeCache()->GetFromId(line_[vsrc]);
36}
37
Andreas Gampead238ce2015-08-24 21:13:08 -070038template <LockOp kLockOp>
Ian Rogers7b078e82014-09-10 14:44:24 -070039inline bool RegisterLine::SetRegisterType(MethodVerifier* verifier, uint32_t vdst,
40 const RegType& new_type) {
41 DCHECK_LT(vdst, num_regs_);
42 if (new_type.IsLowHalf() || new_type.IsHighHalf()) {
43 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected category1 register type not '"
44 << new_type << "'";
45 return false;
Ian Rogers7b078e82014-09-10 14:44:24 -070046 } else {
Andreas Gampe38536282015-07-08 17:22:57 -070047 // Note: previously we failed when asked to set a conflict. However, conflicts are OK as long
48 // as they are not accessed, and our backends can handle this nowadays.
Ian Rogers7b078e82014-09-10 14:44:24 -070049 line_[vdst] = new_type.GetId();
50 }
Andreas Gampead238ce2015-08-24 21:13:08 -070051 switch (kLockOp) {
52 case LockOp::kClear:
53 // Clear the monitor entry bits for this register.
54 ClearAllRegToLockDepths(vdst);
55 break;
56 case LockOp::kKeep:
57 // Should only be doing this with reference types.
58 DCHECK(new_type.IsReferenceTypes());
59 break;
60 }
Ian Rogers7b078e82014-09-10 14:44:24 -070061 return true;
62}
63
64inline bool RegisterLine::SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst,
65 const RegType& new_type1,
66 const RegType& new_type2) {
67 DCHECK_LT(vdst + 1, num_regs_);
68 if (!new_type1.CheckWidePair(new_type2)) {
69 verifier->Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Invalid wide pair '"
70 << new_type1 << "' '" << new_type2 << "'";
71 return false;
72 } else {
73 line_[vdst] = new_type1.GetId();
74 line_[vdst + 1] = new_type2.GetId();
75 }
76 // Clear the monitor entry bits for this register.
77 ClearAllRegToLockDepths(vdst);
78 ClearAllRegToLockDepths(vdst + 1);
79 return true;
80}
81
82inline void RegisterLine::SetResultTypeToUnknown(MethodVerifier* verifier) {
83 result_[0] = verifier->GetRegTypeCache()->Undefined().GetId();
84 result_[1] = result_[0];
85}
86
87inline void RegisterLine::SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type) {
88 DCHECK(!new_type.IsLowHalf());
89 DCHECK(!new_type.IsHighHalf());
90 result_[0] = new_type.GetId();
91 result_[1] = verifier->GetRegTypeCache()->Undefined().GetId();
92}
93
94inline void RegisterLine::SetResultRegisterTypeWide(const RegType& new_type1,
95 const RegType& new_type2) {
96 DCHECK(new_type1.CheckWidePair(new_type2));
97 result_[0] = new_type1.GetId();
98 result_[1] = new_type2.GetId();
99}
100
101inline void RegisterLine::CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc,
102 TypeCategory cat) {
103 DCHECK(cat == kTypeCategory1nr || cat == kTypeCategoryRef);
104 const RegType& type = GetRegisterType(verifier, vsrc);
Andreas Gampead238ce2015-08-24 21:13:08 -0700105 if (!SetRegisterType<LockOp::kClear>(verifier, vdst, type)) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700106 return;
107 }
Andreas Gampe38536282015-07-08 17:22:57 -0700108 if (!type.IsConflict() && // Allow conflicts to be copied around.
109 ((cat == kTypeCategory1nr && !type.IsCategory1Types()) ||
110 (cat == kTypeCategoryRef && !type.IsReferenceTypes()))) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700111 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy1 v" << vdst << "<-v" << vsrc << " type=" << type
112 << " cat=" << static_cast<int>(cat);
113 } else if (cat == kTypeCategoryRef) {
114 CopyRegToLockDepth(vdst, vsrc);
115 }
116}
117
118inline void RegisterLine::CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc) {
119 const RegType& type_l = GetRegisterType(verifier, vsrc);
120 const RegType& type_h = GetRegisterType(verifier, vsrc + 1);
121
122 if (!type_l.CheckWidePair(type_h)) {
123 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "copy2 v" << vdst << "<-v" << vsrc
124 << " type=" << type_l << "/" << type_h;
125 } else {
126 SetRegisterTypeWide(verifier, vdst, type_l, type_h);
127 }
128}
129
130inline bool RegisterLine::VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc,
131 const RegType& check_type) {
132 // Verify the src register type against the check type refining the type of the register
133 const RegType& src_type = GetRegisterType(verifier, vsrc);
David Brazdilca3c8c32016-09-06 14:04:48 +0100134 if (UNLIKELY(!check_type.IsAssignableFrom(src_type, verifier))) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700135 enum VerifyError fail_type;
136 if (!check_type.IsNonZeroReferenceTypes() || !src_type.IsNonZeroReferenceTypes()) {
137 // Hard fail if one of the types is primitive, since they are concretely known.
138 fail_type = VERIFY_ERROR_BAD_CLASS_HARD;
David Brazdil68b5c0b2016-01-19 14:25:29 +0000139 } else if (check_type.IsUninitializedTypes() || src_type.IsUninitializedTypes()) {
140 // Hard fail for uninitialized types, which don't match anything but themselves.
141 fail_type = VERIFY_ERROR_BAD_CLASS_HARD;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 } else if (check_type.IsUnresolvedTypes() || src_type.IsUnresolvedTypes()) {
143 fail_type = VERIFY_ERROR_NO_CLASS;
144 } else {
145 fail_type = VERIFY_ERROR_BAD_CLASS_SOFT;
146 }
147 verifier->Fail(fail_type) << "register v" << vsrc << " has type "
148 << src_type << " but expected " << check_type;
149 return false;
150 }
151 if (check_type.IsLowHalf()) {
152 const RegType& src_type_h = GetRegisterType(verifier, vsrc + 1);
153 if (UNLIKELY(!src_type.CheckWidePair(src_type_h))) {
154 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register v" << vsrc << " has type "
155 << src_type << "/" << src_type_h;
156 return false;
157 }
158 }
159 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
160 // precise than the subtype in vsrc so leave it for reference types. For primitive types
161 // if they are a defined type then they are as precise as we can get, however, for constant
162 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
163 return true;
164}
165
Andreas Gampea727e372015-08-25 09:22:37 -0700166inline void RegisterLine::VerifyMonitorStackEmpty(MethodVerifier* verifier) const {
Ian Rogers7b078e82014-09-10 14:44:24 -0700167 if (MonitorStackDepth() != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -0700168 verifier->Fail(VERIFY_ERROR_LOCKING);
169 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800170 VLOG(verifier) << "expected empty monitor stack in "
171 << PrettyMethod(verifier->GetMethodReference().dex_method_index,
172 *verifier->GetMethodReference().dex_file);
Andreas Gampea727e372015-08-25 09:22:37 -0700173 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 }
Ian Rogers39ebcb82013-05-30 16:57:23 -0700175}
176
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800177inline size_t RegisterLine::ComputeSize(size_t num_regs) {
178 return OFFSETOF_MEMBER(RegisterLine, line_) + num_regs * sizeof(uint16_t);
179}
180
Mathieu Chartierde40d472015-10-15 17:47:48 -0700181inline RegisterLine* RegisterLine::Create(size_t num_regs, MethodVerifier* verifier) {
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800182 void* memory = verifier->GetArena().Alloc(ComputeSize(num_regs));
Mathieu Chartierde40d472015-10-15 17:47:48 -0700183 return new (memory) RegisterLine(num_regs, verifier);
184}
185
186inline RegisterLine::RegisterLine(size_t num_regs, MethodVerifier* verifier)
187 : num_regs_(num_regs),
188 monitors_(verifier->GetArena().Adapter(kArenaAllocVerifier)),
189 reg_to_lock_depths_(std::less<uint32_t>(), verifier->GetArena().Adapter(kArenaAllocVerifier)),
190 this_initialized_(false) {
191 std::uninitialized_fill_n(line_, num_regs_, 0u);
192 SetResultTypeToUnknown(verifier);
193}
194
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800195inline void RegisterLineArenaDelete::operator()(RegisterLine* ptr) const {
Mathieu Chartier88177602016-02-17 11:04:20 -0800196 if (ptr != nullptr) {
197 ptr->~RegisterLine();
198 ProtectMemory(ptr, RegisterLine::ComputeSize(ptr->NumRegs()));
199 }
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800200}
201
Ian Rogers39ebcb82013-05-30 16:57:23 -0700202} // namespace verifier
203} // namespace art
204
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700205#endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_INL_H_