Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ |
| 18 | #define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 21 | #include <sstream> |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 26 | #include "base/arena_allocator.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 28 | #include "base/scoped_arena_containers.h" |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 29 | #include "base/value_object.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 30 | #include "dex/code_item_accessors.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 31 | #include "dex/dex_file_types.h" |
David Sehr | 312f3b2 | 2018-03-19 08:39:26 -0700 | [diff] [blame] | 32 | #include "dex/method_reference.h" |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 33 | #include "handle.h" |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 34 | #include "instruction_flags.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 35 | #include "reg_type_cache.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 36 | #include "register_line.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 37 | #include "verifier_enums.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 41 | class ClassLinker; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 42 | class DexFile; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 43 | class Instruction; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 44 | struct ReferenceMap2Visitor; |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 45 | class Thread; |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 46 | class VariableIndentationOutputStream; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 47 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 48 | namespace dex { |
| 49 | struct ClassDef; |
| 50 | struct CodeItem; |
| 51 | } // namespace dex |
| 52 | |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 53 | namespace mirror { |
| 54 | class DexCache; |
| 55 | } // namespace mirror |
| 56 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 57 | namespace verifier { |
| 58 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 59 | class MethodVerifier; |
| 60 | class RegisterLine; |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 61 | using RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 62 | class RegType; |
Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 63 | struct ScopedNewLine; |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 64 | class VerifierDeps; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 65 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 66 | // A mapping from a dex pc to the register line statuses as they are immediately prior to the |
| 67 | // execution of that instruction. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 68 | class PcToRegisterLineTable { |
| 69 | public: |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 70 | explicit PcToRegisterLineTable(ScopedArenaAllocator& allocator); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 71 | ~PcToRegisterLineTable(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 72 | |
| 73 | // Initialize the RegisterTable. Every instruction address can have a different set of information |
| 74 | // about what's in which register, but for verification purposes we only need to store it at |
| 75 | // branch target addresses (because we merge into that). |
Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 76 | void Init(InstructionFlags* flags, |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 77 | uint32_t insns_size, |
| 78 | uint16_t registers_size, |
| 79 | ScopedArenaAllocator& allocator, |
Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 80 | RegTypeCache* reg_types, |
| 81 | uint32_t interesting_dex_pc); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 82 | |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 83 | bool IsInitialized() const { |
| 84 | return !register_lines_.empty(); |
| 85 | } |
| 86 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 87 | RegisterLine* GetLine(size_t idx) const { |
| 88 | return register_lines_[idx].get(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 92 | ScopedArenaVector<RegisterLineArenaUniquePtr> register_lines_; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // The verifier |
| 98 | class MethodVerifier { |
| 99 | public: |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 100 | static MethodVerifier* VerifyMethodAndDump(Thread* self, |
| 101 | VariableIndentationOutputStream* vios, |
| 102 | uint32_t method_idx, |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 103 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 104 | Handle<mirror::DexCache> dex_cache, |
| 105 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 106 | const dex::ClassDef& class_def, |
Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 107 | const dex::CodeItem* code_item, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 108 | uint32_t method_access_flags, |
| 109 | uint32_t api_level) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 110 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 111 | |
Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 112 | // Calculates the type information at the given `dex_pc`. |
| 113 | // No classes will be loaded. |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 114 | static MethodVerifier* CalculateVerificationInfo(Thread* self, |
| 115 | ArtMethod* method, |
Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 116 | uint32_t dex_pc) |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 117 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 118 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 119 | const DexFile& GetDexFile() const { |
| 120 | DCHECK(dex_file_ != nullptr); |
| 121 | return *dex_file_; |
| 122 | } |
| 123 | |
Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 124 | const dex::ClassDef& GetClassDef() const { |
| 125 | return class_def_; |
| 126 | } |
| 127 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 128 | RegTypeCache* GetRegTypeCache() { |
| 129 | return ®_types_; |
| 130 | } |
| 131 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 132 | // Log a verification failure. |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 133 | std::ostream& Fail(VerifyError error, bool pending_exc = true); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 134 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 135 | // Log for verification information. |
Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 136 | ScopedNewLine LogVerifyInfo(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 137 | |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 138 | // Information structure for a lock held at a certain point in time. |
| 139 | struct DexLockInfo { |
| 140 | // The registers aliasing the lock. |
| 141 | std::set<uint32_t> dex_registers; |
| 142 | // The dex PC of the monitor-enter instruction. |
| 143 | uint32_t dex_pc; |
| 144 | |
| 145 | explicit DexLockInfo(uint32_t dex_pc_in) { |
| 146 | dex_pc = dex_pc_in; |
| 147 | } |
| 148 | }; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 149 | // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 150 | // to the locks held at 'dex_pc' in method 'm'. |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 151 | // Note: this is the only situation where the verifier will visit quickened instructions. |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 152 | static void FindLocksAtDexPc(ArtMethod* m, |
| 153 | uint32_t dex_pc, |
| 154 | std::vector<DexLockInfo>* monitor_enter_dex_pcs, |
| 155 | uint32_t api_level) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 156 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 157 | |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 158 | static void Init(ClassLinker* class_linker) REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 159 | static void Shutdown(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 160 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 161 | virtual ~MethodVerifier(); |
Sebastien Hertz | 33691ab | 2013-08-02 14:19:57 +0200 | [diff] [blame] | 162 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 163 | static void VisitStaticRoots(RootVisitor* visitor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 164 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 165 | void VisitRoots(RootVisitor* visitor, const RootInfo& roots) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 167 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 168 | const CodeItemDataAccessor& CodeItem() const { |
| 169 | return code_item_accessor_; |
| 170 | } |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 171 | RegisterLine* GetRegLine(uint32_t dex_pc); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 172 | ALWAYS_INLINE const InstructionFlags& GetInstructionFlags(size_t index) const; |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 173 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 174 | MethodReference GetMethodReference() const; |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 175 | bool HasFailures() const; |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 176 | bool HasInstructionThatWillThrow() const { |
Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 177 | return (encountered_failure_types_ & VERIFY_ERROR_RUNTIME_THROW) != 0; |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 180 | virtual const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) |
| 181 | REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 182 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 183 | uint32_t GetEncounteredFailureTypes() const { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 184 | return encountered_failure_types_; |
| 185 | } |
| 186 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 187 | ClassLinker* GetClassLinker() const { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 188 | return class_linker_; |
| 189 | } |
| 190 | |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 191 | bool IsAotMode() const { |
| 192 | return flags_.aot_mode_; |
| 193 | } |
| 194 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 195 | VerifierDeps* GetVerifierDeps() const { |
| 196 | return verifier_deps_; |
| 197 | } |
| 198 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 199 | protected: |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 200 | MethodVerifier(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 201 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 202 | ArenaPool* arena_pool, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 203 | VerifierDeps* verifier_deps, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 204 | const DexFile* dex_file, |
Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 205 | const dex::ClassDef& class_def, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 206 | const dex::CodeItem* code_item, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 207 | uint32_t dex_method_idx, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 208 | bool can_load_classes, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 209 | bool allow_thread_suspension, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 210 | bool aot_mode) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 211 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 212 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 213 | // Verification result for method(s). Includes a (maximum) failure kind, and (the union of) |
| 214 | // all failure types. |
| 215 | struct FailureData : ValueObject { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 216 | FailureKind kind = FailureKind::kNoFailure; |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 217 | uint32_t types = 0U; |
| 218 | |
| 219 | // Merge src into this. Uses the most severe failure kind, and the union of types. |
| 220 | void Merge(const FailureData& src); |
| 221 | }; |
| 222 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 223 | /* |
| 224 | * Perform verification on a single method. |
| 225 | * |
| 226 | * We do this in three passes: |
| 227 | * (1) Walk through all code units, determining instruction locations, |
| 228 | * widths, and other characteristics. |
| 229 | * (2) Walk through all code units, performing static checks on |
| 230 | * operands. |
| 231 | * (3) Iterate through the method, checking type safety and looking |
| 232 | * for code flow problems. |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 233 | */ |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 234 | static FailureData VerifyMethod(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 235 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 236 | ArenaPool* arena_pool, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 237 | VerifierDeps* verifier_deps, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 238 | uint32_t method_idx, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 239 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 240 | Handle<mirror::DexCache> dex_cache, |
| 241 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 242 | const dex::ClassDef& class_def_idx, |
| 243 | const dex::CodeItem* code_item, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 244 | uint32_t method_access_flags, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 245 | HardFailLogMode log_level, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 246 | uint32_t api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 247 | bool aot_mode, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 248 | std::string* hard_failure_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 249 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 250 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 251 | template <bool kVerifierDebug> |
| 252 | static FailureData VerifyMethod(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 253 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 254 | ArenaPool* arena_pool, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 255 | VerifierDeps* verifier_deps, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 256 | uint32_t method_idx, |
| 257 | const DexFile* dex_file, |
| 258 | Handle<mirror::DexCache> dex_cache, |
| 259 | Handle<mirror::ClassLoader> class_loader, |
| 260 | const dex::ClassDef& class_def_idx, |
| 261 | const dex::CodeItem* code_item, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 262 | uint32_t method_access_flags, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 263 | HardFailLogMode log_level, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 264 | uint32_t api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 265 | bool aot_mode, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 266 | std::string* hard_failure_msg) |
| 267 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 268 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 269 | // For VerifierDepsTest. TODO: Refactor. |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 270 | |
| 271 | // Run verification on the method. Returns true if verification completes and false if the input |
| 272 | // has an irrecoverable corruption. |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 273 | virtual bool Verify() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 274 | static MethodVerifier* CreateVerifier(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 275 | VerifierDeps* verifier_deps, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 276 | const DexFile* dex_file, |
| 277 | Handle<mirror::DexCache> dex_cache, |
| 278 | Handle<mirror::ClassLoader> class_loader, |
| 279 | const dex::ClassDef& class_def, |
| 280 | const dex::CodeItem* code_item, |
| 281 | uint32_t method_idx, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 282 | uint32_t access_flags, |
| 283 | bool can_load_classes, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 284 | bool verify_to_dump, |
| 285 | bool allow_thread_suspension, |
| 286 | uint32_t api_level) |
| 287 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 288 | |
Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 289 | virtual bool PotentiallyMarkRuntimeThrow() = 0; |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 290 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 291 | // The thread we're verifying on. |
| 292 | Thread* const self_; |
| 293 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 294 | // Arena allocator. |
| 295 | ArenaStack arena_stack_; |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 296 | ScopedArenaAllocator allocator_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 297 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 298 | RegTypeCache reg_types_; |
| 299 | |
| 300 | PcToRegisterLineTable reg_table_; |
| 301 | |
| 302 | // Storage for the register status we're currently working on. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 303 | RegisterLineArenaUniquePtr work_line_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 304 | |
| 305 | // The address of the instruction we're currently working on, note that this is in 2 byte |
| 306 | // quantities |
| 307 | uint32_t work_insn_idx_; |
| 308 | |
| 309 | // Storage for the register status we're saving for later. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 310 | RegisterLineArenaUniquePtr saved_line_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 311 | |
Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 312 | const uint32_t dex_method_idx_; // The method we're working on. |
| 313 | const DexFile* const dex_file_; // The dex file containing the method. |
| 314 | const dex::ClassDef& class_def_; // The class being verified. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 315 | const CodeItemDataAccessor code_item_accessor_; |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 316 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 317 | // Instruction widths and flags, one entry per code unit. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 318 | // Owned, but not unique_ptr since insn_flags_ are allocated in arenas. |
| 319 | ArenaUniquePtr<InstructionFlags[]> insn_flags_; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 320 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 321 | // The types of any error that occurs. |
| 322 | std::vector<VerifyError> failures_; |
| 323 | // Error messages associated with failures. |
| 324 | std::vector<std::ostringstream*> failure_messages_; |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 325 | struct { |
| 326 | // Is there a pending hard failure? |
| 327 | bool have_pending_hard_failure_ : 1; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 328 | |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 329 | // Is there a pending runtime throw failure? A runtime throw failure is when an instruction |
| 330 | // would fail at runtime throwing an exception. Such an instruction causes the following code |
| 331 | // to be unreachable. This is set by Fail and used to ensure we don't process unreachable |
| 332 | // instructions that would hard fail the verification. |
| 333 | // Note: this flag is reset after processing each instruction. |
| 334 | bool have_pending_runtime_throw_failure_ : 1; |
| 335 | |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 336 | // Verify in AoT mode? |
| 337 | bool aot_mode_ : 1; |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 338 | } flags_; |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 339 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 340 | // Info message log use primarily for verifier diagnostics. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 341 | std::ostringstream info_messages_; |
| 342 | |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 343 | // Bitset of the encountered failure types. Bits are according to the values in VerifyError. |
| 344 | uint32_t encountered_failure_types_; |
| 345 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 346 | const bool can_load_classes_; |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 347 | |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 348 | // Classlinker to use when resolving. |
| 349 | ClassLinker* class_linker_; |
| 350 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 351 | // The verifier deps object we are going to report type assigability |
| 352 | // constraints to. Can be null for runtime verification. |
| 353 | VerifierDeps* verifier_deps_; |
| 354 | |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 355 | // Link, for the method verifier root linked list. |
| 356 | MethodVerifier* link_; |
| 357 | |
| 358 | friend class art::Thread; |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 359 | friend class ClassVerifier; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 360 | friend class VerifierDepsTest; |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 361 | |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 362 | DISALLOW_COPY_AND_ASSIGN(MethodVerifier); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 363 | }; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 364 | |
| 365 | } // namespace verifier |
| 366 | } // namespace art |
| 367 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 368 | #endif // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ |