register_line.h revision 848f70a3d73833fc1bf3032a9ff6812e429661d9
1776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
2776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Copyright (C) 2012 The Android Open Source Project
3776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
4776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Licensed under the Apache License, Version 2.0 (the "License");
5776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * you may not use this file except in compliance with the License.
6776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * You may obtain a copy of the License at
7776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
8776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *      http://www.apache.org/licenses/LICENSE-2.0
9776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
10776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Unless required by applicable law or agreed to in writing, software
11776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * distributed under the License is distributed on an "AS IS" BASIS,
12776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * See the License for the specific language governing permissions and
14776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * limitations under the License.
15776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
16776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
19776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
20700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory>
21776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include <vector>
22776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
23776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "safe_map.h"
24776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
25776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace art {
268e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
278e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass Instruction;
288e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
29776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace verifier {
30776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
31776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass MethodVerifier;
328e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass RegType;
33776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
34776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
35776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Register type categories, for type checking.
36776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
37776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * The spec says category 1 includes boolean, byte, char, short, int, float, reference, and
38776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * returnAddress. Category 2 includes long and double.
39776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
40776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * We treat object references separately, so we have "category1nr". We don't support jsr/ret, so
41776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * there is no "returnAddress" type.
42776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
43776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum TypeCategory {
44776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTypeCategoryUnknown = 0,
45776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTypeCategory1nr = 1,         // boolean, byte, char, short, int, float
46776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTypeCategory2 = 2,           // long, double
47776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTypeCategoryRef = 3,         // object reference
48776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
49776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
50776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// During verification, we associate one of these with every "interesting" instruction. We track
51776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// the status of all registers, and (if the method has any monitor-enter instructions) maintain a
52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// stack of entered monitors (identified by code unit offset).
53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass RegisterLine {
54776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
55d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  static RegisterLine* Create(size_t num_regs, MethodVerifier* verifier) {
566afd409d9ff5911d7de6967836535e15219ab31fMathieu Chartier    void* memory = operator new(sizeof(RegisterLine) + (num_regs * sizeof(uint16_t)));
57d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    RegisterLine* rl = new (memory) RegisterLine(num_regs, verifier);
58d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    return rl;
59776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
60776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
61776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Implement category-1 "move" instructions. Copy a 32-bit value from "vsrc" to "vdst".
627b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CopyRegister1(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc, TypeCategory cat)
63b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
65776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Implement category-2 "move" instructions. Copy a 64-bit value from "vsrc" to "vdst". This
66776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // copies both halves of the register.
677b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CopyRegister2(MethodVerifier* verifier, uint32_t vdst, uint32_t vsrc)
68b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
70776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Implement "move-result". Copy the category-1 value from the result register to another
71776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // register, and reset the result register.
727b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CopyResultRegister1(MethodVerifier* verifier, uint32_t vdst, bool is_reference)
73b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
75776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Implement "move-result-wide". Copy the category-2 value from the result register to another
76776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // register, and reset the result register.
777b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CopyResultRegister2(MethodVerifier* verifier, uint32_t vdst)
78b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
79776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
80776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the invisible result register to unknown
817b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
83776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the type of register N, verifying that the register is valid.  If "newType" is the "Lo"
84776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // part of a 64-bit value, register N+1 will be set to "newType+1".
85776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The register index was validated during the static pass, so we don't need to check it here.
867b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  ALWAYS_INLINE bool SetRegisterType(MethodVerifier* verifier, uint32_t vdst,
877b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                                     const RegType& new_type)
88b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
89776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
907b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1,
917b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                           const RegType& new_type2)
922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
94776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Set the type of the "result" register. */
957b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
96b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
97776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
98d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
101776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Get the type of register vsrc.
1027b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  const RegType& GetRegisterType(MethodVerifier* verifier, uint32_t vsrc) const;
103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1047b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  ALWAYS_INLINE bool VerifyRegisterType(MethodVerifier* verifier, uint32_t vsrc,
1057b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                                        const RegType& check_type)
106b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1087b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1,
1097b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                              const RegType& check_type2)
1102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
112776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void CopyFromLine(const RegisterLine* src) {
113776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK_EQ(num_regs_, src->num_regs_);
114d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    memcpy(&line_, &src->line_, num_regs_ * sizeof(uint16_t));
115776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    monitors_ = src->monitors_;
116776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    reg_to_lock_depths_ = src->reg_to_lock_depths_;
117776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
118776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1197b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  std::string Dump(MethodVerifier* verifier) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
121776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void FillWithGarbage() {
122d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
123db0cccd9a74cb9aa5f3b62f377415f653e3c0ca4Logan Chien    monitors_.clear();
124776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    reg_to_lock_depths_.clear();
125776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
126776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
127776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
128776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We're creating a new instance of class C at address A. Any registers holding instances
129776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * previously created at address A must be initialized by now. If not, we mark them as "conflict"
130776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and
131776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the new ones at the same time).
132776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1337b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type)
134b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
136776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
137776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Update all registers holding "uninit_type" to instead hold the corresponding initialized
138776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * reference type. This is called when an appropriate constructor is invoked -- all copies of
139776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the reference must be marked as initialized.
140776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
141848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao  void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type,
142848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao                             uint32_t this_reg, uint32_t dex_pc)
1432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
144776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
145776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
146b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers   * Update all registers to be Conflict except vsrc.
147b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers   */
1487b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
1497b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
1507b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
151b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers
152b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers  /*
153776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Check constraints on constructor return. Specifically, make sure that the "this" argument got
154776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * initialized.
155776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
156776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
157776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * somehow didn't get initialized.
158776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1597b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool CheckConstructorReturn(MethodVerifier* verifier) const;
160776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1617e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  /*
1627e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle   * Check if an UninitializedThis at the specified location has been overwritten before
1637e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle   * being correctly initialized.
1647e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle   */
1657e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  bool WasUninitializedThisOverwritten(MethodVerifier* verifier, size_t this_loc,
1667e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle                                       bool was_invoke_direct) const;
1677e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle
1687e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  /*
1697e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle   * Get the first location of an UninitializedThis type, or return kInvalidVreg if there are none.
1707e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle   */
1717e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  bool GetUninitializedThisLoc(MethodVerifier* verifier, size_t* vreg) const;
1727e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle
173776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Compare two register lines. Returns 0 if they match.
174776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Using this for a sort is unwise, since the value can change based on machine endianness.
175776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  int CompareLine(const RegisterLine* line2) const {
176776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(monitors_ == line2->monitors_);
177776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
178d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
179776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
180776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
181776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t NumRegs() const {
182776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return num_regs_;
183776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
184776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
185776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
186776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Get the "this" pointer from a non-static method invocation. This returns the RegType so the
187776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * caller can decide whether it needs the reference to be initialized or not. (Can also return
188776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * kRegTypeZero if the reference can only be zero at this point.)
189776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
190776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The argument count is in vA, and the first argument is in vC, for both "simple" and "range"
191776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * versions. We just need to make sure vA is >= 1 and then return vC.
192091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier   * allow_failure will return Conflict() instead of causing a verification failure if there is an
193091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier   * error.
194776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1957b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
196091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier                                   bool is_range, bool allow_failure = false)
197b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
198776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
199776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
200776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a simple two-register instruction (e.g. "neg-int").
201776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type" is verified against vB.
202776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2037b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type,
204d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                    const RegType& src_type)
205b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2077b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
208d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                        const RegType& dst_type1, const RegType& dst_type2,
209d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                        const RegType& src_type1, const RegType& src_type2)
2102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2127b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
213d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& dst_type1, const RegType& dst_type2,
214d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& src_type)
2152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2177b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
218d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                            const RegType& dst_type,
219d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                            const RegType& src_type1, const RegType& src_type2)
2202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
222776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
223776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a simple three-register instruction (e.g. "add-int").
224776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
225776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * against vB/vC.
226776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2277b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
228d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                     const RegType& dst_type, const RegType& src_type1, const RegType& src_type2,
22900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                     bool check_boolean_op)
230b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
231776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2327b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
233d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& dst_type1, const RegType& dst_type2,
234d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& src_type1_1, const RegType& src_type1_2,
235d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& src_type2_1, const RegType& src_type2_2)
2362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2387b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
239d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& long_lo_type, const RegType& long_hi_type,
240d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& int_type)
2412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
243776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
244776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
245776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * are verified against vA/vB, then "dst_type" is stored into vA.
246776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2477b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
248d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& dst_type,
249d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& src_type1, const RegType& src_type2,
25000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                          bool check_boolean_op)
251b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
252776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2537b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
254d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& dst_type1, const RegType& dst_type2,
255d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& src_type1_1, const RegType& src_type1_2,
256d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& src_type2_1, const RegType& src_type2_2)
2572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2597b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
260d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                                   const RegType& long_lo_type, const RegType& long_hi_type,
261d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                                   const RegType& int_type)
2622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
264776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
265776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
266776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type" is verified against vB.
267776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
268776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * If "check_boolean_op" is set, we use the constant value in vC.
269776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2707b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
271d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                      const RegType& dst_type, const RegType& src_type,
2725243e912875026f99428088db7e80cb11651d64eSebastien Hertz                      bool check_boolean_op, bool is_lit16)
273b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
274776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
275776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
2767b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
2777b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
278776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
279776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
2807b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
2817b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
282776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
283776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Stack of currently held monitors and where they were locked
284776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t MonitorStackDepth() const {
285776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return monitors_.size();
286776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
287776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
288776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // We expect no monitors to be held at certain points, such a method returns. Verify the stack
289776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // is empty, failing and returning false if not.
2907b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
291776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2927b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
293b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
294776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2958e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
296776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2977b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  // Write a bit at each register location that holds a reference.
2987b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
299776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
30008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  size_t GetMonitorEnterCount() {
30108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return monitors_.size();
30208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
30308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
30408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  uint32_t GetMonitorEnterDexPc(size_t i) {
30508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return monitors_[i];
30608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
30708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
308a21039c3ae2b20e44ceb2735251c04d0aac89afdElliott Hughes private:
309776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void CopyRegToLockDepth(size_t dst, size_t src) {
310bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(src);
311776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it != reg_to_lock_depths_.end()) {
312776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.Put(dst, it->second);
313776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
314776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
315776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
316776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool IsSetLockDepth(size_t reg, size_t depth) {
317bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
318776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it != reg_to_lock_depths_.end()) {
319776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      return (it->second & (1 << depth)) != 0;
320776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
321776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      return false;
322776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
323776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
324776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
3258e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  bool SetRegToLockDepth(size_t reg, size_t depth) {
326776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    CHECK_LT(depth, 32u);
3278e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    if (IsSetLockDepth(reg, depth)) {
3288e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers      return false;  // Register already holds lock so locking twice is erroneous.
3298e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    }
330bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
331776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it == reg_to_lock_depths_.end()) {
332776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.Put(reg, 1 << depth);
333776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
334776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      it->second |= (1 << depth);
335776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
3368e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    return true;
337776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
338776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
339776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void ClearRegToLockDepth(size_t reg, size_t depth) {
340776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    CHECK_LT(depth, 32u);
341776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(IsSetLockDepth(reg, depth));
342bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
343776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(it != reg_to_lock_depths_.end());
344776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    uint32_t depths = it->second ^ (1 << depth);
345776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (depths != 0) {
346776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      it->second = depths;
347776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
348776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.erase(it);
349776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
350776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
351776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
352776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void ClearAllRegToLockDepths(size_t reg) {
353776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    reg_to_lock_depths_.erase(reg);
354776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
355776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
356d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  RegisterLine(size_t num_regs, MethodVerifier* verifier)
3577b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers      : num_regs_(num_regs) {
358d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    memset(&line_, 0, num_regs_ * sizeof(uint16_t));
3597b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers    SetResultTypeToUnknown(verifier);
360d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  }
361d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers
3628e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // Storage for the result register's type, valid after an invocation.
363776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint16_t result_[2];
364776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
365776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Length of reg_types_
366ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  const uint32_t num_regs_;
3677b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers
3688e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // A stack of monitor enter locations.
369bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier  std::vector<uint32_t, TrackingAllocator<uint32_t, kAllocatorTagVerifier>> monitors_;
370776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
371776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
3728e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5.
373bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier  AllocationTrackingSafeMap<uint32_t, uint32_t, kAllocatorTagVerifier> reg_to_lock_depths_;
374d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers
375d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  // An array of RegType Ids associated with each dex register.
376d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  uint16_t line_[0];
3778e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
3788e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  DISALLOW_COPY_AND_ASSIGN(RegisterLine);
379776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
380776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
381776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace verifier
382776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace art
383776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
384fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_VERIFIER_REGISTER_LINE_H_
385