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