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 | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | #include "dex_instruction.h" |
| 23 | #include "reg_type.h" |
| 24 | #include "safe_map.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "UniquePtr.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | namespace verifier { |
| 29 | |
| 30 | class MethodVerifier; |
| 31 | |
| 32 | /* |
| 33 | * Register type categories, for type checking. |
| 34 | * |
| 35 | * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and |
| 36 | * returnAddress. Category 2 includes long and double. |
| 37 | * |
| 38 | * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so |
| 39 | * there is no "returnAddress" type. |
| 40 | */ |
| 41 | enum TypeCategory { |
| 42 | kTypeCategoryUnknown = 0, |
| 43 | kTypeCategory1nr = 1, // boolean, byte, char, short, int, float |
| 44 | kTypeCategory2 = 2, // long, double |
| 45 | kTypeCategoryRef = 3, // object reference |
| 46 | }; |
| 47 | |
| 48 | // During verification, we associate one of these with every "interesting" instruction. We track |
| 49 | // the status of all registers, and (if the method has any monitor-enter instructions) maintain a |
| 50 | // stack of entered monitors (identified by code unit offset). |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 51 | class RegisterLine { |
| 52 | public: |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 53 | static RegisterLine* Create(size_t num_regs, MethodVerifier* verifier) { |
| 54 | uint8_t* memory = new uint8_t[sizeof(RegisterLine) + (num_regs * sizeof(uint16_t))]; |
| 55 | RegisterLine* rl = new (memory) RegisterLine(num_regs, verifier); |
| 56 | return rl; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst". |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 60 | void CopyRegister1(uint32_t vdst, uint32_t vsrc, TypeCategory cat) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 61 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 62 | |
| 63 | // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This |
| 64 | // copies both halves of the register. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | void CopyRegister2(uint32_t vdst, uint32_t vsrc) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 66 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 67 | |
| 68 | // Implement "move-result". Copy the category-1 value from the result register to another |
| 69 | // register, and reset the result register. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | void CopyResultRegister1(uint32_t vdst, bool is_reference) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 72 | |
| 73 | // Implement "move-result-wide". Copy the category-2 value from the result register to another |
| 74 | // register, and reset the result register. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | void CopyResultRegister2(uint32_t vdst) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 76 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 77 | |
| 78 | // Set the invisible result register to unknown |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 79 | void SetResultTypeToUnknown() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 80 | |
| 81 | // Set the type of register N, verifying that the register is valid. If "newType" is the "Lo" |
| 82 | // part of a 64-bit value, register N+1 will be set to "newType+1". |
| 83 | // The register index was validated during the static pass, so we don't need to check it here. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 84 | bool SetRegisterType(uint32_t vdst, const RegType& new_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 85 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 86 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 87 | bool SetRegisterTypeWide(uint32_t vdst, const RegType& new_type1, const RegType& new_type2) |
| 88 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 89 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 90 | /* Set the type of the "result" register. */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | void SetResultRegisterType(const RegType& new_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 92 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 93 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 94 | void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2) |
| 95 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 96 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 97 | // Get the type of register vsrc. |
| 98 | const RegType& GetRegisterType(uint32_t vsrc) const; |
| 99 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 100 | bool VerifyRegisterType(uint32_t vsrc, const RegType& check_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 102 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 103 | bool VerifyRegisterTypeWide(uint32_t vsrc, const RegType& check_type1, const RegType& check_type2) |
| 104 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 105 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 106 | void CopyFromLine(const RegisterLine* src) { |
| 107 | DCHECK_EQ(num_regs_, src->num_regs_); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 108 | memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 109 | monitors_ = src->monitors_; |
| 110 | reg_to_lock_depths_ = src->reg_to_lock_depths_; |
| 111 | } |
| 112 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 113 | std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 114 | |
| 115 | void FillWithGarbage() { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 116 | memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 117 | while (!monitors_.empty()) { |
| 118 | monitors_.pop_back(); |
| 119 | } |
| 120 | reg_to_lock_depths_.clear(); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * We're creating a new instance of class C at address A. Any registers holding instances |
| 125 | * previously created at address A must be initialized by now. If not, we mark them as "conflict" |
| 126 | * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and |
| 127 | * the new ones at the same time). |
| 128 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | void MarkUninitRefsAsInvalid(const RegType& uninit_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 130 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Update all registers holding "uninit_type" to instead hold the corresponding initialized |
| 134 | * reference type. This is called when an appropriate constructor is invoked -- all copies of |
| 135 | * the reference must be marked as initialized. |
| 136 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 137 | void MarkRefsAsInitialized(const RegType& uninit_type) |
| 138 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 141 | * Update all registers to be Conflict except vsrc. |
| 142 | */ |
| 143 | void MarkAllRegistersAsConflicts(); |
| 144 | void MarkAllRegistersAsConflictsExcept(uint32_t vsrc); |
| 145 | void MarkAllRegistersAsConflictsExceptWide(uint32_t vsrc); |
| 146 | |
| 147 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 148 | * Check constraints on constructor return. Specifically, make sure that the "this" argument got |
| 149 | * initialized. |
| 150 | * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start |
| 151 | * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it |
| 152 | * somehow didn't get initialized. |
| 153 | */ |
| 154 | bool CheckConstructorReturn() const; |
| 155 | |
| 156 | // Compare two register lines. Returns 0 if they match. |
| 157 | // Using this for a sort is unwise, since the value can change based on machine endianness. |
| 158 | int CompareLine(const RegisterLine* line2) const { |
| 159 | DCHECK(monitors_ == line2->monitors_); |
| 160 | // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 161 | return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | size_t NumRegs() const { |
| 165 | return num_regs_; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Get the "this" pointer from a non-static method invocation. This returns the RegType so the |
| 170 | * caller can decide whether it needs the reference to be initialized or not. (Can also return |
| 171 | * kRegTypeZero if the reference can only be zero at this point.) |
| 172 | * |
| 173 | * The argument count is in vA, and the first argument is in vC, for both "simple" and "range" |
| 174 | * versions. We just need to make sure vA is >= 1 and then return vC. |
| 175 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 176 | const RegType& GetInvocationThis(const Instruction* inst, bool is_range) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 177 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Verify types for a simple two-register instruction (e.g. "neg-int"). |
| 181 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 182 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 183 | void CheckUnaryOp(const Instruction* inst, const RegType& dst_type, |
| 184 | const RegType& src_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 185 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 186 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 187 | void CheckUnaryOpWide(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 188 | const RegType& dst_type1, const RegType& dst_type2, |
| 189 | const RegType& src_type1, const RegType& src_type2) |
| 190 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 191 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 192 | void CheckUnaryOpToWide(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 193 | const RegType& dst_type1, const RegType& dst_type2, |
| 194 | const RegType& src_type) |
| 195 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 196 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 197 | void CheckUnaryOpFromWide(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 198 | const RegType& dst_type, |
| 199 | const RegType& src_type1, const RegType& src_type2) |
| 200 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 201 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 202 | /* |
| 203 | * Verify types for a simple three-register instruction (e.g. "add-int"). |
| 204 | * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified |
| 205 | * against vB/vC. |
| 206 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 207 | void CheckBinaryOp(const Instruction* inst, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 208 | const RegType& dst_type, const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | bool check_boolean_op) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 210 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 211 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 212 | void CheckBinaryOpWide(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 213 | const RegType& dst_type1, const RegType& dst_type2, |
| 214 | const RegType& src_type1_1, const RegType& src_type1_2, |
| 215 | const RegType& src_type2_1, const RegType& src_type2_2) |
| 216 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 217 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 218 | void CheckBinaryOpWideShift(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 219 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 220 | const RegType& int_type) |
| 221 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 222 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 223 | /* |
| 224 | * Verify types for a binary "2addr" operation. "src_type1"/"src_type2" |
| 225 | * are verified against vA/vB, then "dst_type" is stored into vA. |
| 226 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 227 | void CheckBinaryOp2addr(const Instruction* inst, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 228 | const RegType& dst_type, |
| 229 | const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 230 | bool check_boolean_op) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 231 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 232 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 233 | void CheckBinaryOp2addrWide(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 234 | const RegType& dst_type1, const RegType& dst_type2, |
| 235 | const RegType& src_type1_1, const RegType& src_type1_2, |
| 236 | const RegType& src_type2_1, const RegType& src_type2_2) |
| 237 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 238 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 239 | void CheckBinaryOp2addrWideShift(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 240 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 241 | const RegType& int_type) |
| 242 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 243 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 244 | /* |
| 245 | * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8"). |
| 246 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 247 | * |
| 248 | * If "check_boolean_op" is set, we use the constant value in vC. |
| 249 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 250 | void CheckLiteralOp(const Instruction* inst, |
| 251 | const RegType& dst_type, const RegType& src_type, |
| 252 | bool check_boolean_op, bool is_lit16) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 253 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 254 | |
| 255 | // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 256 | void PushMonitor(uint32_t reg_idx, int32_t insn_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 257 | |
| 258 | // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 259 | void PopMonitor(uint32_t reg_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 260 | |
| 261 | // Stack of currently held monitors and where they were locked |
| 262 | size_t MonitorStackDepth() const { |
| 263 | return monitors_.size(); |
| 264 | } |
| 265 | |
| 266 | // We expect no monitors to be held at certain points, such a method returns. Verify the stack |
| 267 | // is empty, failing and returning false if not. |
Jeff Hao | 673b683 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 268 | bool VerifyMonitorStackEmpty() const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 269 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 270 | bool MergeRegisters(const RegisterLine* incoming_line) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 271 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 272 | |
| 273 | size_t GetMaxNonZeroReferenceReg(size_t max_ref_reg) { |
| 274 | size_t i = static_cast<int>(max_ref_reg) < 0 ? 0 : max_ref_reg; |
| 275 | for (; i < num_regs_; i++) { |
| 276 | if (GetRegisterType(i).IsNonZeroReferenceTypes()) { |
| 277 | max_ref_reg = i; |
| 278 | } |
| 279 | } |
| 280 | return max_ref_reg; |
| 281 | } |
| 282 | |
| 283 | // Write a bit at each register location that holds a reference |
| 284 | void WriteReferenceBitMap(std::vector<uint8_t>& data, size_t max_bytes); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 285 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 286 | size_t GetMonitorEnterCount() { |
| 287 | return monitors_.size(); |
| 288 | } |
| 289 | |
| 290 | uint32_t GetMonitorEnterDexPc(size_t i) { |
| 291 | return monitors_[i]; |
| 292 | } |
| 293 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 294 | private: |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 295 | void CopyRegToLockDepth(size_t dst, size_t src) { |
| 296 | SafeMap<uint32_t, uint32_t>::iterator it = reg_to_lock_depths_.find(src); |
| 297 | if (it != reg_to_lock_depths_.end()) { |
| 298 | reg_to_lock_depths_.Put(dst, it->second); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | bool IsSetLockDepth(size_t reg, size_t depth) { |
| 303 | SafeMap<uint32_t, uint32_t>::iterator it = reg_to_lock_depths_.find(reg); |
| 304 | if (it != reg_to_lock_depths_.end()) { |
| 305 | return (it->second & (1 << depth)) != 0; |
| 306 | } else { |
| 307 | return false; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void SetRegToLockDepth(size_t reg, size_t depth) { |
| 312 | CHECK_LT(depth, 32u); |
| 313 | DCHECK(!IsSetLockDepth(reg, depth)); |
| 314 | SafeMap<uint32_t, uint32_t>::iterator it = reg_to_lock_depths_.find(reg); |
| 315 | if (it == reg_to_lock_depths_.end()) { |
| 316 | reg_to_lock_depths_.Put(reg, 1 << depth); |
| 317 | } else { |
| 318 | it->second |= (1 << depth); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void ClearRegToLockDepth(size_t reg, size_t depth) { |
| 323 | CHECK_LT(depth, 32u); |
| 324 | DCHECK(IsSetLockDepth(reg, depth)); |
| 325 | SafeMap<uint32_t, uint32_t>::iterator it = reg_to_lock_depths_.find(reg); |
| 326 | DCHECK(it != reg_to_lock_depths_.end()); |
| 327 | uint32_t depths = it->second ^ (1 << depth); |
| 328 | if (depths != 0) { |
| 329 | it->second = depths; |
| 330 | } else { |
| 331 | reg_to_lock_depths_.erase(it); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void ClearAllRegToLockDepths(size_t reg) { |
| 336 | reg_to_lock_depths_.erase(reg); |
| 337 | } |
| 338 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 339 | RegisterLine(size_t num_regs, MethodVerifier* verifier) |
| 340 | : verifier_(verifier), |
| 341 | num_regs_(num_regs) { |
| 342 | memset(&line_, 0, num_regs_ * sizeof(uint16_t)); |
| 343 | SetResultTypeToUnknown(); |
| 344 | } |
| 345 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 346 | // Storage for the result register's type, valid after an invocation |
| 347 | uint16_t result_[2]; |
| 348 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 349 | // Back link to the verifier |
| 350 | MethodVerifier* verifier_; |
| 351 | |
| 352 | // Length of reg_types_ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 353 | const uint32_t num_regs_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 354 | // A stack of monitor enter locations |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 355 | std::vector<uint32_t> monitors_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 356 | // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor |
| 357 | // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a |
| 358 | // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5 |
| 359 | SafeMap<uint32_t, uint32_t> reg_to_lock_depths_; |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 360 | |
| 361 | // An array of RegType Ids associated with each dex register. |
| 362 | uint16_t line_[0]; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 363 | }; |
| 364 | std::ostream& operator<<(std::ostream& os, const RegisterLine& rhs); |
| 365 | |
| 366 | } // namespace verifier |
| 367 | } // namespace art |
| 368 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 369 | #endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ |