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>
21637ee0b9c10ab7732a7ee7b8335f3fff4ac1549cVladimir Marko#include <sstream>
22776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include <vector>
23776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
24de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier#include "base/arena_allocator.h"
25761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
26de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier#include "base/scoped_arena_containers.h"
27de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier#include "base/stl_util.h"
289fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe#include "base/value_object.h"
29776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "dex_file.h"
30dc3761719fb5e2d1ced5708e3c73b965f9ef0c73Hiroshi Yamauchi#include "handle.h"
317b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers#include "instruction_flags.h"
3251c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom#include "method_reference.h"
33361e04aaa5d3eca9f978a802ee44b1213f31da58Mathieu Chartier#include "register_line.h"
34576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers#include "reg_type_cache.h"
35776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
36776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace art {
37776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
3853e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampeclass CompilerCallbacks;
398e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass Instruction;
40776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstruct ReferenceMap2Visitor;
41d0ad2eea51850ed5972c23d03380b2305cdf7cb7Mathieu Chartierclass Thread;
428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Markoclass VariableIndentationOutputStream;
43776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
44776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace verifier {
45776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
468e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass MethodVerifier;
478e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass RegisterLine;
48361e04aaa5d3eca9f978a802ee44b1213f31da58Mathieu Chartierusing RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>;
498e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass RegType;
50776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
51776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * method determines which list we search, and whether we travel up into superclasses.
54776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
55776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
56776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * All others are stored in the "virtual" list.)
57776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
58776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum MethodType {
59776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_UNKNOWN  = 0,
60776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_DIRECT,      // <init>, private
61776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_STATIC,      // static
627268d47a6d2d44dcd844fcfff4699ad7689308d6Alex Light  METHOD_VIRTUAL,     // virtual
637268d47a6d2d44dcd844fcfff4699ad7689308d6Alex Light  METHOD_SUPER,       // super
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_INTERFACE    // interface
65776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
662fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogersstd::ostream& operator<<(std::ostream& os, const MethodType& rhs);
67776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
68776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
69776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * An enumeration of problems that can turn up during verification.
70776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
71776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
72776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * that can potentially be corrected, and the verifier will try again at runtime.
73776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
74776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
75776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * to be rewritten to fail at runtime.
76776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
77776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum VerifyError {
78a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_BAD_CLASS_HARD = 1,        // VerifyError; hard error that skips compilation.
79a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_BAD_CLASS_SOFT = 2,        // VerifyError; soft error that verifies again at runtime.
800760a81257fa427646c309500d603194009265efAndreas Gampe
81a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_NO_CLASS = 4,              // NoClassDefFoundError.
82a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_NO_FIELD = 8,              // NoSuchFieldError.
83a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_NO_METHOD = 16,            // NoSuchMethodError.
84a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_ACCESS_CLASS = 32,         // IllegalAccessError.
85a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_ACCESS_FIELD = 64,         // IllegalAccessError.
86a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_ACCESS_METHOD = 128,       // IllegalAccessError.
87a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_CLASS_CHANGE = 256,        // IncompatibleClassChangeError.
88a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_INSTANTIATION = 512,       // InstantiationError.
89158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // For opcodes that don't have complete verifier support (such as lambda opcodes),
90158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // we need a way to continue execution at runtime without attempting to re-verify
91158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // (since we know it will fail no matter what). Instead, run as the interpreter
92158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // in a special "do access checks" mode which will perform verifier-like checking
93158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // on the fly.
94158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  //
95158f35c98e2ec0d40d2c032b8cdce5fb60944a7fIgor Murashkin  // TODO: Once all new opcodes have implemented full verifier support, this can be removed.
96a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_FORCE_INTERPRETER = 1024,  // Skip the verification phase at runtime;
97a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe                                          // force the interpreter to do access checks.
98a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe                                          // (sets a soft fail at compile time).
99a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe  VERIFY_ERROR_LOCKING = 2048,            // Could not guarantee balanced locking. This should be
100a727e372d8f6929cd30b983f6969c7a50fc83bb6Andreas Gampe                                          // punted to the interpreter with access checks.
101776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
102776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstd::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
104776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// We don't need to store the register data for many instructions, because we either only need
105776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// it at branch points (for verification) or GC points and branches (for verification +
106776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// type-precise register analysis).
107776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum RegisterTrackingMode {
108776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsBranches,
10902c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  kTrackCompilerInterestPoints,
110776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsAll,
111776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
112776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// A mapping from a dex pc to the register line statuses as they are immediately prior to the
1142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// execution of that instruction.
115776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass PcToRegisterLineTable {
116776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
117de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  explicit PcToRegisterLineTable(ScopedArenaAllocator& arena);
118d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  ~PcToRegisterLineTable();
119776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
120776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Initialize the RegisterTable. Every instruction address can have a different set of information
121776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // about what's in which register, but for verification purposes we only need to store it at
122776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // branch target addresses (because we merge into that).
1237b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  void Init(RegisterTrackingMode mode, InstructionFlags* flags, uint32_t insns_size,
124776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers            uint16_t registers_size, MethodVerifier* verifier);
125776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
126de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  RegisterLine* GetLine(size_t idx) const {
127de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier    return register_lines_[idx].get();
128776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
129776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
130776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers private:
131361e04aaa5d3eca9f978a802ee44b1213f31da58Mathieu Chartier  ScopedArenaVector<RegisterLineArenaUniquePtr> register_lines_;
1328e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
1338e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable);
134776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
135776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
136776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// The verifier
137776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass MethodVerifier {
138776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
139f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  enum FailureKind {
140f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kNoFailure,
141f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kSoftFailure,
142f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kHardFailure,
143f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  };
144f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao
14562e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray  static bool CanCompilerHandleVerificationFailure(uint32_t encountered_failure_types) {
14662e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray    constexpr uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
14762e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray        | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
14862e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray        | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
14962e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray        | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
15062e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray    return (encountered_failure_types & (~unresolved_mask)) == 0;
15162e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray  }
15262e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray
1537fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe  // Verify a class. Returns "kNoFailure" on success.
154ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe  static FailureKind VerifyClass(Thread* self,
155ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                 mirror::Class* klass,
15653e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                                 CompilerCallbacks* callbacks,
157ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                 bool allow_soft_failures,
1587fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe                                 LogSeverity log_level,
1597b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                                 std::string* error)
16090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
161ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe  static FailureKind VerifyClass(Thread* self,
162ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                 const DexFile* dex_file,
1635a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                 Handle<mirror::DexCache> dex_cache,
1645a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                 Handle<mirror::ClassLoader> class_loader,
1658b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                 const DexFile::ClassDef* class_def,
16653e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                                 CompilerCallbacks* callbacks,
167ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                 bool allow_soft_failures,
1687fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe                                 LogSeverity log_level,
169ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                 std::string* error)
17090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
171776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  static MethodVerifier* VerifyMethodAndDump(Thread* self,
1738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                             VariableIndentationOutputStream* vios,
1748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                             uint32_t method_idx,
1752ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             const DexFile* dex_file,
1765a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                             Handle<mirror::DexCache> dex_cache,
1775a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                             Handle<mirror::ClassLoader> class_loader,
1782ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             const DexFile::ClassDef* class_def,
179e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                             const DexFile::CodeItem* code_item, ArtMethod* method,
1802ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             uint32_t method_access_flags)
18190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
1822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
183776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint8_t EncodePcToReferenceMapData() const;
184776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
185776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t DexFileVersion() const {
186776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return dex_file_->GetVersion();
187776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
188776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
189776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache* GetRegTypeCache() {
190776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return &reg_types_;
191776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
192776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
193ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log a verification failure.
194776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostream& Fail(VerifyError error);
195776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
196ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log for verification information.
197576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers  std::ostream& LogVerifyInfo();
198776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
199ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Dump the failures encountered by the verifier.
200ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::ostream& DumpFailures(std::ostream& os);
201ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
202776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Dump the state of the verifier, namely each instruction, what flags are set on it, register
203776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // information
20490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Dump(std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_);
20590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Dump(VariableIndentationOutputStream* vios) SHARED_REQUIRES(Locks::mutator_lock_);
206776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
20708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
2082d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // to the locks held at 'dex_pc' in method 'm'.
209e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  static void FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
21046960fe5dcc1be07b39a55114338423a73554449Ian Rogers                               std::vector<uint32_t>* monitor_enter_dex_pcs)
21190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
21208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
2132d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the accessed field corresponding to the quick instruction's field
2142d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // offset at 'dex_pc' in method 'm'.
215e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  static ArtField* FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc)
21690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2172d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
2182d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the invoked method corresponding to the quick instruction's vtable
2192d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // index at 'dex_pc' in method 'm'.
220e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  static ArtMethod* FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc)
22190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2222d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
22390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Init() SHARED_REQUIRES(Locks::mutator_lock_);
2240a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes  static void Shutdown();
225776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2264993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  bool CanLoadClasses() const {
2274993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return can_load_classes_;
2284993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
2294993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
230590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  ~MethodVerifier();
23133691abbc43850fd2b2951256c4b6bbc9733ccc8Sebastien Hertz
2327b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Run verification on the method. Returns true if verification completes and false if the input
2337b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // has an irrecoverable corruption.
23490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool Verify() SHARED_REQUIRES(Locks::mutator_lock_);
2357b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
2367b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Describe VRegs at the given dex pc.
2377b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
2387b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
239bb87e0f1a52de656bc77cb01cb887e51a0e5198bMathieu Chartier  static void VisitStaticRoots(RootVisitor* visitor)
24090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
241bb87e0f1a52de656bc77cb01cb887e51a0e5198bMathieu Chartier  void VisitRoots(RootVisitor* visitor, const RootInfo& roots)
24290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
243c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier
2442b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  // Accessors used by the compiler via CompilerCallback
2452b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const DexFile::CodeItem* CodeItem() const;
2462b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  RegisterLine* GetRegLine(uint32_t dex_pc);
247de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ALWAYS_INLINE const InstructionFlags& GetInstructionFlags(size_t index) const;
248de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ALWAYS_INLINE InstructionFlags& GetInstructionFlags(size_t index);
24990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  mirror::ClassLoader* GetClassLoader() SHARED_REQUIRES(Locks::mutator_lock_);
25090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  mirror::DexCache* GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_);
251be10e8e99a78caae01fb65769218800d465144aeVladimir Marko  ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_);
2522b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  MethodReference GetMethodReference() const;
2532b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  uint32_t GetAccessFlags() const;
2542b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasCheckCasts() const;
2552b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasVirtualOrInterfaceInvokes() const;
2562b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasFailures() const;
2574824c27988c8eeb302791624bb3ce1d557b0db6cNicolas Geoffray  bool HasInstructionThatWillThrow() const {
258d12e782bcee03ecb6dec41aa9673ef53b638dceaAndreas Gampe    return have_any_pending_runtime_throw_failure_;
2594824c27988c8eeb302791624bb3ce1d557b0db6cNicolas Geoffray  }
2604824c27988c8eeb302791624bb3ce1d557b0db6cNicolas Geoffray
261d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& ResolveCheckedClass(uint32_t class_idx)
26290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2632cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  // Returns the method of a quick invoke or null if it cannot be found.
264e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
265091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier                                           bool is_range, bool allow_failure)
26690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2672cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  // Returns the access field of a quick field access (iget/iput-quick) or null
268e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  // if it cannot be found.
269c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  ArtField* GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line)
27090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
2712b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko
272e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  uint32_t GetEncounteredFailureTypes() {
273e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe    return encountered_failure_types_;
2747e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  }
2757e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle
276f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  bool IsInstanceConstructor() const {
277f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe    return IsConstructor() && !IsStatic();
278f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe  }
279f10b6e109bfb595b6752d1b59db680694ac1684dAndreas Gampe
280de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ScopedArenaAllocator& GetArena() {
281de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier    return arena_;
282de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  }
283de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier
284e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe private:
28553e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe  MethodVerifier(Thread* self,
28653e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 const DexFile* dex_file,
28753e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 Handle<mirror::DexCache> dex_cache,
28853e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 Handle<mirror::ClassLoader> class_loader,
28953e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 const DexFile::ClassDef* class_def,
29053e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 const DexFile::CodeItem* code_item,
29153e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 uint32_t method_idx,
29253e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 ArtMethod* method,
29353e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 uint32_t access_flags,
29453e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 bool can_load_classes,
29553e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 bool allow_soft_failures,
29653e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 bool need_precise_constants,
29753e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 bool verify_to_dump,
29853e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                 bool allow_thread_suspension)
29990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
3002ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
301bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  void UninstantiableError(const char* descriptor);
302bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  static bool IsInstantiableOrPrimitive(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
303bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe
304bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  // Is the method being verified a constructor? See the comment on the field.
305bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  bool IsConstructor() const {
306bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe    return is_constructor_;
307bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  }
308bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe
309bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  // Is the method verified static?
310bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  bool IsStatic() const {
311bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe    return (method_access_flags_ & kAccStatic) != 0;
312bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe  }
313bf9611f821697b14bf9e170f503c3f47613b046bAndreas Gampe
314ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the beginning of the last failure message.
315ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void PrependToLastFailMessage(std::string);
316ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
317ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the end of the last failure message.
318ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void AppendToLastFailMessage(std::string);
319776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
3209fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  // Verification result for method(s). Includes a (maximum) failure kind, and (the union of)
3219fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  // all failure types.
3229fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  struct FailureData : ValueObject {
3239fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe    FailureKind kind = kNoFailure;
3249fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe    uint32_t types = 0U;
3259fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe
3269fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe    // Merge src into this. Uses the most severe failure kind, and the union of types.
3279fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe    void Merge(const FailureData& src);
3289fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  };
3299fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe
330ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe  // Verify all direct or virtual methods of a class. The method assumes that the iterator is
331ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe  // positioned correctly, and the iterator will be updated.
332ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe  template <bool kDirect>
3339fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  static FailureData VerifyMethods(Thread* self,
3349fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   ClassLinker* linker,
3359fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   const DexFile* dex_file,
3369fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   const DexFile::ClassDef* class_def,
3379fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   ClassDataItemIterator* it,
3389fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   Handle<mirror::DexCache> dex_cache,
3399fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   Handle<mirror::ClassLoader> class_loader,
3409fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   CompilerCallbacks* callbacks,
3419fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   bool allow_soft_failures,
3427fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe                                   LogSeverity log_level,
3439fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   bool need_precise_constants,
3449fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe                                   std::string* error_string)
345ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe      SHARED_REQUIRES(Locks::mutator_lock_);
346ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe
347776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
348776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification on a single method.
349776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
350776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We do this in three passes:
351776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (1) Walk through all code units, determining instruction locations,
352776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      widths, and other characteristics.
353776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (2) Walk through all code units, performing static checks on
354776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      operands.
355776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (3) Iterate through the method, checking type safety and looking
356776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      for code flow problems.
357e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers   */
3589fcfb8add23042713165f761b4f098e14b9a823bAndreas Gampe  static FailureData VerifyMethod(Thread* self, uint32_t method_idx,
359ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  const DexFile* dex_file,
3605a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                  Handle<mirror::DexCache> dex_cache,
3615a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                  Handle<mirror::ClassLoader> class_loader,
3628b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  const DexFile::ClassDef* class_def_idx,
3632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                  const DexFile::CodeItem* code_item,
364ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  ArtMethod* method,
365ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  uint32_t method_access_flags,
36653e32d14d7a51198c6ef09120c15bafdd1d055c2Andreas Gampe                                  CompilerCallbacks* callbacks,
367ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  bool allow_soft_failures,
3687fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe                                  LogSeverity log_level,
369ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  bool need_precise_constants,
370ec6e6c19662260c059b273dfc9c502900756487dAndreas Gampe                                  std::string* hard_failure_msg)
37190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
372e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers
37390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void FindLocksAtDexPc() SHARED_REQUIRES(Locks::mutator_lock_);
37408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
375c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  ArtField* FindAccessedFieldAtDexPc(uint32_t dex_pc)
37690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
3772d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
378e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* FindInvokedMethodAtDexPc(uint32_t dex_pc)
37990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
3802d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
381848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao  SafeMap<uint32_t, std::set<uint32_t>>& FindStringInitMap()
38290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
383848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao
384776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
385776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Compute the width of the instruction at each address in the instruction stream, and store it in
386776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
387776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
388776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
389776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
390776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
391776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Performs some static checks, notably:
392776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcode of first instruction begins at index 0
393776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - only documented instructions may appear
394776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - each instruction follows the last
395776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - last byte of last instruction is at (code_length-1)
396776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
397776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Logs an error and returns "false" on failure.
398776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
399776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool ComputeWidthsAndCountOps();
400776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
401776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
402776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
403776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "branch target" flags for exception handlers.
404776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
405776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Call this after widths have been set in "insn_flags".
406776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
407776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "false" if something in the exception table looks fishy, but we're expecting the
408776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception table to be somewhat sane.
409776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
41090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool ScanTryCatchBlocks() SHARED_REQUIRES(Locks::mutator_lock_);
411776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
412776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
413776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on all instructions in a method.
414776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
415776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Walks through instructions in a method calling VerifyInstruction on each.
416776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
417776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstructions();
418776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
419776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
420776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on an instruction.
421776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
422776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * As a side effect, this sets the "branch target" flags in InsnFlags.
423776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
424776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "(CF)" items are handled during code-flow analysis.
425776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
426776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.10.1
427776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - target of each jump and branch instruction must be valid
428776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of switch statements must be valid
429776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands referencing constant pool entries must be valid
430776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
431776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of method invocation instructions must be valid
432776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) only invoke-direct can call a method starting with '<'
433776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) <clinit> must never be called explicitly
434776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands of instanceof, checkcast, new (and variants) must be valid
435776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - new-array[-type] limited to 255 dimensions
436776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - can't use "new" on an array class
437776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (?) limit dimensions in multi-array creation
438776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variable load/store register values must be in valid range
439776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
440776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
441776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - branches must be within the bounds of the code array
442776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of all control-flow instructions are the start of an instruction
443776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - register accesses fall within range of allocated registers
444776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) access to constant pool must be of appropriate type
445776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - code does not end in the middle of an instruction
446776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
447776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the "try" area must begin and
448776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   end at the start of an instruction (end can be at the end of the code)
449776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the handler must start at a valid
450776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   instruction
451776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
452776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
453776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
454776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the register index is valid for this code item. */
455776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckRegisterIndex(uint32_t idx);
456776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
457776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the wide register index is valid for this code item. */
458776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckWideRegisterIndex(uint32_t idx);
459776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
460eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  // Perform static checks on a field Get or set instruction. All we do here is ensure that the
461776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // field index is in the valid range.
462776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckFieldIndex(uint32_t idx);
463776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
464776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a method invocation instruction. All we do here is ensure that the
465776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // method index is in the valid range.
466776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckMethodIndex(uint32_t idx);
467776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
468776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
469776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // reference isn't for an array class.
470776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewInstance(uint32_t idx);
471776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
472776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the string index is in the valid range. */
473776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckStringIndex(uint32_t idx);
474776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
475776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on an instruction that takes a class constant. Ensure that the class
476776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // index is in the valid range.
477776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckTypeIndex(uint32_t idx);
478776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
479776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
480776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // creating an array of arrays that causes the number of dimensions to exceed 255.
481776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewArray(uint32_t idx);
482776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
483776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
484776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckArrayData(uint32_t cur_offset);
485776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
486776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
487776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // into an exception handler, but it's valid to do so as long as the target isn't a
488776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // "move-exception" instruction. We verify that in a later stage.
489776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The dex format forbids certain instructions from branching to themselves.
49024edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
491776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckBranchTarget(uint32_t cur_offset);
492776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
493776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify a switch table. "cur_offset" is the offset of the switch instruction.
49424edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
495776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckSwitchTargets(uint32_t cur_offset);
496776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
497776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
498776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // filled-new-array.
499776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count (0-5), args[] have values.
500776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
501776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // takes a double is done with consecutive registers. This requires parsing the target method
502776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // signature, which we will be doing later on during the code flow analysis.
503776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
504776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
505776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
506776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // or filled-new-array/range.
507776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count, vC holds index of first reg.
508776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
509776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
510776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Extract the relative offset from a branch instruction.
511776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Returns "false" on failure (e.g. this isn't a branch instruction).
512776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
513776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers                       bool* selfOkay);
514776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
515776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Perform detailed code-flow analysis on a single method. */
51690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool VerifyCodeFlow() SHARED_REQUIRES(Locks::mutator_lock_);
517776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
518776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the register types for the first instruction in the method based on the method signature.
519776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // This has the side-effect of validating the signature.
52090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool SetTypesFromSignature() SHARED_REQUIRES(Locks::mutator_lock_);
521776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
522776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
523776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform code flow on a method.
524776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
525776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
526776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction, process it (setting additional "changed" bits), and repeat until there are no
527776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * more.
528776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
529776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.1
530776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) operand stack is always the same size
531776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operand stack [registers] contain the correct types of values
532776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variables [registers] contain the correct types of values
533776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - methods are invoked with the appropriate arguments
534776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - fields are assigned using values of appropriate types
535776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcodes have the correct type values in operand registers
536776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - there is never an uninitialized class instance in a local variable in code protected by an
537776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception handler (operand stack is okay, because the operand stack is discarded when an
538776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
539776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   register typing]
540776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
541776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
542776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
543776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
544776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * (We also do many of the items described in the "static checks" sections, because it's easier to
545776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * do them here.)
546776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
547776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We need an array of RegType values, one per register, for every instruction. If the method uses
548776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * monitor-enter, we need extra data for every register, and a stack for every "interesting"
549776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction. In theory this could become quite large -- up to several megabytes for a monster
550776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * function.
551776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
552776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * NOTE:
553776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The spec forbids backward branches when there's an uninitialized reference in a register. The
554776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * idea is to prevent something like this:
555776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   loop:
556776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     move r1, r0
557776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     new-instance r0, MyClass
558776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     ...
559776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     if-eq rN, loop  // once
560776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   initialize r0
561776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
562776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This leaves us with two different instances, both allocated by the same instruction, but only
563776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
564776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * it by preventing backward branches. We achieve identical results without restricting code
565776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * reordering by specifying that you can't execute the new-instance instruction if a register
566776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * contains an uninitialized instance created by that same instruction.
567776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
56890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool CodeFlowVerifyMethod() SHARED_REQUIRES(Locks::mutator_lock_);
569776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
570776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
571776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification for a single instruction.
572776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
573776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This requires fully decoding the instruction to determine the effect it has on registers.
574776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
575776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Finds zero or more following instructions and sets the "changed" flag if execution at that
576776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
577776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * addresses. Does not set or clear any other flags in "insn_flags_".
578776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
57900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool CodeFlowVerifyInstruction(uint32_t* start_guess)
58090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
581776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
582776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of a new array instruction
5835243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
58490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
585776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
586fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao  // Helper to perform verification on puts of primitive type.
587d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
58890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier                          const uint32_t vregA) SHARED_REQUIRES(Locks::mutator_lock_);
589fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao
590776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aget instruction. The destination register's type will be set to
591776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // be that of component type of the array unless the array type is unknown, in which case a
592776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // bottom type inferred from the type of instruction is used. is_primitive is false for an
593776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // aget-object.
594d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyAGet(const Instruction* inst, const RegType& insn_type,
59590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier                  bool is_primitive) SHARED_REQUIRES(Locks::mutator_lock_);
596776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
597776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aput instruction.
598d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyAPut(const Instruction* inst, const RegType& insn_type,
59990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier                  bool is_primitive) SHARED_REQUIRES(Locks::mutator_lock_);
600776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
601776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup instance field and fail for resolution violations
602c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
60390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
604776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
605776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup static field and fail for resolution violations
60690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  ArtField* GetStaticField(int field_idx) SHARED_REQUIRES(Locks::mutator_lock_);
607776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
608896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  // Perform verification of an iget/sget/iput/sput instruction.
609896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  enum class FieldAccessType {  // private
610896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe    kAccGet,
611896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe    kAccPut
612896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  };
613896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  template <FieldAccessType kAccType>
614896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
615896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe                           bool is_primitive, bool is_static)
61690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
617776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
618896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  template <FieldAccessType kAccType>
619896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  void VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, bool is_primitive)
62090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
6212d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
622776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Resolves a class based on an index and performs access checks to ensure the referrer can
623776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // access the resolved class.
624d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
62590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
626776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
627776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
628776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
629776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * address, determine the Join of all exceptions that can land here. Fails if no matching
630776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception handler can be found or if the Join of exception types fails.
631776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
632d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& GetCaughtExceptionType()
63390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
634776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
635776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
636776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Resolves a method based on an index and performs access checks to ensure
637776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the referrer can access the resolved method.
638776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Does not throw exceptions.
639776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
6407268d47a6d2d44dcd844fcfff4699ad7689308d6Alex Light  ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
64190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
642776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
643776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
644776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify the arguments to a method. We're executing in "method", making
645776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * a call to the method reference in vB.
646776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
647776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * If this is a "direct" invoke, we allow calls to <init>. For calls to
648776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * <init>, the first argument may be an uninitialized reference. Otherwise,
649776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * calls to anything starting with '<' will be rejected, as will any
650776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * uninitialized reference arguments.
651776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
652776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For non-static method calls, this will verify that the method call is
653776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * appropriate for the "this" argument.
654776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
655776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The method reference is in vBBBB. The "is_range" parameter determines
656776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * whether we use 0-4 "args" values or a range of registers defined by
657776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * vAA and vCCCC.
658776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
659776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Widening conversions on integers and references are allowed, but
660776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * narrowing conversions are not.
661776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
6622cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier   * Returns the resolved method on success, null on failure (with *failure
663776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * set appropriately).
664776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
6657268d47a6d2d44dcd844fcfff4699ad7689308d6Alex Light  ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
66690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
667776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
66895c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // Similar checks to the above, but on the proto. Will be used when the method cannot be
66995c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // resolved.
67095c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
67195c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                            bool is_range)
67290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
67395c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
67495c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  template <class T>
675e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
67695c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                                      MethodType method_type, bool is_range,
677e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                                      ArtMethod* res_method)
67890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
67995c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
680e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range)
68190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  SHARED_REQUIRES(Locks::mutator_lock_);
6822d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
683776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
684776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify that the target instruction is not "move-exception". It's important that the only way
685776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * to execute a move-exception is as the first instruction of an exception handler.
686776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "true" if all is well, "false" if the target instruction is move-exception.
687776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
688776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
689776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
690776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
6919bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * Verify that the target instruction is not "move-result". It is important that we cannot
6929bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * branch to move-result instructions, but we have to make this a distinct check instead of
6939bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
6949bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
6959bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   */
6969bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  bool CheckNotMoveResult(const uint16_t* insns, int insn_idx);
6979bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle
6989bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  /*
6999bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * Verify that the target instruction is not "move-result" or "move-exception". This is to
7009bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * be used when checking branch and switch instructions, but not instructions that can
7019bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * continue.
7029bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   */
7039bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx);
7049bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle
7059bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  /*
706776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
707776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * next_insn, and set the changed flag on the target address if any of the registers were changed.
708ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  * In the case of fall-through, update the merge line on a change as its the working line for the
709ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  * next instruction.
710776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Returns "false" if an error is encountered.
711776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  */
712ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
71390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
714776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
715ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Return the register type for the method.
71690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const RegType& GetMethodReturnType() SHARED_REQUIRES(Locks::mutator_lock_);
717ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
718ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Get a type representing the declaring class of the method.
71990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const RegType& GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_);
720ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
7217b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  InstructionFlags* CurrentInsnFlags();
722776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
723d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& DetermineCat1Constant(int32_t value, bool precise)
72490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
7250a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes
726f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  // Try to create a register type from the given class. In case a precise type is requested, but
727f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
728f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  // non-precise reference will be returned.
729f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
730f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  //       actually touched.
731f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe  const RegType& FromClass(const char* descriptor, mirror::Class* klass, bool precise)
73290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
733f23f33de8b3e9abd16720e0f48d43d63d41dee16Andreas Gampe
7347b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  // The thread we're verifying on.
7357b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  Thread* const self_;
7367b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers
737de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  // Arena allocator.
738de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ArenaStack arena_stack_;
739de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ScopedArenaAllocator arena_;
740de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier
741776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache reg_types_;
742776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
743776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  PcToRegisterLineTable reg_table_;
744776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
745776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're currently working on.
746361e04aaa5d3eca9f978a802ee44b1213f31da58Mathieu Chartier  RegisterLineArenaUniquePtr work_line_;
747776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
748776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The address of the instruction we're currently working on, note that this is in 2 byte
749776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // quantities
750776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t work_insn_idx_;
751776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
752776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're saving for later.
753361e04aaa5d3eca9f978a802ee44b1213f31da58Mathieu Chartier  RegisterLineArenaUniquePtr saved_line_;
754776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
755637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t dex_method_idx_;  // The method we're working on.
75600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Its object representation if known.
757e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* mirror_method_ GUARDED_BY(Locks::mutator_lock_);
758637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t method_access_flags_;  // Method's access flags.
759d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType* return_type_;  // Lazily computed return type of the method.
760637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile* const dex_file_;  // The dex file containing the method.
76100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The dex_cache for the declaring class of the method.
7625a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
76300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The class loader for the declaring class of the method.
7645a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
7658b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers  const DexFile::ClassDef* const class_def_;  // The class def of the declaring class of the method.
766637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile::CodeItem* const code_item_;  // The code item containing the code for the method.
767d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType* declaring_class_;  // Lazily computed reg type of the method's declaring class.
7687b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Instruction widths and flags, one entry per code unit.
769de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  // Owned, but not unique_ptr since insn_flags_ are allocated in arenas.
770de40d478930d0889a2aea5cbf58aa63da24e5dfaMathieu Chartier  ArenaUniquePtr<InstructionFlags[]> insn_flags_;
771c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz  // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
77208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  uint32_t interesting_dex_pc_;
77308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // The container into which FindLocksAtDexPc should write the registers containing held locks,
7742cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  // null if we're not doing FindLocksAtDexPc.
77508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  std::vector<uint32_t>* monitor_enter_dex_pcs_;
77608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
777ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // The types of any error that occurs.
778ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<VerifyError> failures_;
779ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Error messages associated with failures.
780ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<std::ostringstream*> failure_messages_;
781ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Is there a pending hard failure?
782ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  bool have_pending_hard_failure_;
783faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
784faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // would fail at runtime throwing an exception. Such an instruction causes the following code
785faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
786faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // instructions that would hard fail the verification.
787d12e782bcee03ecb6dec41aa9673ef53b638dceaAndreas Gampe  // Note: this flag is reset after processing each instruction.
788faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  bool have_pending_runtime_throw_failure_;
7894d7b75f9cbcf99134c0a1c69b267b6bc8d94134eIgor Murashkin  // Is there a pending experimental failure?
7904d7b75f9cbcf99134c0a1c69b267b6bc8d94134eIgor Murashkin  bool have_pending_experimental_failure_;
791776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
792d12e782bcee03ecb6dec41aa9673ef53b638dceaAndreas Gampe  // A version of the above that is not reset and thus captures if there were *any* throw failures.
793d12e782bcee03ecb6dec41aa9673ef53b638dceaAndreas Gampe  bool have_any_pending_runtime_throw_failure_;
794d12e782bcee03ecb6dec41aa9673ef53b638dceaAndreas Gampe
795ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Info message log use primarily for verifier diagnostics.
796776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostringstream info_messages_;
797776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
798776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The number of occurrences of specific opcodes.
799776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t new_instance_count_;
800776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t monitor_enter_count_;
80180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
8020760a81257fa427646c309500d603194009265efAndreas Gampe  // Bitset of the encountered failure types. Bits are according to the values in VerifyError.
8030760a81257fa427646c309500d603194009265efAndreas Gampe  uint32_t encountered_failure_types_;
8040760a81257fa427646c309500d603194009265efAndreas Gampe
80580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  const bool can_load_classes_;
806ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao
807ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // Converts soft failures to hard failures when false. Only false when the compiler isn't
808ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // running and the verifier is called from the class linker.
809ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  const bool allow_soft_failures_;
8104d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
81146960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // An optimization where instead of generating unique RegTypes for constants we use imprecise
81246960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // constants that cover a range of constants. This isn't good enough for deoptimization that
81346960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // avoids loading from registers in the case of a constant as the dex instruction set lost the
81446960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // notion of whether a value should be in a floating point or general purpose register file.
81546960fe5dcc1be07b39a55114338423a73554449Ian Rogers  const bool need_precise_constants_;
81646960fe5dcc1be07b39a55114338423a73554449Ian Rogers
817a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one check-cast or aput-object
818a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // instruction. Aput-object operations implicitly check for array-store exceptions, similar to
819a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // check-cast.
8204d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_check_casts_;
8214d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
822a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one invoke-virtual/range
8234d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  // or invoke-interface/range.
8244d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_virtual_or_interface_invokes_;
8252ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8262ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // Indicates whether we verify to dump the info. In that case we accept quickened instructions
8272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // even though we might detect to be a compiler. Should only be set when running
8282ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // VerifyMethodAndDump.
8292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  const bool verify_to_dump_;
8308e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
8314306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
8324306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
8334306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // FindLocksAtDexPC, resulting in deadlocks.
8344306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  const bool allow_thread_suspension_;
8354306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier
836e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  // Whether the method seems to be a constructor. Note that this field exists as we can't trust
837e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
838e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  // correctly.
839e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  //
840e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  // Note: this flag is only valid once Verify() has started.
841e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe  bool is_constructor_;
842e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe
843d0ad2eea51850ed5972c23d03380b2305cdf7cb7Mathieu Chartier  // Link, for the method verifier root linked list.
844d0ad2eea51850ed5972c23d03380b2305cdf7cb7Mathieu Chartier  MethodVerifier* link_;
845d0ad2eea51850ed5972c23d03380b2305cdf7cb7Mathieu Chartier
846d0ad2eea51850ed5972c23d03380b2305cdf7cb7Mathieu Chartier  friend class art::Thread;
847848f70a3d73833fc1bf3032a9ff6812e429661d9Jeff Hao
8488e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
849776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
850e4f0b2ab4abd8e942a099e9b9b4682fbdd9eb21cjeffhaostd::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
851776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
852776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace verifier
853776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace art
854776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
855fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
856