xref: /aosp_15_r20/art/runtime/verifier/register_line.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <limits>
21*795d594fSAndroid Build Coastguard Worker #include <memory>
22*795d594fSAndroid Build Coastguard Worker #include <vector>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include "base/arena_containers.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/safe_map.h"
30*795d594fSAndroid Build Coastguard Worker #include "reg_type.h"
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker class Instruction;
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace verifier {
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker class MethodVerifier;
39*795d594fSAndroid Build Coastguard Worker class RegType;
40*795d594fSAndroid Build Coastguard Worker class RegTypeCache;
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker /*
43*795d594fSAndroid Build Coastguard Worker  * Register type categories, for type checking.
44*795d594fSAndroid Build Coastguard Worker  *
45*795d594fSAndroid Build Coastguard Worker  * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and
46*795d594fSAndroid Build Coastguard Worker  * returnAddress. Category 2 includes long and double.
47*795d594fSAndroid Build Coastguard Worker  *
48*795d594fSAndroid Build Coastguard Worker  * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so
49*795d594fSAndroid Build Coastguard Worker  * there is no "returnAddress" type.
50*795d594fSAndroid Build Coastguard Worker  */
51*795d594fSAndroid Build Coastguard Worker enum TypeCategory {
52*795d594fSAndroid Build Coastguard Worker   kTypeCategoryUnknown = 0,
53*795d594fSAndroid Build Coastguard Worker   kTypeCategory1nr = 1,         // boolean, byte, char, short, int, float
54*795d594fSAndroid Build Coastguard Worker   kTypeCategory2 = 2,           // long, double
55*795d594fSAndroid Build Coastguard Worker   kTypeCategoryRef = 3,         // object reference
56*795d594fSAndroid Build Coastguard Worker };
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker // What to do with the lock levels when setting the register type.
59*795d594fSAndroid Build Coastguard Worker enum class LockOp {
60*795d594fSAndroid Build Coastguard Worker   kClear,                       // Clear the lock levels recorded.
61*795d594fSAndroid Build Coastguard Worker   kKeep                         // Leave the lock levels alone.
62*795d594fSAndroid Build Coastguard Worker };
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker // During verification, we associate one of these with every "interesting" instruction. We track
65*795d594fSAndroid Build Coastguard Worker // the status of all registers, and (if the method has any monitor-enter instructions) maintain a
66*795d594fSAndroid Build Coastguard Worker // stack of entered monitors (identified by code unit offset).
67*795d594fSAndroid Build Coastguard Worker class RegisterLine {
68*795d594fSAndroid Build Coastguard Worker  public:
69*795d594fSAndroid Build Coastguard Worker   using RegisterStackMask = uint32_t;
70*795d594fSAndroid Build Coastguard Worker   // A map from register to a bit vector of indices into the monitors_ stack.
71*795d594fSAndroid Build Coastguard Worker   using RegToLockDepthsMap = ArenaSafeMap<uint32_t, RegisterStackMask>;
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   // Maximum number of nested monitors to track before giving up and
74*795d594fSAndroid Build Coastguard Worker   // taking the slow path.
75*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kMaxMonitorStackDepth =
76*795d594fSAndroid Build Coastguard Worker       std::numeric_limits<RegisterStackMask>::digits;
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker   // Create a register line of num_regs registers.
79*795d594fSAndroid Build Coastguard Worker   static RegisterLine* Create(size_t num_regs, ArenaAllocator& allocator, RegTypeCache* reg_types);
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst".
82*795d594fSAndroid Build Coastguard Worker   void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat)
83*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This
86*795d594fSAndroid Build Coastguard Worker   // copies both halves of the register.
87*795d594fSAndroid Build Coastguard Worker   void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc)
88*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   // Implement "move-result". Copy the category-1 value from the result register to another
91*795d594fSAndroid Build Coastguard Worker   // register, and reset the result register.
92*795d594fSAndroid Build Coastguard Worker   void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference)
93*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   // Implement "move-result-wide". Copy the category-2 value from the result register to another
96*795d594fSAndroid Build Coastguard Worker   // register, and reset the result register.
97*795d594fSAndroid Build Coastguard Worker   void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst)
98*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker   // Set the invisible result register to unknown
101*795d594fSAndroid Build Coastguard Worker   void SetResultTypeToUnknown(RegTypeCache* reg_types) REQUIRES_SHARED(Locks::mutator_lock_);
102*795d594fSAndroid Build Coastguard Worker 
103*795d594fSAndroid Build Coastguard Worker   // Set the type of register N, verifying that the register is valid.  If "newType" is the "Lo"
104*795d594fSAndroid Build Coastguard Worker   // part of a 64-bit value, register N+1 will be set to "newType+1".
105*795d594fSAndroid Build Coastguard Worker   // The register index was validated during the static pass, so we don't need to check it here.
106*795d594fSAndroid Build Coastguard Worker   //
107*795d594fSAndroid Build Coastguard Worker   // LockOp::kClear should be used by default; it will clear the lock levels associated with the
108*795d594fSAndroid Build Coastguard Worker   // register. An example is setting the register type because an instruction writes to the
109*795d594fSAndroid Build Coastguard Worker   // register.
110*795d594fSAndroid Build Coastguard Worker   // LockOp::kKeep keeps the lock levels of the register and only changes the register type. This
111*795d594fSAndroid Build Coastguard Worker   // is typical when the underlying value did not change, but we have "different" type information
112*795d594fSAndroid Build Coastguard Worker   // available now. An example is sharpening types after a check-cast. Note that when given kKeep,
113*795d594fSAndroid Build Coastguard Worker   // the new_type is dchecked to be a reference type.
114*795d594fSAndroid Build Coastguard Worker   ALWAYS_INLINE void SetRegisterType(uint32_t vdst, RegType::Kind new_kind)
115*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
116*795d594fSAndroid Build Coastguard Worker   template <LockOp kLockOp>
117*795d594fSAndroid Build Coastguard Worker   ALWAYS_INLINE void SetRegisterType(uint32_t vdst, const RegType& new_type)
118*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
119*795d594fSAndroid Build Coastguard Worker 
120*795d594fSAndroid Build Coastguard Worker   void SetRegisterTypeWide(uint32_t vdst, RegType::Kind new_kind1, RegType::Kind new_kind2)
121*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
122*795d594fSAndroid Build Coastguard Worker   void SetRegisterTypeWide(uint32_t vdst, const RegType& new_type1, const RegType& new_type2)
123*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker   /* Set the type of the "result" register. */
126*795d594fSAndroid Build Coastguard Worker   void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
127*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
128*795d594fSAndroid Build Coastguard Worker 
129*795d594fSAndroid Build Coastguard Worker   void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
130*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker   /*
133*795d594fSAndroid Build Coastguard Worker    * Set register type for a `new-instance` instruction.
134*795d594fSAndroid Build Coastguard Worker    * For `new-instance`, we additionally record the allocation dex pc for vreg `vdst`.
135*795d594fSAndroid Build Coastguard Worker    * This is used to keep track of registers that hold the same uninitialized reference,
136*795d594fSAndroid Build Coastguard Worker    * so that we can update them all when a constructor is called on any of them.
137*795d594fSAndroid Build Coastguard Worker    */
138*795d594fSAndroid Build Coastguard Worker   void SetRegisterTypeForNewInstance(uint32_t vdst, const RegType& uninit_type, uint32_t dex_pc)
139*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker   // Get the id of the register tyoe of register vsrc.
142*795d594fSAndroid Build Coastguard Worker   uint16_t GetRegisterTypeId(uint32_t vsrc) const;
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker   // Get the type of register vsrc.
145*795d594fSAndroid Build Coastguard Worker   const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const;
146*795d594fSAndroid Build Coastguard Worker 
147*795d594fSAndroid Build Coastguard Worker   void CopyFromLine(const RegisterLine* src);
148*795d594fSAndroid Build Coastguard Worker 
149*795d594fSAndroid Build Coastguard Worker   std::string Dump(MethodVerifier* verifier) const REQUIRES_SHARED(Locks::mutator_lock_);
150*795d594fSAndroid Build Coastguard Worker 
FillWithGarbage()151*795d594fSAndroid Build Coastguard Worker   void FillWithGarbage() {
152*795d594fSAndroid Build Coastguard Worker     memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
153*795d594fSAndroid Build Coastguard Worker     monitors_.clear();
154*795d594fSAndroid Build Coastguard Worker     reg_to_lock_depths_.clear();
155*795d594fSAndroid Build Coastguard Worker   }
156*795d594fSAndroid Build Coastguard Worker 
157*795d594fSAndroid Build Coastguard Worker   /*
158*795d594fSAndroid Build Coastguard Worker    * In debug mode, assert that the register line does not contain an uninitialized register
159*795d594fSAndroid Build Coastguard Worker    * type for a `new-instance` allocation at a specific dex pc. We do this check before recording
160*795d594fSAndroid Build Coastguard Worker    * the uninitialized register type and dex pc for a `new-instance` instruction.
161*795d594fSAndroid Build Coastguard Worker    */
162*795d594fSAndroid Build Coastguard Worker   void DCheckUniqueNewInstanceDexPc(MethodVerifier* verifier, uint32_t dex_pc)
163*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker   /*
166*795d594fSAndroid Build Coastguard Worker    * Update all registers holding the uninitialized type currently recorded for vreg `vsrc` to
167*795d594fSAndroid Build Coastguard Worker    * instead hold the corresponding initialized reference type. This is called when an appropriate
168*795d594fSAndroid Build Coastguard Worker    * constructor is invoked -- all copies of the reference must be marked as initialized.
169*795d594fSAndroid Build Coastguard Worker    */
170*795d594fSAndroid Build Coastguard Worker   void MarkRefsAsInitialized(MethodVerifier* verifier, uint32_t vsrc)
171*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
172*795d594fSAndroid Build Coastguard Worker 
173*795d594fSAndroid Build Coastguard Worker   /*
174*795d594fSAndroid Build Coastguard Worker    * Update all registers to be Conflict except vsrc.
175*795d594fSAndroid Build Coastguard Worker    */
176*795d594fSAndroid Build Coastguard Worker   void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
177*795d594fSAndroid Build Coastguard Worker   void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
178*795d594fSAndroid Build Coastguard Worker   void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
179*795d594fSAndroid Build Coastguard Worker 
SetThisInitialized()180*795d594fSAndroid Build Coastguard Worker   void SetThisInitialized() {
181*795d594fSAndroid Build Coastguard Worker     this_initialized_ = true;
182*795d594fSAndroid Build Coastguard Worker   }
183*795d594fSAndroid Build Coastguard Worker 
CopyThisInitialized(const RegisterLine & src)184*795d594fSAndroid Build Coastguard Worker   void CopyThisInitialized(const RegisterLine& src) {
185*795d594fSAndroid Build Coastguard Worker     this_initialized_ = src.this_initialized_;
186*795d594fSAndroid Build Coastguard Worker   }
187*795d594fSAndroid Build Coastguard Worker 
188*795d594fSAndroid Build Coastguard Worker   /*
189*795d594fSAndroid Build Coastguard Worker    * Check constraints on constructor return. Specifically, make sure that the "this" argument got
190*795d594fSAndroid Build Coastguard Worker    * initialized.
191*795d594fSAndroid Build Coastguard Worker    * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
192*795d594fSAndroid Build Coastguard Worker    * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
193*795d594fSAndroid Build Coastguard Worker    * somehow didn't get initialized.
194*795d594fSAndroid Build Coastguard Worker    */
195*795d594fSAndroid Build Coastguard Worker   bool CheckConstructorReturn(MethodVerifier* verifier) const;
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker   // Compare two register lines. Returns 0 if they match.
198*795d594fSAndroid Build Coastguard Worker   // Using this for a sort is unwise, since the value can change based on machine endianness.
CompareLine(const RegisterLine * line2)199*795d594fSAndroid Build Coastguard Worker   int CompareLine(const RegisterLine* line2) const {
200*795d594fSAndroid Build Coastguard Worker     if (monitors_ != line2->monitors_) {
201*795d594fSAndroid Build Coastguard Worker       return 1;
202*795d594fSAndroid Build Coastguard Worker     }
203*795d594fSAndroid Build Coastguard Worker     // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
204*795d594fSAndroid Build Coastguard Worker     return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
205*795d594fSAndroid Build Coastguard Worker   }
206*795d594fSAndroid Build Coastguard Worker 
NumRegs()207*795d594fSAndroid Build Coastguard Worker   size_t NumRegs() const {
208*795d594fSAndroid Build Coastguard Worker     return num_regs_;
209*795d594fSAndroid Build Coastguard Worker   }
210*795d594fSAndroid Build Coastguard Worker 
211*795d594fSAndroid Build Coastguard Worker   // Return how many bytes of memory a register line uses.
212*795d594fSAndroid Build Coastguard Worker   ALWAYS_INLINE static size_t ComputeSize(size_t num_regs);
213*795d594fSAndroid Build Coastguard Worker 
214*795d594fSAndroid Build Coastguard Worker   // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
215*795d594fSAndroid Build Coastguard Worker   void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
216*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
217*795d594fSAndroid Build Coastguard Worker 
218*795d594fSAndroid Build Coastguard Worker   // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
219*795d594fSAndroid Build Coastguard Worker   void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
220*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
221*795d594fSAndroid Build Coastguard Worker 
222*795d594fSAndroid Build Coastguard Worker   // Stack of currently held monitors and where they were locked
MonitorStackDepth()223*795d594fSAndroid Build Coastguard Worker   size_t MonitorStackDepth() const {
224*795d594fSAndroid Build Coastguard Worker     return monitors_.size();
225*795d594fSAndroid Build Coastguard Worker   }
226*795d594fSAndroid Build Coastguard Worker 
227*795d594fSAndroid Build Coastguard Worker   // We expect no monitors to be held at certain points, such a method returns. Verify the stack
228*795d594fSAndroid Build Coastguard Worker   // is empty, queueing a LOCKING error else.
229*795d594fSAndroid Build Coastguard Worker   void VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
230*795d594fSAndroid Build Coastguard Worker 
231*795d594fSAndroid Build Coastguard Worker   bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
232*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
233*795d594fSAndroid Build Coastguard Worker 
GetMonitorEnterCount()234*795d594fSAndroid Build Coastguard Worker   size_t GetMonitorEnterCount() const {
235*795d594fSAndroid Build Coastguard Worker     return monitors_.size();
236*795d594fSAndroid Build Coastguard Worker   }
237*795d594fSAndroid Build Coastguard Worker 
GetMonitorEnterDexPc(size_t i)238*795d594fSAndroid Build Coastguard Worker   uint32_t GetMonitorEnterDexPc(size_t i) const {
239*795d594fSAndroid Build Coastguard Worker     return monitors_[i];
240*795d594fSAndroid Build Coastguard Worker   }
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker   // We give access to the lock depth map to avoid an expensive poll loop for FindLocksAtDexPC.
243*795d594fSAndroid Build Coastguard Worker   template <typename T>
IterateRegToLockDepths(T fn)244*795d594fSAndroid Build Coastguard Worker   void IterateRegToLockDepths(T fn) const {
245*795d594fSAndroid Build Coastguard Worker     for (const auto& pair : reg_to_lock_depths_) {
246*795d594fSAndroid Build Coastguard Worker       const uint32_t reg = pair.first;
247*795d594fSAndroid Build Coastguard Worker       uint32_t depths = pair.second;
248*795d594fSAndroid Build Coastguard Worker       uint32_t depth = 0;
249*795d594fSAndroid Build Coastguard Worker       while (depths != 0) {
250*795d594fSAndroid Build Coastguard Worker         if ((depths & 1) != 0) {
251*795d594fSAndroid Build Coastguard Worker           fn(reg, depth);
252*795d594fSAndroid Build Coastguard Worker         }
253*795d594fSAndroid Build Coastguard Worker         depths >>= 1;
254*795d594fSAndroid Build Coastguard Worker         depth++;
255*795d594fSAndroid Build Coastguard Worker       }
256*795d594fSAndroid Build Coastguard Worker     }
257*795d594fSAndroid Build Coastguard Worker   }
258*795d594fSAndroid Build Coastguard Worker 
259*795d594fSAndroid Build Coastguard Worker  private:
260*795d594fSAndroid Build Coastguard Worker   // For uninitialized types we need to check for allocation dex pc mismatch when merging.
261*795d594fSAndroid Build Coastguard Worker   // This does not apply to uninitialized "this" reference types.
262*795d594fSAndroid Build Coastguard Worker   static bool NeedsAllocationDexPc(const RegType& reg_type);
263*795d594fSAndroid Build Coastguard Worker 
264*795d594fSAndroid Build Coastguard Worker   void EnsureAllocationDexPcsAvailable();
265*795d594fSAndroid Build Coastguard Worker 
266*795d594fSAndroid Build Coastguard Worker   template <LockOp kLockOp>
267*795d594fSAndroid Build Coastguard Worker   ALWAYS_INLINE void SetRegisterTypeImpl(uint32_t vdst, uint16_t new_id)
268*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
269*795d594fSAndroid Build Coastguard Worker   void SetRegisterTypeWideImpl(uint32_t vdst, uint16_t new_id1, uint16_t new_id2)
270*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(Locks::mutator_lock_);
271*795d594fSAndroid Build Coastguard Worker 
CopyRegToLockDepth(size_t dst,size_t src)272*795d594fSAndroid Build Coastguard Worker   void CopyRegToLockDepth(size_t dst, size_t src) {
273*795d594fSAndroid Build Coastguard Worker     auto it = reg_to_lock_depths_.find(src);
274*795d594fSAndroid Build Coastguard Worker     if (it != reg_to_lock_depths_.end()) {
275*795d594fSAndroid Build Coastguard Worker       reg_to_lock_depths_.Put(dst, it->second);
276*795d594fSAndroid Build Coastguard Worker     }
277*795d594fSAndroid Build Coastguard Worker   }
278*795d594fSAndroid Build Coastguard Worker 
IsSetLockDepth(size_t reg,size_t depth)279*795d594fSAndroid Build Coastguard Worker   bool IsSetLockDepth(size_t reg, size_t depth) {
280*795d594fSAndroid Build Coastguard Worker     auto it = reg_to_lock_depths_.find(reg);
281*795d594fSAndroid Build Coastguard Worker     if (it != reg_to_lock_depths_.end()) {
282*795d594fSAndroid Build Coastguard Worker       return (it->second & (1 << depth)) != 0;
283*795d594fSAndroid Build Coastguard Worker     } else {
284*795d594fSAndroid Build Coastguard Worker       return false;
285*795d594fSAndroid Build Coastguard Worker     }
286*795d594fSAndroid Build Coastguard Worker   }
287*795d594fSAndroid Build Coastguard Worker 
SetRegToLockDepth(size_t reg,size_t depth)288*795d594fSAndroid Build Coastguard Worker   bool SetRegToLockDepth(size_t reg, size_t depth) {
289*795d594fSAndroid Build Coastguard Worker     CHECK_LT(depth, kMaxMonitorStackDepth);
290*795d594fSAndroid Build Coastguard Worker     if (IsSetLockDepth(reg, depth)) {
291*795d594fSAndroid Build Coastguard Worker       return false;  // Register already holds lock so locking twice is erroneous.
292*795d594fSAndroid Build Coastguard Worker     }
293*795d594fSAndroid Build Coastguard Worker     auto it = reg_to_lock_depths_.find(reg);
294*795d594fSAndroid Build Coastguard Worker     if (it == reg_to_lock_depths_.end()) {
295*795d594fSAndroid Build Coastguard Worker       reg_to_lock_depths_.Put(reg, 1 << depth);
296*795d594fSAndroid Build Coastguard Worker     } else {
297*795d594fSAndroid Build Coastguard Worker       it->second |= (1 << depth);
298*795d594fSAndroid Build Coastguard Worker     }
299*795d594fSAndroid Build Coastguard Worker     return true;
300*795d594fSAndroid Build Coastguard Worker   }
301*795d594fSAndroid Build Coastguard Worker 
302*795d594fSAndroid Build Coastguard Worker   void ClearRegToLockDepth(size_t reg, size_t depth);
303*795d594fSAndroid Build Coastguard Worker 
ClearAllRegToLockDepths(size_t reg)304*795d594fSAndroid Build Coastguard Worker   void ClearAllRegToLockDepths(size_t reg) {
305*795d594fSAndroid Build Coastguard Worker     reg_to_lock_depths_.erase(reg);
306*795d594fSAndroid Build Coastguard Worker   }
307*795d594fSAndroid Build Coastguard Worker 
308*795d594fSAndroid Build Coastguard Worker   RegisterLine(size_t num_regs, ArenaAllocator& allocator, RegTypeCache* reg_types);
309*795d594fSAndroid Build Coastguard Worker 
310*795d594fSAndroid Build Coastguard Worker   static constexpr uint32_t kNoDexPc = static_cast<uint32_t>(-1);
311*795d594fSAndroid Build Coastguard Worker 
312*795d594fSAndroid Build Coastguard Worker   // Length of reg_types_
313*795d594fSAndroid Build Coastguard Worker   const uint32_t num_regs_;
314*795d594fSAndroid Build Coastguard Worker 
315*795d594fSAndroid Build Coastguard Worker   // Storage for the result register's type, valid after an invocation.
316*795d594fSAndroid Build Coastguard Worker   uint16_t result_[2];
317*795d594fSAndroid Build Coastguard Worker 
318*795d594fSAndroid Build Coastguard Worker   // Track allocation dex pcs for `new-instance` results moved to other registers.
319*795d594fSAndroid Build Coastguard Worker   uint32_t* allocation_dex_pcs_;
320*795d594fSAndroid Build Coastguard Worker 
321*795d594fSAndroid Build Coastguard Worker   // A stack of monitor enter locations.
322*795d594fSAndroid Build Coastguard Worker   ArenaVector<uint32_t> monitors_;
323*795d594fSAndroid Build Coastguard Worker 
324*795d594fSAndroid Build Coastguard Worker   // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
325*795d594fSAndroid Build Coastguard Worker   // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
326*795d594fSAndroid Build Coastguard Worker   // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5.
327*795d594fSAndroid Build Coastguard Worker   RegToLockDepthsMap reg_to_lock_depths_;
328*795d594fSAndroid Build Coastguard Worker 
329*795d594fSAndroid Build Coastguard Worker   // Whether "this" initialization (a constructor supercall) has happened.
330*795d594fSAndroid Build Coastguard Worker   bool this_initialized_;
331*795d594fSAndroid Build Coastguard Worker 
332*795d594fSAndroid Build Coastguard Worker   // An array of RegType Ids associated with each dex register.
333*795d594fSAndroid Build Coastguard Worker   uint16_t line_[1];
334*795d594fSAndroid Build Coastguard Worker 
335*795d594fSAndroid Build Coastguard Worker   friend class RegisterLineArenaDelete;
336*795d594fSAndroid Build Coastguard Worker 
337*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(RegisterLine);
338*795d594fSAndroid Build Coastguard Worker };
339*795d594fSAndroid Build Coastguard Worker 
340*795d594fSAndroid Build Coastguard Worker class RegisterLineArenaDelete : public ArenaDelete<RegisterLine> {
341*795d594fSAndroid Build Coastguard Worker  public:
342*795d594fSAndroid Build Coastguard Worker   void operator()(RegisterLine* ptr) const;
343*795d594fSAndroid Build Coastguard Worker };
344*795d594fSAndroid Build Coastguard Worker 
345*795d594fSAndroid Build Coastguard Worker using RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>;
346*795d594fSAndroid Build Coastguard Worker 
347*795d594fSAndroid Build Coastguard Worker }  // namespace verifier
348*795d594fSAndroid Build Coastguard Worker }  // namespace art
349*795d594fSAndroid Build Coastguard Worker 
350*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
351