blob: 383d8907022c28c25b3883715279a1f7c7c308be [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
2 * Copyright (C) 2012 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#include "register_line.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Sebastien Hertz5243e912013-05-21 10:55:07 +020021#include "dex_instruction-inl.h"
Andreas Gampeaf318022015-08-12 16:42:06 -070022#include "method_verifier-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070023#include "register_line-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070024#include "reg_type-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070025
26namespace art {
27namespace verifier {
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029using android::base::StringPrintf;
30
Ian Rogers7b078e82014-09-10 14:44:24 -070031bool RegisterLine::CheckConstructorReturn(MethodVerifier* verifier) const {
Andreas Gampef10b6e12015-08-12 10:48:12 -070032 if (kIsDebugBuild && this_initialized_) {
33 // Ensure that there is no UninitializedThisReference type anymore if this_initialized_ is true.
34 for (size_t i = 0; i < num_regs_; i++) {
35 const RegType& type = GetRegisterType(verifier, i);
36 CHECK(!type.IsUninitializedThisReference() &&
37 !type.IsUnresolvedAndUninitializedThisReference())
38 << i << ": " << type.IsUninitializedThisReference() << " in "
David Sehr709b0702016-10-13 09:12:37 -070039 << verifier->GetMethodReference().PrettyMethod();
Ian Rogers776ac1f2012-04-13 23:36:36 -070040 }
41 }
Andreas Gampef10b6e12015-08-12 10:48:12 -070042 if (!this_initialized_) {
43 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
44 << "Constructor returning without calling superclass constructor";
45 }
46 return this_initialized_;
Ian Rogers776ac1f2012-04-13 23:36:36 -070047}
48
Ian Rogers7b078e82014-09-10 14:44:24 -070049const RegType& RegisterLine::GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
Orion Hodsoncfa325e2016-10-13 10:25:54 +010050 bool allow_failure) {
51 DCHECK(inst->IsInvoke());
52 const size_t args_count = inst->VRegA();
Sebastien Hertz5243e912013-05-21 10:55:07 +020053 if (args_count < 1) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080054 if (!allow_failure) {
55 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke lacks 'this'";
56 }
Ian Rogers7b078e82014-09-10 14:44:24 -070057 return verifier->GetRegTypeCache()->Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070059 /* Get the element type of the array held in vsrc */
Orion Hodsoncfa325e2016-10-13 10:25:54 +010060 const uint32_t this_reg = inst->VRegC();
Ian Rogers7b078e82014-09-10 14:44:24 -070061 const RegType& this_type = GetRegisterType(verifier, this_reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -070062 if (!this_type.IsReferenceTypes()) {
Mathieu Chartier091d2382015-03-06 10:59:06 -080063 if (!allow_failure) {
64 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
65 << "tried to get class from non-reference register v" << this_reg
66 << " (type=" << this_type << ")";
67 }
Ian Rogers7b078e82014-09-10 14:44:24 -070068 return verifier->GetRegTypeCache()->Conflict();
Ian Rogers776ac1f2012-04-13 23:36:36 -070069 }
70 return this_type;
71}
72
Ian Rogers7b078e82014-09-10 14:44:24 -070073bool RegisterLine::VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc,
74 const RegType& check_type1,
Ian Rogersd8f69b02014-09-10 21:43:52 +000075 const RegType& check_type2) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -080076 DCHECK(check_type1.CheckWidePair(check_type2));
77 // Verify the src register type against the check type refining the type of the register
Ian Rogers7b078e82014-09-10 14:44:24 -070078 const RegType& src_type = GetRegisterType(verifier, vsrc);
David Brazdilca3c8c32016-09-06 14:04:48 +010079 if (!check_type1.IsAssignableFrom(src_type, verifier)) {
Ian Rogers7b078e82014-09-10 14:44:24 -070080 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << vsrc << " has type " << src_type
Ian Rogers2bcb4a42012-11-08 10:39:18 -080081 << " but expected " << check_type1;
82 return false;
83 }
Ian Rogers7b078e82014-09-10 14:44:24 -070084 const RegType& src_type_h = GetRegisterType(verifier, vsrc + 1);
Ian Rogers2bcb4a42012-11-08 10:39:18 -080085 if (!src_type.CheckWidePair(src_type_h)) {
Ian Rogers7b078e82014-09-10 14:44:24 -070086 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register v" << vsrc << " has type "
Ian Rogers2bcb4a42012-11-08 10:39:18 -080087 << src_type << "/" << src_type_h;
88 return false;
89 }
90 // The register at vsrc has a defined type, we know the lower-upper-bound, but this is less
91 // precise than the subtype in vsrc so leave it for reference types. For primitive types
92 // if they are a defined type then they are as precise as we can get, however, for constant
93 // types we may wish to refine them. Unfortunately constant propagation has rendered this useless.
94 return true;
95}
96
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +000097void RegisterLine::MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -070098 DCHECK(uninit_type.IsUninitializedTypes());
Ian Rogers7b078e82014-09-10 14:44:24 -070099 const RegType& init_type = verifier->GetRegTypeCache()->FromUninitialized(uninit_type);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700100 size_t changed = 0;
Ian Rogersb8c78592013-07-25 23:52:52 +0000101 for (uint32_t i = 0; i < num_regs_; i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700102 if (GetRegisterType(verifier, i).Equals(uninit_type)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700103 line_[i] = init_type.GetId();
104 changed++;
105 }
106 }
Andreas Gampef10b6e12015-08-12 10:48:12 -0700107 // Is this initializing "this"?
108 if (uninit_type.IsUninitializedThisReference() ||
109 uninit_type.IsUnresolvedAndUninitializedThisReference()) {
110 this_initialized_ = true;
111 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700112 DCHECK_GT(changed, 0u);
113}
114
Ian Rogers7b078e82014-09-10 14:44:24 -0700115void RegisterLine::MarkAllRegistersAsConflicts(MethodVerifier* verifier) {
116 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
Ian Rogersb8c78592013-07-25 23:52:52 +0000117 for (uint32_t i = 0; i < num_regs_; i++) {
118 line_[i] = conflict_type_id;
119 }
120}
121
Ian Rogers7b078e82014-09-10 14:44:24 -0700122void RegisterLine::MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc) {
123 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
Ian Rogersb8c78592013-07-25 23:52:52 +0000124 for (uint32_t i = 0; i < num_regs_; i++) {
125 if (i != vsrc) {
126 line_[i] = conflict_type_id;
127 }
128 }
129}
130
Ian Rogers7b078e82014-09-10 14:44:24 -0700131void RegisterLine::MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc) {
132 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
Ian Rogersb8c78592013-07-25 23:52:52 +0000133 for (uint32_t i = 0; i < num_regs_; i++) {
134 if ((i != vsrc) && (i != (vsrc + 1))) {
135 line_[i] = conflict_type_id;
136 }
137 }
138}
139
Ian Rogers7b078e82014-09-10 14:44:24 -0700140std::string RegisterLine::Dump(MethodVerifier* verifier) const {
Ian Rogers529781d2012-07-23 17:24:29 -0700141 std::string result;
142 for (size_t i = 0; i < num_regs_; i++) {
143 result += StringPrintf("%zd:[", i);
Ian Rogers7b078e82014-09-10 14:44:24 -0700144 result += GetRegisterType(verifier, i).Dump();
Ian Rogers529781d2012-07-23 17:24:29 -0700145 result += "],";
146 }
Mathieu Chartier02e25112013-08-14 16:14:24 -0700147 for (const auto& monitor : monitors_) {
148 result += StringPrintf("{%d},", monitor);
Ian Rogers529781d2012-07-23 17:24:29 -0700149 }
Andreas Gampead238ce2015-08-24 21:13:08 -0700150 for (auto& pairs : reg_to_lock_depths_) {
151 result += StringPrintf("<%d -> %x>", pairs.first, pairs.second);
152 }
Ian Rogers529781d2012-07-23 17:24:29 -0700153 return result;
154}
155
Ian Rogers7b078e82014-09-10 14:44:24 -0700156void RegisterLine::MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700157 for (size_t i = 0; i < num_regs_; i++) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700158 if (GetRegisterType(verifier, i).Equals(uninit_type)) {
159 line_[i] = verifier->GetRegTypeCache()->Conflict().GetId();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700160 ClearAllRegToLockDepths(i);
161 }
162 }
163}
164
Ian Rogers7b078e82014-09-10 14:44:24 -0700165void RegisterLine::CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference) {
166 const RegType& type = verifier->GetRegTypeCache()->GetFromId(result_[0]);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700167 if ((!is_reference && !type.IsCategory1Types()) ||
168 (is_reference && !type.IsReferenceTypes())) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700169 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700170 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
171 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -0700172 DCHECK(verifier->GetRegTypeCache()->GetFromId(result_[1]).IsUndefined());
Andreas Gampead238ce2015-08-24 21:13:08 -0700173 SetRegisterType<LockOp::kClear>(verifier, vdst, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700174 result_[0] = verifier->GetRegTypeCache()->Undefined().GetId();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700175 }
176}
177
178/*
179 * Implement "move-result-wide". Copy the category-2 value from the result
180 * register to another register, and reset the result register.
181 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700182void RegisterLine::CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst) {
183 const RegType& type_l = verifier->GetRegTypeCache()->GetFromId(result_[0]);
184 const RegType& type_h = verifier->GetRegTypeCache()->GetFromId(result_[1]);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700185 if (!type_l.IsCategory2Types()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700186 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700187 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
188 } else {
189 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
Ian Rogers7b078e82014-09-10 14:44:24 -0700190 SetRegisterTypeWide(verifier, vdst, type_l, type_h); // also sets the high
191 result_[0] = verifier->GetRegTypeCache()->Undefined().GetId();
192 result_[1] = verifier->GetRegTypeCache()->Undefined().GetId();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700193 }
194}
195
Ian Rogers7b078e82014-09-10 14:44:24 -0700196void RegisterLine::CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst,
197 const RegType& dst_type, const RegType& src_type) {
198 if (VerifyRegisterType(verifier, inst->VRegB_12x(), src_type)) {
Andreas Gampead238ce2015-08-24 21:13:08 -0700199 SetRegisterType<LockOp::kClear>(verifier, inst->VRegA_12x(), dst_type);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700200 }
201}
202
Ian Rogers7b078e82014-09-10 14:44:24 -0700203void RegisterLine::CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000204 const RegType& dst_type1, const RegType& dst_type2,
205 const RegType& src_type1, const RegType& src_type2) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700206 if (VerifyRegisterTypeWide(verifier, inst->VRegB_12x(), src_type1, src_type2)) {
207 SetRegisterTypeWide(verifier, inst->VRegA_12x(), dst_type1, dst_type2);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800208 }
209}
210
Ian Rogers7b078e82014-09-10 14:44:24 -0700211void RegisterLine::CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000212 const RegType& dst_type1, const RegType& dst_type2,
213 const RegType& src_type) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 if (VerifyRegisterType(verifier, inst->VRegB_12x(), src_type)) {
215 SetRegisterTypeWide(verifier, inst->VRegA_12x(), dst_type1, dst_type2);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800216 }
217}
218
Ian Rogers7b078e82014-09-10 14:44:24 -0700219void RegisterLine::CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000220 const RegType& dst_type,
221 const RegType& src_type1, const RegType& src_type2) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700222 if (VerifyRegisterTypeWide(verifier, inst->VRegB_12x(), src_type1, src_type2)) {
Andreas Gampead238ce2015-08-24 21:13:08 -0700223 SetRegisterType<LockOp::kClear>(verifier, inst->VRegA_12x(), dst_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800224 }
225}
226
Ian Rogers7b078e82014-09-10 14:44:24 -0700227void RegisterLine::CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000228 const RegType& dst_type,
229 const RegType& src_type1, const RegType& src_type2,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700230 bool check_boolean_op) {
Sebastien Hertz5243e912013-05-21 10:55:07 +0200231 const uint32_t vregB = inst->VRegB_23x();
232 const uint32_t vregC = inst->VRegC_23x();
Ian Rogers7b078e82014-09-10 14:44:24 -0700233 if (VerifyRegisterType(verifier, vregB, src_type1) &&
234 VerifyRegisterType(verifier, vregC, src_type2)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700235 if (check_boolean_op) {
236 DCHECK(dst_type.IsInteger());
Ian Rogers7b078e82014-09-10 14:44:24 -0700237 if (GetRegisterType(verifier, vregB).IsBooleanTypes() &&
238 GetRegisterType(verifier, vregC).IsBooleanTypes()) {
Andreas Gampead238ce2015-08-24 21:13:08 -0700239 SetRegisterType<LockOp::kClear>(verifier,
240 inst->VRegA_23x(),
241 verifier->GetRegTypeCache()->Boolean());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700242 return;
243 }
244 }
Andreas Gampead238ce2015-08-24 21:13:08 -0700245 SetRegisterType<LockOp::kClear>(verifier, inst->VRegA_23x(), dst_type);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700246 }
247}
248
Ian Rogers7b078e82014-09-10 14:44:24 -0700249void RegisterLine::CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000250 const RegType& dst_type1, const RegType& dst_type2,
251 const RegType& src_type1_1, const RegType& src_type1_2,
252 const RegType& src_type2_1, const RegType& src_type2_2) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700253 if (VerifyRegisterTypeWide(verifier, inst->VRegB_23x(), src_type1_1, src_type1_2) &&
254 VerifyRegisterTypeWide(verifier, inst->VRegC_23x(), src_type2_1, src_type2_2)) {
255 SetRegisterTypeWide(verifier, inst->VRegA_23x(), dst_type1, dst_type2);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800256 }
257}
258
Ian Rogers7b078e82014-09-10 14:44:24 -0700259void RegisterLine::CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000260 const RegType& long_lo_type, const RegType& long_hi_type,
261 const RegType& int_type) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700262 if (VerifyRegisterTypeWide(verifier, inst->VRegB_23x(), long_lo_type, long_hi_type) &&
263 VerifyRegisterType(verifier, inst->VRegC_23x(), int_type)) {
264 SetRegisterTypeWide(verifier, inst->VRegA_23x(), long_lo_type, long_hi_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800265 }
266}
267
Ian Rogers7b078e82014-09-10 14:44:24 -0700268void RegisterLine::CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000269 const RegType& dst_type, const RegType& src_type1,
270 const RegType& src_type2, bool check_boolean_op) {
Sebastien Hertz5243e912013-05-21 10:55:07 +0200271 const uint32_t vregA = inst->VRegA_12x();
272 const uint32_t vregB = inst->VRegB_12x();
Ian Rogers7b078e82014-09-10 14:44:24 -0700273 if (VerifyRegisterType(verifier, vregA, src_type1) &&
274 VerifyRegisterType(verifier, vregB, src_type2)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700275 if (check_boolean_op) {
276 DCHECK(dst_type.IsInteger());
Ian Rogers7b078e82014-09-10 14:44:24 -0700277 if (GetRegisterType(verifier, vregA).IsBooleanTypes() &&
278 GetRegisterType(verifier, vregB).IsBooleanTypes()) {
Andreas Gampead238ce2015-08-24 21:13:08 -0700279 SetRegisterType<LockOp::kClear>(verifier,
280 vregA,
281 verifier->GetRegTypeCache()->Boolean());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700282 return;
283 }
284 }
Andreas Gampead238ce2015-08-24 21:13:08 -0700285 SetRegisterType<LockOp::kClear>(verifier, vregA, dst_type);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700286 }
287}
288
Ian Rogers7b078e82014-09-10 14:44:24 -0700289void RegisterLine::CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000290 const RegType& dst_type1, const RegType& dst_type2,
291 const RegType& src_type1_1, const RegType& src_type1_2,
292 const RegType& src_type2_1, const RegType& src_type2_2) {
Sebastien Hertz5243e912013-05-21 10:55:07 +0200293 const uint32_t vregA = inst->VRegA_12x();
294 const uint32_t vregB = inst->VRegB_12x();
Ian Rogers7b078e82014-09-10 14:44:24 -0700295 if (VerifyRegisterTypeWide(verifier, vregA, src_type1_1, src_type1_2) &&
296 VerifyRegisterTypeWide(verifier, vregB, src_type2_1, src_type2_2)) {
297 SetRegisterTypeWide(verifier, vregA, dst_type1, dst_type2);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800298 }
299}
300
Ian Rogers7b078e82014-09-10 14:44:24 -0700301void RegisterLine::CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000302 const RegType& long_lo_type, const RegType& long_hi_type,
303 const RegType& int_type) {
Sebastien Hertz5243e912013-05-21 10:55:07 +0200304 const uint32_t vregA = inst->VRegA_12x();
305 const uint32_t vregB = inst->VRegB_12x();
Ian Rogers7b078e82014-09-10 14:44:24 -0700306 if (VerifyRegisterTypeWide(verifier, vregA, long_lo_type, long_hi_type) &&
307 VerifyRegisterType(verifier, vregB, int_type)) {
308 SetRegisterTypeWide(verifier, vregA, long_lo_type, long_hi_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800309 }
310}
311
Ian Rogers7b078e82014-09-10 14:44:24 -0700312void RegisterLine::CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000313 const RegType& dst_type, const RegType& src_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +0200314 bool check_boolean_op, bool is_lit16) {
315 const uint32_t vregA = is_lit16 ? inst->VRegA_22s() : inst->VRegA_22b();
316 const uint32_t vregB = is_lit16 ? inst->VRegB_22s() : inst->VRegB_22b();
Ian Rogers7b078e82014-09-10 14:44:24 -0700317 if (VerifyRegisterType(verifier, vregB, src_type)) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700318 if (check_boolean_op) {
319 DCHECK(dst_type.IsInteger());
320 /* check vB with the call, then check the constant manually */
Sebastien Hertz5243e912013-05-21 10:55:07 +0200321 const uint32_t val = is_lit16 ? inst->VRegC_22s() : inst->VRegC_22b();
Ian Rogers7b078e82014-09-10 14:44:24 -0700322 if (GetRegisterType(verifier, vregB).IsBooleanTypes() && (val == 0 || val == 1)) {
Andreas Gampead238ce2015-08-24 21:13:08 -0700323 SetRegisterType<LockOp::kClear>(verifier,
324 vregA,
325 verifier->GetRegTypeCache()->Boolean());
Ian Rogers776ac1f2012-04-13 23:36:36 -0700326 return;
327 }
328 }
Andreas Gampead238ce2015-08-24 21:13:08 -0700329 SetRegisterType<LockOp::kClear>(verifier, vregA, dst_type);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700330 }
331}
332
Andreas Gampe895bb5f2015-10-14 12:55:48 -0700333static constexpr uint32_t kVirtualNullRegister = std::numeric_limits<uint32_t>::max();
334
Ian Rogers7b078e82014-09-10 14:44:24 -0700335void RegisterLine::PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx) {
336 const RegType& reg_type = GetRegisterType(verifier, reg_idx);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700337 if (!reg_type.IsReferenceTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700338 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter on non-object ("
339 << reg_type << ")";
Ian Rogers776ac1f2012-04-13 23:36:36 -0700340 } else if (monitors_.size() >= 32) {
Andreas Gampea727e372015-08-25 09:22:37 -0700341 verifier->Fail(VERIFY_ERROR_LOCKING);
342 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800343 VLOG(verifier) << "monitor-enter stack overflow while verifying "
David Sehr709b0702016-10-13 09:12:37 -0700344 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700345 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700346 } else {
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800347 if (SetRegToLockDepth(reg_idx, monitors_.size())) {
Andreas Gampe895bb5f2015-10-14 12:55:48 -0700348 // Null literals can establish aliases that we can't easily track. As such, handle the zero
349 // case as the 2^32-1 register (which isn't available in dex bytecode).
350 if (reg_type.IsZero()) {
351 SetRegToLockDepth(kVirtualNullRegister, monitors_.size());
352 }
353
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800354 monitors_.push_back(insn_idx);
355 } else {
Andreas Gampea727e372015-08-25 09:22:37 -0700356 verifier->Fail(VERIFY_ERROR_LOCKING);
357 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800358 VLOG(verifier) << "unexpected monitor-enter on register v" << reg_idx << " in "
David Sehr709b0702016-10-13 09:12:37 -0700359 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700360 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800361 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700362 }
363}
364
Ian Rogers7b078e82014-09-10 14:44:24 -0700365void RegisterLine::PopMonitor(MethodVerifier* verifier, uint32_t reg_idx) {
366 const RegType& reg_type = GetRegisterType(verifier, reg_idx);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700367 if (!reg_type.IsReferenceTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit on non-object (" << reg_type << ")";
Ian Rogers776ac1f2012-04-13 23:36:36 -0700369 } else if (monitors_.empty()) {
Andreas Gampea727e372015-08-25 09:22:37 -0700370 verifier->Fail(VERIFY_ERROR_LOCKING);
371 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800372 VLOG(verifier) << "monitor-exit stack underflow while verifying "
David Sehr709b0702016-10-13 09:12:37 -0700373 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700374 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700375 } else {
376 monitors_.pop_back();
Andreas Gampe895bb5f2015-10-14 12:55:48 -0700377
378 bool success = IsSetLockDepth(reg_idx, monitors_.size());
379
380 if (!success && reg_type.IsZero()) {
381 // Null literals can establish aliases that we can't easily track. As such, handle the zero
382 // case as the 2^32-1 register (which isn't available in dex bytecode).
383 success = IsSetLockDepth(kVirtualNullRegister, monitors_.size());
384 if (success) {
385 reg_idx = kVirtualNullRegister;
386 }
387 }
388
389 if (!success) {
Andreas Gampea727e372015-08-25 09:22:37 -0700390 verifier->Fail(VERIFY_ERROR_LOCKING);
391 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800392 VLOG(verifier) << "monitor-exit not unlocking the top of the monitor stack while verifying "
David Sehr709b0702016-10-13 09:12:37 -0700393 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700394 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700395 } else {
Andreas Gampe895bb5f2015-10-14 12:55:48 -0700396 // Record the register was unlocked. This clears all aliases, thus it will also clear the
397 // null lock, if necessary.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700398 ClearRegToLockDepth(reg_idx, monitors_.size());
399 }
400 }
401}
402
Mathieu Chartierde40d472015-10-15 17:47:48 -0700403bool FindLockAliasedRegister(uint32_t src,
404 const RegisterLine::RegToLockDepthsMap& src_map,
405 const RegisterLine::RegToLockDepthsMap& search_map) {
Andreas Gampe27583182015-10-09 19:13:39 -0700406 auto it = src_map.find(src);
407 if (it == src_map.end()) {
408 // "Not locked" is trivially aliased.
409 return true;
410 }
411 uint32_t src_lock_levels = it->second;
412 if (src_lock_levels == 0) {
413 // "Not locked" is trivially aliased.
414 return true;
415 }
416
417 // Scan the map for the same value.
Andreas Gampe1eeb00c2016-11-03 08:19:01 -0700418 for (const std::pair<const uint32_t, uint32_t>& pair : search_map) {
Andreas Gampe27583182015-10-09 19:13:39 -0700419 if (pair.first != src && pair.second == src_lock_levels) {
420 return true;
421 }
422 }
423
424 // Nothing found, no alias.
425 return false;
426}
427
Ian Rogers7b078e82014-09-10 14:44:24 -0700428bool RegisterLine::MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700429 bool changed = false;
Ian Rogersd0fbd852013-09-24 18:17:04 -0700430 DCHECK(incoming_line != nullptr);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700431 for (size_t idx = 0; idx < num_regs_; idx++) {
432 if (line_[idx] != incoming_line->line_[idx]) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700433 const RegType& incoming_reg_type = incoming_line->GetRegisterType(verifier, idx);
434 const RegType& cur_type = GetRegisterType(verifier, idx);
David Brazdilca3c8c32016-09-06 14:04:48 +0100435 const RegType& new_type = cur_type.Merge(
436 incoming_reg_type, verifier->GetRegTypeCache(), verifier);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700437 changed = changed || !cur_type.Equals(new_type);
438 line_[idx] = new_type.GetId();
439 }
440 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700441 if (monitors_.size() > 0 || incoming_line->monitors_.size() > 0) {
442 if (monitors_.size() != incoming_line->monitors_.size()) {
Andreas Gampea727e372015-08-25 09:22:37 -0700443 verifier->Fail(VERIFY_ERROR_LOCKING);
444 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800445 VLOG(verifier) << "mismatched stack depths (depth=" << MonitorStackDepth()
446 << ", incoming depth=" << incoming_line->MonitorStackDepth() << ") in "
David Sehr709b0702016-10-13 09:12:37 -0700447 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700448 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700449 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
450 for (uint32_t idx = 0; idx < num_regs_; idx++) {
451 size_t depths = reg_to_lock_depths_.count(idx);
452 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
453 if (depths != incoming_depths) {
Andreas Gampe27583182015-10-09 19:13:39 -0700454 // Stack levels aren't matching. This is potentially bad, as we don't do a
455 // flow-sensitive analysis.
456 // However, this could be an alias of something locked in one path, and the alias was
457 // destroyed in another path. It is fine to drop this as long as there's another alias
458 // for the lock around. The last vanishing alias will then report that things would be
459 // left unlocked. We need to check for aliases for both lock levels.
460 //
461 // Example (lock status in curly braces as pair of register and lock leels):
462 //
463 // lock v1 {v1=1}
Andreas Gampe0a67a9c2015-10-14 13:26:49 -0700464 // | |
Andreas Gampe27583182015-10-09 19:13:39 -0700465 // v0 = v1 {v0=1, v1=1} v0 = v2 {v1=1}
Andreas Gampe0a67a9c2015-10-14 13:26:49 -0700466 // | |
Andreas Gampe27583182015-10-09 19:13:39 -0700467 // {v1=1}
468 // // Dropping v0, as the status can't be merged
469 // // but the lock info ("locked at depth 1" and)
470 // // "not locked at all") is available.
471 if (!FindLockAliasedRegister(idx,
472 reg_to_lock_depths_,
473 reg_to_lock_depths_) ||
474 !FindLockAliasedRegister(idx,
475 incoming_line->reg_to_lock_depths_,
476 reg_to_lock_depths_)) {
Andreas Gampea727e372015-08-25 09:22:37 -0700477 verifier->Fail(VERIFY_ERROR_LOCKING);
478 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800479 VLOG(verifier) << "mismatched stack depths for register v" << idx
480 << ": " << depths << " != " << incoming_depths << " in "
David Sehr709b0702016-10-13 09:12:37 -0700481 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampea727e372015-08-25 09:22:37 -0700482 }
483 break;
484 }
Andreas Gampe27583182015-10-09 19:13:39 -0700485 // We found aliases, set this to zero.
486 reg_to_lock_depths_.erase(idx);
Andreas Gampea727e372015-08-25 09:22:37 -0700487 } else if (depths > 0) {
488 // Check whether they're actually the same levels.
489 uint32_t locked_levels = reg_to_lock_depths_.find(idx)->second;
490 uint32_t incoming_locked_levels = incoming_line->reg_to_lock_depths_.find(idx)->second;
491 if (locked_levels != incoming_locked_levels) {
Andreas Gampe27583182015-10-09 19:13:39 -0700492 // Lock levels aren't matching. This is potentially bad, as we don't do a
493 // flow-sensitive analysis.
494 // However, this could be an alias of something locked in one path, and the alias was
495 // destroyed in another path. It is fine to drop this as long as there's another alias
496 // for the lock around. The last vanishing alias will then report that things would be
497 // left unlocked. We need to check for aliases for both lock levels.
498 //
499 // Example (lock status in curly braces as pair of register and lock leels):
500 //
501 // lock v1 {v1=1}
502 // lock v2 {v1=1, v2=2}
Andreas Gampe0a67a9c2015-10-14 13:26:49 -0700503 // | |
Andreas Gampe27583182015-10-09 19:13:39 -0700504 // v0 = v1 {v0=1, v1=1, v2=2} v0 = v2 {v0=2, v1=1, v2=2}
Andreas Gampe0a67a9c2015-10-14 13:26:49 -0700505 // | |
Andreas Gampe27583182015-10-09 19:13:39 -0700506 // {v1=1, v2=2}
507 // // Dropping v0, as the status can't be
508 // // merged but the lock info ("locked at
509 // // depth 1" and "locked at depth 2") is
510 // // available.
511 if (!FindLockAliasedRegister(idx,
512 reg_to_lock_depths_,
513 reg_to_lock_depths_) ||
514 !FindLockAliasedRegister(idx,
515 incoming_line->reg_to_lock_depths_,
516 reg_to_lock_depths_)) {
517 // No aliases for both current and incoming, we'll lose information.
518 verifier->Fail(VERIFY_ERROR_LOCKING);
519 if (kDumpLockFailures) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800520 VLOG(verifier) << "mismatched lock levels for register v" << idx << ": "
521 << std::hex << locked_levels << std::dec << " != "
522 << std::hex << incoming_locked_levels << std::dec << " in "
David Sehr709b0702016-10-13 09:12:37 -0700523 << verifier->GetMethodReference().PrettyMethod();
Andreas Gampe27583182015-10-09 19:13:39 -0700524 }
525 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700526 }
Andreas Gampe27583182015-10-09 19:13:39 -0700527 // We found aliases, set this to zero.
528 reg_to_lock_depths_.erase(idx);
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700530 }
531 }
532 }
533 }
Andreas Gampea727e372015-08-25 09:22:37 -0700534
Andreas Gampef10b6e12015-08-12 10:48:12 -0700535 // Check whether "this" was initialized in both paths.
536 if (this_initialized_ && !incoming_line->this_initialized_) {
537 this_initialized_ = false;
538 changed = true;
539 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700540 return changed;
541}
542
Ian Rogers776ac1f2012-04-13 23:36:36 -0700543} // namespace verifier
544} // namespace art