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 17 #ifndef ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ 18 #define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ 19 20 #include <memory> 21 #include <sstream> 22 #include <vector> 23 24 #include <android-base/logging.h> 25 26 #include "base/arena_allocator.h" 27 #include "base/macros.h" 28 #include "base/scoped_arena_containers.h" 29 #include "base/value_object.h" 30 #include "dex/code_item_accessors.h" 31 #include "dex/dex_file_types.h" 32 #include "dex/method_reference.h" 33 #include "handle.h" 34 #include "instruction_flags.h" 35 #include "reg_type_cache.h" 36 #include "register_line.h" 37 #include "verifier_enums.h" 38 39 namespace art { 40 41 class ClassLinker; 42 class DexFile; 43 class Instruction; 44 struct ReferenceMap2Visitor; 45 class Thread; 46 class VariableIndentationOutputStream; 47 48 namespace dex { 49 struct ClassDef; 50 struct CodeItem; 51 } // namespace dex 52 53 namespace mirror { 54 class DexCache; 55 } // namespace mirror 56 57 namespace verifier { 58 59 class MethodVerifier; 60 class RegisterLine; 61 using RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>; 62 class RegType; 63 struct ScopedNewLine; 64 class VerifierDeps; 65 66 // A mapping from a dex pc to the register line statuses as they are immediately prior to the 67 // execution of that instruction. 68 class PcToRegisterLineTable { 69 public: 70 explicit PcToRegisterLineTable(ScopedArenaAllocator& allocator); 71 ~PcToRegisterLineTable(); 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). 76 void Init(InstructionFlags* flags, 77 uint32_t insns_size, 78 uint16_t registers_size, 79 ScopedArenaAllocator& allocator, 80 RegTypeCache* reg_types, 81 uint32_t interesting_dex_pc); 82 IsInitialized()83 bool IsInitialized() const { 84 return !register_lines_.empty(); 85 } 86 GetLine(size_t idx)87 RegisterLine* GetLine(size_t idx) const { 88 return register_lines_[idx].get(); 89 } 90 91 private: 92 ScopedArenaVector<RegisterLineArenaUniquePtr> register_lines_; 93 94 DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable); 95 }; 96 97 // The verifier 98 class MethodVerifier { 99 public: 100 static MethodVerifier* VerifyMethodAndDump(Thread* self, 101 VariableIndentationOutputStream* vios, 102 uint32_t method_idx, 103 const DexFile* dex_file, 104 Handle<mirror::DexCache> dex_cache, 105 Handle<mirror::ClassLoader> class_loader, 106 const dex::ClassDef& class_def, 107 const dex::CodeItem* code_item, 108 uint32_t method_access_flags, 109 uint32_t api_level) 110 REQUIRES_SHARED(Locks::mutator_lock_); 111 112 // Calculates the type information at the given `dex_pc`. 113 // No classes will be loaded. 114 static MethodVerifier* CalculateVerificationInfo(Thread* self, 115 ArtMethod* method, 116 uint32_t dex_pc) 117 REQUIRES_SHARED(Locks::mutator_lock_); 118 GetDexFile()119 const DexFile& GetDexFile() const { 120 DCHECK(dex_file_ != nullptr); 121 return *dex_file_; 122 } 123 GetClassDef()124 const dex::ClassDef& GetClassDef() const { 125 return class_def_; 126 } 127 GetRegTypeCache()128 RegTypeCache* GetRegTypeCache() { 129 return ®_types_; 130 } 131 132 // Log a verification failure. 133 std::ostream& Fail(VerifyError error, bool pending_exc = true); 134 135 // Log for verification information. 136 ScopedNewLine LogVerifyInfo(); 137 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 DexLockInfoDexLockInfo145 explicit DexLockInfo(uint32_t dex_pc_in) { 146 dex_pc = dex_pc_in; 147 } 148 }; 149 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding 150 // to the locks held at 'dex_pc' in method 'm'. 151 // Note: this is the only situation where the verifier will visit quickened instructions. 152 static void FindLocksAtDexPc(ArtMethod* m, 153 uint32_t dex_pc, 154 std::vector<DexLockInfo>* monitor_enter_dex_pcs, 155 uint32_t api_level) 156 REQUIRES_SHARED(Locks::mutator_lock_); 157 158 static void Init(ClassLinker* class_linker) REQUIRES_SHARED(Locks::mutator_lock_); 159 static void Shutdown(); 160 161 virtual ~MethodVerifier(); 162 163 static void VisitStaticRoots(RootVisitor* visitor) 164 REQUIRES_SHARED(Locks::mutator_lock_); 165 void VisitRoots(RootVisitor* visitor, const RootInfo& roots) 166 REQUIRES_SHARED(Locks::mutator_lock_); 167 CodeItem()168 const CodeItemDataAccessor& CodeItem() const { 169 return code_item_accessor_; 170 } 171 RegisterLine* GetRegLine(uint32_t dex_pc); 172 ALWAYS_INLINE const InstructionFlags& GetInstructionFlags(size_t index) const; 173 174 MethodReference GetMethodReference() const; 175 bool HasFailures() const; HasInstructionThatWillThrow()176 bool HasInstructionThatWillThrow() const { 177 return (encountered_failure_types_ & VERIFY_ERROR_RUNTIME_THROW) != 0; 178 } 179 180 virtual const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) 181 REQUIRES_SHARED(Locks::mutator_lock_) = 0; 182 GetEncounteredFailureTypes()183 uint32_t GetEncounteredFailureTypes() const { 184 return encountered_failure_types_; 185 } 186 GetClassLinker()187 ClassLinker* GetClassLinker() const { 188 return class_linker_; 189 } 190 IsAotMode()191 bool IsAotMode() const { 192 return flags_.aot_mode_; 193 } 194 GetVerifierDeps()195 VerifierDeps* GetVerifierDeps() const { 196 return verifier_deps_; 197 } 198 199 protected: 200 MethodVerifier(Thread* self, 201 ClassLinker* class_linker, 202 ArenaPool* arena_pool, 203 VerifierDeps* verifier_deps, 204 const DexFile* dex_file, 205 const dex::ClassDef& class_def, 206 const dex::CodeItem* code_item, 207 uint32_t dex_method_idx, 208 bool can_load_classes, 209 bool allow_thread_suspension, 210 bool aot_mode) 211 REQUIRES_SHARED(Locks::mutator_lock_); 212 213 // Verification result for method(s). Includes a (maximum) failure kind, and (the union of) 214 // all failure types. 215 struct FailureData : ValueObject { 216 FailureKind kind = FailureKind::kNoFailure; 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 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. 233 */ 234 static FailureData VerifyMethod(Thread* self, 235 ClassLinker* class_linker, 236 ArenaPool* arena_pool, 237 VerifierDeps* verifier_deps, 238 uint32_t method_idx, 239 const DexFile* dex_file, 240 Handle<mirror::DexCache> dex_cache, 241 Handle<mirror::ClassLoader> class_loader, 242 const dex::ClassDef& class_def_idx, 243 const dex::CodeItem* code_item, 244 uint32_t method_access_flags, 245 HardFailLogMode log_level, 246 uint32_t api_level, 247 bool aot_mode, 248 std::string* hard_failure_msg) 249 REQUIRES_SHARED(Locks::mutator_lock_); 250 251 template <bool kVerifierDebug> 252 static FailureData VerifyMethod(Thread* self, 253 ClassLinker* class_linker, 254 ArenaPool* arena_pool, 255 VerifierDeps* verifier_deps, 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, 262 uint32_t method_access_flags, 263 HardFailLogMode log_level, 264 uint32_t api_level, 265 bool aot_mode, 266 std::string* hard_failure_msg) 267 REQUIRES_SHARED(Locks::mutator_lock_); 268 269 // For VerifierDepsTest. TODO: Refactor. 270 271 // Run verification on the method. Returns true if verification completes and false if the input 272 // has an irrecoverable corruption. 273 virtual bool Verify() REQUIRES_SHARED(Locks::mutator_lock_) = 0; 274 static MethodVerifier* CreateVerifier(Thread* self, 275 VerifierDeps* verifier_deps, 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, 282 uint32_t access_flags, 283 bool can_load_classes, 284 bool verify_to_dump, 285 bool allow_thread_suspension, 286 uint32_t api_level) 287 REQUIRES_SHARED(Locks::mutator_lock_); 288 289 virtual bool PotentiallyMarkRuntimeThrow() = 0; 290 291 // The thread we're verifying on. 292 Thread* const self_; 293 294 // Arena allocator. 295 ArenaStack arena_stack_; 296 ScopedArenaAllocator allocator_; 297 298 RegTypeCache reg_types_; 299 300 PcToRegisterLineTable reg_table_; 301 302 // Storage for the register status we're currently working on. 303 RegisterLineArenaUniquePtr work_line_; 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. 310 RegisterLineArenaUniquePtr saved_line_; 311 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. 315 const CodeItemDataAccessor code_item_accessor_; 316 317 // Instruction widths and flags, one entry per code unit. 318 // Owned, but not unique_ptr since insn_flags_ are allocated in arenas. 319 ArenaUniquePtr<InstructionFlags[]> insn_flags_; 320 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_; 325 struct { 326 // Is there a pending hard failure? 327 bool have_pending_hard_failure_ : 1; 328 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 336 // Verify in AoT mode? 337 bool aot_mode_ : 1; 338 } flags_; 339 340 // Info message log use primarily for verifier diagnostics. 341 std::ostringstream info_messages_; 342 343 // Bitset of the encountered failure types. Bits are according to the values in VerifyError. 344 uint32_t encountered_failure_types_; 345 346 const bool can_load_classes_; 347 348 // Classlinker to use when resolving. 349 ClassLinker* class_linker_; 350 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 355 // Link, for the method verifier root linked list. 356 MethodVerifier* link_; 357 358 friend class art::Thread; 359 friend class ClassVerifier; 360 friend class VerifierDepsTest; 361 362 DISALLOW_COPY_AND_ASSIGN(MethodVerifier); 363 }; 364 365 } // namespace verifier 366 } // namespace art 367 368 #endif // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_ 369