blob: 5fe95c2c28484a4b8773b99fea5714627d5b7bed [file] [log] [blame]
Ian Rogers776ac1f2012-04-13 23:36:36 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
18#define ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_
Ian Rogers776ac1f2012-04-13 23:36:36 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Vladimir Marko637ee0b2015-09-04 12:47:41 +010021#include <sstream>
Ian Rogers776ac1f2012-04-13 23:36:36 -070022#include <vector>
23
Mathieu Chartierde40d472015-10-15 17:47:48 -070024#include "base/arena_allocator.h"
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Mathieu Chartierde40d472015-10-15 17:47:48 -070026#include "base/scoped_arena_containers.h"
27#include "base/stl_util.h"
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080028#include "base/value_object.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070029#include "dex_file.h"
Hiroshi Yamauchidc376172014-08-22 11:13:12 -070030#include "handle.h"
Ian Rogers7b3ddd22013-02-21 15:19:52 -080031#include "instruction_flags.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070032#include "method_reference.h"
Mathieu Chartier361e04a2016-02-16 14:06:35 -080033#include "register_line.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070034#include "reg_type_cache.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070035
36namespace art {
37
Andreas Gampe53e32d12015-12-09 21:03:23 -080038class CompilerCallbacks;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080039class Instruction;
Ian Rogers776ac1f2012-04-13 23:36:36 -070040struct ReferenceMap2Visitor;
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -070041class Thread;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +010042class VariableIndentationOutputStream;
Ian Rogers776ac1f2012-04-13 23:36:36 -070043
Ian Rogers776ac1f2012-04-13 23:36:36 -070044namespace verifier {
45
Ian Rogers8e1f4f82014-11-05 11:07:30 -080046class MethodVerifier;
47class RegisterLine;
Mathieu Chartier361e04a2016-02-16 14:06:35 -080048using RegisterLineArenaUniquePtr = std::unique_ptr<RegisterLine, RegisterLineArenaDelete>;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080049class RegType;
Ian Rogers776ac1f2012-04-13 23:36:36 -070050
51/*
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
53 * method determines which list we search, and whether we travel up into superclasses.
54 *
55 * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
56 * All others are stored in the "virtual" list.)
57 */
58enum MethodType {
59 METHOD_UNKNOWN = 0,
60 METHOD_DIRECT, // <init>, private
61 METHOD_STATIC, // static
Alex Light7268d472016-01-20 15:50:01 -080062 METHOD_VIRTUAL, // virtual
63 METHOD_SUPER, // super
Ian Rogers776ac1f2012-04-13 23:36:36 -070064 METHOD_INTERFACE // interface
65};
Ian Rogers2fc14272012-08-30 10:56:57 -070066std::ostream& operator<<(std::ostream& os, const MethodType& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -070067
68/*
69 * An enumeration of problems that can turn up during verification.
70 * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
71 * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
72 * that can potentially be corrected, and the verifier will try again at runtime.
73 * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
74 * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
75 * to be rewritten to fail at runtime.
76 */
77enum VerifyError {
Andreas Gampea727e372015-08-25 09:22:37 -070078 VERIFY_ERROR_BAD_CLASS_HARD = 1, // VerifyError; hard error that skips compilation.
79 VERIFY_ERROR_BAD_CLASS_SOFT = 2, // VerifyError; soft error that verifies again at runtime.
Ian Rogers776ac1f2012-04-13 23:36:36 -070080
Andreas Gampea727e372015-08-25 09:22:37 -070081 VERIFY_ERROR_NO_CLASS = 4, // NoClassDefFoundError.
82 VERIFY_ERROR_NO_FIELD = 8, // NoSuchFieldError.
83 VERIFY_ERROR_NO_METHOD = 16, // NoSuchMethodError.
84 VERIFY_ERROR_ACCESS_CLASS = 32, // IllegalAccessError.
85 VERIFY_ERROR_ACCESS_FIELD = 64, // IllegalAccessError.
86 VERIFY_ERROR_ACCESS_METHOD = 128, // IllegalAccessError.
87 VERIFY_ERROR_CLASS_CHANGE = 256, // IncompatibleClassChangeError.
88 VERIFY_ERROR_INSTANTIATION = 512, // InstantiationError.
Narayan Kamath14832ef2016-08-05 11:44:32 +010089 // For opcodes that don't have complete verifier support, we need a way to continue
90 // execution at runtime without attempting to re-verify (since we know it will fail no
91 // matter what). Instead, run as the interpreter in a special "do access checks" mode
92 // which will perform verifier-like checking on the fly.
Andreas Gampea727e372015-08-25 09:22:37 -070093 VERIFY_ERROR_FORCE_INTERPRETER = 1024, // Skip the verification phase at runtime;
94 // force the interpreter to do access checks.
95 // (sets a soft fail at compile time).
96 VERIFY_ERROR_LOCKING = 2048, // Could not guarantee balanced locking. This should be
97 // punted to the interpreter with access checks.
Ian Rogers776ac1f2012-04-13 23:36:36 -070098};
99std::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
100
Ian Rogers776ac1f2012-04-13 23:36:36 -0700101// We don't need to store the register data for many instructions, because we either only need
102// it at branch points (for verification) or GC points and branches (for verification +
103// type-precise register analysis).
104enum RegisterTrackingMode {
105 kTrackRegsBranches,
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700106 kTrackCompilerInterestPoints,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700107 kTrackRegsAll,
108};
109
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800110// A mapping from a dex pc to the register line statuses as they are immediately prior to the
111// execution of that instruction.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700112class PcToRegisterLineTable {
113 public:
Mathieu Chartierde40d472015-10-15 17:47:48 -0700114 explicit PcToRegisterLineTable(ScopedArenaAllocator& arena);
Ian Rogersd0fbd852013-09-24 18:17:04 -0700115 ~PcToRegisterLineTable();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700116
117 // Initialize the RegisterTable. Every instruction address can have a different set of information
118 // about what's in which register, but for verification purposes we only need to store it at
119 // branch target addresses (because we merge into that).
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800120 void Init(RegisterTrackingMode mode, InstructionFlags* flags, uint32_t insns_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700121 uint16_t registers_size, MethodVerifier* verifier);
122
Mathieu Chartierde40d472015-10-15 17:47:48 -0700123 RegisterLine* GetLine(size_t idx) const {
124 return register_lines_[idx].get();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700125 }
126
127 private:
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800128 ScopedArenaVector<RegisterLineArenaUniquePtr> register_lines_;
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800129
130 DISALLOW_COPY_AND_ASSIGN(PcToRegisterLineTable);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700131};
132
133// The verifier
134class MethodVerifier {
135 public:
jeffhaof1e6b7c2012-06-05 18:33:30 -0700136 enum FailureKind {
137 kNoFailure,
138 kSoftFailure,
139 kHardFailure,
140 };
141
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100142 static bool CanCompilerHandleVerificationFailure(uint32_t encountered_failure_types) {
143 constexpr uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
144 | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
145 | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
146 | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
147 return (encountered_failure_types & (~unresolved_mask)) == 0;
148 }
149
Andreas Gampe7fe30232016-03-25 16:58:00 -0700150 // Verify a class. Returns "kNoFailure" on success.
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800151 static FailureKind VerifyClass(Thread* self,
152 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800153 CompilerCallbacks* callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800154 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700155 LogSeverity log_level,
Ian Rogers7b078e82014-09-10 14:44:24 -0700156 std::string* error)
Mathieu Chartier90443472015-07-16 20:32:27 -0700157 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800158 static FailureKind VerifyClass(Thread* self,
159 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700160 Handle<mirror::DexCache> dex_cache,
161 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700162 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800163 CompilerCallbacks* callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800164 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700165 LogSeverity log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800166 std::string* error)
Mathieu Chartier90443472015-07-16 20:32:27 -0700167 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700168
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100169 static MethodVerifier* VerifyMethodAndDump(Thread* self,
170 VariableIndentationOutputStream* vios,
171 uint32_t method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700172 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700173 Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700175 const DexFile::ClassDef* class_def,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 const DexFile::CodeItem* code_item, ArtMethod* method,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700177 uint32_t method_access_flags)
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800179
Ian Rogers776ac1f2012-04-13 23:36:36 -0700180 uint8_t EncodePcToReferenceMapData() const;
181
182 uint32_t DexFileVersion() const {
183 return dex_file_->GetVersion();
184 }
185
186 RegTypeCache* GetRegTypeCache() {
187 return &reg_types_;
188 }
189
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190 // Log a verification failure.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700191 std::ostream& Fail(VerifyError error);
192
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 // Log for verification information.
Ian Rogers576ca0c2014-06-06 15:58:22 -0700194 std::ostream& LogVerifyInfo();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700195
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 // Dump the failures encountered by the verifier.
197 std::ostream& DumpFailures(std::ostream& os);
198
Ian Rogers776ac1f2012-04-13 23:36:36 -0700199 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
200 // information
Mathieu Chartier90443472015-07-16 20:32:27 -0700201 void Dump(std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_);
202 void Dump(VariableIndentationOutputStream* vios) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700203
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700204 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200205 // to the locks held at 'dex_pc' in method 'm'.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 static void FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700207 std::vector<uint32_t>* monitor_enter_dex_pcs)
Mathieu Chartier90443472015-07-16 20:32:27 -0700208 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700209
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200210 // Returns the accessed field corresponding to the quick instruction's field
211 // offset at 'dex_pc' in method 'm'.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 static ArtField* FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700213 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200214
215 // Returns the invoked method corresponding to the quick instruction's vtable
216 // index at 'dex_pc' in method 'm'.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 static ArtMethod* FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700218 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200219
Mathieu Chartier90443472015-07-16 20:32:27 -0700220 static void Init() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700221 static void Shutdown();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700222
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800223 bool CanLoadClasses() const {
224 return can_load_classes_;
225 }
226
Mathieu Chartier590fee92013-09-13 13:46:47 -0700227 ~MethodVerifier();
Sebastien Hertz33691ab2013-08-02 14:19:57 +0200228
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800229 // Run verification on the method. Returns true if verification completes and false if the input
230 // has an irrecoverable corruption.
Mathieu Chartier90443472015-07-16 20:32:27 -0700231 bool Verify() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800232
233 // Describe VRegs at the given dex pc.
234 std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
235
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700236 static void VisitStaticRoots(RootVisitor* visitor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700237 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700238 void VisitRoots(RootVisitor* visitor, const RootInfo& roots)
Mathieu Chartier90443472015-07-16 20:32:27 -0700239 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800240
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000241 // Accessors used by the compiler via CompilerCallback
242 const DexFile::CodeItem* CodeItem() const;
243 RegisterLine* GetRegLine(uint32_t dex_pc);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700244 ALWAYS_INLINE const InstructionFlags& GetInstructionFlags(size_t index) const;
245 ALWAYS_INLINE InstructionFlags& GetInstructionFlags(size_t index);
Mathieu Chartier90443472015-07-16 20:32:27 -0700246 mirror::ClassLoader* GetClassLoader() SHARED_REQUIRES(Locks::mutator_lock_);
247 mirror::DexCache* GetDexCache() SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Markobe10e8e2016-01-22 12:09:44 +0000248 ArtMethod* GetMethod() const SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000249 MethodReference GetMethodReference() const;
250 uint32_t GetAccessFlags() const;
251 bool HasCheckCasts() const;
252 bool HasVirtualOrInterfaceInvokes() const;
253 bool HasFailures() const;
Nicolas Geoffray4824c272015-06-24 15:53:03 +0100254 bool HasInstructionThatWillThrow() const {
Andreas Gamped12e7822015-06-25 10:26:40 -0700255 return have_any_pending_runtime_throw_failure_;
Nicolas Geoffray4824c272015-06-24 15:53:03 +0100256 }
257
Ian Rogersd8f69b02014-09-10 21:43:52 +0000258 const RegType& ResolveCheckedClass(uint32_t class_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700259 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700260 // Returns the method of a quick invoke or null if it cannot be found.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 ArtMethod* GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
Mathieu Chartier091d2382015-03-06 10:59:06 -0800262 bool is_range, bool allow_failure)
Mathieu Chartier90443472015-07-16 20:32:27 -0700263 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700264 // Returns the access field of a quick field access (iget/iput-quick) or null
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800265 // if it cannot be found.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700266 ArtField* GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line)
Mathieu Chartier90443472015-07-16 20:32:27 -0700267 SHARED_REQUIRES(Locks::mutator_lock_);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000268
Andreas Gampe0760a812015-08-26 17:12:51 -0700269 uint32_t GetEncounteredFailureTypes() {
270 return encountered_failure_types_;
271 }
272
Andreas Gampee6215c02015-08-31 18:54:38 -0700273 bool IsInstanceConstructor() const {
274 return IsConstructor() && !IsStatic();
275 }
276
Mathieu Chartierde40d472015-10-15 17:47:48 -0700277 ScopedArenaAllocator& GetArena() {
278 return arena_;
279 }
280
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800281 private:
Andreas Gampe53e32d12015-12-09 21:03:23 -0800282 MethodVerifier(Thread* self,
283 const DexFile* dex_file,
284 Handle<mirror::DexCache> dex_cache,
285 Handle<mirror::ClassLoader> class_loader,
286 const DexFile::ClassDef* class_def,
287 const DexFile::CodeItem* code_item,
288 uint32_t method_idx,
289 ArtMethod* method,
290 uint32_t access_flags,
291 bool can_load_classes,
292 bool allow_soft_failures,
293 bool need_precise_constants,
294 bool verify_to_dump,
295 bool allow_thread_suspension)
Mathieu Chartier90443472015-07-16 20:32:27 -0700296 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700297
Andreas Gampebf9611f2016-03-25 16:58:00 -0700298 void UninstantiableError(const char* descriptor);
299 static bool IsInstantiableOrPrimitive(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_);
300
301 // Is the method being verified a constructor? See the comment on the field.
302 bool IsConstructor() const {
303 return is_constructor_;
304 }
305
306 // Is the method verified static?
307 bool IsStatic() const {
308 return (method_access_flags_ & kAccStatic) != 0;
309 }
310
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311 // Adds the given string to the beginning of the last failure message.
312 void PrependToLastFailMessage(std::string);
313
314 // Adds the given string to the end of the last failure message.
315 void AppendToLastFailMessage(std::string);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700316
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800317 // Verification result for method(s). Includes a (maximum) failure kind, and (the union of)
318 // all failure types.
319 struct FailureData : ValueObject {
320 FailureKind kind = kNoFailure;
321 uint32_t types = 0U;
322
323 // Merge src into this. Uses the most severe failure kind, and the union of types.
324 void Merge(const FailureData& src);
325 };
326
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800327 // Verify all direct or virtual methods of a class. The method assumes that the iterator is
328 // positioned correctly, and the iterator will be updated.
329 template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800330 static FailureData VerifyMethods(Thread* self,
331 ClassLinker* linker,
332 const DexFile* dex_file,
333 const DexFile::ClassDef* class_def,
334 ClassDataItemIterator* it,
335 Handle<mirror::DexCache> dex_cache,
336 Handle<mirror::ClassLoader> class_loader,
337 CompilerCallbacks* callbacks,
338 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700339 LogSeverity log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800340 bool need_precise_constants,
341 std::string* error_string)
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800342 SHARED_REQUIRES(Locks::mutator_lock_);
343
Ian Rogers776ac1f2012-04-13 23:36:36 -0700344 /*
345 * Perform verification on a single method.
346 *
347 * We do this in three passes:
348 * (1) Walk through all code units, determining instruction locations,
349 * widths, and other characteristics.
350 * (2) Walk through all code units, performing static checks on
351 * operands.
352 * (3) Iterate through the method, checking type safety and looking
353 * for code flow problems.
Ian Rogerse1758fe2012-04-19 11:31:15 -0700354 */
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800355 static FailureData VerifyMethod(Thread* self, uint32_t method_idx,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800356 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700357 Handle<mirror::DexCache> dex_cache,
358 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700359 const DexFile::ClassDef* class_def_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 const DexFile::CodeItem* code_item,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800361 ArtMethod* method,
362 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800363 CompilerCallbacks* callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800364 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700365 LogSeverity log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800366 bool need_precise_constants,
367 std::string* hard_failure_msg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700368 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogerse1758fe2012-04-19 11:31:15 -0700369
Mathieu Chartier90443472015-07-16 20:32:27 -0700370 void FindLocksAtDexPc() SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700371
Mathieu Chartierc7853442015-03-27 14:35:38 -0700372 ArtField* FindAccessedFieldAtDexPc(uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700373 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200374
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 ArtMethod* FindInvokedMethodAtDexPc(uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700376 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377
Jeff Hao848f70a2014-01-15 13:49:50 -0800378 SafeMap<uint32_t, std::set<uint32_t>>& FindStringInitMap()
Mathieu Chartier90443472015-07-16 20:32:27 -0700379 SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Hao848f70a2014-01-15 13:49:50 -0800380
Ian Rogers776ac1f2012-04-13 23:36:36 -0700381 /*
382 * Compute the width of the instruction at each address in the instruction stream, and store it in
383 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
384 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
385 *
386 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
387 *
388 * Performs some static checks, notably:
389 * - opcode of first instruction begins at index 0
390 * - only documented instructions may appear
391 * - each instruction follows the last
392 * - last byte of last instruction is at (code_length-1)
393 *
394 * Logs an error and returns "false" on failure.
395 */
396 bool ComputeWidthsAndCountOps();
397
398 /*
399 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
400 * "branch target" flags for exception handlers.
401 *
402 * Call this after widths have been set in "insn_flags".
403 *
404 * Returns "false" if something in the exception table looks fishy, but we're expecting the
405 * exception table to be somewhat sane.
406 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700407 bool ScanTryCatchBlocks() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700408
409 /*
410 * Perform static verification on all instructions in a method.
411 *
412 * Walks through instructions in a method calling VerifyInstruction on each.
413 */
414 bool VerifyInstructions();
415
416 /*
417 * Perform static verification on an instruction.
418 *
419 * As a side effect, this sets the "branch target" flags in InsnFlags.
420 *
421 * "(CF)" items are handled during code-flow analysis.
422 *
423 * v3 4.10.1
424 * - target of each jump and branch instruction must be valid
425 * - targets of switch statements must be valid
426 * - operands referencing constant pool entries must be valid
427 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
428 * - (CF) operands of method invocation instructions must be valid
429 * - (CF) only invoke-direct can call a method starting with '<'
430 * - (CF) <clinit> must never be called explicitly
431 * - operands of instanceof, checkcast, new (and variants) must be valid
432 * - new-array[-type] limited to 255 dimensions
433 * - can't use "new" on an array class
434 * - (?) limit dimensions in multi-array creation
435 * - local variable load/store register values must be in valid range
436 *
437 * v3 4.11.1.2
438 * - branches must be within the bounds of the code array
439 * - targets of all control-flow instructions are the start of an instruction
440 * - register accesses fall within range of allocated registers
441 * - (N/A) access to constant pool must be of appropriate type
442 * - code does not end in the middle of an instruction
443 * - execution cannot fall off the end of the code
444 * - (earlier) for each exception handler, the "try" area must begin and
445 * end at the start of an instruction (end can be at the end of the code)
446 * - (earlier) for each exception handler, the handler must start at a valid
447 * instruction
448 */
449 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
450
451 /* Ensure that the register index is valid for this code item. */
452 bool CheckRegisterIndex(uint32_t idx);
453
454 /* Ensure that the wide register index is valid for this code item. */
455 bool CheckWideRegisterIndex(uint32_t idx);
456
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700457 // Perform static checks on a field Get or set instruction. All we do here is ensure that the
Ian Rogers776ac1f2012-04-13 23:36:36 -0700458 // field index is in the valid range.
459 bool CheckFieldIndex(uint32_t idx);
460
461 // Perform static checks on a method invocation instruction. All we do here is ensure that the
462 // method index is in the valid range.
463 bool CheckMethodIndex(uint32_t idx);
464
465 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
466 // reference isn't for an array class.
467 bool CheckNewInstance(uint32_t idx);
468
469 /* Ensure that the string index is in the valid range. */
470 bool CheckStringIndex(uint32_t idx);
471
472 // Perform static checks on an instruction that takes a class constant. Ensure that the class
473 // index is in the valid range.
474 bool CheckTypeIndex(uint32_t idx);
475
476 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
477 // creating an array of arrays that causes the number of dimensions to exceed 255.
478 bool CheckNewArray(uint32_t idx);
479
480 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
481 bool CheckArrayData(uint32_t cur_offset);
482
483 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
484 // into an exception handler, but it's valid to do so as long as the target isn't a
485 // "move-exception" instruction. We verify that in a later stage.
486 // The dex format forbids certain instructions from branching to themselves.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700487 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700488 bool CheckBranchTarget(uint32_t cur_offset);
489
490 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700491 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700492 bool CheckSwitchTargets(uint32_t cur_offset);
493
494 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
495 // filled-new-array.
496 // - vA holds word count (0-5), args[] have values.
497 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
498 // takes a double is done with consecutive registers. This requires parsing the target method
499 // signature, which we will be doing later on during the code flow analysis.
500 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
501
502 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
503 // or filled-new-array/range.
504 // - vA holds word count, vC holds index of first reg.
505 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
506
507 // Extract the relative offset from a branch instruction.
508 // Returns "false" on failure (e.g. this isn't a branch instruction).
509 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
510 bool* selfOkay);
511
512 /* Perform detailed code-flow analysis on a single method. */
Mathieu Chartier90443472015-07-16 20:32:27 -0700513 bool VerifyCodeFlow() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700514
515 // Set the register types for the first instruction in the method based on the method signature.
516 // This has the side-effect of validating the signature.
Mathieu Chartier90443472015-07-16 20:32:27 -0700517 bool SetTypesFromSignature() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700518
519 /*
520 * Perform code flow on a method.
521 *
522 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
523 * instruction, process it (setting additional "changed" bits), and repeat until there are no
524 * more.
525 *
526 * v3 4.11.1.1
527 * - (N/A) operand stack is always the same size
528 * - operand stack [registers] contain the correct types of values
529 * - local variables [registers] contain the correct types of values
530 * - methods are invoked with the appropriate arguments
531 * - fields are assigned using values of appropriate types
532 * - opcodes have the correct type values in operand registers
533 * - there is never an uninitialized class instance in a local variable in code protected by an
534 * exception handler (operand stack is okay, because the operand stack is discarded when an
535 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
536 * register typing]
537 *
538 * v3 4.11.1.2
539 * - execution cannot fall off the end of the code
540 *
541 * (We also do many of the items described in the "static checks" sections, because it's easier to
542 * do them here.)
543 *
544 * We need an array of RegType values, one per register, for every instruction. If the method uses
545 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
546 * instruction. In theory this could become quite large -- up to several megabytes for a monster
547 * function.
548 *
549 * NOTE:
550 * The spec forbids backward branches when there's an uninitialized reference in a register. The
551 * idea is to prevent something like this:
552 * loop:
553 * move r1, r0
554 * new-instance r0, MyClass
555 * ...
556 * if-eq rN, loop // once
557 * initialize r0
558 *
559 * This leaves us with two different instances, both allocated by the same instruction, but only
560 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
561 * it by preventing backward branches. We achieve identical results without restricting code
562 * reordering by specifying that you can't execute the new-instance instruction if a register
563 * contains an uninitialized instance created by that same instruction.
564 */
Mathieu Chartier90443472015-07-16 20:32:27 -0700565 bool CodeFlowVerifyMethod() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700566
567 /*
568 * Perform verification for a single instruction.
569 *
570 * This requires fully decoding the instruction to determine the effect it has on registers.
571 *
572 * Finds zero or more following instructions and sets the "changed" flag if execution at that
573 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
574 * addresses. Does not set or clear any other flags in "insn_flags_".
575 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700576 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
Mathieu Chartier90443472015-07-16 20:32:27 -0700577 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700578
579 // Perform verification of a new array instruction
Sebastien Hertz5243e912013-05-21 10:55:07 +0200580 void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
Mathieu Chartier90443472015-07-16 20:32:27 -0700581 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700582
Jeff Haofe1f7c82013-08-01 14:50:24 -0700583 // Helper to perform verification on puts of primitive type.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000584 void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Mathieu Chartier90443472015-07-16 20:32:27 -0700585 const uint32_t vregA) SHARED_REQUIRES(Locks::mutator_lock_);
Jeff Haofe1f7c82013-08-01 14:50:24 -0700586
Ian Rogers776ac1f2012-04-13 23:36:36 -0700587 // Perform verification of an aget instruction. The destination register's type will be set to
588 // be that of component type of the array unless the array type is unknown, in which case a
589 // bottom type inferred from the type of instruction is used. is_primitive is false for an
590 // aget-object.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000591 void VerifyAGet(const Instruction* inst, const RegType& insn_type,
Mathieu Chartier90443472015-07-16 20:32:27 -0700592 bool is_primitive) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700593
594 // Perform verification of an aput instruction.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000595 void VerifyAPut(const Instruction* inst, const RegType& insn_type,
Mathieu Chartier90443472015-07-16 20:32:27 -0700596 bool is_primitive) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700597
598 // Lookup instance field and fail for resolution violations
Mathieu Chartierc7853442015-03-27 14:35:38 -0700599 ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700600 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700601
602 // Lookup static field and fail for resolution violations
Mathieu Chartier90443472015-07-16 20:32:27 -0700603 ArtField* GetStaticField(int field_idx) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700604
Andreas Gampe896df402014-10-20 22:25:29 -0700605 // Perform verification of an iget/sget/iput/sput instruction.
606 enum class FieldAccessType { // private
607 kAccGet,
608 kAccPut
609 };
610 template <FieldAccessType kAccType>
611 void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
612 bool is_primitive, bool is_static)
Mathieu Chartier90443472015-07-16 20:32:27 -0700613 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700614
Andreas Gampe896df402014-10-20 22:25:29 -0700615 template <FieldAccessType kAccType>
616 void VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, bool is_primitive)
Mathieu Chartier90443472015-07-16 20:32:27 -0700617 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618
Ian Rogers776ac1f2012-04-13 23:36:36 -0700619 // Resolves a class based on an index and performs access checks to ensure the referrer can
620 // access the resolved class.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000621 const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700622 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700623
624 /*
625 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
626 * address, determine the Join of all exceptions that can land here. Fails if no matching
627 * exception handler can be found or if the Join of exception types fails.
628 */
Ian Rogersd8f69b02014-09-10 21:43:52 +0000629 const RegType& GetCaughtExceptionType()
Mathieu Chartier90443472015-07-16 20:32:27 -0700630 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631
632 /*
633 * Resolves a method based on an index and performs access checks to ensure
634 * the referrer can access the resolved method.
635 * Does not throw exceptions.
636 */
Alex Light7268d472016-01-20 15:50:01 -0800637 ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700638 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700639
640 /*
641 * Verify the arguments to a method. We're executing in "method", making
642 * a call to the method reference in vB.
643 *
644 * If this is a "direct" invoke, we allow calls to <init>. For calls to
645 * <init>, the first argument may be an uninitialized reference. Otherwise,
646 * calls to anything starting with '<' will be rejected, as will any
647 * uninitialized reference arguments.
648 *
649 * For non-static method calls, this will verify that the method call is
650 * appropriate for the "this" argument.
651 *
652 * The method reference is in vBBBB. The "is_range" parameter determines
653 * whether we use 0-4 "args" values or a range of registers defined by
654 * vAA and vCCCC.
655 *
656 * Widening conversions on integers and references are allowed, but
657 * narrowing conversions are not.
658 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700659 * Returns the resolved method on success, null on failure (with *failure
Ian Rogers776ac1f2012-04-13 23:36:36 -0700660 * set appropriately).
661 */
Alex Light7268d472016-01-20 15:50:01 -0800662 ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
Mathieu Chartier90443472015-07-16 20:32:27 -0700663 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664
Andreas Gampe95c0bf82014-06-16 14:06:52 -0700665 // Similar checks to the above, but on the proto. Will be used when the method cannot be
666 // resolved.
667 void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
668 bool is_range)
Mathieu Chartier90443472015-07-16 20:32:27 -0700669 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -0700670
671 template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700672 ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
Andreas Gampe95c0bf82014-06-16 14:06:52 -0700673 MethodType method_type, bool is_range,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700674 ArtMethod* res_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700675 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe95c0bf82014-06-16 14:06:52 -0700676
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677 ArtMethod* VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range)
Mathieu Chartier90443472015-07-16 20:32:27 -0700678 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681 * Verify that the target instruction is not "move-exception". It's important that the only way
682 * to execute a move-exception is as the first instruction of an exception handler.
683 * Returns "true" if all is well, "false" if the target instruction is move-exception.
684 */
685 bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
686
687 /*
Stephen Kyle9bc61992014-09-22 13:53:15 +0100688 * Verify that the target instruction is not "move-result". It is important that we cannot
689 * branch to move-result instructions, but we have to make this a distinct check instead of
690 * adding it to CheckNotMoveException, because it is legal to continue into "move-result"
691 * instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
692 */
693 bool CheckNotMoveResult(const uint16_t* insns, int insn_idx);
694
695 /*
696 * Verify that the target instruction is not "move-result" or "move-exception". This is to
697 * be used when checking branch and switch instructions, but not instructions that can
698 * continue.
699 */
700 bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx);
701
702 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700703 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
704 * next_insn, and set the changed flag on the target address if any of the registers were changed.
Ian Rogersebbdd872014-07-07 23:53:08 -0700705 * In the case of fall-through, update the merge line on a change as its the working line for the
706 * next instruction.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707 * Returns "false" if an error is encountered.
708 */
Ian Rogersebbdd872014-07-07 23:53:08 -0700709 bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
Mathieu Chartier90443472015-07-16 20:32:27 -0700710 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700711
Ian Rogersad0b3a32012-04-16 14:50:24 -0700712 // Return the register type for the method.
Mathieu Chartier90443472015-07-16 20:32:27 -0700713 const RegType& GetMethodReturnType() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700714
715 // Get a type representing the declaring class of the method.
Mathieu Chartier90443472015-07-16 20:32:27 -0700716 const RegType& GetDeclaringClass() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700717
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800718 InstructionFlags* CurrentInsnFlags();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700719
Ian Rogersd8f69b02014-09-10 21:43:52 +0000720 const RegType& DetermineCat1Constant(int32_t value, bool precise)
Mathieu Chartier90443472015-07-16 20:32:27 -0700721 SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700722
Andreas Gampef23f33d2015-06-23 14:18:17 -0700723 // Try to create a register type from the given class. In case a precise type is requested, but
724 // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
725 // non-precise reference will be returned.
726 // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
727 // actually touched.
728 const RegType& FromClass(const char* descriptor, mirror::Class* klass, bool precise)
Mathieu Chartier90443472015-07-16 20:32:27 -0700729 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampef23f33d2015-06-23 14:18:17 -0700730
Ian Rogers7b078e82014-09-10 14:44:24 -0700731 // The thread we're verifying on.
732 Thread* const self_;
733
Mathieu Chartierde40d472015-10-15 17:47:48 -0700734 // Arena allocator.
735 ArenaStack arena_stack_;
736 ScopedArenaAllocator arena_;
737
Ian Rogers776ac1f2012-04-13 23:36:36 -0700738 RegTypeCache reg_types_;
739
740 PcToRegisterLineTable reg_table_;
741
742 // Storage for the register status we're currently working on.
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800743 RegisterLineArenaUniquePtr work_line_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700744
745 // The address of the instruction we're currently working on, note that this is in 2 byte
746 // quantities
747 uint32_t work_insn_idx_;
748
749 // Storage for the register status we're saving for later.
Mathieu Chartier361e04a2016-02-16 14:06:35 -0800750 RegisterLineArenaUniquePtr saved_line_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700751
Ian Rogers637c65b2013-05-31 11:46:00 -0700752 const uint32_t dex_method_idx_; // The method we're working on.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 // Its object representation if known.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700754 ArtMethod* mirror_method_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers637c65b2013-05-31 11:46:00 -0700755 const uint32_t method_access_flags_; // Method's access flags.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000756 const RegType* return_type_; // Lazily computed return type of the method.
Ian Rogers637c65b2013-05-31 11:46:00 -0700757 const DexFile* const dex_file_; // The dex file containing the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758 // The dex_cache for the declaring class of the method.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700759 Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700760 // The class loader for the declaring class of the method.
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700761 Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700762 const DexFile::ClassDef* const class_def_; // The class def of the declaring class of the method.
Ian Rogers637c65b2013-05-31 11:46:00 -0700763 const DexFile::CodeItem* const code_item_; // The code item containing the code for the method.
Ian Rogersd8f69b02014-09-10 21:43:52 +0000764 const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800765 // Instruction widths and flags, one entry per code unit.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700766 // Owned, but not unique_ptr since insn_flags_ are allocated in arenas.
767 ArenaUniquePtr<InstructionFlags[]> insn_flags_;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200768 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700769 uint32_t interesting_dex_pc_;
770 // The container into which FindLocksAtDexPc should write the registers containing held locks,
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700771 // null if we're not doing FindLocksAtDexPc.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700772 std::vector<uint32_t>* monitor_enter_dex_pcs_;
773
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 // The types of any error that occurs.
775 std::vector<VerifyError> failures_;
776 // Error messages associated with failures.
777 std::vector<std::ostringstream*> failure_messages_;
778 // Is there a pending hard failure?
779 bool have_pending_hard_failure_;
jeffhaofaf459e2012-08-31 15:32:47 -0700780 // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
781 // would fail at runtime throwing an exception. Such an instruction causes the following code
782 // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
783 // instructions that would hard fail the verification.
Andreas Gamped12e7822015-06-25 10:26:40 -0700784 // Note: this flag is reset after processing each instruction.
jeffhaofaf459e2012-08-31 15:32:47 -0700785 bool have_pending_runtime_throw_failure_;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700786 // Is there a pending experimental failure?
787 bool have_pending_experimental_failure_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700788
Andreas Gamped12e7822015-06-25 10:26:40 -0700789 // A version of the above that is not reset and thus captures if there were *any* throw failures.
790 bool have_any_pending_runtime_throw_failure_;
791
Ian Rogersad0b3a32012-04-16 14:50:24 -0700792 // Info message log use primarily for verifier diagnostics.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793 std::ostringstream info_messages_;
794
795 // The number of occurrences of specific opcodes.
796 size_t new_instance_count_;
797 size_t monitor_enter_count_;
Elliott Hughes80537bb2013-01-04 16:37:26 -0800798
Andreas Gampe0760a812015-08-26 17:12:51 -0700799 // Bitset of the encountered failure types. Bits are according to the values in VerifyError.
800 uint32_t encountered_failure_types_;
801
Elliott Hughes80537bb2013-01-04 16:37:26 -0800802 const bool can_load_classes_;
Jeff Haoee988952013-04-16 14:23:47 -0700803
804 // Converts soft failures to hard failures when false. Only false when the compiler isn't
805 // running and the verifier is called from the class linker.
806 const bool allow_soft_failures_;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200807
Ian Rogers46960fe2014-05-23 10:43:43 -0700808 // An optimization where instead of generating unique RegTypes for constants we use imprecise
809 // constants that cover a range of constants. This isn't good enough for deoptimization that
810 // avoids loading from registers in the case of a constant as the dex instruction set lost the
811 // notion of whether a value should be in a floating point or general purpose register file.
812 const bool need_precise_constants_;
813
Ian Rogersa9a82542013-10-04 11:17:26 -0700814 // Indicates the method being verified contains at least one check-cast or aput-object
815 // instruction. Aput-object operations implicitly check for array-store exceptions, similar to
816 // check-cast.
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200817 bool has_check_casts_;
818
Ian Rogersa9a82542013-10-04 11:17:26 -0700819 // Indicates the method being verified contains at least one invoke-virtual/range
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200820 // or invoke-interface/range.
821 bool has_virtual_or_interface_invokes_;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700822
823 // Indicates whether we verify to dump the info. In that case we accept quickened instructions
824 // even though we might detect to be a compiler. Should only be set when running
825 // VerifyMethodAndDump.
826 const bool verify_to_dump_;
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800827
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800828 // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
829 // thread dumping checkpoints since we may get thread suspension at an inopportune time due to
830 // FindLocksAtDexPC, resulting in deadlocks.
831 const bool allow_thread_suspension_;
832
Andreas Gampee6215c02015-08-31 18:54:38 -0700833 // Whether the method seems to be a constructor. Note that this field exists as we can't trust
834 // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
835 // correctly.
836 //
837 // Note: this flag is only valid once Verify() has started.
838 bool is_constructor_;
839
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700840 // Link, for the method verifier root linked list.
841 MethodVerifier* link_;
842
843 friend class art::Thread;
Jeff Hao848f70a2014-01-15 13:49:50 -0800844
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800845 DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700846};
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700847std::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700848
849} // namespace verifier
850} // namespace art
851
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700852#endif // ART_RUNTIME_VERIFIER_METHOD_VERIFIER_H_