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