register_line.h revision f10b6e109bfb595b6752d1b59db680694ac1684d
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)
6390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(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)
6890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(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)
7390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(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)
7890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
79776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
80776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the invisible result register to unknown
8190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void SetResultTypeToUnknown(MethodVerifier* verifier) SHARED_REQUIRES(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)
8890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
89776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
907b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool SetRegisterTypeWide(MethodVerifier* verifier, uint32_t vdst, const RegType& new_type1,
917b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                           const RegType& new_type2)
9290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
94776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Set the type of the "result" register. */
957b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void SetResultRegisterType(MethodVerifier* verifier, const RegType& new_type)
9690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
97776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
98d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void SetResultRegisterTypeWide(const RegType& new_type1, const RegType& new_type2)
9990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(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)
10690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
107776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1087b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool VerifyRegisterTypeWide(MethodVerifier* verifier, uint32_t vsrc, const RegType& check_type1,
1097b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                              const RegType& check_type2)
11090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(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_;
117f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe    this_initialized_ = src->this_initialized_;
118776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
119776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
12090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  std::string Dump(MethodVerifier* verifier) const SHARED_REQUIRES(Locks::mutator_lock_);
121776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
122776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void FillWithGarbage() {
123d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    memset(&line_, 0xf1, num_regs_ * sizeof(uint16_t));
124db0cccd9a74cb9aa5f3b62f377415f653e3c0ca4Logan Chien    monitors_.clear();
125776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    reg_to_lock_depths_.clear();
126776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
127776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
128776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
129776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We're creating a new instance of class C at address A. Any registers holding instances
130776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * previously created at address A must be initialized by now. If not, we mark them as "conflict"
131776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * to prevent them from being used (otherwise, MarkRefsAsInitialized would mark the old ones and
132776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the new ones at the same time).
133776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1347b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkUninitRefsAsInvalid(MethodVerifier* verifier, const RegType& uninit_type)
13590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
136776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
137776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
138776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Update all registers holding "uninit_type" to instead hold the corresponding initialized
139776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * reference type. This is called when an appropriate constructor is invoked -- all copies of
140776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the reference must be marked as initialized.
141776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
142848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao  void MarkRefsAsInitialized(MethodVerifier* verifier, const RegType& uninit_type,
143848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao                             uint32_t this_reg, uint32_t dex_pc)
14490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
145776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
146776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
147b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers   * Update all registers to be Conflict except vsrc.
148b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers   */
1497b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflicts(MethodVerifier* verifier);
1507b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflictsExcept(MethodVerifier* verifier, uint32_t vsrc);
1517b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void MarkAllRegistersAsConflictsExceptWide(MethodVerifier* verifier, uint32_t vsrc);
152b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers
153f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  void SetThisInitialized() {
154f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe    this_initialized_ = true;
155f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  }
156f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe
157f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  void CopyThisInitialized(const RegisterLine& src) {
158f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe    this_initialized_ = src.this_initialized_;
159f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  }
160f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe
161b8c7859f21f5ae4c9b90f2ef2effc51967299737Ian Rogers  /*
162776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Check constraints on constructor return. Specifically, make sure that the "this" argument got
163776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * initialized.
164776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The "this" argument to <init> uses code offset kUninitThisArgAddr, which puts it at the start
165776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * of the list in slot 0. If we see a register with an uninitialized slot 0 reference, we know it
166776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * somehow didn't get initialized.
167776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1687b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool CheckConstructorReturn(MethodVerifier* verifier) const;
169776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
170776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Compare two register lines. Returns 0 if they match.
171776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Using this for a sort is unwise, since the value can change based on machine endianness.
172776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  int CompareLine(const RegisterLine* line2) const {
173776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(monitors_ == line2->monitors_);
174776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    // TODO: DCHECK(reg_to_lock_depths_ == line2->reg_to_lock_depths_);
175d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    return memcmp(&line_, &line2->line_, num_regs_ * sizeof(uint16_t));
176776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
177776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
178776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t NumRegs() const {
179776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return num_regs_;
180776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
181776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
182776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
183776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Get the "this" pointer from a non-static method invocation. This returns the RegType so the
184776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * caller can decide whether it needs the reference to be initialized or not. (Can also return
185776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * kRegTypeZero if the reference can only be zero at this point.)
186776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
187776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The argument count is in vA, and the first argument is in vC, for both "simple" and "range"
188776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * versions. We just need to make sure vA is >= 1 and then return vC.
189091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier   * allow_failure will return Conflict() instead of causing a verification failure if there is an
190091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier   * error.
191776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
1927b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  const RegType& GetInvocationThis(MethodVerifier* verifier, const Instruction* inst,
193091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier                                   bool is_range, bool allow_failure = false)
19490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
195776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
196776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
197776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a simple two-register instruction (e.g. "neg-int").
198776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type" is verified against vB.
199776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2007b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOp(MethodVerifier* verifier, const Instruction* inst, const RegType& dst_type,
201d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                    const RegType& src_type)
20290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
203776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2047b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpWide(MethodVerifier* verifier, const Instruction* inst,
205d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                        const RegType& dst_type1, const RegType& dst_type2,
206d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                        const RegType& src_type1, const RegType& src_type2)
20790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2097b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpToWide(MethodVerifier* verifier, const Instruction* inst,
210d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& dst_type1, const RegType& dst_type2,
211d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& src_type)
21290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2147b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckUnaryOpFromWide(MethodVerifier* verifier, const Instruction* inst,
215d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                            const RegType& dst_type,
216d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                            const RegType& src_type1, const RegType& src_type2)
21790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
219776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
220776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a simple three-register instruction (e.g. "add-int").
221776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type1"/"src_type2" are verified
222776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * against vB/vC.
223776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2247b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp(MethodVerifier* verifier, const Instruction* inst,
225d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                     const RegType& dst_type, const RegType& src_type1, const RegType& src_type2,
22600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                     bool check_boolean_op)
22790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
228776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2297b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOpWide(MethodVerifier* verifier, const Instruction* inst,
230d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& dst_type1, const RegType& dst_type2,
231d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& src_type1_1, const RegType& src_type1_2,
232d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                         const RegType& src_type2_1, const RegType& src_type2_2)
23390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2357b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOpWideShift(MethodVerifier* verifier, const Instruction* inst,
236d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& long_lo_type, const RegType& long_hi_type,
237d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& int_type)
23890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
240776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
241776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for a binary "2addr" operation. "src_type1"/"src_type2"
242776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * are verified against vA/vB, then "dst_type" is stored into vA.
243776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2447b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addr(MethodVerifier* verifier, const Instruction* inst,
245d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& dst_type,
246d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                          const RegType& src_type1, const RegType& src_type2,
24700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                          bool check_boolean_op)
24890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
249776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2507b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addrWide(MethodVerifier* verifier, const Instruction* inst,
251d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& dst_type1, const RegType& dst_type2,
252d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& src_type1_1, const RegType& src_type1_2,
253d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                              const RegType& src_type2_1, const RegType& src_type2_2)
25490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
2567b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckBinaryOp2addrWideShift(MethodVerifier* verifier, const Instruction* inst,
257d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                                   const RegType& long_lo_type, const RegType& long_hi_type,
258d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                                   const RegType& int_type)
25990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
261776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
262776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify types for A two-register instruction with a literal constant (e.g. "add-int/lit8").
263776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "dst_type" is stored into vA, and "src_type" is verified against vB.
264776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
265776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * If "check_boolean_op" is set, we use the constant value in vC.
266776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
2677b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void CheckLiteralOp(MethodVerifier* verifier, const Instruction* inst,
268d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers                      const RegType& dst_type, const RegType& src_type,
2695243e912875026f99428088db7e80cb11651d64eSebastien Hertz                      bool check_boolean_op, bool is_lit16)
27090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
271776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
272776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify/push monitor onto the monitor stack, locking the value in reg_idx at location insn_idx.
2737b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void PushMonitor(MethodVerifier* verifier, uint32_t reg_idx, int32_t insn_idx)
27490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
275776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
276776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify/pop monitor from monitor stack ensuring that we believe the monitor is locked
2777b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void PopMonitor(MethodVerifier* verifier, uint32_t reg_idx)
27890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
279776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
280776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Stack of currently held monitors and where they were locked
281776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t MonitorStackDepth() const {
282776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return monitors_.size();
283776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
284776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
285776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // We expect no monitors to be held at certain points, such a method returns. Verify the stack
286776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // is empty, failing and returning false if not.
2877b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool VerifyMonitorStackEmpty(MethodVerifier* verifier) const;
288776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2897b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  bool MergeRegisters(MethodVerifier* verifier, const RegisterLine* incoming_line)
29090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
291776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2928e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  size_t GetMaxNonZeroReferenceReg(MethodVerifier* verifier, size_t max_ref_reg) const;
293776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2947b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  // Write a bit at each register location that holds a reference.
2957b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  void WriteReferenceBitMap(MethodVerifier* verifier, std::vector<uint8_t>* data, size_t max_bytes);
296776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
29708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  size_t GetMonitorEnterCount() {
29808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return monitors_.size();
29908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
30008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
30108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  uint32_t GetMonitorEnterDexPc(size_t i) {
30208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return monitors_[i];
30308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
30408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
305a21039c3ae2b20e44ceb2735251c04d0aac89afdElliott Hughes private:
306776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void CopyRegToLockDepth(size_t dst, size_t src) {
307bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(src);
308776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it != reg_to_lock_depths_.end()) {
309776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.Put(dst, it->second);
310776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
311776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
312776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
313776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool IsSetLockDepth(size_t reg, size_t depth) {
314bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
315776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it != reg_to_lock_depths_.end()) {
316776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      return (it->second & (1 << depth)) != 0;
317776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
318776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      return false;
319776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
320776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
321776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
3228e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  bool SetRegToLockDepth(size_t reg, size_t depth) {
323776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    CHECK_LT(depth, 32u);
3248e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    if (IsSetLockDepth(reg, depth)) {
3258e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers      return false;  // Register already holds lock so locking twice is erroneous.
3268e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    }
327bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
328776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (it == reg_to_lock_depths_.end()) {
329776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.Put(reg, 1 << depth);
330776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
331776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      it->second |= (1 << depth);
332776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
3338e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers    return true;
334776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
335776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
336776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void ClearRegToLockDepth(size_t reg, size_t depth) {
337776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    CHECK_LT(depth, 32u);
338776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(IsSetLockDepth(reg, depth));
339bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier    auto it = reg_to_lock_depths_.find(reg);
340776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    DCHECK(it != reg_to_lock_depths_.end());
341776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    uint32_t depths = it->second ^ (1 << depth);
342776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    if (depths != 0) {
343776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      it->second = depths;
344776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    } else {
345776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers      reg_to_lock_depths_.erase(it);
346776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    }
347776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
348776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
349776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  void ClearAllRegToLockDepths(size_t reg) {
350776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    reg_to_lock_depths_.erase(reg);
351776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
352776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
353d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  RegisterLine(size_t num_regs, MethodVerifier* verifier)
354f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe      : num_regs_(num_regs), this_initialized_(false) {
355d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    memset(&line_, 0, num_regs_ * sizeof(uint16_t));
3567b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers    SetResultTypeToUnknown(verifier);
357d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  }
358d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers
3598e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // Storage for the result register's type, valid after an invocation.
360776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint16_t result_[2];
361776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
362776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Length of reg_types_
363ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  const uint32_t num_regs_;
3647b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers
3658e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // A stack of monitor enter locations.
366bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier  std::vector<uint32_t, TrackingAllocator<uint32_t, kAllocatorTagVerifier>> monitors_;
367776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // A map from register to a bit vector of indices into the monitors_ stack. As we pop the monitor
368776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // stack we verify that monitor-enter/exit are correctly nested. That is, if there was a
3698e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  // monitor-enter on v5 and then on v6, we expect the monitor-exit to be on v6 then on v5.
370bad0267eaab9d6a522d05469ff90501deefdb88bMathieu Chartier  AllocationTrackingSafeMap<uint32_t, uint32_t, kAllocatorTagVerifier> reg_to_lock_depths_;
371d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers
372f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  // Whether "this" initialization (a constructor supercall) has happened.
373f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  bool this_initialized_;
374f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe
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