blob: 9b4b8e55035eb67ea20ba5f5e2dba8ee703b52b5 [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
17#ifndef ART_SRC_VERIFIER_METHOD_VERIFIER_H_
18#define ART_SRC_VERIFIER_METHOD_VERIFIER_H_
19
Ian Rogers776ac1f2012-04-13 23:36:36 -070020#include <set>
21#include <vector>
22
Elliott Hughes1aa246d2012-12-13 09:29:36 -080023#include "base/casts.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Ian Rogers1212a022013-03-04 10:48:41 -080026#include "compiler/driver/compiler_driver.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070027#include "dex_file.h"
28#include "dex_instruction.h"
Ian Rogers7b3ddd22013-02-21 15:19:52 -080029#include "instruction_flags.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070031#include "reg_type.h"
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -080032#include "reg_type_cache-inl.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070033#include "register_line.h"
34#include "safe_map.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070035#include "UniquePtr.h"
36
37namespace art {
38
39struct ReferenceMap2Visitor;
40
Ian Rogers776ac1f2012-04-13 23:36:36 -070041namespace verifier {
42
43class MethodVerifier;
Ian Rogers46c6bb22012-09-18 13:47:36 -070044class DexPcToReferenceMap;
Ian Rogers776ac1f2012-04-13 23:36:36 -070045
46/*
Ian Rogers776ac1f2012-04-13 23:36:36 -070047 * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
48 * method determines which list we search, and whether we travel up into superclasses.
49 *
50 * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
51 * All others are stored in the "virtual" list.)
52 */
53enum MethodType {
54 METHOD_UNKNOWN = 0,
55 METHOD_DIRECT, // <init>, private
56 METHOD_STATIC, // static
57 METHOD_VIRTUAL, // virtual, super
58 METHOD_INTERFACE // interface
59};
Ian Rogers2fc14272012-08-30 10:56:57 -070060std::ostream& operator<<(std::ostream& os, const MethodType& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -070061
62/*
63 * An enumeration of problems that can turn up during verification.
64 * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
65 * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
66 * that can potentially be corrected, and the verifier will try again at runtime.
67 * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
68 * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
69 * to be rewritten to fail at runtime.
70 */
71enum VerifyError {
Ian Rogers776ac1f2012-04-13 23:36:36 -070072 VERIFY_ERROR_BAD_CLASS_HARD, // VerifyError; hard error that skips compilation.
73 VERIFY_ERROR_BAD_CLASS_SOFT, // VerifyError; soft error that verifies again at runtime.
74
75 VERIFY_ERROR_NO_CLASS, // NoClassDefFoundError.
76 VERIFY_ERROR_NO_FIELD, // NoSuchFieldError.
77 VERIFY_ERROR_NO_METHOD, // NoSuchMethodError.
78 VERIFY_ERROR_ACCESS_CLASS, // IllegalAccessError.
79 VERIFY_ERROR_ACCESS_FIELD, // IllegalAccessError.
80 VERIFY_ERROR_ACCESS_METHOD, // IllegalAccessError.
81 VERIFY_ERROR_CLASS_CHANGE, // IncompatibleClassChangeError.
82 VERIFY_ERROR_INSTANTIATION, // InstantiationError.
83};
84std::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
85
86/*
87 * Identifies the type of reference in the instruction that generated the verify error
88 * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference).
89 *
90 * This must fit in two bits.
91 */
92enum VerifyErrorRefType {
93 VERIFY_ERROR_REF_CLASS = 0,
94 VERIFY_ERROR_REF_FIELD = 1,
95 VERIFY_ERROR_REF_METHOD = 2,
96};
97const int kVerifyErrorRefTypeShift = 6;
98
99// We don't need to store the register data for many instructions, because we either only need
100// it at branch points (for verification) or GC points and branches (for verification +
101// type-precise register analysis).
102enum RegisterTrackingMode {
103 kTrackRegsBranches,
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700104 kTrackCompilerInterestPoints,
Ian Rogers776ac1f2012-04-13 23:36:36 -0700105 kTrackRegsAll,
106};
107
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800108// A mapping from a dex pc to the register line statuses as they are immediately prior to the
109// execution of that instruction.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700110class PcToRegisterLineTable {
111 public:
112 PcToRegisterLineTable() {}
113 ~PcToRegisterLineTable() {
114 STLDeleteValues(&pc_to_register_line_);
115 }
116
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
123 RegisterLine* GetLine(size_t idx) {
124 Table::iterator result = pc_to_register_line_.find(idx); // TODO: C++0x auto
125 if (result == pc_to_register_line_.end()) {
126 return NULL;
127 } else {
128 return result->second;
129 }
130 }
131
132 private:
133 typedef SafeMap<int32_t, RegisterLine*> Table;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700134 Table pc_to_register_line_;
135};
136
137// The verifier
138class MethodVerifier {
139 public:
jeffhaof1e6b7c2012-06-05 18:33:30 -0700140 enum FailureKind {
141 kNoFailure,
142 kSoftFailure,
143 kHardFailure,
144 };
145
146 /* Verify a class. Returns "kNoFailure" on success. */
Jeff Haoee988952013-04-16 14:23:47 -0700147 static FailureKind VerifyClass(const mirror::Class* klass, std::string& error,
148 bool allow_soft_failures)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 static FailureKind VerifyClass(const DexFile* dex_file, mirror::DexCache* dex_cache,
151 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700152 std::string& error, bool allow_soft_failures)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700154
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800155 static void VerifyMethodAndDump(std::ostream& os, uint32_t method_idx, const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::DexCache* dex_cache, mirror::ClassLoader* class_loader,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800157 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 mirror::AbstractMethod* method, uint32_t method_access_flags)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
160
Ian Rogers776ac1f2012-04-13 23:36:36 -0700161 uint8_t EncodePcToReferenceMapData() const;
162
163 uint32_t DexFileVersion() const {
164 return dex_file_->GetVersion();
165 }
166
167 RegTypeCache* GetRegTypeCache() {
168 return &reg_types_;
169 }
170
Ian Rogersad0b3a32012-04-16 14:50:24 -0700171 // Log a verification failure.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700172 std::ostream& Fail(VerifyError error);
173
Ian Rogersad0b3a32012-04-16 14:50:24 -0700174 // Log for verification information.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700175 std::ostream& LogVerifyInfo() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800176 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700177 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
178 }
179
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 // Dump the failures encountered by the verifier.
181 std::ostream& DumpFailures(std::ostream& os);
182
Ian Rogers776ac1f2012-04-13 23:36:36 -0700183 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
184 // information
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700186
Ian Rogers1212a022013-03-04 10:48:41 -0800187 static const std::vector<uint8_t>* GetDexGcMap(CompilerDriver::MethodReference ref)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700188 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700189
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700190 static const CompilerDriver::MethodReference* GetDevirtMap(const CompilerDriver::MethodReference& ref,
191 uint32_t dex_pc)
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700192 LOCKS_EXCLUDED(devirt_maps_lock_);
193
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700194 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
195 // to the locks held at 'dex_pc' in 'm'.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 static void FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 std::vector<uint32_t>& monitor_enter_dex_pcs)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700199
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -0800200 static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700201 static void Shutdown();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700202
Ian Rogers1212a022013-03-04 10:48:41 -0800203 static bool IsClassRejected(CompilerDriver::ClassReference ref)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700205
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800206 bool CanLoadClasses() const {
207 return can_load_classes_;
208 }
209
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800210 MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
211 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
212 const DexFile::CodeItem* code_item,
213 uint32_t method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700214 uint32_t access_flags, bool can_load_classes, bool allow_soft_failures)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800217 // Run verification on the method. Returns true if verification completes and false if the input
218 // has an irrecoverable corruption.
219 bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
220
221 // Describe VRegs at the given dex pc.
222 std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
223
224 private:
Ian Rogersad0b3a32012-04-16 14:50:24 -0700225 // Adds the given string to the beginning of the last failure message.
226 void PrependToLastFailMessage(std::string);
227
228 // Adds the given string to the end of the last failure message.
229 void AppendToLastFailMessage(std::string);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700230
231 /*
232 * Perform verification on a single method.
233 *
234 * We do this in three passes:
235 * (1) Walk through all code units, determining instruction locations,
236 * widths, and other characteristics.
237 * (2) Walk through all code units, performing static checks on
238 * operands.
239 * (3) Iterate through the method, checking type safety and looking
240 * for code flow problems.
Ian Rogerse1758fe2012-04-19 11:31:15 -0700241 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
243 mirror::DexCache* dex_cache,
244 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
245 const DexFile::CodeItem* code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700246 mirror::AbstractMethod* method, uint32_t method_access_flags,
247 bool allow_soft_failures)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700248 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogerse1758fe2012-04-19 11:31:15 -0700249
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700251
Ian Rogers776ac1f2012-04-13 23:36:36 -0700252 /*
253 * Compute the width of the instruction at each address in the instruction stream, and store it in
254 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
255 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
256 *
257 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
258 *
259 * Performs some static checks, notably:
260 * - opcode of first instruction begins at index 0
261 * - only documented instructions may appear
262 * - each instruction follows the last
263 * - last byte of last instruction is at (code_length-1)
264 *
265 * Logs an error and returns "false" on failure.
266 */
267 bool ComputeWidthsAndCountOps();
268
269 /*
270 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
271 * "branch target" flags for exception handlers.
272 *
273 * Call this after widths have been set in "insn_flags".
274 *
275 * Returns "false" if something in the exception table looks fishy, but we're expecting the
276 * exception table to be somewhat sane.
277 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700278 bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700279
280 /*
281 * Perform static verification on all instructions in a method.
282 *
283 * Walks through instructions in a method calling VerifyInstruction on each.
284 */
285 bool VerifyInstructions();
286
287 /*
288 * Perform static verification on an instruction.
289 *
290 * As a side effect, this sets the "branch target" flags in InsnFlags.
291 *
292 * "(CF)" items are handled during code-flow analysis.
293 *
294 * v3 4.10.1
295 * - target of each jump and branch instruction must be valid
296 * - targets of switch statements must be valid
297 * - operands referencing constant pool entries must be valid
298 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
299 * - (CF) operands of method invocation instructions must be valid
300 * - (CF) only invoke-direct can call a method starting with '<'
301 * - (CF) <clinit> must never be called explicitly
302 * - operands of instanceof, checkcast, new (and variants) must be valid
303 * - new-array[-type] limited to 255 dimensions
304 * - can't use "new" on an array class
305 * - (?) limit dimensions in multi-array creation
306 * - local variable load/store register values must be in valid range
307 *
308 * v3 4.11.1.2
309 * - branches must be within the bounds of the code array
310 * - targets of all control-flow instructions are the start of an instruction
311 * - register accesses fall within range of allocated registers
312 * - (N/A) access to constant pool must be of appropriate type
313 * - code does not end in the middle of an instruction
314 * - execution cannot fall off the end of the code
315 * - (earlier) for each exception handler, the "try" area must begin and
316 * end at the start of an instruction (end can be at the end of the code)
317 * - (earlier) for each exception handler, the handler must start at a valid
318 * instruction
319 */
320 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
321
322 /* Ensure that the register index is valid for this code item. */
323 bool CheckRegisterIndex(uint32_t idx);
324
325 /* Ensure that the wide register index is valid for this code item. */
326 bool CheckWideRegisterIndex(uint32_t idx);
327
328 // Perform static checks on a field get or set instruction. All we do here is ensure that the
329 // field index is in the valid range.
330 bool CheckFieldIndex(uint32_t idx);
331
332 // Perform static checks on a method invocation instruction. All we do here is ensure that the
333 // method index is in the valid range.
334 bool CheckMethodIndex(uint32_t idx);
335
336 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
337 // reference isn't for an array class.
338 bool CheckNewInstance(uint32_t idx);
339
340 /* Ensure that the string index is in the valid range. */
341 bool CheckStringIndex(uint32_t idx);
342
343 // Perform static checks on an instruction that takes a class constant. Ensure that the class
344 // index is in the valid range.
345 bool CheckTypeIndex(uint32_t idx);
346
347 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
348 // creating an array of arrays that causes the number of dimensions to exceed 255.
349 bool CheckNewArray(uint32_t idx);
350
351 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
352 bool CheckArrayData(uint32_t cur_offset);
353
354 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
355 // into an exception handler, but it's valid to do so as long as the target isn't a
356 // "move-exception" instruction. We verify that in a later stage.
357 // The dex format forbids certain instructions from branching to themselves.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700358 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700359 bool CheckBranchTarget(uint32_t cur_offset);
360
361 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700362 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700363 bool CheckSwitchTargets(uint32_t cur_offset);
364
365 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
366 // filled-new-array.
367 // - vA holds word count (0-5), args[] have values.
368 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
369 // takes a double is done with consecutive registers. This requires parsing the target method
370 // signature, which we will be doing later on during the code flow analysis.
371 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
372
373 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
374 // or filled-new-array/range.
375 // - vA holds word count, vC holds index of first reg.
376 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
377
378 // Extract the relative offset from a branch instruction.
379 // Returns "false" on failure (e.g. this isn't a branch instruction).
380 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
381 bool* selfOkay);
382
383 /* Perform detailed code-flow analysis on a single method. */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700384 bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700385
386 // Set the register types for the first instruction in the method based on the method signature.
387 // This has the side-effect of validating the signature.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700389
390 /*
391 * Perform code flow on a method.
392 *
393 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
394 * instruction, process it (setting additional "changed" bits), and repeat until there are no
395 * more.
396 *
397 * v3 4.11.1.1
398 * - (N/A) operand stack is always the same size
399 * - operand stack [registers] contain the correct types of values
400 * - local variables [registers] contain the correct types of values
401 * - methods are invoked with the appropriate arguments
402 * - fields are assigned using values of appropriate types
403 * - opcodes have the correct type values in operand registers
404 * - there is never an uninitialized class instance in a local variable in code protected by an
405 * exception handler (operand stack is okay, because the operand stack is discarded when an
406 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
407 * register typing]
408 *
409 * v3 4.11.1.2
410 * - execution cannot fall off the end of the code
411 *
412 * (We also do many of the items described in the "static checks" sections, because it's easier to
413 * do them here.)
414 *
415 * We need an array of RegType values, one per register, for every instruction. If the method uses
416 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
417 * instruction. In theory this could become quite large -- up to several megabytes for a monster
418 * function.
419 *
420 * NOTE:
421 * The spec forbids backward branches when there's an uninitialized reference in a register. The
422 * idea is to prevent something like this:
423 * loop:
424 * move r1, r0
425 * new-instance r0, MyClass
426 * ...
427 * if-eq rN, loop // once
428 * initialize r0
429 *
430 * This leaves us with two different instances, both allocated by the same instruction, but only
431 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
432 * it by preventing backward branches. We achieve identical results without restricting code
433 * reordering by specifying that you can't execute the new-instance instruction if a register
434 * contains an uninitialized instance created by that same instruction.
435 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700436 bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700437
438 /*
439 * Perform verification for a single instruction.
440 *
441 * This requires fully decoding the instruction to determine the effect it has on registers.
442 *
443 * Finds zero or more following instructions and sets the "changed" flag if execution at that
444 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
445 * addresses. Does not set or clear any other flags in "insn_flags_".
446 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700448 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700449
450 // Perform verification of a new array instruction
451 void VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 bool is_range)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700454
455 // Perform verification of an aget instruction. The destination register's type will be set to
456 // be that of component type of the array unless the array type is unknown, in which case a
457 // bottom type inferred from the type of instruction is used. is_primitive is false for an
458 // aget-object.
459 void VerifyAGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700460 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700461
462 // Perform verification of an aput instruction.
463 void VerifyAPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700464 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700465
466 // Lookup instance field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 mirror::Field* GetInstanceField(const RegType& obj_type, int field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700468 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700469
470 // Lookup static field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 mirror::Field* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700472
473 // Perform verification of an iget or sget instruction.
474 void VerifyISGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700475 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700476 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700477
478 // Perform verification of an iput or sput instruction.
479 void VerifyISPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700480 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700482
483 // Resolves a class based on an index and performs access checks to ensure the referrer can
484 // access the resolved class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700485 const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700486 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700487
488 /*
489 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
490 * address, determine the Join of all exceptions that can land here. Fails if no matching
491 * exception handler can be found or if the Join of exception types fails.
492 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 const RegType& GetCaughtExceptionType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700495
496 /*
497 * Resolves a method based on an index and performs access checks to ensure
498 * the referrer can access the resolved method.
499 * Does not throw exceptions.
500 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501 mirror::AbstractMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700503
504 /*
505 * Verify the arguments to a method. We're executing in "method", making
506 * a call to the method reference in vB.
507 *
508 * If this is a "direct" invoke, we allow calls to <init>. For calls to
509 * <init>, the first argument may be an uninitialized reference. Otherwise,
510 * calls to anything starting with '<' will be rejected, as will any
511 * uninitialized reference arguments.
512 *
513 * For non-static method calls, this will verify that the method call is
514 * appropriate for the "this" argument.
515 *
516 * The method reference is in vBBBB. The "is_range" parameter determines
517 * whether we use 0-4 "args" values or a range of registers defined by
518 * vAA and vCCCC.
519 *
520 * Widening conversions on integers and references are allowed, but
521 * narrowing conversions are not.
522 *
523 * Returns the resolved method on success, NULL on failure (with *failure
524 * set appropriately).
525 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526 mirror::AbstractMethod* VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700527 MethodType method_type, bool is_range, bool is_super)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700529
530 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700531 * Verify that the target instruction is not "move-exception". It's important that the only way
532 * to execute a move-exception is as the first instruction of an exception handler.
533 * Returns "true" if all is well, "false" if the target instruction is move-exception.
534 */
535 bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
536
537 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700538 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
539 * next_insn, and set the changed flag on the target address if any of the registers were changed.
540 * Returns "false" if an error is encountered.
541 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700542 bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700544
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545 // Is the method being verified a constructor?
546 bool IsConstructor() const {
547 return (method_access_flags_ & kAccConstructor) != 0;
548 }
549
550 // Is the method verified static?
551 bool IsStatic() const {
552 return (method_access_flags_ & kAccStatic) != 0;
553 }
554
555 // Return the register type for the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700556 const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700557
558 // Get a type representing the declaring class of the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700559 const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560
Ian Rogers776ac1f2012-04-13 23:36:36 -0700561 /*
562 * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
563 * verification). For type-precise determination we have all the data we need, so we just need to
564 * encode it in some clever fashion.
565 * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
566 */
567 const std::vector<uint8_t>* GenerateGcMap();
568
569 // Verify that the GC map associated with method_ is well formed
570 void VerifyGcMap(const std::vector<uint8_t>& data);
571
572 // Compute sizes for GC map data
573 void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
574
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800575 InstructionFlags* CurrentInsnFlags();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576
577 // All the GC maps that the verifier has created
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700578 typedef SafeMap<const CompilerDriver::MethodReference, const std::vector<uint8_t>*,
579 CompilerDriver::MethodReferenceComparator> DexGcMapTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700580 static Mutex* dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
581 static DexGcMapTable* dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
Ian Rogers1212a022013-03-04 10:48:41 -0800582 static void SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& dex_gc_map)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700583 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700584
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700585
586 // Devirtualization map.
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700587 typedef SafeMap<const uint32_t, CompilerDriver::MethodReference> PcToConcreteMethod;
588 typedef SafeMap<const CompilerDriver::MethodReference, const PcToConcreteMethod*,
Ian Rogerse3cd2f02013-05-24 15:32:56 -0700589 CompilerDriver::MethodReferenceComparator> DevirtualizationMapTable;
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700590 MethodVerifier::PcToConcreteMethod* GenerateDevirtMap()
Ian Rogers33e95662013-05-20 20:29:14 -0700591 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700592
593 static Mutex* devirt_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
594 static DevirtualizationMapTable* devirt_maps_ GUARDED_BY(devirt_maps_lock_);
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700595 static void SetDevirtMap(CompilerDriver::MethodReference ref,
596 const PcToConcreteMethod* pc_method_map)
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700597 LOCKS_EXCLUDED(devirt_maps_lock_);
Ian Rogers1212a022013-03-04 10:48:41 -0800598 typedef std::set<CompilerDriver::ClassReference> RejectedClassesTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700599 static Mutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700600 static RejectedClassesTable* rejected_classes_;
601
Ian Rogers1212a022013-03-04 10:48:41 -0800602 static void AddRejectedClass(CompilerDriver::ClassReference ref)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700604
605 RegTypeCache reg_types_;
606
607 PcToRegisterLineTable reg_table_;
608
609 // Storage for the register status we're currently working on.
610 UniquePtr<RegisterLine> work_line_;
611
612 // The address of the instruction we're currently working on, note that this is in 2 byte
613 // quantities
614 uint32_t work_insn_idx_;
615
616 // Storage for the register status we're saving for later.
617 UniquePtr<RegisterLine> saved_line_;
618
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800619 uint32_t dex_method_idx_; // The method we're working on.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 // Its object representation if known.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 mirror::AbstractMethod* foo_method_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622 uint32_t method_access_flags_; // Method's access flags.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700623 const DexFile* dex_file_; // The dex file containing the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700624 // The dex_cache for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 mirror::DexCache* dex_cache_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700626 // The class loader for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 mirror::ClassLoader* class_loader_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700628 uint32_t class_def_idx_; // The class def index of the declaring class of the method.
629 const DexFile::CodeItem* code_item_; // The code item containing the code for the method.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800630 // Instruction widths and flags, one entry per code unit.
631 UniquePtr<InstructionFlags[]> insn_flags_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700632
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700633 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
634 uint32_t interesting_dex_pc_;
635 // The container into which FindLocksAtDexPc should write the registers containing held locks,
636 // NULL if we're not doing FindLocksAtDexPc.
637 std::vector<uint32_t>* monitor_enter_dex_pcs_;
638
Ian Rogersad0b3a32012-04-16 14:50:24 -0700639 // The types of any error that occurs.
640 std::vector<VerifyError> failures_;
641 // Error messages associated with failures.
642 std::vector<std::ostringstream*> failure_messages_;
643 // Is there a pending hard failure?
644 bool have_pending_hard_failure_;
jeffhaofaf459e2012-08-31 15:32:47 -0700645 // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
646 // would fail at runtime throwing an exception. Such an instruction causes the following code
647 // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
648 // instructions that would hard fail the verification.
649 bool have_pending_runtime_throw_failure_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700650
Ian Rogersad0b3a32012-04-16 14:50:24 -0700651 // Info message log use primarily for verifier diagnostics.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700652 std::ostringstream info_messages_;
653
654 // The number of occurrences of specific opcodes.
655 size_t new_instance_count_;
656 size_t monitor_enter_count_;
Elliott Hughes80537bb2013-01-04 16:37:26 -0800657
658 const bool can_load_classes_;
Jeff Haoee988952013-04-16 14:23:47 -0700659
660 // Converts soft failures to hard failures when false. Only false when the compiler isn't
661 // running and the verifier is called from the class linker.
662 const bool allow_soft_failures_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663};
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700664std::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665
666} // namespace verifier
667} // namespace art
668
669#endif // ART_SRC_VERIFIER_METHOD_VERIFIER_H_