blob: ca61a0b8f09ec3150e7b5909c05cfe88f6b73a96 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
18#define ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
Ian Rogers776ac1f2012-04-13 23:36:36 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Ian Rogers776ac1f2012-04-13 23:36:36 -070021#include <vector>
22
Ian Rogers776ac1f2012-04-13 23:36:36 -070023#include "safe_map.h"
24
25namespace art {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080026
27class Instruction;
28
Ian Rogers776ac1f2012-04-13 23:36:36 -070029namespace verifier {
30
31class MethodVerifier;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080032class RegType;
Ian Rogers776ac1f2012-04-13 23:36:36 -070033
34/*
35 * Register type categories, for type checking.
36 *
37 * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and
38 * returnAddress. Category 2 includes long and double.
39 *
40 * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so
41 * there is no "returnAddress" type.
42 */
43enum TypeCategory {
44 kTypeCategoryUnknown = 0,
45 kTypeCategory1nr = 1, // boolean, byte, char, short, int, float
46 kTypeCategory2 = 2, // long, double
47 kTypeCategoryRef = 3, // object reference
48};
49
50// During verification, we associate one of these with every "interesting" instruction. We track
51// the status of all registers, and (if the method has any monitor-enter instructions) maintain a
52// stack of entered monitors (identified by code unit offset).
Ian Rogers776ac1f2012-04-13 23:36:36 -070053class RegisterLine {
54 public:
Ian Rogersd0fbd852013-09-24 18:17:04 -070055 static RegisterLine* Create(size_t num_regs, MethodVerifier* verifier) {
Mathieu Chartier6afd4092014-06-06 13:59:39 -070056 void* memory = operator new(sizeof(RegisterLine) + (num_regs * sizeof(uint16_t)));
Ian Rogersd0fbd852013-09-24 18:17:04 -070057 RegisterLine* rl = new (memory) RegisterLine(num_regs, verifier);
58 return rl;
Ian Rogers776ac1f2012-04-13 23:36:36 -070059 }
60
61 // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst".
Ian Rogers7b078e82014-09-10 14:44:24 -070062 void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat)
Ian Rogersb726dcb2012-09-05 08:57:23 -070063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070064
65 // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This
66 // copies both halves of the register.
Ian Rogers7b078e82014-09-10 14:44:24 -070067 void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc)
Ian Rogersb726dcb2012-09-05 08:57:23 -070068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070069
70 // Implement "move-result". Copy the category-1 value from the result register to another
71 // register, and reset the result register.
Ian Rogers7b078e82014-09-10 14:44:24 -070072 void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference)
Ian Rogersb726dcb2012-09-05 08:57:23 -070073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070074
75 // Implement "move-result-wide". Copy the category-2 value from the result register to another
76 // register, and reset the result register.
Ian Rogers7b078e82014-09-10 14:44:24 -070077 void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070079
80 // Set the invisible result register to unknown
Ian Rogers7b078e82014-09-10 14:44:24 -070081 void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070082
83 // Set the type of register N, verifying that the register is valid. If "newType" is the "Lo"
84 // part of a 64-bit value, register N+1 will be set to "newType+1".
85 // The register index was validated during the static pass, so we don't need to check it here.
Ian Rogers7b078e82014-09-10 14:44:24 -070086 ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier, uint32_t vdst,
87 const RegType& new_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070089
Ian Rogers7b078e82014-09-10 14:44:24 -070090 bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1,
91 const RegType& new_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -080092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
Ian Rogers776ac1f2012-04-13 23:36:36 -070094 /* Set the type of the "result" register. */
Ian Rogers7b078e82014-09-10 14:44:24 -070095 void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070097
Ian Rogersd8f69b02014-09-10 21:43:52 +000098 void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -080099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100
Ian Rogers776ac1f2012-04-13 23:36:36 -0700101 // Get the type of register vsrc.
Ian Rogers7b078e82014-09-10 14:44:24 -0700102 const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700103
Ian Rogers7b078e82014-09-10 14:44:24 -0700104 ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc,
105 const RegType& check_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700106 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107
Ian Rogers7b078e82014-09-10 14:44:24 -0700108 bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1,
109 const RegType& check_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111
Ian Rogers776ac1f2012-04-13 23:36:36 -0700112 void CopyFromLine(const RegisterLine* src) {
113 DCHECK_EQ(num_regs_, src->num_regs_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700114 memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700115 monitors_ = src->monitors_;
116 reg_to_lock_depths_ = src->reg_to_lock_depths_;
117 }
118
Ian Rogers7b078e82014-09-10 14:44:24 -0700119 std::string Dump(MethodVerifier* verifier) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700120
121 void FillWithGarbage() {
Ian Rogersd0fbd852013-09-24 18:17:04 -0700122 memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
Logan Chiendb0cccd2014-09-30 19:07:55 +0800123 monitors_.clear();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700124 reg_to_lock_depths_.clear();
125 }
126
127 /*
128 * We're creating a new instance of class C at address A. Any registers holding instances
129 * previously created at address A must be initialized by now. If not, we mark them as "conflict"
130 * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and
131 * the new ones at the same time).
132 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700133 void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700135
136 /*
137 * Update all registers holding "uninit_type" to instead hold the corresponding initialized
138 * reference type. This is called when an appropriate constructor is invoked -- all copies of
139 * the reference must be marked as initialized.
140 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700141 void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700143
144 /*
Ian Rogersb8c78592013-07-25 23:52:52 +0000145 * Update all registers to be Conflict except vsrc.
146 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
148 void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
149 void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
Ian Rogersb8c78592013-07-25 23:52:52 +0000150
151 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700152 * Check constraints on constructor return. Specifically, make sure that the "this" argument got
153 * initialized.
154 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
155 * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
156 * somehow didn't get initialized.
157 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700158 bool CheckConstructorReturn(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700159
Stephen Kyle7e541c92014-12-17 17:10:02 +0000160 /*
161 * Check if an UninitializedThis at the specified location has been overwritten before
162 * being correctly initialized.
163 */
164 bool WasUninitializedThisOverwritten(MethodVerifier* verifier, size_t this_loc,
165 bool was_invoke_direct) const;
166
167 /*
168 * Get the first location of an UninitializedThis type, or return kInvalidVreg if there are none.
169 */
170 bool GetUninitializedThisLoc(MethodVerifier* verifier, size_t* vreg) const;
171
Ian Rogers776ac1f2012-04-13 23:36:36 -0700172 // Compare two register lines. Returns 0 if they match.
173 // Using this for a sort is unwise, since the value can change based on machine endianness.
174 int CompareLine(const RegisterLine* line2) const {
175 DCHECK(monitors_ == line2->monitors_);
176 // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700177 return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700178 }
179
180 size_t NumRegs() const {
181 return num_regs_;
182 }
183
184 /*
185 * Get the "this" pointer from a non-static method invocation. This returns the RegType so the
186 * caller can decide whether it needs the reference to be initialized or not. (Can also return
187 * kRegTypeZero if the reference can only be zero at this point.)
188 *
189 * The argument count is in vA, and the first argument is in vC, for both "simple" and "range"
190 * versions. We just need to make sure vA is >= 1 and then return vC.
191 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700192 const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
193 bool is_range)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700195
196 /*
197 * Verify types for a simple two-register instruction (e.g. "neg-int").
198 * "dst_type" is stored into vA, and "src_type" is verified against vB.
199 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700200 void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000201 const RegType& src_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700203
Ian Rogers7b078e82014-09-10 14:44:24 -0700204 void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000205 const RegType& dst_type1, const RegType& dst_type2,
206 const RegType& src_type1, const RegType& src_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
Ian Rogers7b078e82014-09-10 14:44:24 -0700209 void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000210 const RegType& dst_type1, const RegType& dst_type2,
211 const RegType& src_type)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213
Ian Rogers7b078e82014-09-10 14:44:24 -0700214 void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000215 const RegType& dst_type,
216 const RegType& src_type1, const RegType& src_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800217 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218
Ian Rogers776ac1f2012-04-13 23:36:36 -0700219 /*
220 * Verify types for a simple three-register instruction (e.g. "add-int").
221 * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
222 * against vB/vC.
223 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700224 void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000225 const RegType& dst_type, const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 bool check_boolean_op)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700228
Ian Rogers7b078e82014-09-10 14:44:24 -0700229 void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000230 const RegType& dst_type1, const RegType& dst_type2,
231 const RegType& src_type1_1, const RegType& src_type1_2,
232 const RegType& src_type2_1, const RegType& src_type2_2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
234
Ian Rogers7b078e82014-09-10 14:44:24 -0700235 void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000236 const RegType& long_lo_type, const RegType& long_hi_type,
237 const RegType& int_type)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
239
Ian Rogers776ac1f2012-04-13 23:36:36 -0700240 /*
241 * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
242 * are verified against vA/vB, then "dst_type" is stored into vA.
243 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700244 void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000245 const RegType& dst_type,
246 const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 bool check_boolean_op)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700249
Ian Rogers7b078e82014-09-10 14:44:24 -0700250 void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000251 const RegType& dst_type1, const RegType& dst_type2,
252 const RegType& src_type1_1, const RegType& src_type1_2,
253 const RegType& src_type2_1, const RegType& src_type2_2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800254 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
255
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000257 const RegType& long_lo_type, const RegType& long_hi_type,
258 const RegType& int_type)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800259 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
260
Ian Rogers776ac1f2012-04-13 23:36:36 -0700261 /*
262 * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
263 * "dst_type" is stored into vA, and "src_type" is verified against vB.
264 *
265 * If "check_boolean_op" is set, we use the constant value in vC.
266 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700267 void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000268 const RegType& dst_type, const RegType& src_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +0200269 bool check_boolean_op, bool is_lit16)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700271
272 // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
Ian Rogers7b078e82014-09-10 14:44:24 -0700273 void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700275
276 // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
Ian Rogers7b078e82014-09-10 14:44:24 -0700277 void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700279
280 // Stack of currently held monitors and where they were locked
281 size_t MonitorStackDepth() const {
282 return monitors_.size();
283 }
284
285 // We expect no monitors to be held at certain points, such a method returns. Verify the stack
286 // is empty, failing and returning false if not.
Ian Rogers7b078e82014-09-10 14:44:24 -0700287 bool VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700288
Ian Rogers7b078e82014-09-10 14:44:24 -0700289 bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700291
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800292 size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700293
Ian Rogers7b078e82014-09-10 14:44:24 -0700294 // Write a bit at each register location that holds a reference.
295 void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700296
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700297 size_t GetMonitorEnterCount() {
298 return monitors_.size();
299 }
300
301 uint32_t GetMonitorEnterDexPc(size_t i) {
302 return monitors_[i];
303 }
304
Elliott Hughesa21039c2012-06-21 12:09:25 -0700305 private:
Ian Rogers776ac1f2012-04-13 23:36:36 -0700306 void CopyRegToLockDepth(size_t dst, size_t src) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700307 auto it = reg_to_lock_depths_.find(src);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700308 if (it != reg_to_lock_depths_.end()) {
309 reg_to_lock_depths_.Put(dst, it->second);
310 }
311 }
312
313 bool IsSetLockDepth(size_t reg, size_t depth) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700314 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700315 if (it != reg_to_lock_depths_.end()) {
316 return (it->second & (1 << depth)) != 0;
317 } else {
318 return false;
319 }
320 }
321
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800322 bool SetRegToLockDepth(size_t reg, size_t depth) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700323 CHECK_LT(depth, 32u);
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800324 if (IsSetLockDepth(reg, depth)) {
325 return false; // Register already holds lock so locking twice is erroneous.
326 }
Mathieu Chartierbad02672014-08-25 13:08:22 -0700327 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700328 if (it == reg_to_lock_depths_.end()) {
329 reg_to_lock_depths_.Put(reg, 1 << depth);
330 } else {
331 it->second |= (1 << depth);
332 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800333 return true;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700334 }
335
336 void ClearRegToLockDepth(size_t reg, size_t depth) {
337 CHECK_LT(depth, 32u);
338 DCHECK(IsSetLockDepth(reg, depth));
Mathieu Chartierbad02672014-08-25 13:08:22 -0700339 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700340 DCHECK(it != reg_to_lock_depths_.end());
341 uint32_t depths = it->second ^ (1 << depth);
342 if (depths != 0) {
343 it->second = depths;
344 } else {
345 reg_to_lock_depths_.erase(it);
346 }
347 }
348
349 void ClearAllRegToLockDepths(size_t reg) {
350 reg_to_lock_depths_.erase(reg);
351 }
352
Ian Rogersd0fbd852013-09-24 18:17:04 -0700353 RegisterLine(size_t num_regs, MethodVerifier* verifier)
Ian Rogers7b078e82014-09-10 14:44:24 -0700354 : num_regs_(num_regs) {
Ian Rogersd0fbd852013-09-24 18:17:04 -0700355 memset(&line_, 0, num_regs_ * sizeof(uint16_t));
Ian Rogers7b078e82014-09-10 14:44:24 -0700356 SetResultTypeToUnknown(verifier);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700357 }
358
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800359 // Storage for the result register's type, valid after an invocation.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700360 uint16_t result_[2];
361
Ian Rogers776ac1f2012-04-13 23:36:36 -0700362 // Length of reg_types_
Ian Rogersad0b3a32012-04-16 14:50:24 -0700363 const uint32_t num_regs_;
Ian Rogers7b078e82014-09-10 14:44:24 -0700364
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800365 // A stack of monitor enter locations.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700366 std::vector<uint32_t, TrackingAllocator<uint32_t, kAllocatorTagVerifier>> monitors_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700367 // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
368 // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800369 // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700370 AllocationTrackingSafeMap<uint32_t, uint32_t, kAllocatorTagVerifier> reg_to_lock_depths_;
Ian Rogersd0fbd852013-09-24 18:17:04 -0700371
372 // An array of RegType Ids associated with each dex register.
373 uint16_t line_[0];
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800374
375 DISALLOW_COPY_AND_ASSIGN(RegisterLine);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700376};
Ian Rogers776ac1f2012-04-13 23:36:36 -0700377
378} // namespace verifier
379} // namespace art
380
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700381#endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_