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