method_verifier.h revision bb87e0f1a52de656bc77cb01cb887e51a0e5198b
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 <vector>
22776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
23761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
24776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers#include "dex_file.h"
25dc3761719fb5e2d1ced5708e3c73b965f9ef0c73Hiroshi Yamauchi#include "handle.h"
267b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers#include "instruction_flags.h"
2751c2467e8771b56e25ae4f17f66522f979f57a7eBrian Carlstrom#include "method_reference.h"
28576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers#include "reg_type_cache.h"
29776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
30776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace art {
31776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
328e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass Instruction;
33776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstruct ReferenceMap2Visitor;
34776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
35776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersnamespace verifier {
36776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
3746c6bb2f52cef82660b9be7576b49f83845df93aIan Rogersclass DexPcToReferenceMap;
388e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass MethodVerifier;
398e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass RegisterLine;
408e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogersclass RegType;
41776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
42776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
43776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
44776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * method determines which list we search, and whether we travel up into superclasses.
45776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
46776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
47776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * All others are stored in the "virtual" list.)
48776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
49776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum MethodType {
50776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_UNKNOWN  = 0,
51776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_DIRECT,      // <init>, private
52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_STATIC,      // static
53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_VIRTUAL,     // virtual, super
54776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  METHOD_INTERFACE    // interface
55776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
562fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogersstd::ostream& operator<<(std::ostream& os, const MethodType& rhs);
57776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
58776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
59776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * An enumeration of problems that can turn up during verification.
60776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
61776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
62776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * that can potentially be corrected, and the verifier will try again at runtime.
63776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
65776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * to be rewritten to fail at runtime.
66776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
67776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum VerifyError {
687934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_BAD_CLASS_HARD,  // VerifyError; hard error that skips compilation.
697934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_BAD_CLASS_SOFT,  // VerifyError; soft error that verifies again at runtime.
70776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
717934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_CLASS,        // NoClassDefFoundError.
727934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_FIELD,        // NoSuchFieldError.
737934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_NO_METHOD,       // NoSuchMethodError.
747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_CLASS,    // IllegalAccessError.
757934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_FIELD,    // IllegalAccessError.
767934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_ACCESS_METHOD,   // IllegalAccessError.
777934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_CLASS_CHANGE,    // IncompatibleClassChangeError.
787934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  VERIFY_ERROR_INSTANTIATION,   // InstantiationError.
79776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
80776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersstd::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
81776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
82776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
83776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Identifies the type of reference in the instruction that generated the verify error
84776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference).
85776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers *
86776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * This must fit in two bits.
87776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
88776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum VerifyErrorRefType {
89776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_CLASS  = 0,
90776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_FIELD  = 1,
91776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  VERIFY_ERROR_REF_METHOD = 2,
92776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
93776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersconst int kVerifyErrorRefTypeShift = 6;
94776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
95776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// We don't need to store the register data for many instructions, because we either only need
96776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// it at branch points (for verification) or GC points and branches (for verification +
97776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// type-precise register analysis).
98776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersenum RegisterTrackingMode {
99776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsBranches,
10002c42237741b5573f9d790a5a0f17f408dceb543Sameer Abu Asal  kTrackCompilerInterestPoints,
101776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  kTrackRegsAll,
102776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
103776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// A mapping from a dex pc to the register line statuses as they are immediately prior to the
1052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers// execution of that instruction.
106776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass PcToRegisterLineTable {
107776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
108d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  PcToRegisterLineTable() : size_(0) {}
109d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  ~PcToRegisterLineTable();
110776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
111776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Initialize the RegisterTable. Every instruction address can have a different set of information
112776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // about what's in which register, but for verification purposes we only need to store it at
113776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // branch target addresses (because we merge into that).
1147b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  void Init(RegisterTrackingMode mode, InstructionFlags* flags, uint32_t insns_size,
115776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers            uint16_t registers_size, MethodVerifier* verifier);
116776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
117776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegisterLine* GetLine(size_t idx) {
118d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    DCHECK_LT(idx, size_);
119d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers    return register_lines_[idx];
120776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
121776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
122776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers private:
123700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine*[]> register_lines_;
124d0fbd85a82a266c21d6b72c61d6dc098ec362de7Ian Rogers  size_t size_;
1258e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
1268e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable);
127776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
128776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
129776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers// The verifier
130776ac1fa61237db645adb4370a4aab888530caf4Ian Rogersclass MethodVerifier {
131776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers public:
132f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  enum FailureKind {
133f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kNoFailure,
134f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kSoftFailure,
135f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao    kHardFailure,
136f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  };
137f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao
138f1e6b7c8cd78c02f4eb36574f0e417c4edc2b91ejeffhao  /* Verify a class. Returns "kNoFailure" on success. */
1397b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  static FailureKind VerifyClass(Thread* self, mirror::Class* klass, bool allow_soft_failures,
1407b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers                                 std::string* error)
141b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1427b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  static FailureKind VerifyClass(Thread* self, const DexFile* dex_file,
1435a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                 Handle<mirror::DexCache> dex_cache,
1445a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                 Handle<mirror::ClassLoader> class_loader,
1458b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                 const DexFile::ClassDef* class_def,
1468b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                 bool allow_soft_failures, std::string* error)
147b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
1492ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  static MethodVerifier* VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t method_idx,
1502ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             const DexFile* dex_file,
1515a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                             Handle<mirror::DexCache> dex_cache,
1525a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                             Handle<mirror::ClassLoader> class_loader,
1532ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             const DexFile::ClassDef* class_def,
1542ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             const DexFile::CodeItem* code_item,
1555a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                             Handle<mirror::ArtMethod> method,
1562ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                             uint32_t method_access_flags)
1572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
159e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  static FailureKind VerifyMethod(mirror::ArtMethod* method, bool allow_soft_failures,
160e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier                                  std::string* error) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier
162776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint8_t EncodePcToReferenceMapData() const;
163776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
164776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t DexFileVersion() const {
165776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return dex_file_->GetVersion();
166776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
167776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
168776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache* GetRegTypeCache() {
169776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return &reg_types_;
170776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
171776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
172ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log a verification failure.
173776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostream& Fail(VerifyError error);
174776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
175ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Log for verification information.
176576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogers  std::ostream& LogVerifyInfo();
177776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
178ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Dump the failures encountered by the verifier.
179ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::ostream& DumpFailures(std::ostream& os);
180ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
181776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Dump the state of the verifier, namely each instruction, what flags are set on it, register
182776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // information
183b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
184776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
18508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
1862d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // to the locks held at 'dex_pc' in method 'm'.
187ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static void FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
18846960fe5dcc1be07b39a55114338423a73554449Ian Rogers                               std::vector<uint32_t>* monitor_enter_dex_pcs)
189b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
19008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
1912d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the accessed field corresponding to the quick instruction's field
1922d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // offset at 'dex_pc' in method 'm'.
193ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static mirror::ArtField* FindAccessedFieldAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc)
1942d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1952d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
1962d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // Returns the invoked method corresponding to the quick instruction's vtable
1972d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  // index at 'dex_pc' in method 'm'.
198ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static mirror::ArtMethod* FindInvokedMethodAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc)
1992d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2002d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
20151a5fb78d1b03b5235c2ae45414235282182bb86Sameer Abu Asal  static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2020a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes  static void Shutdown();
203776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
2044993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  bool CanLoadClasses() const {
2054993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return can_load_classes_;
2064993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
2074993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
2085a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  MethodVerifier(Thread* self, const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
2095a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                 Handle<mirror::ClassLoader> class_loader, const DexFile::ClassDef* class_def,
210bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier                 const DexFile::CodeItem* code_item, uint32_t method_idx,
2115a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                 Handle<mirror::ArtMethod> method,
21246960fe5dcc1be07b39a55114338423a73554449Ian Rogers                 uint32_t access_flags, bool can_load_classes, bool allow_soft_failures,
2134306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier                 bool need_precise_constants, bool allow_thread_suspension)
2144306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier          SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
2152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      : MethodVerifier(self, dex_file, dex_cache, class_loader, class_def, code_item, method_idx,
2162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                       method, access_flags, can_load_classes, allow_soft_failures,
2174306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier                       need_precise_constants, false, allow_thread_suspension) {}
218ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
219590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  ~MethodVerifier();
22033691abbc43850fd2b2951256c4b6bbc9733ccc8Sebastien Hertz
2217b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Run verification on the method. Returns true if verification completes and false if the input
2227b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // has an irrecoverable corruption.
2237b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2247b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
2257b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Describe VRegs at the given dex pc.
2267b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
2277b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers
228bb87e0f1a52de656bc77cb01cb887e51a0e5198bMathieu Chartier  static void VisitStaticRoots(RootVisitor* visitor)
2297c438b19b71932ac8a44eff44f20744a01559c8dMathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230bb87e0f1a52de656bc77cb01cb887e51a0e5198bMathieu Chartier  void VisitRoots(RootVisitor* visitor, const RootInfo& roots)
23112d625f87bcd6c4059a205bb39007a255f57f382Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
232c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier
2332b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  // Accessors used by the compiler via CompilerCallback
2342b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const DexFile::CodeItem* CodeItem() const;
2352b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  RegisterLine* GetRegLine(uint32_t dex_pc);
2362b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  const InstructionFlags& GetInstructionFlags(size_t index) const;
2372b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2382b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2392b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  MethodReference GetMethodReference() const;
2402b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  uint32_t GetAccessFlags() const;
2412b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasCheckCasts() const;
2422b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasVirtualOrInterfaceInvokes() const;
2432b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko  bool HasFailures() const;
244d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& ResolveCheckedClass(uint32_t class_idx)
2452b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
246e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  // Returns the method of a quick invoke or nullptr if it cannot be found.
247e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  mirror::ArtMethod* GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
248091d238936809f6668ca6b7606c62bc224add430Mathieu Chartier                                           bool is_range, bool allow_failure)
249e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
250e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  // Returns the access field of a quick field access (iget/iput-quick) or nullptr
251e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  // if it cannot be found.
252e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  mirror::ArtField* GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line)
253e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2542b5eaa2b49f7489bafdadc4b4463ae27e4261817Vladimir Marko
2557e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  // Is the method being verified a constructor?
2567e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  bool IsConstructor() const {
2577e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle    return (method_access_flags_ & kAccConstructor) != 0;
2587e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  }
2597e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle
2607e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  // Is the method verified static?
2617e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  bool IsStatic() const {
2627e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle    return (method_access_flags_ & kAccStatic) != 0;
2637e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle  }
2647e541c91997b7747fa79014a8ea540395e54efc8Stephen Kyle
2657b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers private:
2662ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // Private constructor for dumping.
2675a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  MethodVerifier(Thread* self, const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
2685a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                 Handle<mirror::ClassLoader> class_loader, const DexFile::ClassDef* class_def,
2692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                 const DexFile::CodeItem* code_item, uint32_t method_idx,
2705a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                 Handle<mirror::ArtMethod> method, uint32_t access_flags,
2712ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                 bool can_load_classes, bool allow_soft_failures, bool need_precise_constants,
2724306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier                 bool verify_to_dump, bool allow_thread_suspension)
2732ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
2742ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
275ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the beginning of the last failure message.
276ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void PrependToLastFailMessage(std::string);
277ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
278ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Adds the given string to the end of the last failure message.
279ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  void AppendToLastFailMessage(std::string);
280776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
281776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
282776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification on a single method.
283776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
284776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We do this in three passes:
285776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (1) Walk through all code units, determining instruction locations,
286776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      widths, and other characteristics.
287776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (2) Walk through all code units, performing static checks on
288776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      operands.
289776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *  (3) Iterate through the method, checking type safety and looking
290776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *      for code flow problems.
291e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers   */
2927b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  static FailureKind VerifyMethod(Thread* self, uint32_t method_idx, const DexFile* dex_file,
2935a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                  Handle<mirror::DexCache> dex_cache,
2945a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                  Handle<mirror::ClassLoader> class_loader,
2958b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                                  const DexFile::ClassDef* class_def_idx,
2962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                  const DexFile::CodeItem* code_item,
2975a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe                                  Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
29846960fe5dcc1be07b39a55114338423a73554449Ian Rogers                                  bool allow_soft_failures, bool need_precise_constants)
2992ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
300e1758feb293c7ff67d6fe59dbc31af0811863ce5Ian Rogers
301b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
30208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
303ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* FindAccessedFieldAtDexPc(uint32_t dex_pc)
304c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3052d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
306ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* FindInvokedMethodAtDexPc(uint32_t dex_pc)
307c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3082d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
309776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
310776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Compute the width of the instruction at each address in the instruction stream, and store it in
311776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
312776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
313776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
314776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
315776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
316776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Performs some static checks, notably:
317776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcode of first instruction begins at index 0
318776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - only documented instructions may appear
319776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - each instruction follows the last
320776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - last byte of last instruction is at (code_length-1)
321776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
322776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Logs an error and returns "false" on failure.
323776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
324776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool ComputeWidthsAndCountOps();
325776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
326776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
327776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
328776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "branch target" flags for exception handlers.
329776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
330776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Call this after widths have been set in "insn_flags".
331776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
332776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "false" if something in the exception table looks fishy, but we're expecting the
333776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception table to be somewhat sane.
334776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
335b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
336776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
337776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
338776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on all instructions in a method.
339776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
340776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Walks through instructions in a method calling VerifyInstruction on each.
341776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
342776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstructions();
343776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
344776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
345776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform static verification on an instruction.
346776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
347776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * As a side effect, this sets the "branch target" flags in InsnFlags.
348776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
349776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * "(CF)" items are handled during code-flow analysis.
350776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
351776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.10.1
352776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - target of each jump and branch instruction must be valid
353776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of switch statements must be valid
354776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands referencing constant pool entries must be valid
355776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
356776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) operands of method invocation instructions must be valid
357776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) only invoke-direct can call a method starting with '<'
358776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (CF) <clinit> must never be called explicitly
359776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operands of instanceof, checkcast, new (and variants) must be valid
360776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - new-array[-type] limited to 255 dimensions
361776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - can't use "new" on an array class
362776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (?) limit dimensions in multi-array creation
363776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variable load/store register values must be in valid range
364776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
365776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
366776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - branches must be within the bounds of the code array
367776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - targets of all control-flow instructions are the start of an instruction
368776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - register accesses fall within range of allocated registers
369776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) access to constant pool must be of appropriate type
370776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - code does not end in the middle of an instruction
371776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
372776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the "try" area must begin and
373776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   end at the start of an instruction (end can be at the end of the code)
374776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (earlier) for each exception handler, the handler must start at a valid
375776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   instruction
376776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
377776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
378776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
379776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the register index is valid for this code item. */
380776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckRegisterIndex(uint32_t idx);
381776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
382776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the wide register index is valid for this code item. */
383776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckWideRegisterIndex(uint32_t idx);
384776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
385eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  // Perform static checks on a field Get or set instruction. All we do here is ensure that the
386776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // field index is in the valid range.
387776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckFieldIndex(uint32_t idx);
388776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
389776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a method invocation instruction. All we do here is ensure that the
390776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // method index is in the valid range.
391776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckMethodIndex(uint32_t idx);
392776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
393776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
394776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // reference isn't for an array class.
395776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewInstance(uint32_t idx);
396776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
397776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Ensure that the string index is in the valid range. */
398776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckStringIndex(uint32_t idx);
399776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
400776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on an instruction that takes a class constant. Ensure that the class
401776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // index is in the valid range.
402776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckTypeIndex(uint32_t idx);
403776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
404776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
405776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // creating an array of arrays that causes the number of dimensions to exceed 255.
406776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNewArray(uint32_t idx);
407776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
408776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
409776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckArrayData(uint32_t cur_offset);
410776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
411776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
412776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // into an exception handler, but it's valid to do so as long as the target isn't a
413776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // "move-exception" instruction. We verify that in a later stage.
414776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The dex format forbids certain instructions from branching to themselves.
41524edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
416776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckBranchTarget(uint32_t cur_offset);
417776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
418776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Verify a switch table. "cur_offset" is the offset of the switch instruction.
41924edeb5f13c2fe67344cab01cb248b8f94e6faf9Elliott Hughes  // Updates "insn_flags_", setting the "branch target" flag.
420776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckSwitchTargets(uint32_t cur_offset);
421776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
422776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
423776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // filled-new-array.
424776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count (0-5), args[] have values.
425776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
426776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // takes a double is done with consecutive registers. This requires parsing the target method
427776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // signature, which we will be doing later on during the code flow analysis.
428776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
429776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
430776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
431776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // or filled-new-array/range.
432776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // - vA holds word count, vC holds index of first reg.
433776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
434776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
435776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Extract the relative offset from a branch instruction.
436776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Returns "false" on failure (e.g. this isn't a branch instruction).
437776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
438776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers                       bool* selfOkay);
439776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
440776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /* Perform detailed code-flow analysis on a single method. */
441b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
442776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
443776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Set the register types for the first instruction in the method based on the method signature.
444776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // This has the side-effect of validating the signature.
445b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
446776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
447776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
448776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform code flow on a method.
449776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
450776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
451776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction, process it (setting additional "changed" bits), and repeat until there are no
452776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * more.
453776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
454776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.1
455776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - (N/A) operand stack is always the same size
456776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - operand stack [registers] contain the correct types of values
457776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - local variables [registers] contain the correct types of values
458776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - methods are invoked with the appropriate arguments
459776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - fields are assigned using values of appropriate types
460776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - opcodes have the correct type values in operand registers
461776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - there is never an uninitialized class instance in a local variable in code protected by an
462776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception handler (operand stack is okay, because the operand stack is discarded when an
463776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
464776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   register typing]
465776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
466776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * v3 4.11.1.2
467776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * - execution cannot fall off the end of the code
468776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
469776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * (We also do many of the items described in the "static checks" sections, because it's easier to
470776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * do them here.)
471776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
472776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * We need an array of RegType values, one per register, for every instruction. If the method uses
473776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * monitor-enter, we need extra data for every register, and a stack for every "interesting"
474776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * instruction. In theory this could become quite large -- up to several megabytes for a monster
475776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * function.
476776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
477776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * NOTE:
478776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The spec forbids backward branches when there's an uninitialized reference in a register. The
479776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * idea is to prevent something like this:
480776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   loop:
481776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     move r1, r0
482776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     new-instance r0, MyClass
483776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     ...
484776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *     if-eq rN, loop  // once
485776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *   initialize r0
486776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
487776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This leaves us with two different instances, both allocated by the same instruction, but only
488776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
489776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * it by preventing backward branches. We achieve identical results without restricting code
490776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * reordering by specifying that you can't execute the new-instance instruction if a register
491776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * contains an uninitialized instance created by that same instruction.
492776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
493b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
494776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
495776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
496776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Perform verification for a single instruction.
497776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
498776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * This requires fully decoding the instruction to determine the effect it has on registers.
499776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
500776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Finds zero or more following instructions and sets the "changed" flag if execution at that
501776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
502776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * addresses. Does not set or clear any other flags in "insn_flags_".
503776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
50400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool CodeFlowVerifyInstruction(uint32_t* start_guess)
505b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
506776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
507776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of a new array instruction
5085243e912875026f99428088db7e80cb11651d64eSebastien Hertz  void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
509b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
510776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
511fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao  // Helper to perform verification on puts of primitive type.
512d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
513fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao                          const uint32_t vregA) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
514fe1f7c84369abbf5a0121557aa0c6c58e9477710Jeff Hao
515776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aget instruction. The destination register's type will be set to
516776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // be that of component type of the array unless the array type is unknown, in which case a
517776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // bottom type inferred from the type of instruction is used. is_primitive is false for an
518776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // aget-object.
519d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyAGet(const Instruction* inst, const RegType& insn_type,
520b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                  bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
521776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
522776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Perform verification of an aput instruction.
523d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  void VerifyAPut(const Instruction* inst, const RegType& insn_type,
524b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                  bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
525776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
526776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup instance field and fail for resolution violations
527d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  mirror::ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
528b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
529776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
530776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Lookup static field and fail for resolution violations
531ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
532776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
533896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  // Perform verification of an iget/sget/iput/sput instruction.
534896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  enum class FieldAccessType {  // private
535896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe    kAccGet,
536896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe    kAccPut
537896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  };
538896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  template <FieldAccessType kAccType>
539896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
540896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe                           bool is_primitive, bool is_static)
541b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
542776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
543896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  template <FieldAccessType kAccType>
544896df40bbb20f4a1c468e87313b510c082016dd3Andreas Gampe  void VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, bool is_primitive)
5452d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
5462d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
547776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Resolves a class based on an index and performs access checks to ensure the referrer can
548776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // access the resolved class.
549d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
550b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
551776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
552776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
553776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
554776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * address, determine the Join of all exceptions that can land here. Fails if no matching
555776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * exception handler can be found or if the Join of exception types fails.
556776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
557d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& GetCaughtExceptionType()
558b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
559776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
560776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
561776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Resolves a method based on an index and performs access checks to ensure
562776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * the referrer can access the resolved method.
563776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Does not throw exceptions.
564776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
565ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
566b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
567776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
568776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
569776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify the arguments to a method. We're executing in "method", making
570776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * a call to the method reference in vB.
571776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
572776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * If this is a "direct" invoke, we allow calls to <init>. For calls to
573776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * <init>, the first argument may be an uninitialized reference. Otherwise,
574776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * calls to anything starting with '<' will be rejected, as will any
575776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * uninitialized reference arguments.
576776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
577776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * For non-static method calls, this will verify that the method call is
578776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * appropriate for the "this" argument.
579776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
580776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * The method reference is in vBBBB. The "is_range" parameter determines
581776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * whether we use 0-4 "args" values or a range of registers defined by
582776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * vAA and vCCCC.
583776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
584776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Widening conversions on integers and references are allowed, but
585776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * narrowing conversions are not.
586776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   *
5872ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe   * Returns the resolved method on success, nullptr on failure (with *failure
588776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * set appropriately).
589776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
590ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* VerifyInvocationArgs(const Instruction* inst,
591ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                          MethodType method_type,
592ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                                          bool is_range, bool is_super)
593b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
594776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
59595c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // Similar checks to the above, but on the proto. Will be used when the method cannot be
59695c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  // resolved.
59795c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
59895c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                            bool is_range)
59995c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
60095c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
60195c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  template <class T>
60295c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe  mirror::ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
60395c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                                      MethodType method_type, bool is_range,
60495c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe                                                      mirror::ArtMethod* res_method)
60595c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
60695c0bf8fb5847cff263639f889d04c7c3c26eeddAndreas Gampe
607ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range)
6082d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz  SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
6092d6ba5158d7fd459db2870df47300b517dc4d08cSebastien Hertz
610776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
611776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Verify that the target instruction is not "move-exception". It's important that the only way
612776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * to execute a move-exception is as the first instruction of an exception handler.
613776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   * Returns "true" if all is well, "false" if the target instruction is move-exception.
614776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers   */
615776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
616776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
617776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  /*
6189bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * Verify that the target instruction is not "move-result". It is important that we cannot
6199bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * branch to move-result instructions, but we have to make this a distinct check instead of
6209bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
6219bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
6229bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   */
6239bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  bool CheckNotMoveResult(const uint16_t* insns, int insn_idx);
6249bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle
6259bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  /*
6269bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * Verify that the target instruction is not "move-result" or "move-exception". This is to
6279bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * be used when checking branch and switch instructions, but not instructions that can
6289bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   * continue.
6299bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle   */
6309bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx);
6319bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle
6329bc6199a9a6e140102951f6f38845b43b561af83Stephen Kyle  /*
633776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
634776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * next_insn, and set the changed flag on the target address if any of the registers were changed.
635ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  * In the case of fall-through, update the merge line on a change as its the working line for the
636ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  * next instruction.
637776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  * Returns "false" if an error is encountered.
638776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  */
639ebbdd87cbb57e45da341fbf7325406e982810c10Ian Rogers  bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
640b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
641776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
642ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Return the register type for the method.
643d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
644ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
645ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Get a type representing the declaring class of the method.
646d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
647ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers
6487b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  InstructionFlags* CurrentInsnFlags();
649776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
650d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType& DetermineCat1Constant(int32_t value, bool precise)
651849600bb5cfc02bf5ab4aa9a810667ebd3b53328Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
6520a1038b0a30a52dff1a449a989825e808a83df80Elliott Hughes
6537b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  // The thread we're verifying on.
6547b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers  Thread* const self_;
6557b078e8c04f3e1451dbdd18543c8b9692b5b067eIan Rogers
656776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  RegTypeCache reg_types_;
657776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
658776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  PcToRegisterLineTable reg_table_;
659776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
660776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're currently working on.
661700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine> work_line_;
662776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
663776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The address of the instruction we're currently working on, note that this is in 2 byte
664776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // quantities
665776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  uint32_t work_insn_idx_;
666776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
667776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // Storage for the register status we're saving for later.
668700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<RegisterLine> saved_line_;
669776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
670637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t dex_method_idx_;  // The method we're working on.
67100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Its object representation if known.
6725a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  Handle<mirror::ArtMethod> mirror_method_ GUARDED_BY(Locks::mutator_lock_);
673637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const uint32_t method_access_flags_;  // Method's access flags.
674d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType* return_type_;  // Lazily computed return type of the method.
675637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile* const dex_file_;  // The dex file containing the method.
67600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The dex_cache for the declaring class of the method.
6775a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
67800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // The class loader for the declaring class of the method.
6795a4b8a236030460651a3136397d23ca6744e7eb7Andreas Gampe  Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
6808b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers  const DexFile::ClassDef* const class_def_;  // The class def of the declaring class of the method.
681637c65b1e431fd90195b71c141b3590bd81cc91aIan Rogers  const DexFile::CodeItem* const code_item_;  // The code item containing the code for the method.
682d8f69b086baf6717ce949d1c4de90d73b91083b0Ian Rogers  const RegType* declaring_class_;  // Lazily computed reg type of the method's declaring class.
6837b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers  // Instruction widths and flags, one entry per code unit.
684700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<InstructionFlags[]> insn_flags_;
685c15853b8b5da9dd8fb28e2adbbd61af34555e4c2Sebastien Hertz  // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
68608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  uint32_t interesting_dex_pc_;
68708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // The container into which FindLocksAtDexPc should write the registers containing held locks,
6882ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // nullptr if we're not doing FindLocksAtDexPc.
68908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  std::vector<uint32_t>* monitor_enter_dex_pcs_;
69008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
691ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // The types of any error that occurs.
692ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<VerifyError> failures_;
693ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Error messages associated with failures.
694ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  std::vector<std::ostringstream*> failure_messages_;
695ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Is there a pending hard failure?
696ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  bool have_pending_hard_failure_;
697faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
698faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // would fail at runtime throwing an exception. Such an instruction causes the following code
699faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
700faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  // instructions that would hard fail the verification.
701faf459e5decdfcf6dd7844947898beefe31e6435jeffhao  bool have_pending_runtime_throw_failure_;
702776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
703ad0b3a35857d2cd2db720028ebc51176191e2219Ian Rogers  // Info message log use primarily for verifier diagnostics.
704776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  std::ostringstream info_messages_;
705776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
706776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  // The number of occurrences of specific opcodes.
707776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t new_instance_count_;
708776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  size_t monitor_enter_count_;
70980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
71080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  const bool can_load_classes_;
711ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao
712ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // Converts soft failures to hard failures when false. Only false when the compiler isn't
713ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  // running and the verifier is called from the class linker.
714ee9889502a34a08741a6f8ecc02917202de9d773Jeff Hao  const bool allow_soft_failures_;
7154d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
71646960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // An optimization where instead of generating unique RegTypes for constants we use imprecise
71746960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // constants that cover a range of constants. This isn't good enough for deoptimization that
71846960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // avoids loading from registers in the case of a constant as the dex instruction set lost the
71946960fe5dcc1be07b39a55114338423a73554449Ian Rogers  // notion of whether a value should be in a floating point or general purpose register file.
72046960fe5dcc1be07b39a55114338423a73554449Ian Rogers  const bool need_precise_constants_;
72146960fe5dcc1be07b39a55114338423a73554449Ian Rogers
722a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one check-cast or aput-object
723a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // instruction. Aput-object operations implicitly check for array-store exceptions, similar to
724a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // check-cast.
7254d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_check_casts_;
7264d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz
727a9a8254c920ce8e22210abfc16c9842ce0aea28fIan Rogers  // Indicates the method being verified contains at least one invoke-virtual/range
7284d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  // or invoke-interface/range.
7294d4adb1dae07bb7421e863732ab789413a3b43f0Sebastien Hertz  bool has_virtual_or_interface_invokes_;
7302ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
7312ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // Indicates whether we verify to dump the info. In that case we accept quickened instructions
7322ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // even though we might detect to be a compiler. Should only be set when running
7332ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  // VerifyMethodAndDump.
7342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  const bool verify_to_dump_;
7358e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers
7364306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
7374306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
7384306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  // FindLocksAtDexPC, resulting in deadlocks.
7394306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier  const bool allow_thread_suspension_;
7404306ef8a7ec8e3887e51f64e80d940d974cc3ac3Mathieu Chartier
7418e1f4f8f848f2dbb36265a019310498a61cd674dIan Rogers  DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
742776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers};
743e4f0b2ab4abd8e942a099e9b9b4682fbdd9eb21cjeffhaostd::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
744776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
745776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace verifier
746776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}  // namespace art
747776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
748fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
749