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 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 23 | #include "base/scoped_arena_containers.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 24 | #include "safe_map.h" |
| 25 | |
| 26 | namespace art { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 27 | |
| 28 | class Instruction; |
| 29 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | namespace verifier { |
| 31 | |
| 32 | class MethodVerifier; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 33 | class RegType; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 34 | |
| 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 | */ |
| 44 | enum 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 Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 51 | // What to do with the lock levels when setting the register type. |
| 52 | enum class LockOp { |
| 53 | kClear, // Clear the lock levels recorded. |
| 54 | kKeep // Leave the lock levels alone. |
| 55 | }; |
| 56 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 57 | // 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 60 | class RegisterLine { |
| 61 | public: |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 62 | // 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 67 | |
| 68 | // 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] | 69 | void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 70 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 71 | |
| 72 | // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This |
| 73 | // copies both halves of the register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 74 | void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 75 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 76 | |
| 77 | // Implement "move-result". Copy the category-1 value from the result register to another |
| 78 | // register, and reset the result register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 79 | void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 80 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 81 | |
| 82 | // Implement "move-result-wide". Copy the category-2 value from the result register to another |
| 83 | // register, and reset the result register. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 84 | void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 85 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 86 | |
| 87 | // Set the invisible result register to unknown |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 88 | void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 89 | |
| 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 Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 93 | // |
| 94 | // LockOp::kClear should be used by default; it will clear the lock levels associated with the |
| 95 | // register. An example is setting the register type because an instruction writes to the |
| 96 | // register. |
| 97 | // LockOp::kKeep keeps the lock levels of the register and only changes the register type. This |
| 98 | // is typical when the underlying value did not change, but we have "different" type information |
| 99 | // available now. An example is sharpening types after a check-cast. Note that when given kKeep, |
| 100 | // the new_type is dchecked to be a reference type. |
| 101 | template <LockOp kLockOp> |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 102 | ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier, uint32_t vdst, |
| 103 | const RegType& new_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 105 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 106 | bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1, |
| 107 | const RegType& new_type2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 108 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 109 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 110 | /* Set the type of the "result" register. */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 111 | void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 112 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 113 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 114 | void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 115 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 116 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 117 | // Get the type of register vsrc. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 118 | const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 119 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 120 | ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc, |
| 121 | const RegType& check_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 122 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 124 | bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1, |
| 125 | const RegType& check_type2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 126 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 127 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 128 | void CopyFromLine(const RegisterLine* src) { |
| 129 | DCHECK_EQ(num_regs_, src->num_regs_); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 130 | memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 131 | monitors_ = src->monitors_; |
| 132 | reg_to_lock_depths_ = src->reg_to_lock_depths_; |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 133 | this_initialized_ = src->this_initialized_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 136 | std::string Dump(MethodVerifier* verifier) const SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 137 | |
| 138 | void FillWithGarbage() { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 139 | memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t)); |
Logan Chien | db0cccd | 2014-09-30 19:07:55 +0800 | [diff] [blame] | 140 | monitors_.clear(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 141 | reg_to_lock_depths_.clear(); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * We're creating a new instance of class C at address A. Any registers holding instances |
| 146 | * previously created at address A must be initialized by now. If not, we mark them as "conflict" |
| 147 | * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and |
| 148 | * the new ones at the same time). |
| 149 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 150 | void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 151 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * Update all registers holding "uninit_type" to instead hold the corresponding initialized |
| 155 | * reference type. This is called when an appropriate constructor is invoked -- all copies of |
| 156 | * the reference must be marked as initialized. |
| 157 | */ |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 158 | void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type, |
| 159 | uint32_t this_reg, uint32_t dex_pc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 160 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 163 | * Update all registers to be Conflict except vsrc. |
| 164 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 165 | void MarkAllRegistersAsConflicts(MethodVerifier* verifier); |
| 166 | void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc); |
| 167 | void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 168 | |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 169 | void SetThisInitialized() { |
| 170 | this_initialized_ = true; |
| 171 | } |
| 172 | |
| 173 | void CopyThisInitialized(const RegisterLine& src) { |
| 174 | this_initialized_ = src.this_initialized_; |
| 175 | } |
| 176 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 177 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 178 | * Check constraints on constructor return. Specifically, make sure that the "this" argument got |
| 179 | * initialized. |
| 180 | * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start |
| 181 | * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it |
| 182 | * somehow didn't get initialized. |
| 183 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 184 | bool CheckConstructorReturn(MethodVerifier* verifier) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 185 | |
| 186 | // Compare two register lines. Returns 0 if they match. |
| 187 | // Using this for a sort is unwise, since the value can change based on machine endianness. |
| 188 | int CompareLine(const RegisterLine* line2) const { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 189 | if (monitors_ != line2->monitors_) { |
| 190 | return 1; |
| 191 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 192 | // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 193 | return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | size_t NumRegs() const { |
| 197 | return num_regs_; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Get the "this" pointer from a non-static method invocation. This returns the RegType so the |
| 202 | * caller can decide whether it needs the reference to be initialized or not. (Can also return |
| 203 | * kRegTypeZero if the reference can only be zero at this point.) |
| 204 | * |
| 205 | * The argument count is in vA, and the first argument is in vC, for both "simple" and "range" |
| 206 | * versions. We just need to make sure vA is >= 1 and then return vC. |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 207 | * allow_failure will return Conflict() instead of causing a verification failure if there is an |
| 208 | * error. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 209 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 210 | const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst, |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 211 | bool is_range, bool allow_failure = false) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 212 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * Verify types for a simple two-register instruction (e.g. "neg-int"). |
| 216 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 217 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 218 | void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 219 | const RegType& src_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 220 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 222 | void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 223 | const RegType& dst_type1, const RegType& dst_type2, |
| 224 | const RegType& src_type1, const RegType& src_type2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 225 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 226 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 227 | void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 228 | const RegType& dst_type1, const RegType& dst_type2, |
| 229 | const RegType& src_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 230 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 231 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 232 | void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 233 | const RegType& dst_type, |
| 234 | const RegType& src_type1, const RegType& src_type2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 235 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 236 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 237 | /* |
| 238 | * Verify types for a simple three-register instruction (e.g. "add-int"). |
| 239 | * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified |
| 240 | * against vB/vC. |
| 241 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 242 | void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 243 | const RegType& dst_type, const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 244 | bool check_boolean_op) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 245 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 246 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 247 | void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 248 | const RegType& dst_type1, const RegType& dst_type2, |
| 249 | const RegType& src_type1_1, const RegType& src_type1_2, |
| 250 | const RegType& src_type2_1, const RegType& src_type2_2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 251 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 252 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 253 | void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 254 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 255 | const RegType& int_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 256 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 257 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 258 | /* |
| 259 | * Verify types for a binary "2addr" operation. "src_type1"/"src_type2" |
| 260 | * are verified against vA/vB, then "dst_type" is stored into vA. |
| 261 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 262 | void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 263 | const RegType& dst_type, |
| 264 | const RegType& src_type1, const RegType& src_type2, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | bool check_boolean_op) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 266 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 267 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 268 | void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 269 | const RegType& dst_type1, const RegType& dst_type2, |
| 270 | const RegType& src_type1_1, const RegType& src_type1_2, |
| 271 | const RegType& src_type2_1, const RegType& src_type2_2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 272 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 273 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 274 | void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 275 | const RegType& long_lo_type, const RegType& long_hi_type, |
| 276 | const RegType& int_type) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 277 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 278 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 279 | /* |
| 280 | * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8"). |
| 281 | * "dst_type" is stored into vA, and "src_type" is verified against vB. |
| 282 | * |
| 283 | * If "check_boolean_op" is set, we use the constant value in vC. |
| 284 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 285 | void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 286 | const RegType& dst_type, const RegType& src_type, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 287 | bool check_boolean_op, bool is_lit16) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 288 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 289 | |
| 290 | // 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] | 291 | void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 292 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 293 | |
| 294 | // 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] | 295 | void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 296 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 297 | |
| 298 | // Stack of currently held monitors and where they were locked |
| 299 | size_t MonitorStackDepth() const { |
| 300 | return monitors_.size(); |
| 301 | } |
| 302 | |
| 303 | // We expect no monitors to be held at certain points, such a method returns. Verify the stack |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 304 | // is empty, queueing a LOCKING error else. |
| 305 | void VerifyMonitorStackEmpty(MethodVerifier* verifier) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 306 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 307 | bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 308 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 309 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 310 | size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 311 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 312 | // Write a bit at each register location that holds a reference. |
| 313 | void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 314 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 315 | size_t GetMonitorEnterCount() const { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 316 | return monitors_.size(); |
| 317 | } |
| 318 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 319 | uint32_t GetMonitorEnterDexPc(size_t i) const { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 320 | return monitors_[i]; |
| 321 | } |
| 322 | |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 323 | private: |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 324 | void CopyRegToLockDepth(size_t dst, size_t src) { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 325 | auto it = reg_to_lock_depths_.find(src); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 326 | if (it != reg_to_lock_depths_.end()) { |
| 327 | reg_to_lock_depths_.Put(dst, it->second); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | bool IsSetLockDepth(size_t reg, size_t depth) { |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 332 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 333 | if (it != reg_to_lock_depths_.end()) { |
| 334 | return (it->second & (1 << depth)) != 0; |
| 335 | } else { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 340 | bool SetRegToLockDepth(size_t reg, size_t depth) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 341 | CHECK_LT(depth, 32u); |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 342 | if (IsSetLockDepth(reg, depth)) { |
| 343 | return false; // Register already holds lock so locking twice is erroneous. |
| 344 | } |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 345 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 346 | if (it == reg_to_lock_depths_.end()) { |
| 347 | reg_to_lock_depths_.Put(reg, 1 << depth); |
| 348 | } else { |
| 349 | it->second |= (1 << depth); |
| 350 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 351 | return true; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void ClearRegToLockDepth(size_t reg, size_t depth) { |
| 355 | CHECK_LT(depth, 32u); |
| 356 | DCHECK(IsSetLockDepth(reg, depth)); |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 357 | auto it = reg_to_lock_depths_.find(reg); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 358 | DCHECK(it != reg_to_lock_depths_.end()); |
| 359 | uint32_t depths = it->second ^ (1 << depth); |
| 360 | if (depths != 0) { |
| 361 | it->second = depths; |
| 362 | } else { |
| 363 | reg_to_lock_depths_.erase(it); |
| 364 | } |
Andreas Gampe | f5cdc41 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 365 | // Need to unlock every register at the same lock depth. These are aliased locks. |
| 366 | uint32_t mask = 1 << depth; |
| 367 | for (auto& pair : reg_to_lock_depths_) { |
| 368 | if ((pair.second & mask) != 0) { |
| 369 | VLOG(verifier) << "Also unlocking " << pair.first; |
| 370 | pair.second ^= mask; |
| 371 | } |
| 372 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void ClearAllRegToLockDepths(size_t reg) { |
| 376 | reg_to_lock_depths_.erase(reg); |
| 377 | } |
| 378 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 379 | RegisterLine(size_t num_regs, MethodVerifier* verifier); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 380 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 381 | // Storage for the result register's type, valid after an invocation. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 382 | uint16_t result_[2]; |
| 383 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 384 | // Length of reg_types_ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 385 | const uint32_t num_regs_; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 386 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 387 | // A stack of monitor enter locations. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 388 | ScopedArenaVector<uint32_t> monitors_; |
| 389 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 390 | // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor |
| 391 | // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 392 | // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 393 | RegToLockDepthsMap reg_to_lock_depths_; |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 394 | |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 395 | // Whether "this" initialization (a constructor supercall) has happened. |
| 396 | bool this_initialized_; |
| 397 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 398 | // An array of RegType Ids associated with each dex register. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 399 | uint16_t line_[1]; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 400 | |
| 401 | DISALLOW_COPY_AND_ASSIGN(RegisterLine); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 402 | }; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 403 | |
| 404 | } // namespace verifier |
| 405 | } // namespace art |
| 406 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 407 | #endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ |