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 17 #ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ 18 #define ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ 19 20 #include <limits> 21 #include <memory> 22 #include <vector> 23 24 #include <android-base/logging.h> 25 26 #include "base/locks.h" 27 #include "base/safe_map.h" 28 #include "base/scoped_arena_containers.h" 29 30 namespace art { 31 32 class Instruction; 33 34 namespace verifier { 35 36 class MethodVerifier; 37 class RegType; 38 class RegTypeCache; 39 40 /* 41 * Register type categories, for type checking. 42 * 43 * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and 44 * returnAddress. Category 2 includes long and double. 45 * 46 * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so 47 * there is no "returnAddress" type. 48 */ 49 enum TypeCategory { 50 kTypeCategoryUnknown = 0, 51 kTypeCategory1nr = 1, // boolean, byte, char, short, int, float 52 kTypeCategory2 = 2, // long, double 53 kTypeCategoryRef = 3, // object reference 54 }; 55 56 // What to do with the lock levels when setting the register type. 57 enum class LockOp { 58 kClear, // Clear the lock levels recorded. 59 kKeep // Leave the lock levels alone. 60 }; 61 62 // During verification, we associate one of these with every "interesting" instruction. We track 63 // the status of all registers, and (if the method has any monitor-enter instructions) maintain a 64 // stack of entered monitors (identified by code unit offset). 65 class RegisterLine { 66 public: 67 using RegisterStackMask = uint32_t; 68 // A map from register to a bit vector of indices into the monitors_ stack. 69 using RegToLockDepthsMap = ScopedArenaSafeMap<uint32_t, RegisterStackMask>; 70 71 // Maximum number of nested monitors to track before giving up and 72 // taking the slow path. 73 static constexpr size_t kMaxMonitorStackDepth = 74 std::numeric_limits<RegisterStackMask>::digits; 75 76 // Create a register line of num_regs registers. 77 static RegisterLine* Create(size_t num_regs, 78 ScopedArenaAllocator& allocator, 79 RegTypeCache* reg_types); 80 81 // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst". 82 void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat) 83 REQUIRES_SHARED(Locks::mutator_lock_); 84 85 // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This 86 // copies both halves of the register. 87 void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc) 88 REQUIRES_SHARED(Locks::mutator_lock_); 89 90 // Implement "move-result". Copy the category-1 value from the result register to another 91 // register, and reset the result register. 92 void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference) 93 REQUIRES_SHARED(Locks::mutator_lock_); 94 95 // Implement "move-result-wide". Copy the category-2 value from the result register to another 96 // register, and reset the result register. 97 void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst) 98 REQUIRES_SHARED(Locks::mutator_lock_); 99 100 // Set the invisible result register to unknown 101 void SetResultTypeToUnknown(RegTypeCache* reg_types) REQUIRES_SHARED(Locks::mutator_lock_); 102 103 // Set the type of register N, verifying that the register is valid. If "newType" is the "Lo" 104 // part of a 64-bit value, register N+1 will be set to "newType+1". 105 // The register index was validated during the static pass, so we don't need to check it here. 106 // 107 // LockOp::kClear should be used by default; it will clear the lock levels associated with the 108 // register. An example is setting the register type because an instruction writes to the 109 // register. 110 // LockOp::kKeep keeps the lock levels of the register and only changes the register type. This 111 // is typical when the underlying value did not change, but we have "different" type information 112 // available now. An example is sharpening types after a check-cast. Note that when given kKeep, 113 // the new_type is dchecked to be a reference type. 114 template <LockOp kLockOp> 115 ALWAYS_INLINE void SetRegisterType(uint32_t vdst, const RegType& new_type) 116 REQUIRES_SHARED(Locks::mutator_lock_); 117 118 void SetRegisterTypeWide(uint32_t vdst, 119 const RegType& new_type1, 120 const RegType& new_type2) 121 REQUIRES_SHARED(Locks::mutator_lock_); 122 123 /* Set the type of the "result" register. */ 124 void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type) 125 REQUIRES_SHARED(Locks::mutator_lock_); 126 127 void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2) 128 REQUIRES_SHARED(Locks::mutator_lock_); 129 130 // Get the type of register vsrc. 131 const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const; 132 133 ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, 134 uint32_t vsrc, 135 const RegType& check_type) 136 REQUIRES_SHARED(Locks::mutator_lock_); 137 138 bool VerifyRegisterTypeWide(MethodVerifier* verifier, 139 uint32_t vsrc, 140 const RegType& check_type1, 141 const RegType& check_type2) 142 REQUIRES_SHARED(Locks::mutator_lock_); 143 CopyFromLine(const RegisterLine * src)144 void CopyFromLine(const RegisterLine* src) { 145 DCHECK_EQ(num_regs_, src->num_regs_); 146 memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t)); 147 monitors_ = src->monitors_; 148 reg_to_lock_depths_ = src->reg_to_lock_depths_; 149 this_initialized_ = src->this_initialized_; 150 } 151 152 std::string Dump(MethodVerifier* verifier) const REQUIRES_SHARED(Locks::mutator_lock_); 153 FillWithGarbage()154 void FillWithGarbage() { 155 memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t)); 156 monitors_.clear(); 157 reg_to_lock_depths_.clear(); 158 } 159 160 /* 161 * We're creating a new instance of class C at address A. Any registers holding instances 162 * previously created at address A must be initialized by now. If not, we mark them as "conflict" 163 * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and 164 * the new ones at the same time). 165 */ 166 void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type) 167 REQUIRES_SHARED(Locks::mutator_lock_); 168 169 /* 170 * Update all registers holding "uninit_type" to instead hold the corresponding initialized 171 * reference type. This is called when an appropriate constructor is invoked -- all copies of 172 * the reference must be marked as initialized. 173 */ 174 void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type) 175 REQUIRES_SHARED(Locks::mutator_lock_); 176 177 /* 178 * Update all registers to be Conflict except vsrc. 179 */ 180 void MarkAllRegistersAsConflicts(MethodVerifier* verifier); 181 void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc); 182 void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc); 183 SetThisInitialized()184 void SetThisInitialized() { 185 this_initialized_ = true; 186 } 187 CopyThisInitialized(const RegisterLine & src)188 void CopyThisInitialized(const RegisterLine& src) { 189 this_initialized_ = src.this_initialized_; 190 } 191 192 /* 193 * Check constraints on constructor return. Specifically, make sure that the "this" argument got 194 * initialized. 195 * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start 196 * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it 197 * somehow didn't get initialized. 198 */ 199 bool CheckConstructorReturn(MethodVerifier* verifier) const; 200 201 // Compare two register lines. Returns 0 if they match. 202 // Using this for a sort is unwise, since the value can change based on machine endianness. CompareLine(const RegisterLine * line2)203 int CompareLine(const RegisterLine* line2) const { 204 if (monitors_ != line2->monitors_) { 205 return 1; 206 } 207 // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_); 208 return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t)); 209 } 210 NumRegs()211 size_t NumRegs() const { 212 return num_regs_; 213 } 214 215 // Return how many bytes of memory a register line uses. 216 ALWAYS_INLINE static size_t ComputeSize(size_t num_regs); 217 218 /* 219 * Get the "this" pointer from a non-static method invocation. This returns the RegType so the 220 * caller can decide whether it needs the reference to be initialized or not. (Can also return 221 * kRegTypeZero if the reference can only be zero at this point.) 222 * 223 * The argument count is in vA, and the first argument is in vC, for both "simple" and "range" 224 * versions. We just need to make sure vA is >= 1 and then return vC. 225 * allow_failure will return Conflict() instead of causing a verification failure if there is an 226 * error. 227 */ 228 const RegType& GetInvocationThis(MethodVerifier* verifier, 229 const Instruction* inst, 230 bool allow_failure = false) 231 REQUIRES_SHARED(Locks::mutator_lock_); 232 233 /* 234 * Verify types for a simple two-register instruction (e.g. "neg-int"). 235 * "dst_type" is stored into vA, and "src_type" is verified against vB. 236 */ 237 void CheckUnaryOp(MethodVerifier* verifier, 238 const Instruction* inst, 239 const RegType& dst_type, 240 const RegType& src_type) 241 REQUIRES_SHARED(Locks::mutator_lock_); 242 243 void CheckUnaryOpWide(MethodVerifier* verifier, 244 const Instruction* inst, 245 const RegType& dst_type1, 246 const RegType& dst_type2, 247 const RegType& src_type1, 248 const RegType& src_type2) 249 REQUIRES_SHARED(Locks::mutator_lock_); 250 251 void CheckUnaryOpToWide(MethodVerifier* verifier, 252 const Instruction* inst, 253 const RegType& dst_type1, 254 const RegType& dst_type2, 255 const RegType& src_type) 256 REQUIRES_SHARED(Locks::mutator_lock_); 257 258 void CheckUnaryOpFromWide(MethodVerifier* verifier, 259 const Instruction* inst, 260 const RegType& dst_type, 261 const RegType& src_type1, 262 const RegType& src_type2) 263 REQUIRES_SHARED(Locks::mutator_lock_); 264 265 /* 266 * Verify types for a simple three-register instruction (e.g. "add-int"). 267 * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified 268 * against vB/vC. 269 */ 270 void CheckBinaryOp(MethodVerifier* verifier, 271 const Instruction* inst, 272 const RegType& dst_type, 273 const RegType& src_type1, 274 const RegType& src_type2, 275 bool check_boolean_op) 276 REQUIRES_SHARED(Locks::mutator_lock_); 277 278 void CheckBinaryOpWide(MethodVerifier* verifier, 279 const Instruction* inst, 280 const RegType& dst_type1, 281 const RegType& dst_type2, 282 const RegType& src_type1_1, 283 const RegType& src_type1_2, 284 const RegType& src_type2_1, 285 const RegType& src_type2_2) 286 REQUIRES_SHARED(Locks::mutator_lock_); 287 288 void CheckBinaryOpWideShift(MethodVerifier* verifier, 289 const Instruction* inst, 290 const RegType& long_lo_type, 291 const RegType& long_hi_type, 292 const RegType& int_type) 293 REQUIRES_SHARED(Locks::mutator_lock_); 294 295 /* 296 * Verify types for a binary "2addr" operation. "src_type1"/"src_type2" 297 * are verified against vA/vB, then "dst_type" is stored into vA. 298 */ 299 void CheckBinaryOp2addr(MethodVerifier* verifier, 300 const Instruction* inst, 301 const RegType& dst_type, 302 const RegType& src_type1, 303 const RegType& src_type2, 304 bool check_boolean_op) 305 REQUIRES_SHARED(Locks::mutator_lock_); 306 307 void CheckBinaryOp2addrWide(MethodVerifier* verifier, 308 const Instruction* inst, 309 const RegType& dst_type1, 310 const RegType& dst_type2, 311 const RegType& src_type1_1, 312 const RegType& src_type1_2, 313 const RegType& src_type2_1, 314 const RegType& src_type2_2) 315 REQUIRES_SHARED(Locks::mutator_lock_); 316 317 void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, 318 const Instruction* inst, 319 const RegType& long_lo_type, 320 const RegType& long_hi_type, 321 const RegType& int_type) 322 REQUIRES_SHARED(Locks::mutator_lock_); 323 324 /* 325 * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8"). 326 * "dst_type" is stored into vA, and "src_type" is verified against vB. 327 * 328 * If "check_boolean_op" is set, we use the constant value in vC. 329 */ 330 void CheckLiteralOp(MethodVerifier* verifier, 331 const Instruction* inst, 332 const RegType& dst_type, 333 const RegType& src_type, 334 bool check_boolean_op, 335 bool is_lit16) 336 REQUIRES_SHARED(Locks::mutator_lock_); 337 338 // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx. 339 void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx) 340 REQUIRES_SHARED(Locks::mutator_lock_); 341 342 // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked 343 void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx) 344 REQUIRES_SHARED(Locks::mutator_lock_); 345 346 // Stack of currently held monitors and where they were locked MonitorStackDepth()347 size_t MonitorStackDepth() const { 348 return monitors_.size(); 349 } 350 351 // We expect no monitors to be held at certain points, such a method returns. Verify the stack 352 // is empty, queueing a LOCKING error else. 353 void VerifyMonitorStackEmpty(MethodVerifier* verifier) const; 354 355 bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line) 356 REQUIRES_SHARED(Locks::mutator_lock_); 357 GetMonitorEnterCount()358 size_t GetMonitorEnterCount() const { 359 return monitors_.size(); 360 } 361 GetMonitorEnterDexPc(size_t i)362 uint32_t GetMonitorEnterDexPc(size_t i) const { 363 return monitors_[i]; 364 } 365 366 // We give access to the lock depth map to avoid an expensive poll loop for FindLocksAtDexPC. 367 template <typename T> IterateRegToLockDepths(T fn)368 void IterateRegToLockDepths(T fn) const { 369 for (const auto& pair : reg_to_lock_depths_) { 370 const uint32_t reg = pair.first; 371 uint32_t depths = pair.second; 372 uint32_t depth = 0; 373 while (depths != 0) { 374 if ((depths & 1) != 0) { 375 fn(reg, depth); 376 } 377 depths >>= 1; 378 depth++; 379 } 380 } 381 } 382 383 private: CopyRegToLockDepth(size_t dst,size_t src)384 void CopyRegToLockDepth(size_t dst, size_t src) { 385 auto it = reg_to_lock_depths_.find(src); 386 if (it != reg_to_lock_depths_.end()) { 387 reg_to_lock_depths_.Put(dst, it->second); 388 } 389 } 390 IsSetLockDepth(size_t reg,size_t depth)391 bool IsSetLockDepth(size_t reg, size_t depth) { 392 auto it = reg_to_lock_depths_.find(reg); 393 if (it != reg_to_lock_depths_.end()) { 394 return (it->second & (1 << depth)) != 0; 395 } else { 396 return false; 397 } 398 } 399 SetRegToLockDepth(size_t reg,size_t depth)400 bool SetRegToLockDepth(size_t reg, size_t depth) { 401 CHECK_LT(depth, kMaxMonitorStackDepth); 402 if (IsSetLockDepth(reg, depth)) { 403 return false; // Register already holds lock so locking twice is erroneous. 404 } 405 auto it = reg_to_lock_depths_.find(reg); 406 if (it == reg_to_lock_depths_.end()) { 407 reg_to_lock_depths_.Put(reg, 1 << depth); 408 } else { 409 it->second |= (1 << depth); 410 } 411 return true; 412 } 413 414 void ClearRegToLockDepth(size_t reg, size_t depth); 415 ClearAllRegToLockDepths(size_t reg)416 void ClearAllRegToLockDepths(size_t reg) { 417 reg_to_lock_depths_.erase(reg); 418 } 419 420 RegisterLine(size_t num_regs, ScopedArenaAllocator& allocator, RegTypeCache* reg_types); 421 422 // Storage for the result register's type, valid after an invocation. 423 uint16_t result_[2]; 424 425 // Length of reg_types_ 426 const uint32_t num_regs_; 427 428 // A stack of monitor enter locations. 429 ScopedArenaVector<uint32_t> monitors_; 430 431 // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor 432 // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a 433 // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5. 434 RegToLockDepthsMap reg_to_lock_depths_; 435 436 // Whether "this" initialization (a constructor supercall) has happened. 437 bool this_initialized_; 438 439 // An array of RegType Ids associated with each dex register. 440 uint16_t line_[1]; 441 442 DISALLOW_COPY_AND_ASSIGN(RegisterLine); 443 }; 444 445 class RegisterLineArenaDelete : public ArenaDelete<RegisterLine> { 446 public: 447 void operator()(RegisterLine* ptr) const; 448 }; 449 450 } // namespace verifier 451 } // namespace art 452 453 #endif // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_ 454