blob: 9ea9cb763b193694bc1628a1dedec552928993a5 [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>
Mathieu Chartier88177602016-02-17 11:04:20 -0800102 ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier,
103 uint32_t vdst,
Ian Rogers7b078e82014-09-10 14:44:24 -0700104 const RegType& new_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700105 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700106
Mathieu Chartier88177602016-02-17 11:04:20 -0800107 bool SetRegisterTypeWide(MethodVerifier* verifier,
108 uint32_t vdst,
109 const RegType& new_type1,
Ian Rogers7b078e82014-09-10 14:44:24 -0700110 const RegType& new_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700111 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800112
Ian Rogers776ac1f2012-04-13 23:36:36 -0700113 /* Set the type of the "result" register. */
Ian Rogers7b078e82014-09-10 14:44:24 -0700114 void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700116
Ian Rogersd8f69b02014-09-10 21:43:52 +0000117 void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800119
Ian Rogers776ac1f2012-04-13 23:36:36 -0700120 // Get the type of register vsrc.
Ian Rogers7b078e82014-09-10 14:44:24 -0700121 const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700122
Mathieu Chartier88177602016-02-17 11:04:20 -0800123 ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier,
124 uint32_t vsrc,
Ian Rogers7b078e82014-09-10 14:44:24 -0700125 const RegType& check_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700127
Mathieu Chartier88177602016-02-17 11:04:20 -0800128 bool VerifyRegisterTypeWide(MethodVerifier* verifier,
129 uint32_t vsrc,
130 const RegType& check_type1,
Ian Rogers7b078e82014-09-10 14:44:24 -0700131 const RegType& check_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800133
Ian Rogers776ac1f2012-04-13 23:36:36 -0700134 void CopyFromLine(const RegisterLine* src) {
135 DCHECK_EQ(num_regs_, src->num_regs_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700136 memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700137 monitors_ = src->monitors_;
138 reg_to_lock_depths_ = src->reg_to_lock_depths_;
Andreas Gampef10b6e12015-08-12 10:48:12 -0700139 this_initialized_ = src->this_initialized_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700140 }
141
Mathieu Chartier90443472015-07-16 20:32:27 -0700142 std::string Dump(MethodVerifier* verifier) const SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700143
144 void FillWithGarbage() {
Ian Rogersd0fbd852013-09-24 18:17:04 -0700145 memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
Logan Chiendb0cccd2014-09-30 19:07:55 +0800146 monitors_.clear();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700147 reg_to_lock_depths_.clear();
148 }
149
150 /*
151 * We're creating a new instance of class C at address A. Any registers holding instances
152 * previously created at address A must be initialized by now. If not, we mark them as "conflict"
153 * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and
154 * the new ones at the same time).
155 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700156 void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700158
159 /*
160 * Update all registers holding "uninit_type" to instead hold the corresponding initialized
161 * reference type. This is called when an appropriate constructor is invoked -- all copies of
162 * the reference must be marked as initialized.
163 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800164 void MarkRefsAsInitialized(MethodVerifier* verifier,
165 const RegType& uninit_type,
166 uint32_t this_reg,
167 uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700168 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700169
170 /*
Ian Rogersb8c78592013-07-25 23:52:52 +0000171 * Update all registers to be Conflict except vsrc.
172 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700173 void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
174 void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
175 void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
Ian Rogersb8c78592013-07-25 23:52:52 +0000176
Andreas Gampef10b6e12015-08-12 10:48:12 -0700177 void SetThisInitialized() {
178 this_initialized_ = true;
179 }
180
181 void CopyThisInitialized(const RegisterLine& src) {
182 this_initialized_ = src.this_initialized_;
183 }
184
Ian Rogersb8c78592013-07-25 23:52:52 +0000185 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700186 * Check constraints on constructor return. Specifically, make sure that the "this" argument got
187 * initialized.
188 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
189 * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
190 * somehow didn't get initialized.
191 */
Ian Rogers7b078e82014-09-10 14:44:24 -0700192 bool CheckConstructorReturn(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700193
194 // Compare two register lines. Returns 0 if they match.
195 // Using this for a sort is unwise, since the value can change based on machine endianness.
196 int CompareLine(const RegisterLine* line2) const {
Andreas Gampea727e372015-08-25 09:22:37 -0700197 if (monitors_ != line2->monitors_) {
198 return 1;
199 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700200 // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700201 return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
Ian Rogers776ac1f2012-04-13 23:36:36 -0700202 }
203
204 size_t NumRegs() const {
205 return num_regs_;
206 }
207
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800208 // Return how many bytes of memory a register line uses.
209 ALWAYS_INLINE static size_t ComputeSize(size_t num_regs);
210
Ian Rogers776ac1f2012-04-13 23:36:36 -0700211 /*
212 * Get the "this" pointer from a non-static method invocation. This returns the RegType so the
213 * caller can decide whether it needs the reference to be initialized or not. (Can also return
214 * kRegTypeZero if the reference can only be zero at this point.)
215 *
216 * The argument count is in vA, and the first argument is in vC, for both "simple" and "range"
217 * versions. We just need to make sure vA is >= 1 and then return vC.
Mathieu Chartier091d2382015-03-06 10:59:06 -0800218 * allow_failure will return Conflict() instead of causing a verification failure if there is an
219 * error.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700220 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800221 const RegType& GetInvocationThis(MethodVerifier* verifier,
222 const Instruction* inst,
223 bool is_range,
224 bool allow_failure = false)
Mathieu Chartier90443472015-07-16 20:32:27 -0700225 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700226
227 /*
228 * Verify types for a simple two-register instruction (e.g. "neg-int").
229 * "dst_type" is stored into vA, and "src_type" is verified against vB.
230 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800231 void CheckUnaryOp(MethodVerifier* verifier,
232 const Instruction* inst,
233 const RegType& dst_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000234 const RegType& src_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700235 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700236
Mathieu Chartier88177602016-02-17 11:04:20 -0800237 void CheckUnaryOpWide(MethodVerifier* verifier,
238 const Instruction* inst,
239 const RegType& dst_type1,
240 const RegType& dst_type2,
241 const RegType& src_type1,
242 const RegType& src_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700243 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800244
Mathieu Chartier88177602016-02-17 11:04:20 -0800245 void CheckUnaryOpToWide(MethodVerifier* verifier,
246 const Instruction* inst,
247 const RegType& dst_type1,
248 const RegType& dst_type2,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000249 const RegType& src_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700250 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800251
Mathieu Chartier88177602016-02-17 11:04:20 -0800252 void CheckUnaryOpFromWide(MethodVerifier* verifier,
253 const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000254 const RegType& dst_type,
Mathieu Chartier88177602016-02-17 11:04:20 -0800255 const RegType& src_type1,
256 const RegType& src_type2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700257 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800258
Ian Rogers776ac1f2012-04-13 23:36:36 -0700259 /*
260 * Verify types for a simple three-register instruction (e.g. "add-int").
261 * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
262 * against vB/vC.
263 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800264 void CheckBinaryOp(MethodVerifier* verifier,
265 const Instruction* inst,
266 const RegType& dst_type,
267 const RegType& src_type1,
268 const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 bool check_boolean_op)
Mathieu Chartier90443472015-07-16 20:32:27 -0700270 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700271
Mathieu Chartier88177602016-02-17 11:04:20 -0800272 void CheckBinaryOpWide(MethodVerifier* verifier,
273 const Instruction* inst,
274 const RegType& dst_type1,
275 const RegType& dst_type2,
276 const RegType& src_type1_1,
277 const RegType& src_type1_2,
278 const RegType& src_type2_1,
279 const RegType& src_type2_2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700280 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800281
Mathieu Chartier88177602016-02-17 11:04:20 -0800282 void CheckBinaryOpWideShift(MethodVerifier* verifier,
283 const Instruction* inst,
284 const RegType& long_lo_type,
285 const RegType& long_hi_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000286 const RegType& int_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700287 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800288
Ian Rogers776ac1f2012-04-13 23:36:36 -0700289 /*
290 * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
291 * are verified against vA/vB, then "dst_type" is stored into vA.
292 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800293 void CheckBinaryOp2addr(MethodVerifier* verifier,
294 const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000295 const RegType& dst_type,
Mathieu Chartier88177602016-02-17 11:04:20 -0800296 const RegType& src_type1,
297 const RegType& src_type2,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 bool check_boolean_op)
Mathieu Chartier90443472015-07-16 20:32:27 -0700299 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700300
Mathieu Chartier88177602016-02-17 11:04:20 -0800301 void CheckBinaryOp2addrWide(MethodVerifier* verifier,
302 const Instruction* inst,
303 const RegType& dst_type1,
304 const RegType& dst_type2,
305 const RegType& src_type1_1,
306 const RegType& src_type1_2,
307 const RegType& src_type2_1,
308 const RegType& src_type2_2)
Mathieu Chartier90443472015-07-16 20:32:27 -0700309 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800310
Mathieu Chartier88177602016-02-17 11:04:20 -0800311 void CheckBinaryOp2addrWideShift(MethodVerifier* verifier,
312 const Instruction* inst,
313 const RegType& long_lo_type,
314 const RegType& long_hi_type,
Ian Rogersd8f69b02014-09-10 21:43:52 +0000315 const RegType& int_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700316 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800317
Ian Rogers776ac1f2012-04-13 23:36:36 -0700318 /*
319 * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
320 * "dst_type" is stored into vA, and "src_type" is verified against vB.
321 *
322 * If "check_boolean_op" is set, we use the constant value in vC.
323 */
Mathieu Chartier88177602016-02-17 11:04:20 -0800324 void CheckLiteralOp(MethodVerifier* verifier,
325 const Instruction* inst,
326 const RegType& dst_type,
327 const RegType& src_type,
328 bool check_boolean_op,
329 bool is_lit16)
Mathieu Chartier90443472015-07-16 20:32:27 -0700330 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700331
332 // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
Ian Rogers7b078e82014-09-10 14:44:24 -0700333 void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700334 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700335
336 // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
Ian Rogers7b078e82014-09-10 14:44:24 -0700337 void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700338 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700339
340 // Stack of currently held monitors and where they were locked
341 size_t MonitorStackDepth() const {
342 return monitors_.size();
343 }
344
345 // We expect no monitors to be held at certain points, such a method returns. Verify the stack
Andreas Gampea727e372015-08-25 09:22:37 -0700346 // is empty, queueing a LOCKING error else.
347 void VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700348
Ian Rogers7b078e82014-09-10 14:44:24 -0700349 bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700351
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800352 size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700353
Ian Rogers7b078e82014-09-10 14:44:24 -0700354 // Write a bit at each register location that holds a reference.
355 void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700356
Mathieu Chartierde40d472015-10-15 17:47:48 -0700357 size_t GetMonitorEnterCount() const {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700358 return monitors_.size();
359 }
360
Mathieu Chartierde40d472015-10-15 17:47:48 -0700361 uint32_t GetMonitorEnterDexPc(size_t i) const {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700362 return monitors_[i];
363 }
364
Elliott Hughesa21039c2012-06-21 12:09:25 -0700365 private:
Ian Rogers776ac1f2012-04-13 23:36:36 -0700366 void CopyRegToLockDepth(size_t dst, size_t src) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700367 auto it = reg_to_lock_depths_.find(src);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700368 if (it != reg_to_lock_depths_.end()) {
369 reg_to_lock_depths_.Put(dst, it->second);
370 }
371 }
372
373 bool IsSetLockDepth(size_t reg, size_t depth) {
Mathieu Chartierbad02672014-08-25 13:08:22 -0700374 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700375 if (it != reg_to_lock_depths_.end()) {
376 return (it->second & (1 << depth)) != 0;
377 } else {
378 return false;
379 }
380 }
381
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800382 bool SetRegToLockDepth(size_t reg, size_t depth) {
Ian Rogers776ac1f2012-04-13 23:36:36 -0700383 CHECK_LT(depth, 32u);
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800384 if (IsSetLockDepth(reg, depth)) {
385 return false; // Register already holds lock so locking twice is erroneous.
386 }
Mathieu Chartierbad02672014-08-25 13:08:22 -0700387 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388 if (it == reg_to_lock_depths_.end()) {
389 reg_to_lock_depths_.Put(reg, 1 << depth);
390 } else {
391 it->second |= (1 << depth);
392 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800393 return true;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700394 }
395
396 void ClearRegToLockDepth(size_t reg, size_t depth) {
397 CHECK_LT(depth, 32u);
398 DCHECK(IsSetLockDepth(reg, depth));
Mathieu Chartierbad02672014-08-25 13:08:22 -0700399 auto it = reg_to_lock_depths_.find(reg);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700400 DCHECK(it != reg_to_lock_depths_.end());
401 uint32_t depths = it->second ^ (1 << depth);
402 if (depths != 0) {
403 it->second = depths;
404 } else {
405 reg_to_lock_depths_.erase(it);
406 }
Andreas Gampef5cdc412015-08-18 08:57:44 -0700407 // Need to unlock every register at the same lock depth. These are aliased locks.
408 uint32_t mask = 1 << depth;
409 for (auto& pair : reg_to_lock_depths_) {
410 if ((pair.second & mask) != 0) {
411 VLOG(verifier) << "Also unlocking " << pair.first;
412 pair.second ^= mask;
413 }
414 }
Ian Rogers776ac1f2012-04-13 23:36:36 -0700415 }
416
417 void ClearAllRegToLockDepths(size_t reg) {
418 reg_to_lock_depths_.erase(reg);
419 }
420
Mathieu Chartierde40d472015-10-15 17:47:48 -0700421 RegisterLine(size_t num_regs, MethodVerifier* verifier);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700422
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800423 // Storage for the result register's type, valid after an invocation.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700424 uint16_t result_[2];
425
Ian Rogers776ac1f2012-04-13 23:36:36 -0700426 // Length of reg_types_
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427 const uint32_t num_regs_;
Ian Rogers7b078e82014-09-10 14:44:24 -0700428
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800429 // A stack of monitor enter locations.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700430 ScopedArenaVector<uint32_t> monitors_;
431
Ian Rogers776ac1f2012-04-13 23:36:36 -0700432 // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
433 // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800434 // 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 -0700435 RegToLockDepthsMap reg_to_lock_depths_;
Ian Rogersd0fbd852013-09-24 18:17:04 -0700436
Andreas Gampef10b6e12015-08-12 10:48:12 -0700437 // Whether "this" initialization (a constructor supercall) has happened.
438 bool this_initialized_;
439
Ian Rogersd0fbd852013-09-24 18:17:04 -0700440 // An array of RegType Ids associated with each dex register.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700441 uint16_t line_[1];
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800442
443 DISALLOW_COPY_AND_ASSIGN(RegisterLine);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700444};
Ian Rogers776ac1f2012-04-13 23:36:36 -0700445
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800446class RegisterLineArenaDelete : public ArenaDelete<RegisterLine> {
447 public:
448 void operator()(RegisterLine* ptr) const;
449};
450
Ian Rogers776ac1f2012-04-13 23:36:36 -0700451} // namespace verifier
452} // namespace art
453
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700454#endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_