blob: b2f5555d8b86d25dce455c79feb8d61f7e8bca15 [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
Mathieu Chartierde40d472015-10-15 17:47:48 -070023#include "base/scoped_arena_containers.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070024#include "safe_map.h"
25
26namespace art {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080027
28class Instruction;
29
Ian Rogers776ac1f2012-04-13 23:36:36 -070030namespace verifier {
31
32class MethodVerifier;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080033class RegType;
Ian Rogers776ac1f2012-04-13 23:36:36 -070034
35/*
36 * Register type categories, for type checking.
37 *
38 * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and
39 * returnAddress. Category 2 includes long and double.
40 *
41 * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so
42 * there is no "returnAddress" type.
43 */
44enum TypeCategory {
45 kTypeCategoryUnknown = 0,
46 kTypeCategory1nr = 1, // boolean, byte, char, short, int, float
47 kTypeCategory2 = 2, // long, double
48 kTypeCategoryRef = 3, // object reference
49};
50
Andreas Gampead238ce2015-08-24 21:13:08 -070051// What to do with the lock levels when setting the register type.
52enum class LockOp {
53 kClear, // Clear the lock levels recorded.
54 kKeep // Leave the lock levels alone.
55};
56
Ian Rogers776ac1f2012-04-13 23:36:36 -070057// During verification, we associate one of these with every "interesting" instruction. We track
58// the status of all registers, and (if the method has any monitor-enter instructions) maintain a
59// stack of entered monitors (identified by code unit offset).
Ian Rogers776ac1f2012-04-13 23:36:36 -070060class RegisterLine {
61 public:
Mathieu Chartierde40d472015-10-15 17:47:48 -070062 // A map from register to a bit vector of indices into the monitors_ stack.
63 using RegToLockDepthsMap = ScopedArenaSafeMap<uint32_t, uint32_t>;
64
65 // Create a register line of num_regs registers.
66 static RegisterLine* Create(size_t num_regs, MethodVerifier* verifier);
Ian Rogers776ac1f2012-04-13 23:36:36 -070067
68 // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst".
Ian Rogers7b078e82014-09-10 14:44:24 -070069 void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat)
Mathieu Chartier90443472015-07-16 20:32:27 -070070 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070071
72 // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This
73 // copies both halves of the register.
Ian Rogers7b078e82014-09-10 14:44:24 -070074 void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc)
Mathieu Chartier90443472015-07-16 20:32:27 -070075 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070076
77 // Implement "move-result". Copy the category-1 value from the result register to another
78 // register, and reset the result register.
Ian Rogers7b078e82014-09-10 14:44:24 -070079 void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference)
Mathieu Chartier90443472015-07-16 20:32:27 -070080 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070081
82 // Implement "move-result-wide". Copy the category-2 value from the result register to another
83 // register, and reset the result register.
Ian Rogers7b078e82014-09-10 14:44:24 -070084 void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst)
Mathieu Chartier90443472015-07-16 20:32:27 -070085 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070086
87 // Set the invisible result register to unknown
Mathieu Chartier90443472015-07-16 20:32:27 -070088 void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -070089
90 // Set the type of register N, verifying that the register is valid. If "newType" is the "Lo"
91 // part of a 64-bit value, register N+1 will be set to "newType+1".
92 // The register index was validated during the static pass, so we don't need to check it here.
Andreas Gampead238ce2015-08-24 21:13:08 -070093 //
94 // LockOp::kClear should be used by default; it will clear the lock levels associated with the
95 // register. An example is setting the register type because an instruction writes to the
96 // register.
97 // LockOp::kKeep keeps the lock levels of the register and only changes the register type. This
98 // is typical when the underlying value did not change, but we have "different" type information
99 // available now. An example is sharpening types after a check-cast. Note that when given kKeep,
100 // the new_type is dchecked to be a reference type.
101 template <LockOp kLockOp>
Ian Rogers7b078e82014-09-10 14:44:24 -0700102 ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier, uint32_t vdst,
103 const RegType& new_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700105
Ian Rogers7b078e82014-09-10 14:44:24 -0700106 bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1,
107 const RegType& new_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800109
Ian Rogers776ac1f2012-04-13 23:36:36 -0700110 /* Set the type of the "result" register. */
Ian Rogers7b078e82014-09-10 14:44:24 -0700111 void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700113
Ian Rogersd8f69b02014-09-10 21:43:52 +0000114 void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800116
Ian Rogers776ac1f2012-04-13 23:36:36 -0700117 // Get the type of register vsrc.
Ian Rogers7b078e82014-09-10 14:44:24 -0700118 const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700119
Ian Rogers7b078e82014-09-10 14:44:24 -0700120 ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc,
121 const RegType& check_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700123
Ian Rogers7b078e82014-09-10 14:44:24 -0700124 bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1,
125 const RegType& check_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800127
Ian Rogers776ac1f2012-04-13 23:36:36 -0700128 void CopyFromLine(const RegisterLine* src) {
129 DCHECK_EQ(num_regs_, src->num_regs_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700130 memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700131 monitors_ = src->monitors_;
132 reg_to_lock_depths_ = src->reg_to_lock_depths_;
Andreas Gampef10b6e12015-08-12 10:48:12 -0700133 this_initialized_ = src->this_initialized_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700134 }
135
Mathieu Chartier90443472015-07-16 20:32:27 -0700136 std::string Dump(MethodVerifier* verifier) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700137
138 void FillWithGarbage() {
Ian Rogersd0fbd852013-09-24 18:17:04 -0700139 memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
Logan Chiendb0cccd2014-09-30 19:07:55 +0800140 monitors_.clear();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700141 reg_to_lock_depths_.clear();
142 }
143
144 /*
145 * We're creating a new instance of class C at address A. Any registers holding instances
146 * previously created at address A must be initialized by now. If not, we mark them as "conflict"
147 * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and
148 * the new ones at the same time).
149 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700151 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700152
153 /*
154 * Update all registers holding "uninit_type" to instead hold the corresponding initialized
155 * reference type. This is called when an appropriate constructor is invoked -- all copies of
156 * the reference must be marked as initialized.
157 */
Jeff Hao848f70a2014-01-15 13:49:50 -0800158 void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type,
159 uint32_t this_reg, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700161
162 /*
Ian Rogersb8c78592013-07-25 23:52:52 +0000163 * Update all registers to be Conflict except vsrc.
164 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700165 void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
166 void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
167 void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
Ian Rogersb8c78592013-07-25 23:52:52 +0000168
Andreas Gampef10b6e12015-08-12 10:48:12 -0700169 void SetThisInitialized() {
170 this_initialized_ = true;
171 }
172
173 void CopyThisInitialized(const RegisterLine& src) {
174 this_initialized_ = src.this_initialized_;
175 }
176
Ian Rogersb8c78592013-07-25 23:52:52 +0000177 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700178 * Check constraints on constructor return. Specifically, make sure that the "this" argument got
179 * initialized.
180 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
181 * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
182 * somehow didn't get initialized.
183 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700184 bool CheckConstructorReturn(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700185
186 // Compare two register lines. Returns 0 if they match.
187 // Using this for a sort is unwise, since the value can change based on machine endianness.
188 int CompareLine(const RegisterLine* line2) const {
Andreas Gampea727e372015-08-25 09:22:37 -0700189 if (monitors_ != line2->monitors_) {
190 return 1;
191 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700192 // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700193 return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700194 }
195
196 size_t NumRegs() const {
197 return num_regs_;
198 }
199
200 /*
201 * Get the "this" pointer from a non-static method invocation. This returns the RegType so the
202 * caller can decide whether it needs the reference to be initialized or not. (Can also return
203 * kRegTypeZero if the reference can only be zero at this point.)
204 *
205 * The argument count is in vA, and the first argument is in vC, for both "simple" and "range"
206 * versions. We just need to make sure vA is >= 1 and then return vC.
Mathieu Chartier091d2382015-03-06 10:59:06 -0800207 * allow_failure will return Conflict() instead of causing a verification failure if there is an
208 * error.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700209 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -0800211 bool is_range, bool allow_failure = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700212 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700213
214 /*
215 * Verify types for a simple two-register instruction (e.g. "neg-int").
216 * "dst_type" is stored into vA, and "src_type" is verified against vB.
217 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000219 const RegType& src_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700220 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700221
Ian Rogers7b078e82014-09-10 14:44:24 -0700222 void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000223 const RegType& dst_type1, const RegType& dst_type2,
224 const RegType& src_type1, const RegType& src_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700225 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800226
Ian Rogers7b078e82014-09-10 14:44:24 -0700227 void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000228 const RegType& dst_type1, const RegType& dst_type2,
229 const RegType& src_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700230 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800231
Ian Rogers7b078e82014-09-10 14:44:24 -0700232 void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000233 const RegType& dst_type,
234 const RegType& src_type1, const RegType& src_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700235 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800236
Ian Rogers776ac1f2012-04-13 23:36:36 -0700237 /*
238 * Verify types for a simple three-register instruction (e.g. "add-int").
239 * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
240 * against vB/vC.
241 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700242 void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000243 const RegType& dst_type, const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 bool check_boolean_op)
Mathieu Chartier90443472015-07-16 20:32:27 -0700245 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700246
Ian Rogers7b078e82014-09-10 14:44:24 -0700247 void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000248 const RegType& dst_type1, const RegType& dst_type2,
249 const RegType& src_type1_1, const RegType& src_type1_2,
250 const RegType& src_type2_1, const RegType& src_type2_2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700251 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800252
Ian Rogers7b078e82014-09-10 14:44:24 -0700253 void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000254 const RegType& long_lo_type, const RegType& long_hi_type,
255 const RegType& int_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700256 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257
Ian Rogers776ac1f2012-04-13 23:36:36 -0700258 /*
259 * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
260 * are verified against vA/vB, then "dst_type" is stored into vA.
261 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700262 void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000263 const RegType& dst_type,
264 const RegType& src_type1, const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 bool check_boolean_op)
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700267
Ian Rogers7b078e82014-09-10 14:44:24 -0700268 void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000269 const RegType& dst_type1, const RegType& dst_type2,
270 const RegType& src_type1_1, const RegType& src_type1_2,
271 const RegType& src_type2_1, const RegType& src_type2_2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700272 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800273
Ian Rogers7b078e82014-09-10 14:44:24 -0700274 void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000275 const RegType& long_lo_type, const RegType& long_hi_type,
276 const RegType& int_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700277 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800278
Ian Rogers776ac1f2012-04-13 23:36:36 -0700279 /*
280 * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
281 * "dst_type" is stored into vA, and "src_type" is verified against vB.
282 *
283 * If "check_boolean_op" is set, we use the constant value in vC.
284 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700285 void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000286 const RegType& dst_type, const RegType& src_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +0200287 bool check_boolean_op, bool is_lit16)
Mathieu Chartier90443472015-07-16 20:32:27 -0700288 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700289
290 // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
Ian Rogers7b078e82014-09-10 14:44:24 -0700291 void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700292 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700293
294 // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
Ian Rogers7b078e82014-09-10 14:44:24 -0700295 void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700296 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700297
298 // Stack of currently held monitors and where they were locked
299 size_t MonitorStackDepth() const {
300 return monitors_.size();
301 }
302
303 // We expect no monitors to be held at certain points, such a method returns. Verify the stack
Andreas Gampea727e372015-08-25 09:22:37 -0700304 // is empty, queueing a LOCKING error else.
305 void VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700306
Ian Rogers7b078e82014-09-10 14:44:24 -0700307 bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
Mathieu Chartier90443472015-07-16 20:32:27 -0700308 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700309
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800310 size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700311
Ian Rogers7b078e82014-09-10 14:44:24 -0700312 // Write a bit at each register location that holds a reference.
313 void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700314
Mathieu Chartierde40d472015-10-15 17:47:48 -0700315 size_t GetMonitorEnterCount() const {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700316 return monitors_.size();
317 }
318
Mathieu Chartierde40d472015-10-15 17:47:48 -0700319 uint32_t GetMonitorEnterDexPc(size_t i) const {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700320 return monitors_[i];
321 }
322
Elliott Hughesa21039c2012-06-21 12:09:25 -0700323 private:
Ian Rogers776ac1f2012-04-13 23:36:36 -0700324 void CopyRegToLockDepth(size_t dst, size_t src) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700325 auto it = reg_to_lock_depths_.find(src);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700326 if (it != reg_to_lock_depths_.end()) {
327 reg_to_lock_depths_.Put(dst, it->second);
328 }
329 }
330
331 bool IsSetLockDepth(size_t reg, size_t depth) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700332 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700333 if (it != reg_to_lock_depths_.end()) {
334 return (it->second & (1 << depth)) != 0;
335 } else {
336 return false;
337 }
338 }
339
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800340 bool SetRegToLockDepth(size_t reg, size_t depth) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700341 CHECK_LT(depth, 32u);
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800342 if (IsSetLockDepth(reg, depth)) {
343 return false; // Register already holds lock so locking twice is erroneous.
344 }
Mathieu Chartierbad02672014-08-25 13:08:22 -0700345 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700346 if (it == reg_to_lock_depths_.end()) {
347 reg_to_lock_depths_.Put(reg, 1 << depth);
348 } else {
349 it->second |= (1 << depth);
350 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800351 return true;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700352 }
353
354 void ClearRegToLockDepth(size_t reg, size_t depth) {
355 CHECK_LT(depth, 32u);
356 DCHECK(IsSetLockDepth(reg, depth));
Mathieu Chartierbad02672014-08-25 13:08:22 -0700357 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700358 DCHECK(it != reg_to_lock_depths_.end());
359 uint32_t depths = it->second ^ (1 << depth);
360 if (depths != 0) {
361 it->second = depths;
362 } else {
363 reg_to_lock_depths_.erase(it);
364 }
Andreas Gampef5cdc412015-08-18 08:57:44 -0700365 // Need to unlock every register at the same lock depth. These are aliased locks.
366 uint32_t mask = 1 << depth;
367 for (auto& pair : reg_to_lock_depths_) {
368 if ((pair.second & mask) != 0) {
369 VLOG(verifier) << "Also unlocking " << pair.first;
370 pair.second ^= mask;
371 }
372 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700373 }
374
375 void ClearAllRegToLockDepths(size_t reg) {
376 reg_to_lock_depths_.erase(reg);
377 }
378
Mathieu Chartierde40d472015-10-15 17:47:48 -0700379 RegisterLine(size_t num_regs, MethodVerifier* verifier);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700380
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800381 // Storage for the result register's type, valid after an invocation.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700382 uint16_t result_[2];
383
Ian Rogers776ac1f2012-04-13 23:36:36 -0700384 // Length of reg_types_
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 const uint32_t num_regs_;
Ian Rogers7b078e82014-09-10 14:44:24 -0700386
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800387 // A stack of monitor enter locations.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700388 ScopedArenaVector<uint32_t> monitors_;
389
Ian Rogers776ac1f2012-04-13 23:36:36 -0700390 // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
391 // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800392 // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700393 RegToLockDepthsMap reg_to_lock_depths_;
Ian Rogersd0fbd852013-09-24 18:17:04 -0700394
Andreas Gampef10b6e12015-08-12 10:48:12 -0700395 // Whether "this" initialization (a constructor supercall) has happened.
396 bool this_initialized_;
397
Ian Rogersd0fbd852013-09-24 18:17:04 -0700398 // An array of RegType Ids associated with each dex register.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700399 uint16_t line_[1];
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800400
401 DISALLOW_COPY_AND_ASSIGN(RegisterLine);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700402};
Ian Rogers776ac1f2012-04-13 23:36:36 -0700403
404} // namespace verifier
405} // namespace art
406
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700407#endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_