method_verifier.h revision 95c0bf8fb5847cff263639f889d04c7c3c26eedd
1776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
2776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Copyright (C) 2011 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_METHOD_VERIFIER_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
19776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
20700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory>
21776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include <set>
22776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include <vector>
23776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
241aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/casts.h"
25761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
261aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
2751c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom#include "class_reference.h"
28776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "dex_file.h"
29776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "dex_instruction.h"
307b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers#include "instruction_flags.h"
3151c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom#include "method_reference.h"
32776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "reg_type.h"
33576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers#include "reg_type_cache.h"
34776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "register_line.h"
35776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "safe_map.h"
36776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
37776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace art {
38776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
39776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstruct ReferenceMap2Visitor;
40eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartiertemplate<class T> class Handle;
41776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
42776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace verifier {
43776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
44776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass MethodVerifier;
4546c6bb2f52cef82660b9be7576b49f83845df93aIan Rogersclass DexPcToReferenceMap;
46776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
47776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
48776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
49776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * method determines which list we search, and whether we travel up into superclasses.
50776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
51776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * All others are stored in the "virtual" list.)
53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
54776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum MethodType {
55776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_UNKNOWN  = 0,
56776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_DIRECT,      // <init>, private
57776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_STATIC,      // static
58776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_VIRTUAL,     // virtual, super
59776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_INTERFACE    // interface
60776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
612fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogersstd::ostream& operator<<(std::ostream& os, const MethodType& rhs);
62776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
63776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * An enumeration of problems that can turn up during verification.
65776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
66776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
67776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * that can potentially be corrected, and the verifier will try again at runtime.
68776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
69776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
70776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * to be rewritten to fail at runtime.
71776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
72776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum VerifyError {
737934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_BAD_CLASS_HARD,  // VerifyError; hard error that skips compilation.
747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_BAD_CLASS_SOFT,  // VerifyError; soft error that verifies again at runtime.
75776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
767934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_CLASS,        // NoClassDefFoundError.
777934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_FIELD,        // NoSuchFieldError.
787934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_METHOD,       // NoSuchMethodError.
797934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_CLASS,    // IllegalAccessError.
807934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_FIELD,    // IllegalAccessError.
817934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_METHOD,   // IllegalAccessError.
827934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_CLASS_CHANGE,    // IncompatibleClassChangeError.
837934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_INSTANTIATION,   // InstantiationError.
84776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
85776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstd::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
86776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
87776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
88776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Identifies the type of reference in the instruction that generated the verify error
89776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference).
90776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
91776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * This must fit in two bits.
92776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
93776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum VerifyErrorRefType {
94776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_CLASS  = 0,
95776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_FIELD  = 1,
96776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_METHOD = 2,
97776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
98776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersconst int kVerifyErrorRefTypeShift = 6;
99776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
100776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// We don't need to store the register data for many instructions, because we either only need
101776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// it at branch points (for verification) or GC points and branches (for verification +
102776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// type-precise register analysis).
103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum RegisterTrackingMode {
104776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsBranches,
10502c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  kTrackCompilerInterestPoints,
106776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsAll,
107776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
108776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// A mapping from a dex pc to the register line statuses as they are immediately prior to the
1102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// execution of that instruction.
111776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass PcToRegisterLineTable {
112776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
113d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  PcToRegisterLineTable() : size_(0) {}
114d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  ~PcToRegisterLineTable();
115776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
116776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Initialize the RegisterTable. Every instruction address can have a different set of information
117776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // about what's in which register, but for verification purposes we only need to store it at
118776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // branch target addresses (because we merge into that).
1197b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  void Init(RegisterTrackingMode mode, InstructionFlags* flags, uint32_t insns_size,
120776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers            uint16_t registers_size, MethodVerifier* verifier);
121776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
122776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegisterLine* GetLine(size_t idx) {
123d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    DCHECK_LT(idx, size_);
124d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    return register_lines_[idx];
125776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
126776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
127776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers private:
128700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine*[]> register_lines_;
129d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  size_t size_;
130776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
131776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
132776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// The verifier
133776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass MethodVerifier {
134776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
135f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  enum FailureKind {
136f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kNoFailure,
137f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kSoftFailure,
138f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kHardFailure,
139f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  };
140f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao
141f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  /* Verify a class. Returns "kNoFailure" on success. */
142ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  static FailureKind VerifyClass(mirror::Class* klass, bool allow_soft_failures, std::string* error)
143b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1440cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier  static FailureKind VerifyClass(const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
1450cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                 Handle<mirror::ClassLoader> class_loader,
1468b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                 const DexFile::ClassDef* class_def,
1478b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                 bool allow_soft_failures, std::string* error)
148b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  static void VerifyMethodAndDump(std::ostream& os, uint32_t method_idx, const DexFile* dex_file,
1510cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                  Handle<mirror::DexCache> dex_cache,
1520cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                  Handle<mirror::ClassLoader> class_loader,
1538b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  const DexFile::ClassDef* class_def,
1548b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  const DexFile::CodeItem* code_item,
155ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                  mirror::ArtMethod* method, uint32_t method_access_flags)
1562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
158776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint8_t EncodePcToReferenceMapData() const;
159776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
160776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t DexFileVersion() const {
161776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return dex_file_->GetVersion();
162776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
163776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
164776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache* GetRegTypeCache() {
165776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return &reg_types_;
166776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
167776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
168ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log a verification failure.
169776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostream& Fail(VerifyError error);
170776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
171ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log for verification information.
172576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers  std::ostream& LogVerifyInfo();
173776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
174ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Dump the failures encountered by the verifier.
175ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::ostream& DumpFailures(std::ostream& os);
176ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
177776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Dump the state of the verifier, namely each instruction, what flags are set on it, register
178776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // information
179b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
180776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
18108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
1822d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // to the locks held at 'dex_pc' in method 'm'.
183ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static void FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
18446960fe5dcc1be07b39a55114338423a73554449Ian Rogers                               std::vector<uint32_t>* monitor_enter_dex_pcs)
185b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
18608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
1872d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the accessed field corresponding to the quick instruction's field
1882d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // offset at 'dex_pc' in method 'm'.
189ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static mirror::ArtField* FindAccessedFieldAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc)
1902d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1912d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
1922d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the invoked method corresponding to the quick instruction's vtable
1932d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // index at 'dex_pc' in method 'm'.
194ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static mirror::ArtMethod* FindInvokedMethodAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc)
1952d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1962d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
19751a5fb78d1b03b5235c2ae45414235282182bb86Sameer Abu Asal  static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1980a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes  static void Shutdown();
199776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2004993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  bool CanLoadClasses() const {
2014993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return can_load_classes_;
2024993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
2034993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
204eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
205eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier                 Handle<mirror::ClassLoader>* class_loader, const DexFile::ClassDef* class_def,
206590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                 const DexFile::CodeItem* code_item, uint32_t method_idx, mirror::ArtMethod* method,
20746960fe5dcc1be07b39a55114338423a73554449Ian Rogers                 uint32_t access_flags, bool can_load_classes, bool allow_soft_failures,
20846960fe5dcc1be07b39a55114338423a73554449Ian Rogers                 bool need_precise_constants)
209b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers          SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
210ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
211590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  ~MethodVerifier();
21233691abbc43850fd2b2951256c4b6bbc9733ccc8Sebastien Hertz
2137b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Run verification on the method. Returns true if verification completes and false if the input
2147b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // has an irrecoverable corruption.
2157b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2167b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
2177b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Describe VRegs at the given dex pc.
2187b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
2197b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
22083c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier  void VisitRoots(RootCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
221c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier
2222b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  // Accessors used by the compiler via CompilerCallback
2232b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const DexFile::CodeItem* CodeItem() const;
2242b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  RegisterLine* GetRegLine(uint32_t dex_pc);
2252b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const InstructionFlags& GetInstructionFlags(size_t index) const;
2262b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2272b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2282b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  MethodReference GetMethodReference() const;
2292b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  uint32_t GetAccessFlags() const;
2302b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasCheckCasts() const;
2312b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasVirtualOrInterfaceInvokes() const;
2322b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasFailures() const;
2332b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const RegType& ResolveCheckedClass(uint32_t class_idx)
2342b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2352b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko
2367b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers private:
237ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the beginning of the last failure message.
238ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void PrependToLastFailMessage(std::string);
239ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
240ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the end of the last failure message.
241ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void AppendToLastFailMessage(std::string);
242776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
243776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
244776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification on a single method.
245776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
246776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We do this in three passes:
247776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (1) Walk through all code units, determining instruction locations,
248776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      widths, and other characteristics.
249776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (2) Walk through all code units, performing static checks on
250776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      operands.
251776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (3) Iterate through the method, checking type safety and looking
252776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      for code flow problems.
253e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers   */
2542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
2550cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                  Handle<mirror::DexCache> dex_cache,
2560cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartier                                  Handle<mirror::ClassLoader> class_loader,
2578b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  const DexFile::ClassDef* class_def_idx,
2582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                  const DexFile::CodeItem* code_item,
259ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                  mirror::ArtMethod* method, uint32_t method_access_flags,
26046960fe5dcc1be07b39a55114338423a73554449Ian Rogers                                  bool allow_soft_failures, bool need_precise_constants)
261b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers          SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
262e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers
263b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
26408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
265ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* FindAccessedFieldAtDexPc(uint32_t dex_pc)
266c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2672d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
268ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* FindInvokedMethodAtDexPc(uint32_t dex_pc)
269c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2702d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
271776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
272776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Compute the width of the instruction at each address in the instruction stream, and store it in
273776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
274776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
275776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
276776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
277776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
278776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Performs some static checks, notably:
279776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcode of first instruction begins at index 0
280776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - only documented instructions may appear
281776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - each instruction follows the last
282776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - last byte of last instruction is at (code_length-1)
283776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
284776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Logs an error and returns "false" on failure.
285776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
286776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool ComputeWidthsAndCountOps();
287776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
288776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
289776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
290776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "branch target" flags for exception handlers.
291776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
292776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Call this after widths have been set in "insn_flags".
293776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
294776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "false" if something in the exception table looks fishy, but we're expecting the
295776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception table to be somewhat sane.
296776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
297b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
298776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
299776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
300776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on all instructions in a method.
301776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
302776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Walks through instructions in a method calling VerifyInstruction on each.
303776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
304776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstructions();
305776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
306776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
307776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on an instruction.
308776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
309776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * As a side effect, this sets the "branch target" flags in InsnFlags.
310776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
311776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "(CF)" items are handled during code-flow analysis.
312776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
313776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.10.1
314776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - target of each jump and branch instruction must be valid
315776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of switch statements must be valid
316776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands referencing constant pool entries must be valid
317776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
318776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of method invocation instructions must be valid
319776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) only invoke-direct can call a method starting with '<'
320776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) <clinit> must never be called explicitly
321776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands of instanceof, checkcast, new (and variants) must be valid
322776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - new-array[-type] limited to 255 dimensions
323776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - can't use "new" on an array class
324776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (?) limit dimensions in multi-array creation
325776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variable load/store register values must be in valid range
326776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
327776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
328776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - branches must be within the bounds of the code array
329776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of all control-flow instructions are the start of an instruction
330776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - register accesses fall within range of allocated registers
331776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) access to constant pool must be of appropriate type
332776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - code does not end in the middle of an instruction
333776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
334776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the "try" area must begin and
335776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   end at the start of an instruction (end can be at the end of the code)
336776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the handler must start at a valid
337776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   instruction
338776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
339776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
340776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
341776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the register index is valid for this code item. */
342776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckRegisterIndex(uint32_t idx);
343776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
344776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the wide register index is valid for this code item. */
345776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckWideRegisterIndex(uint32_t idx);
346776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
347eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  // Perform static checks on a field Get or set instruction. All we do here is ensure that the
348776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // field index is in the valid range.
349776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckFieldIndex(uint32_t idx);
350776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
351776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a method invocation instruction. All we do here is ensure that the
352776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // method index is in the valid range.
353776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckMethodIndex(uint32_t idx);
354776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
355776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
356776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // reference isn't for an array class.
357776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewInstance(uint32_t idx);
358776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
359776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the string index is in the valid range. */
360776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckStringIndex(uint32_t idx);
361776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
362776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on an instruction that takes a class constant. Ensure that the class
363776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // index is in the valid range.
364776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckTypeIndex(uint32_t idx);
365776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
366776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
367776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // creating an array of arrays that causes the number of dimensions to exceed 255.
368776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewArray(uint32_t idx);
369776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
370776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
371776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckArrayData(uint32_t cur_offset);
372776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
373776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
374776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // into an exception handler, but it's valid to do so as long as the target isn't a
375776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // "move-exception" instruction. We verify that in a later stage.
376776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The dex format forbids certain instructions from branching to themselves.
37724edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
378776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckBranchTarget(uint32_t cur_offset);
379776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
380776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify a switch table. "cur_offset" is the offset of the switch instruction.
38124edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
382776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckSwitchTargets(uint32_t cur_offset);
383776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
384776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
385776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // filled-new-array.
386776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count (0-5), args[] have values.
387776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
388776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // takes a double is done with consecutive registers. This requires parsing the target method
389776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // signature, which we will be doing later on during the code flow analysis.
390776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
391776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
392776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
393776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // or filled-new-array/range.
394776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count, vC holds index of first reg.
395776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
396776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
397776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Extract the relative offset from a branch instruction.
398776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Returns "false" on failure (e.g. this isn't a branch instruction).
399776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
400776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers                       bool* selfOkay);
401776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
402776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Perform detailed code-flow analysis on a single method. */
403b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
404776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
405776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the register types for the first instruction in the method based on the method signature.
406776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // This has the side-effect of validating the signature.
407b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
409776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
410776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform code flow on a method.
411776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
412776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
413776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction, process it (setting additional "changed" bits), and repeat until there are no
414776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * more.
415776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
416776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.1
417776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) operand stack is always the same size
418776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operand stack [registers] contain the correct types of values
419776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variables [registers] contain the correct types of values
420776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - methods are invoked with the appropriate arguments
421776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - fields are assigned using values of appropriate types
422776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcodes have the correct type values in operand registers
423776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - there is never an uninitialized class instance in a local variable in code protected by an
424776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception handler (operand stack is okay, because the operand stack is discarded when an
425776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
426776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   register typing]
427776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
428776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
429776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
430776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
431776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * (We also do many of the items described in the "static checks" sections, because it's easier to
432776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * do them here.)
433776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
434776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We need an array of RegType values, one per register, for every instruction. If the method uses
435776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * monitor-enter, we need extra data for every register, and a stack for every "interesting"
436776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction. In theory this could become quite large -- up to several megabytes for a monster
437776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * function.
438776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
439776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * NOTE:
440776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The spec forbids backward branches when there's an uninitialized reference in a register. The
441776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * idea is to prevent something like this:
442776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   loop:
443776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     move r1, r0
444776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     new-instance r0, MyClass
445776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     ...
446776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     if-eq rN, loop  // once
447776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   initialize r0
448776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
449776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This leaves us with two different instances, both allocated by the same instruction, but only
450776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
451776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * it by preventing backward branches. We achieve identical results without restricting code
452776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * reordering by specifying that you can't execute the new-instance instruction if a register
453776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * contains an uninitialized instance created by that same instruction.
454776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
455b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
456776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
457776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
458776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification for a single instruction.
459776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
460776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This requires fully decoding the instruction to determine the effect it has on registers.
461776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
462776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Finds zero or more following instructions and sets the "changed" flag if execution at that
463776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
464776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * addresses. Does not set or clear any other flags in "insn_flags_".
465776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool CodeFlowVerifyInstruction(uint32_t* start_guess)
467b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
468776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
469776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of a new array instruction
4705243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
471b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
472776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
473fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao  // Helper to perform verification on puts of primitive type.
474fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao  void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
475fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao                          const uint32_t vregA) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
476fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao
477776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aget instruction. The destination register's type will be set to
478776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // be that of component type of the array unless the array type is unknown, in which case a
479776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // bottom type inferred from the type of instruction is used. is_primitive is false for an
480776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // aget-object.
4815243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyAGet(const Instruction* inst, const RegType& insn_type,
482b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                  bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
483776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
484776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aput instruction.
4855243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyAPut(const Instruction* inst, const RegType& insn_type,
486b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                  bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
487776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
488776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup instance field and fail for resolution violations
489ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
490b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
492776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup static field and fail for resolution violations
493ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
494776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
495776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an iget or sget instruction.
4965243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyISGet(const Instruction* inst, const RegType& insn_type,
49700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                   bool is_primitive, bool is_static)
498b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
499776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
500776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an iput or sput instruction.
5015243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyISPut(const Instruction* inst, const RegType& insn_type,
50200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                   bool is_primitive, bool is_static)
503b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
504776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
505c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz  // Returns the access field of a quick field access (iget/iput-quick) or NULL
506c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz  // if it cannot be found.
507ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line)
508c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
509c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz
5102d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Perform verification of an iget-quick instruction.
5112d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  void VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
5122d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz                       bool is_primitive)
5132d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
5142d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
5152d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Perform verification of an iput-quick instruction.
5162d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  void VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
5172d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz                       bool is_primitive)
5182d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
5192d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
520776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Resolves a class based on an index and performs access checks to ensure the referrer can
521776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // access the resolved class.
52200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
523b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
524776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
525776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
526776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
527776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * address, determine the Join of all exceptions that can land here. Fails if no matching
528776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception handler can be found or if the Join of exception types fails.
529776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
53000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  const RegType& GetCaughtExceptionType()
531b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
532776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
533776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
534776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Resolves a method based on an index and performs access checks to ensure
535776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the referrer can access the resolved method.
536776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Does not throw exceptions.
537776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
538ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
539b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
540776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
541776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
542776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify the arguments to a method. We're executing in "method", making
543776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * a call to the method reference in vB.
544776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
545776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * If this is a "direct" invoke, we allow calls to <init>. For calls to
546776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * <init>, the first argument may be an uninitialized reference. Otherwise,
547776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * calls to anything starting with '<' will be rejected, as will any
548776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * uninitialized reference arguments.
549776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
550776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For non-static method calls, this will verify that the method call is
551776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * appropriate for the "this" argument.
552776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
553776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The method reference is in vBBBB. The "is_range" parameter determines
554776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * whether we use 0-4 "args" values or a range of registers defined by
555776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * vAA and vCCCC.
556776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
557776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Widening conversions on integers and references are allowed, but
558776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * narrowing conversions are not.
559776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
560776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns the resolved method on success, NULL on failure (with *failure
561776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * set appropriately).
562776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
563ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* VerifyInvocationArgs(const Instruction* inst,
564ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                          MethodType method_type,
565ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                          bool is_range, bool is_super)
566b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
567776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
56895c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // Similar checks to the above, but on the proto. Will be used when the method cannot be
56995c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // resolved.
57095c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
57195c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                            bool is_range)
57295c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57395c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
57495c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  template <class T>
57595c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  mirror::ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
57695c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                                      MethodType method_type, bool is_range,
57795c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                                      mirror::ArtMethod* res_method)
57895c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
57995c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
580ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* GetQuickInvokedMethod(const Instruction* inst,
581ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                           RegisterLine* reg_line,
582ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                           bool is_range)
583c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
584c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz
585ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range)
5862d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
5872d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
588776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
589776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify that the target instruction is not "move-exception". It's important that the only way
590776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * to execute a move-exception is as the first instruction of an exception handler.
591776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "true" if all is well, "false" if the target instruction is move-exception.
592776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
593776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
594776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
595776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
596776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
597776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * next_insn, and set the changed flag on the target address if any of the registers were changed.
598776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Returns "false" if an error is encountered.
599776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  */
60000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line)
601b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
602776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
603ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Is the method being verified a constructor?
604ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  bool IsConstructor() const {
605ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers    return (method_access_flags_ & kAccConstructor) != 0;
606ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  }
607ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
608ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Is the method verified static?
609ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  bool IsStatic() const {
610ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers    return (method_access_flags_ & kAccStatic) != 0;
611ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  }
612ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
613ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Return the register type for the method.
614b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
615ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
616ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Get a type representing the declaring class of the method.
617b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
618ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
6197b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  InstructionFlags* CurrentInsnFlags();
620776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
621849600bb5cfc02bf5ab4aa9a810667ebd3b53328Sebastien Hertz  const RegType& DetermineCat1Constant(int32_t value, bool precise)
622849600bb5cfc02bf5ab4aa9a810667ebd3b53328Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
6230a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes
624776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache reg_types_;
625776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
626776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  PcToRegisterLineTable reg_table_;
627776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
628776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're currently working on.
629700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine> work_line_;
630776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
631776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The address of the instruction we're currently working on, note that this is in 2 byte
632776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // quantities
633776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t work_insn_idx_;
634776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
635776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're saving for later.
636700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine> saved_line_;
637776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
638637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t dex_method_idx_;  // The method we're working on.
63900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Its object representation if known.
640ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* mirror_method_ GUARDED_BY(Locks::mutator_lock_);
641637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t method_access_flags_;  // Method's access flags.
642fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  const RegType* return_type_;  // Lazily computed return type of the method.
643637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile* const dex_file_;  // The dex file containing the method.
64400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The dex_cache for the declaring class of the method.
645eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache>* dex_cache_ GUARDED_BY(Locks::mutator_lock_);
64600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The class loader for the declaring class of the method.
647eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader>* class_loader_ GUARDED_BY(Locks::mutator_lock_);
6488b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers  const DexFile::ClassDef* const class_def_;  // The class def of the declaring class of the method.
649637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile::CodeItem* const code_item_;  // The code item containing the code for the method.
650637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const RegType* declaring_class_;  // Lazily computed reg type of the method's declaring class.
6517b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Instruction widths and flags, one entry per code unit.
652700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<InstructionFlags[]> insn_flags_;
653c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz  // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
65408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  uint32_t interesting_dex_pc_;
65508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // The container into which FindLocksAtDexPc should write the registers containing held locks,
65608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // NULL if we're not doing FindLocksAtDexPc.
65708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  std::vector<uint32_t>* monitor_enter_dex_pcs_;
65808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
659ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // The types of any error that occurs.
660ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<VerifyError> failures_;
661ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Error messages associated with failures.
662ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<std::ostringstream*> failure_messages_;
663ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Is there a pending hard failure?
664ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  bool have_pending_hard_failure_;
665faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
666faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // would fail at runtime throwing an exception. Such an instruction causes the following code
667faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
668faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // instructions that would hard fail the verification.
669faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  bool have_pending_runtime_throw_failure_;
670776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
671ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Info message log use primarily for verifier diagnostics.
672776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostringstream info_messages_;
673776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
674776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The number of occurrences of specific opcodes.
675776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t new_instance_count_;
676776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t monitor_enter_count_;
67780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
67880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  const bool can_load_classes_;
679ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao
680ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // Converts soft failures to hard failures when false. Only false when the compiler isn't
681ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // running and the verifier is called from the class linker.
682ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  const bool allow_soft_failures_;
6834d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
68446960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // An optimization where instead of generating unique RegTypes for constants we use imprecise
68546960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // constants that cover a range of constants. This isn't good enough for deoptimization that
68646960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // avoids loading from registers in the case of a constant as the dex instruction set lost the
68746960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // notion of whether a value should be in a floating point or general purpose register file.
68846960fe5dcc1be07b39a55114338423a73554449Ian Rogers  const bool need_precise_constants_;
68946960fe5dcc1be07b39a55114338423a73554449Ian Rogers
690a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one check-cast or aput-object
691a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // instruction. Aput-object operations implicitly check for array-store exceptions, similar to
692a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // check-cast.
6934d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_check_casts_;
6944d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
695a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one invoke-virtual/range
6964d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  // or invoke-interface/range.
6974d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_virtual_or_interface_invokes_;
698776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
699e4f0b2ab4abd8e942a099e9b9b4682fbdd9eb21cjeffhaostd::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
700776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
701776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace verifier
702776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace art
703776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
704fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
705