blob: 376dbf1fad62152223cbfa27c1a90c3da16d8aba [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.
Mathieu Chartier091d2382015-03-06 10:59:06 -0800191 * allow_failure will return Conflict() instead of causing a verification failure if there is an
192 * error.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700193 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700194 const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -0800195 bool is_range, bool allow_failure = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700196 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700197
198 /*
199 * Verify types for a simple two-register instruction (e.g. "neg-int").
200 * "dst_type" is stored into vA, and "src_type" is verified against vB.
201 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700202 void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000203 const RegType& src_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700205
Ian Rogers7b078e82014-09-10 14:44:24 -0700206 void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000207 const RegType& dst_type1, const RegType& dst_type2,
208 const RegType& src_type1, const RegType& src_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
210
Ian Rogers7b078e82014-09-10 14:44:24 -0700211 void 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 Rogers2bcb4a42012-11-08 10:39:18 -0800214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
215
Ian Rogers7b078e82014-09-10 14:44:24 -0700216 void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000217 const RegType& dst_type,
218 const RegType& src_type1, const RegType& src_type2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
220
Ian Rogers776ac1f2012-04-13 23:36:36 -0700221 /*
222 * Verify types for a simple three-register instruction (e.g. "add-int").
223 * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
224 * against vB/vC.
225 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700226 void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000227 const RegType& dst_type, const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 bool check_boolean_op)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700230
Ian Rogers7b078e82014-09-10 14:44:24 -0700231 void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000232 const RegType& dst_type1, const RegType& dst_type2,
233 const RegType& src_type1_1, const RegType& src_type1_2,
234 const RegType& src_type2_1, const RegType& src_type2_2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
236
Ian Rogers7b078e82014-09-10 14:44:24 -0700237 void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000238 const RegType& long_lo_type, const RegType& long_hi_type,
239 const RegType& int_type)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800240 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
241
Ian Rogers776ac1f2012-04-13 23:36:36 -0700242 /*
243 * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
244 * are verified against vA/vB, then "dst_type" is stored into vA.
245 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000247 const RegType& dst_type,
248 const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 bool check_boolean_op)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700251
Ian Rogers7b078e82014-09-10 14:44:24 -0700252 void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000253 const RegType& dst_type1, const RegType& dst_type2,
254 const RegType& src_type1_1, const RegType& src_type1_2,
255 const RegType& src_type2_1, const RegType& src_type2_2)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800256 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
257
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000259 const RegType& long_lo_type, const RegType& long_hi_type,
260 const RegType& int_type)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
262
Ian Rogers776ac1f2012-04-13 23:36:36 -0700263 /*
264 * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
265 * "dst_type" is stored into vA, and "src_type" is verified against vB.
266 *
267 * If "check_boolean_op" is set, we use the constant value in vC.
268 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700269 void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000270 const RegType& dst_type, const RegType& src_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +0200271 bool check_boolean_op, bool is_lit16)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700273
274 // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
Ian Rogers7b078e82014-09-10 14:44:24 -0700275 void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
276 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700277
278 // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
Ian Rogers7b078e82014-09-10 14:44:24 -0700279 void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700281
282 // Stack of currently held monitors and where they were locked
283 size_t MonitorStackDepth() const {
284 return monitors_.size();
285 }
286
287 // We expect no monitors to be held at certain points, such a method returns. Verify the stack
288 // is empty, failing and returning false if not.
Ian Rogers7b078e82014-09-10 14:44:24 -0700289 bool VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700290
Ian Rogers7b078e82014-09-10 14:44:24 -0700291 bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700293
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800294 size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700295
Ian Rogers7b078e82014-09-10 14:44:24 -0700296 // Write a bit at each register location that holds a reference.
297 void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700298
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700299 size_t GetMonitorEnterCount() {
300 return monitors_.size();
301 }
302
303 uint32_t GetMonitorEnterDexPc(size_t i) {
304 return monitors_[i];
305 }
306
Elliott Hughesa21039c2012-06-21 12:09:25 -0700307 private:
Ian Rogers776ac1f2012-04-13 23:36:36 -0700308 void CopyRegToLockDepth(size_t dst, size_t src) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700309 auto it = reg_to_lock_depths_.find(src);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700310 if (it != reg_to_lock_depths_.end()) {
311 reg_to_lock_depths_.Put(dst, it->second);
312 }
313 }
314
315 bool IsSetLockDepth(size_t reg, size_t depth) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700316 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700317 if (it != reg_to_lock_depths_.end()) {
318 return (it->second & (1 << depth)) != 0;
319 } else {
320 return false;
321 }
322 }
323
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800324 bool SetRegToLockDepth(size_t reg, size_t depth) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700325 CHECK_LT(depth, 32u);
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800326 if (IsSetLockDepth(reg, depth)) {
327 return false; // Register already holds lock so locking twice is erroneous.
328 }
Mathieu Chartierbad02672014-08-25 13:08:22 -0700329 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700330 if (it == reg_to_lock_depths_.end()) {
331 reg_to_lock_depths_.Put(reg, 1 << depth);
332 } else {
333 it->second |= (1 << depth);
334 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800335 return true;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700336 }
337
338 void ClearRegToLockDepth(size_t reg, size_t depth) {
339 CHECK_LT(depth, 32u);
340 DCHECK(IsSetLockDepth(reg, depth));
Mathieu Chartierbad02672014-08-25 13:08:22 -0700341 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700342 DCHECK(it != reg_to_lock_depths_.end());
343 uint32_t depths = it->second ^ (1 << depth);
344 if (depths != 0) {
345 it->second = depths;
346 } else {
347 reg_to_lock_depths_.erase(it);
348 }
349 }
350
351 void ClearAllRegToLockDepths(size_t reg) {
352 reg_to_lock_depths_.erase(reg);
353 }
354
Ian Rogersd0fbd852013-09-24 18:17:04 -0700355 RegisterLine(size_t num_regs, MethodVerifier* verifier)
Ian Rogers7b078e82014-09-10 14:44:24 -0700356 : num_regs_(num_regs) {
Ian Rogersd0fbd852013-09-24 18:17:04 -0700357 memset(&line_, 0, num_regs_ * sizeof(uint16_t));
Ian Rogers7b078e82014-09-10 14:44:24 -0700358 SetResultTypeToUnknown(verifier);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700359 }
360
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800361 // Storage for the result register's type, valid after an invocation.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700362 uint16_t result_[2];
363
Ian Rogers776ac1f2012-04-13 23:36:36 -0700364 // Length of reg_types_
Ian Rogersad0b3a32012-04-16 14:50:24 -0700365 const uint32_t num_regs_;
Ian Rogers7b078e82014-09-10 14:44:24 -0700366
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800367 // A stack of monitor enter locations.
Mathieu Chartierbad02672014-08-25 13:08:22 -0700368 std::vector<uint32_t, TrackingAllocator<uint32_t, kAllocatorTagVerifier>> monitors_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700369 // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
370 // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800371 // 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 -0700372 AllocationTrackingSafeMap<uint32_t, uint32_t, kAllocatorTagVerifier> reg_to_lock_depths_;
Ian Rogersd0fbd852013-09-24 18:17:04 -0700373
374 // An array of RegType Ids associated with each dex register.
375 uint16_t line_[0];
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800376
377 DISALLOW_COPY_AND_ASSIGN(RegisterLine);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700378};
Ian Rogers776ac1f2012-04-13 23:36:36 -0700379
380} // namespace verifier
381} // namespace art
382
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700383#endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_