Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ |
| 18 | #define ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 23 | #include "safe_map.h" |
| 24 | |
| 25 | namespace art { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 26 | |
| 27 | class Instruction; |
| 28 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 29 | namespace verifier { |
| 30 | |
| 31 | class MethodVerifier; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 32 | class RegType; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 33 | |
| 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 | */ |
| 43 | enum 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 53 | class RegisterLine { |
| 54 | public: |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 55 | static RegisterLine* Create(size_t num_regs, MethodVerifier* verifier) { |
Mathieu Chartier | 6afd409 | 2014-06-06 13:59:39 -0700 | [diff] [blame] | 56 | void* memory = operator new(sizeof(RegisterLine) + (num_regs * sizeof(uint16_t))); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 57 | RegisterLine* rl = new (memory) RegisterLine(num_regs, verifier); |
| 58 | return rl; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst". |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 62 | void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 63 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 64 | |
| 65 | // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This |
| 66 | // copies both halves of the register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 67 | void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 68 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 69 | |
| 70 | // Implement "move-result". Copy the category-1 value from the result register to another |
| 71 | // register, and reset the result register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 72 | void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 73 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 74 | |
| 75 | // Implement "move-result-wide". Copy the category-2 value from the result register to another |
| 76 | // register, and reset the result register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 77 | void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 79 | |
| 80 | // Set the invisible result register to unknown |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 81 | void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 82 | |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 86 | ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier, uint32_t vdst, |
| 87 | const RegType& new_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 89 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 90 | bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1, |
| 91 | const RegType& new_type2) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 93 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 94 | /* Set the type of the "result" register. */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 95 | void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 96 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 97 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 98 | void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 99 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 100 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 101 | // Get the type of register vsrc. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 102 | const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 104 | ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc, |
| 105 | const RegType& check_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 106 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 107 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 108 | bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1, |
| 109 | const RegType& check_type2) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 110 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 111 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 112 | void CopyFromLine(const RegisterLine* src) { |
| 113 | DCHECK_EQ(num_regs_, src->num_regs_); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 114 | memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 115 | monitors_ = src->monitors_; |
| 116 | reg_to_lock_depths_ = src->reg_to_lock_depths_; |
| 117 | } |
| 118 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 119 | std::string Dump(MethodVerifier* verifier) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 120 | |
| 121 | void FillWithGarbage() { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 122 | memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t)); |
Logan Chien | db0cccd | 2014-09-30 19:07:55 +0800 | [diff] [blame] | 123 | monitors_.clear(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 124 | 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 133 | void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 134 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 135 | |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 141 | void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 145 | * Update all registers to be Conflict except vsrc. |
| 146 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 147 | void MarkAllRegistersAsConflicts(MethodVerifier* verifier); |
| 148 | void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc); |
| 149 | void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 152 | * 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 158 | bool CheckConstructorReturn(MethodVerifier* verifier) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 159 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 160 | /* |
| 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 172 | // 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 Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 177 | return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 178 | } |
| 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 Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 191 | * allow_failure will return Conflict() instead of causing a verification failure if there is an |
| 192 | * error. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 193 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 194 | const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst, |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 195 | bool is_range, bool allow_failure = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 196 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 197 | |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 202 | void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 203 | const RegType& src_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 204 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 205 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 206 | void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 207 | const RegType& dst_type1, const RegType& dst_type2, |
| 208 | const RegType& src_type1, const RegType& src_type2) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 209 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 210 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 211 | void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 212 | const RegType& dst_type1, const RegType& dst_type2, |
| 213 | const RegType& src_type) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 214 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 215 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 216 | void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 217 | const RegType& dst_type, |
| 218 | const RegType& src_type1, const RegType& src_type2) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 219 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 220 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 221 | /* |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 226 | void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 227 | const RegType& dst_type, const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 228 | bool check_boolean_op) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 229 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 230 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 231 | void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 232 | 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 Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 235 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 236 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 237 | void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 238 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 239 | const RegType& int_type) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 240 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 241 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 242 | /* |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 246 | void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 247 | const RegType& dst_type, |
| 248 | const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 249 | bool check_boolean_op) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 250 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 251 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 252 | void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 253 | 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 Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 256 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 257 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 258 | void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 259 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 260 | const RegType& int_type) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 261 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 262 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 263 | /* |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 269 | void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 270 | const RegType& dst_type, const RegType& src_type, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 271 | bool check_boolean_op, bool is_lit16) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 272 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 273 | |
| 274 | // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 275 | void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx) |
| 276 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 277 | |
| 278 | // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 279 | void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx) |
| 280 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 281 | |
| 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 289 | bool VerifyMonitorStackEmpty(MethodVerifier* verifier) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 290 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 291 | bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 292 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 293 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 294 | size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 295 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 296 | // 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 298 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 299 | size_t GetMonitorEnterCount() { |
| 300 | return monitors_.size(); |
| 301 | } |
| 302 | |
| 303 | uint32_t GetMonitorEnterDexPc(size_t i) { |
| 304 | return monitors_[i]; |
| 305 | } |
| 306 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 307 | private: |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 308 | void CopyRegToLockDepth(size_t dst, size_t src) { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 309 | auto it = reg_to_lock_depths_.find(src); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 310 | 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 Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 316 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 317 | if (it != reg_to_lock_depths_.end()) { |
| 318 | return (it->second & (1 << depth)) != 0; |
| 319 | } else { |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 324 | bool SetRegToLockDepth(size_t reg, size_t depth) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 325 | CHECK_LT(depth, 32u); |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 326 | if (IsSetLockDepth(reg, depth)) { |
| 327 | return false; // Register already holds lock so locking twice is erroneous. |
| 328 | } |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 329 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 330 | 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 Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 335 | return true; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void ClearRegToLockDepth(size_t reg, size_t depth) { |
| 339 | CHECK_LT(depth, 32u); |
| 340 | DCHECK(IsSetLockDepth(reg, depth)); |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 341 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 342 | 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 Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 355 | RegisterLine(size_t num_regs, MethodVerifier* verifier) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 356 | : num_regs_(num_regs) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 357 | memset(&line_, 0, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 358 | SetResultTypeToUnknown(verifier); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 361 | // Storage for the result register's type, valid after an invocation. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 362 | uint16_t result_[2]; |
| 363 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 364 | // Length of reg_types_ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 365 | const uint32_t num_regs_; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 366 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 367 | // A stack of monitor enter locations. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 368 | std::vector<uint32_t, TrackingAllocator<uint32_t, kAllocatorTagVerifier>> monitors_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 369 | // 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 Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 371 | // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5. |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 372 | AllocationTrackingSafeMap<uint32_t, uint32_t, kAllocatorTagVerifier> reg_to_lock_depths_; |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 373 | |
| 374 | // An array of RegType Ids associated with each dex register. |
| 375 | uint16_t line_[0]; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 376 | |
| 377 | DISALLOW_COPY_AND_ASSIGN(RegisterLine); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 378 | }; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 379 | |
| 380 | } // namespace verifier |
| 381 | } // namespace art |
| 382 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 383 | #endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ |