1 /*
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
19 #include "android-base/stringprintf.h"
20
21 #include "dex/dex_instruction-inl.h"
22 #include "method_verifier-inl.h"
23 #include "reg_type-inl.h"
24 #include "register_line-inl.h"
25
26 namespace art HIDDEN {
27 namespace verifier {
28
29 using android::base::StringPrintf;
30
CheckConstructorReturn(MethodVerifier * verifier) const31 bool RegisterLine::CheckConstructorReturn(MethodVerifier* verifier) const {
32 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 "
39 << verifier->GetMethodReference().PrettyMethod();
40 }
41 }
42 if (!this_initialized_) {
43 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
44 << "Constructor returning without calling superclass constructor";
45 }
46 return this_initialized_;
47 }
48
GetInvocationThis(MethodVerifier * verifier,const Instruction * inst,bool allow_failure)49 const RegType& RegisterLine::GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
50 bool allow_failure) {
51 DCHECK(inst->IsInvoke());
52 const size_t args_count = inst->VRegA();
53 if (args_count < 1) {
54 if (!allow_failure) {
55 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke lacks 'this'";
56 }
57 return verifier->GetRegTypeCache()->Conflict();
58 }
59 /* Get the element type of the array held in vsrc */
60 const uint32_t this_reg = inst->VRegC();
61 const RegType& this_type = GetRegisterType(verifier, this_reg);
62 if (!this_type.IsReferenceTypes()) {
63 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 }
68 return verifier->GetRegTypeCache()->Conflict();
69 }
70 return this_type;
71 }
72
VerifyRegisterTypeWide(MethodVerifier * verifier,uint32_t vsrc,const RegType & check_type1,const RegType & check_type2)73 bool RegisterLine::VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc,
74 const RegType& check_type1,
75 const RegType& check_type2) {
76 DCHECK(check_type1.CheckWidePair(check_type2));
77 // Verify the src register type against the check type refining the type of the register
78 const RegType& src_type = GetRegisterType(verifier, vsrc);
79 if (!check_type1.IsAssignableFrom(src_type, verifier)) {
80 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << vsrc << " has type " << src_type
81 << " but expected " << check_type1;
82 return false;
83 }
84 const RegType& src_type_h = GetRegisterType(verifier, vsrc + 1);
85 if (!src_type.CheckWidePair(src_type_h)) {
86 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register v" << vsrc << " has type "
87 << 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
MarkRefsAsInitialized(MethodVerifier * verifier,const RegType & uninit_type)97 void RegisterLine::MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type) {
98 DCHECK(uninit_type.IsUninitializedTypes());
99 const RegType& init_type = verifier->GetRegTypeCache()->FromUninitialized(uninit_type);
100 size_t changed = 0;
101 for (uint32_t i = 0; i < num_regs_; i++) {
102 if (GetRegisterType(verifier, i).Equals(uninit_type)) {
103 line_[i] = init_type.GetId();
104 changed++;
105 }
106 }
107 // Is this initializing "this"?
108 if (uninit_type.IsUninitializedThisReference() ||
109 uninit_type.IsUnresolvedAndUninitializedThisReference()) {
110 this_initialized_ = true;
111 }
112 DCHECK_GT(changed, 0u);
113 }
114
MarkAllRegistersAsConflicts(MethodVerifier * verifier)115 void RegisterLine::MarkAllRegistersAsConflicts(MethodVerifier* verifier) {
116 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
117 for (uint32_t i = 0; i < num_regs_; i++) {
118 line_[i] = conflict_type_id;
119 }
120 }
121
MarkAllRegistersAsConflictsExcept(MethodVerifier * verifier,uint32_t vsrc)122 void RegisterLine::MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc) {
123 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
124 for (uint32_t i = 0; i < num_regs_; i++) {
125 if (i != vsrc) {
126 line_[i] = conflict_type_id;
127 }
128 }
129 }
130
MarkAllRegistersAsConflictsExceptWide(MethodVerifier * verifier,uint32_t vsrc)131 void RegisterLine::MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc) {
132 uint16_t conflict_type_id = verifier->GetRegTypeCache()->Conflict().GetId();
133 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
Dump(MethodVerifier * verifier) const140 std::string RegisterLine::Dump(MethodVerifier* verifier) const {
141 std::string result;
142 for (size_t i = 0; i < num_regs_; i++) {
143 result += StringPrintf("%zd:[", i);
144 result += GetRegisterType(verifier, i).Dump();
145 result += "],";
146 }
147 for (const auto& monitor : monitors_) {
148 result += StringPrintf("{%d},", monitor);
149 }
150 for (auto& pairs : reg_to_lock_depths_) {
151 result += StringPrintf("<%d -> %" PRIx64 ">",
152 pairs.first,
153 static_cast<uint64_t>(pairs.second));
154 }
155 return result;
156 }
157
MarkUninitRefsAsInvalid(MethodVerifier * verifier,const RegType & uninit_type)158 void RegisterLine::MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type) {
159 for (size_t i = 0; i < num_regs_; i++) {
160 if (GetRegisterType(verifier, i).Equals(uninit_type)) {
161 line_[i] = verifier->GetRegTypeCache()->Conflict().GetId();
162 ClearAllRegToLockDepths(i);
163 }
164 }
165 }
166
CopyResultRegister1(MethodVerifier * verifier,uint32_t vdst,bool is_reference)167 void RegisterLine::CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference) {
168 const RegType& type = verifier->GetRegTypeCache()->GetFromId(result_[0]);
169 if ((!is_reference && !type.IsCategory1Types()) ||
170 (is_reference && !type.IsReferenceTypes())) {
171 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
172 << "copyRes1 v" << vdst << "<- result0" << " type=" << type;
173 } else {
174 DCHECK(verifier->GetRegTypeCache()->GetFromId(result_[1]).IsUndefined());
175 SetRegisterType<LockOp::kClear>(vdst, type);
176 result_[0] = verifier->GetRegTypeCache()->Undefined().GetId();
177 }
178 }
179
180 /*
181 * Implement "move-result-wide". Copy the category-2 value from the result
182 * register to another register, and reset the result register.
183 */
CopyResultRegister2(MethodVerifier * verifier,uint32_t vdst)184 void RegisterLine::CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst) {
185 const RegType& type_l = verifier->GetRegTypeCache()->GetFromId(result_[0]);
186 const RegType& type_h = verifier->GetRegTypeCache()->GetFromId(result_[1]);
187 if (!type_l.IsCategory2Types()) {
188 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD)
189 << "copyRes2 v" << vdst << "<- result0" << " type=" << type_l;
190 } else {
191 DCHECK(type_l.CheckWidePair(type_h)); // Set should never allow this case
192 SetRegisterTypeWide(vdst, type_l, type_h); // also sets the high
193 result_[0] = verifier->GetRegTypeCache()->Undefined().GetId();
194 result_[1] = verifier->GetRegTypeCache()->Undefined().GetId();
195 }
196 }
197
CheckUnaryOp(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type,const RegType & src_type)198 void RegisterLine::CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst,
199 const RegType& dst_type, const RegType& src_type) {
200 if (VerifyRegisterType(verifier, inst->VRegB_12x(), src_type)) {
201 SetRegisterType<LockOp::kClear>(inst->VRegA_12x(), dst_type);
202 }
203 }
204
CheckUnaryOpWide(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type1,const RegType & dst_type2,const RegType & src_type1,const RegType & src_type2)205 void RegisterLine::CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
206 const RegType& dst_type1, const RegType& dst_type2,
207 const RegType& src_type1, const RegType& src_type2) {
208 if (VerifyRegisterTypeWide(verifier, inst->VRegB_12x(), src_type1, src_type2)) {
209 SetRegisterTypeWide(inst->VRegA_12x(), dst_type1, dst_type2);
210 }
211 }
212
CheckUnaryOpToWide(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type1,const RegType & dst_type2,const RegType & src_type)213 void RegisterLine::CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
214 const RegType& dst_type1, const RegType& dst_type2,
215 const RegType& src_type) {
216 if (VerifyRegisterType(verifier, inst->VRegB_12x(), src_type)) {
217 SetRegisterTypeWide(inst->VRegA_12x(), dst_type1, dst_type2);
218 }
219 }
220
CheckUnaryOpFromWide(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type,const RegType & src_type1,const RegType & src_type2)221 void RegisterLine::CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
222 const RegType& dst_type,
223 const RegType& src_type1, const RegType& src_type2) {
224 if (VerifyRegisterTypeWide(verifier, inst->VRegB_12x(), src_type1, src_type2)) {
225 SetRegisterType<LockOp::kClear>(inst->VRegA_12x(), dst_type);
226 }
227 }
228
CheckBinaryOp(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type,const RegType & src_type1,const RegType & src_type2,bool check_boolean_op)229 void RegisterLine::CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
230 const RegType& dst_type,
231 const RegType& src_type1, const RegType& src_type2,
232 bool check_boolean_op) {
233 const uint32_t vregB = inst->VRegB_23x();
234 const uint32_t vregC = inst->VRegC_23x();
235 if (VerifyRegisterType(verifier, vregB, src_type1) &&
236 VerifyRegisterType(verifier, vregC, src_type2)) {
237 if (check_boolean_op) {
238 DCHECK(dst_type.IsInteger());
239 if (GetRegisterType(verifier, vregB).IsBooleanTypes() &&
240 GetRegisterType(verifier, vregC).IsBooleanTypes()) {
241 SetRegisterType<LockOp::kClear>(inst->VRegA_23x(), verifier->GetRegTypeCache()->Boolean());
242 return;
243 }
244 }
245 SetRegisterType<LockOp::kClear>(inst->VRegA_23x(), dst_type);
246 }
247 }
248
CheckBinaryOpWide(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type1,const RegType & dst_type2,const RegType & src_type1_1,const RegType & src_type1_2,const RegType & src_type2_1,const RegType & src_type2_2)249 void RegisterLine::CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
250 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) {
253 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(inst->VRegA_23x(), dst_type1, dst_type2);
256 }
257 }
258
CheckBinaryOpWideShift(MethodVerifier * verifier,const Instruction * inst,const RegType & long_lo_type,const RegType & long_hi_type,const RegType & int_type)259 void RegisterLine::CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
260 const RegType& long_lo_type, const RegType& long_hi_type,
261 const RegType& int_type) {
262 if (VerifyRegisterTypeWide(verifier, inst->VRegB_23x(), long_lo_type, long_hi_type) &&
263 VerifyRegisterType(verifier, inst->VRegC_23x(), int_type)) {
264 SetRegisterTypeWide(inst->VRegA_23x(), long_lo_type, long_hi_type);
265 }
266 }
267
CheckBinaryOp2addr(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type,const RegType & src_type1,const RegType & src_type2,bool check_boolean_op)268 void RegisterLine::CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
269 const RegType& dst_type, const RegType& src_type1,
270 const RegType& src_type2, bool check_boolean_op) {
271 const uint32_t vregA = inst->VRegA_12x();
272 const uint32_t vregB = inst->VRegB_12x();
273 if (VerifyRegisterType(verifier, vregA, src_type1) &&
274 VerifyRegisterType(verifier, vregB, src_type2)) {
275 if (check_boolean_op) {
276 DCHECK(dst_type.IsInteger());
277 if (GetRegisterType(verifier, vregA).IsBooleanTypes() &&
278 GetRegisterType(verifier, vregB).IsBooleanTypes()) {
279 SetRegisterType<LockOp::kClear>(vregA, verifier->GetRegTypeCache()->Boolean());
280 return;
281 }
282 }
283 SetRegisterType<LockOp::kClear>(vregA, dst_type);
284 }
285 }
286
CheckBinaryOp2addrWide(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type1,const RegType & dst_type2,const RegType & src_type1_1,const RegType & src_type1_2,const RegType & src_type2_1,const RegType & src_type2_2)287 void RegisterLine::CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
288 const RegType& dst_type1, const RegType& dst_type2,
289 const RegType& src_type1_1, const RegType& src_type1_2,
290 const RegType& src_type2_1, const RegType& src_type2_2) {
291 const uint32_t vregA = inst->VRegA_12x();
292 const uint32_t vregB = inst->VRegB_12x();
293 if (VerifyRegisterTypeWide(verifier, vregA, src_type1_1, src_type1_2) &&
294 VerifyRegisterTypeWide(verifier, vregB, src_type2_1, src_type2_2)) {
295 SetRegisterTypeWide(vregA, dst_type1, dst_type2);
296 }
297 }
298
CheckBinaryOp2addrWideShift(MethodVerifier * verifier,const Instruction * inst,const RegType & long_lo_type,const RegType & long_hi_type,const RegType & int_type)299 void RegisterLine::CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
300 const RegType& long_lo_type, const RegType& long_hi_type,
301 const RegType& int_type) {
302 const uint32_t vregA = inst->VRegA_12x();
303 const uint32_t vregB = inst->VRegB_12x();
304 if (VerifyRegisterTypeWide(verifier, vregA, long_lo_type, long_hi_type) &&
305 VerifyRegisterType(verifier, vregB, int_type)) {
306 SetRegisterTypeWide(vregA, long_lo_type, long_hi_type);
307 }
308 }
309
CheckLiteralOp(MethodVerifier * verifier,const Instruction * inst,const RegType & dst_type,const RegType & src_type,bool check_boolean_op,bool is_lit16)310 void RegisterLine::CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
311 const RegType& dst_type, const RegType& src_type,
312 bool check_boolean_op, bool is_lit16) {
313 const uint32_t vregA = is_lit16 ? inst->VRegA_22s() : inst->VRegA_22b();
314 const uint32_t vregB = is_lit16 ? inst->VRegB_22s() : inst->VRegB_22b();
315 if (VerifyRegisterType(verifier, vregB, src_type)) {
316 if (check_boolean_op) {
317 DCHECK(dst_type.IsInteger());
318 /* check vB with the call, then check the constant manually */
319 const uint32_t val = is_lit16 ? inst->VRegC_22s() : inst->VRegC_22b();
320 if (GetRegisterType(verifier, vregB).IsBooleanTypes() && (val == 0 || val == 1)) {
321 SetRegisterType<LockOp::kClear>(vregA, verifier->GetRegTypeCache()->Boolean());
322 return;
323 }
324 }
325 SetRegisterType<LockOp::kClear>(vregA, dst_type);
326 }
327 }
328
329 static constexpr uint32_t kVirtualNullRegister = std::numeric_limits<uint32_t>::max();
330
PushMonitor(MethodVerifier * verifier,uint32_t reg_idx,int32_t insn_idx)331 void RegisterLine::PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx) {
332 const RegType& reg_type = GetRegisterType(verifier, reg_idx);
333 if (!reg_type.IsReferenceTypes()) {
334 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-enter on non-object ("
335 << reg_type << ")";
336 } else if (monitors_.size() >= kMaxMonitorStackDepth) {
337 verifier->Fail(VERIFY_ERROR_LOCKING);
338 if (kDumpLockFailures) {
339 VLOG(verifier) << "monitor-enter stack overflow while verifying "
340 << verifier->GetMethodReference().PrettyMethod();
341 }
342 } else {
343 if (SetRegToLockDepth(reg_idx, monitors_.size())) {
344 // Null literals can establish aliases that we can't easily track. As such, handle the zero
345 // case as the 2^32-1 register (which isn't available in dex bytecode).
346 if (reg_type.IsZero()) {
347 SetRegToLockDepth(kVirtualNullRegister, monitors_.size());
348 }
349
350 monitors_.push_back(insn_idx);
351 } else {
352 verifier->Fail(VERIFY_ERROR_LOCKING);
353 if (kDumpLockFailures) {
354 VLOG(verifier) << "unexpected monitor-enter on register v" << reg_idx << " in "
355 << verifier->GetMethodReference().PrettyMethod();
356 }
357 }
358 }
359 }
360
PopMonitor(MethodVerifier * verifier,uint32_t reg_idx)361 void RegisterLine::PopMonitor(MethodVerifier* verifier, uint32_t reg_idx) {
362 const RegType& reg_type = GetRegisterType(verifier, reg_idx);
363 if (!reg_type.IsReferenceTypes()) {
364 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "monitor-exit on non-object (" << reg_type << ")";
365 } else if (monitors_.empty()) {
366 verifier->Fail(VERIFY_ERROR_LOCKING);
367 if (kDumpLockFailures) {
368 VLOG(verifier) << "monitor-exit stack underflow while verifying "
369 << verifier->GetMethodReference().PrettyMethod();
370 }
371 } else {
372 monitors_.pop_back();
373
374 bool success = IsSetLockDepth(reg_idx, monitors_.size());
375
376 if (!success && reg_type.IsZero()) {
377 // Null literals can establish aliases that we can't easily track. As such, handle the zero
378 // case as the 2^32-1 register (which isn't available in dex bytecode).
379 success = IsSetLockDepth(kVirtualNullRegister, monitors_.size());
380 if (success) {
381 reg_idx = kVirtualNullRegister;
382 }
383 }
384
385 if (!success) {
386 verifier->Fail(VERIFY_ERROR_LOCKING);
387 if (kDumpLockFailures) {
388 VLOG(verifier) << "monitor-exit not unlocking the top of the monitor stack while verifying "
389 << verifier->GetMethodReference().PrettyMethod();
390 }
391 } else {
392 // Record the register was unlocked. This clears all aliases, thus it will also clear the
393 // null lock, if necessary.
394 ClearRegToLockDepth(reg_idx, monitors_.size());
395 }
396 }
397 }
398
FindLockAliasedRegister(uint32_t src,const RegisterLine::RegToLockDepthsMap & src_map,const RegisterLine::RegToLockDepthsMap & search_map)399 bool FindLockAliasedRegister(uint32_t src,
400 const RegisterLine::RegToLockDepthsMap& src_map,
401 const RegisterLine::RegToLockDepthsMap& search_map) {
402 auto it = src_map.find(src);
403 if (it == src_map.end()) {
404 // "Not locked" is trivially aliased.
405 return true;
406 }
407 uint32_t src_lock_levels = it->second;
408 if (src_lock_levels == 0) {
409 // "Not locked" is trivially aliased.
410 return true;
411 }
412
413 // Scan the map for the same value.
414 for (const std::pair<const uint32_t, uint32_t>& pair : search_map) {
415 if (pair.first != src && pair.second == src_lock_levels) {
416 return true;
417 }
418 }
419
420 // Nothing found, no alias.
421 return false;
422 }
423
MergeRegisters(MethodVerifier * verifier,const RegisterLine * incoming_line)424 bool RegisterLine::MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line) {
425 bool changed = false;
426 DCHECK(incoming_line != nullptr);
427 for (size_t idx = 0; idx < num_regs_; idx++) {
428 if (line_[idx] != incoming_line->line_[idx]) {
429 const RegType& incoming_reg_type = incoming_line->GetRegisterType(verifier, idx);
430 const RegType& cur_type = GetRegisterType(verifier, idx);
431 const RegType& new_type = cur_type.Merge(
432 incoming_reg_type, verifier->GetRegTypeCache(), verifier);
433 changed = changed || !cur_type.Equals(new_type);
434 line_[idx] = new_type.GetId();
435 }
436 }
437 if (monitors_.size() > 0 || incoming_line->monitors_.size() > 0) {
438 if (monitors_.size() != incoming_line->monitors_.size()) {
439 verifier->Fail(VERIFY_ERROR_LOCKING, /*pending_exc=*/ false);
440 if (kDumpLockFailures) {
441 VLOG(verifier) << "mismatched stack depths (depth=" << MonitorStackDepth()
442 << ", incoming depth=" << incoming_line->MonitorStackDepth() << ") in "
443 << verifier->GetMethodReference().PrettyMethod();
444 }
445 } else if (reg_to_lock_depths_ != incoming_line->reg_to_lock_depths_) {
446 for (uint32_t idx = 0; idx < num_regs_; idx++) {
447 size_t depths = reg_to_lock_depths_.count(idx);
448 size_t incoming_depths = incoming_line->reg_to_lock_depths_.count(idx);
449 if (depths != incoming_depths) {
450 // Stack levels aren't matching. This is potentially bad, as we don't do a
451 // flow-sensitive analysis.
452 // However, this could be an alias of something locked in one path, and the alias was
453 // destroyed in another path. It is fine to drop this as long as there's another alias
454 // for the lock around. The last vanishing alias will then report that things would be
455 // left unlocked. We need to check for aliases for both lock levels.
456 //
457 // Example (lock status in curly braces as pair of register and lock leels):
458 //
459 // lock v1 {v1=1}
460 // | |
461 // v0 = v1 {v0=1, v1=1} v0 = v2 {v1=1}
462 // | |
463 // {v1=1}
464 // // Dropping v0, as the status can't be merged
465 // // but the lock info ("locked at depth 1" and)
466 // // "not locked at all") is available.
467 if (!FindLockAliasedRegister(idx,
468 reg_to_lock_depths_,
469 reg_to_lock_depths_) ||
470 !FindLockAliasedRegister(idx,
471 incoming_line->reg_to_lock_depths_,
472 reg_to_lock_depths_)) {
473 verifier->Fail(VERIFY_ERROR_LOCKING, /*pending_exc=*/ false);
474 if (kDumpLockFailures) {
475 VLOG(verifier) << "mismatched stack depths for register v" << idx
476 << ": " << depths << " != " << incoming_depths << " in "
477 << verifier->GetMethodReference().PrettyMethod();
478 }
479 break;
480 }
481 // We found aliases, set this to zero.
482 reg_to_lock_depths_.erase(idx);
483 } else if (depths > 0) {
484 // Check whether they're actually the same levels.
485 uint32_t locked_levels = reg_to_lock_depths_.find(idx)->second;
486 uint32_t incoming_locked_levels = incoming_line->reg_to_lock_depths_.find(idx)->second;
487 if (locked_levels != incoming_locked_levels) {
488 // Lock levels aren't matching. This is potentially bad, as we don't do a
489 // flow-sensitive analysis.
490 // However, this could be an alias of something locked in one path, and the alias was
491 // destroyed in another path. It is fine to drop this as long as there's another alias
492 // for the lock around. The last vanishing alias will then report that things would be
493 // left unlocked. We need to check for aliases for both lock levels.
494 //
495 // Example (lock status in curly braces as pair of register and lock leels):
496 //
497 // lock v1 {v1=1}
498 // lock v2 {v1=1, v2=2}
499 // | |
500 // v0 = v1 {v0=1, v1=1, v2=2} v0 = v2 {v0=2, v1=1, v2=2}
501 // | |
502 // {v1=1, v2=2}
503 // // Dropping v0, as the status can't be
504 // // merged but the lock info ("locked at
505 // // depth 1" and "locked at depth 2") is
506 // // available.
507 if (!FindLockAliasedRegister(idx,
508 reg_to_lock_depths_,
509 reg_to_lock_depths_) ||
510 !FindLockAliasedRegister(idx,
511 incoming_line->reg_to_lock_depths_,
512 reg_to_lock_depths_)) {
513 // No aliases for both current and incoming, we'll lose information.
514 verifier->Fail(VERIFY_ERROR_LOCKING, /*pending_exc=*/ false);
515 if (kDumpLockFailures) {
516 VLOG(verifier) << "mismatched lock levels for register v" << idx << ": "
517 << std::hex << locked_levels << std::dec << " != "
518 << std::hex << incoming_locked_levels << std::dec << " in "
519 << verifier->GetMethodReference().PrettyMethod();
520 }
521 break;
522 }
523 // We found aliases, set this to zero.
524 reg_to_lock_depths_.erase(idx);
525 }
526 }
527 }
528 }
529 }
530
531 // Check whether "this" was initialized in both paths.
532 if (this_initialized_ && !incoming_line->this_initialized_) {
533 this_initialized_ = false;
534 changed = true;
535 }
536 return changed;
537 }
538
539 } // namespace verifier
540 } // namespace art
541