blob: 49bc808f0df27a200eed1f96d7057613466dfb76 [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"
32#include "reg_type_cache.h"
33#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,
104 kTrackRegsGcPoints,
105 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. */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 static FailureKind VerifyClass(const mirror::Class* klass, std::string& error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 static FailureKind VerifyClass(const DexFile* dex_file, mirror::DexCache* dex_cache,
150 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 std::string& error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700153
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800154 static void VerifyMethodAndDump(std::ostream& os, uint32_t method_idx, const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 mirror::DexCache* dex_cache, mirror::ClassLoader* class_loader,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800156 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 mirror::AbstractMethod* method, uint32_t method_access_flags)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800158 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159
Ian Rogers776ac1f2012-04-13 23:36:36 -0700160 uint8_t EncodePcToReferenceMapData() const;
161
162 uint32_t DexFileVersion() const {
163 return dex_file_->GetVersion();
164 }
165
166 RegTypeCache* GetRegTypeCache() {
167 return &reg_types_;
168 }
169
Ian Rogersad0b3a32012-04-16 14:50:24 -0700170 // Log a verification failure.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700171 std::ostream& Fail(VerifyError error);
172
Ian Rogersad0b3a32012-04-16 14:50:24 -0700173 // Log for verification information.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700174 std::ostream& LogVerifyInfo() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800175 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700176 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
177 }
178
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 // Dump the failures encountered by the verifier.
180 std::ostream& DumpFailures(std::ostream& os);
181
Ian Rogers776ac1f2012-04-13 23:36:36 -0700182 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
183 // information
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700185
Ian Rogers1212a022013-03-04 10:48:41 -0800186 static const std::vector<uint8_t>* GetDexGcMap(CompilerDriver::MethodReference ref)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700187 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700188
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700189 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
190 // to the locks held at 'dex_pc' in 'm'.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 static void FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192 std::vector<uint32_t>& monitor_enter_dex_pcs)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700194
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700195 static void Init();
196 static void Shutdown();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700197
Ian Rogers1212a022013-03-04 10:48:41 -0800198 static bool IsClassRejected(CompilerDriver::ClassReference ref)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700200
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800201 bool CanLoadClasses() const {
202 return can_load_classes_;
203 }
204
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800205 MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
206 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
207 const DexFile::CodeItem* code_item,
208 uint32_t method_idx, mirror::AbstractMethod* method,
209 uint32_t access_flags, bool can_load_classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800212 // Run verification on the method. Returns true if verification completes and false if the input
213 // has an irrecoverable corruption.
214 bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
215
216 // Describe VRegs at the given dex pc.
217 std::vector<int32_t> DescribeVRegs(uint32_t dex_pc);
218
219 private:
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Adds the given string to the beginning of the last failure message.
221 void PrependToLastFailMessage(std::string);
222
223 // Adds the given string to the end of the last failure message.
224 void AppendToLastFailMessage(std::string);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700225
226 /*
227 * Perform verification on a single method.
228 *
229 * We do this in three passes:
230 * (1) Walk through all code units, determining instruction locations,
231 * widths, and other characteristics.
232 * (2) Walk through all code units, performing static checks on
233 * operands.
234 * (3) Iterate through the method, checking type safety and looking
235 * for code flow problems.
Ian Rogerse1758fe2012-04-19 11:31:15 -0700236 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
238 mirror::DexCache* dex_cache,
239 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
240 const DexFile::CodeItem* code_item,
241 mirror::AbstractMethod* method, uint32_t method_access_flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogerse1758fe2012-04-19 11:31:15 -0700243
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700245
Ian Rogers776ac1f2012-04-13 23:36:36 -0700246 /*
247 * Compute the width of the instruction at each address in the instruction stream, and store it in
248 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
249 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
250 *
251 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
252 *
253 * Performs some static checks, notably:
254 * - opcode of first instruction begins at index 0
255 * - only documented instructions may appear
256 * - each instruction follows the last
257 * - last byte of last instruction is at (code_length-1)
258 *
259 * Logs an error and returns "false" on failure.
260 */
261 bool ComputeWidthsAndCountOps();
262
263 /*
264 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
265 * "branch target" flags for exception handlers.
266 *
267 * Call this after widths have been set in "insn_flags".
268 *
269 * Returns "false" if something in the exception table looks fishy, but we're expecting the
270 * exception table to be somewhat sane.
271 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700272 bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700273
274 /*
275 * Perform static verification on all instructions in a method.
276 *
277 * Walks through instructions in a method calling VerifyInstruction on each.
278 */
279 bool VerifyInstructions();
280
281 /*
282 * Perform static verification on an instruction.
283 *
284 * As a side effect, this sets the "branch target" flags in InsnFlags.
285 *
286 * "(CF)" items are handled during code-flow analysis.
287 *
288 * v3 4.10.1
289 * - target of each jump and branch instruction must be valid
290 * - targets of switch statements must be valid
291 * - operands referencing constant pool entries must be valid
292 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
293 * - (CF) operands of method invocation instructions must be valid
294 * - (CF) only invoke-direct can call a method starting with '<'
295 * - (CF) <clinit> must never be called explicitly
296 * - operands of instanceof, checkcast, new (and variants) must be valid
297 * - new-array[-type] limited to 255 dimensions
298 * - can't use "new" on an array class
299 * - (?) limit dimensions in multi-array creation
300 * - local variable load/store register values must be in valid range
301 *
302 * v3 4.11.1.2
303 * - branches must be within the bounds of the code array
304 * - targets of all control-flow instructions are the start of an instruction
305 * - register accesses fall within range of allocated registers
306 * - (N/A) access to constant pool must be of appropriate type
307 * - code does not end in the middle of an instruction
308 * - execution cannot fall off the end of the code
309 * - (earlier) for each exception handler, the "try" area must begin and
310 * end at the start of an instruction (end can be at the end of the code)
311 * - (earlier) for each exception handler, the handler must start at a valid
312 * instruction
313 */
314 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
315
316 /* Ensure that the register index is valid for this code item. */
317 bool CheckRegisterIndex(uint32_t idx);
318
319 /* Ensure that the wide register index is valid for this code item. */
320 bool CheckWideRegisterIndex(uint32_t idx);
321
322 // Perform static checks on a field get or set instruction. All we do here is ensure that the
323 // field index is in the valid range.
324 bool CheckFieldIndex(uint32_t idx);
325
326 // Perform static checks on a method invocation instruction. All we do here is ensure that the
327 // method index is in the valid range.
328 bool CheckMethodIndex(uint32_t idx);
329
330 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
331 // reference isn't for an array class.
332 bool CheckNewInstance(uint32_t idx);
333
334 /* Ensure that the string index is in the valid range. */
335 bool CheckStringIndex(uint32_t idx);
336
337 // Perform static checks on an instruction that takes a class constant. Ensure that the class
338 // index is in the valid range.
339 bool CheckTypeIndex(uint32_t idx);
340
341 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
342 // creating an array of arrays that causes the number of dimensions to exceed 255.
343 bool CheckNewArray(uint32_t idx);
344
345 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
346 bool CheckArrayData(uint32_t cur_offset);
347
348 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
349 // into an exception handler, but it's valid to do so as long as the target isn't a
350 // "move-exception" instruction. We verify that in a later stage.
351 // The dex format forbids certain instructions from branching to themselves.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700352 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700353 bool CheckBranchTarget(uint32_t cur_offset);
354
355 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700356 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700357 bool CheckSwitchTargets(uint32_t cur_offset);
358
359 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
360 // filled-new-array.
361 // - vA holds word count (0-5), args[] have values.
362 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
363 // takes a double is done with consecutive registers. This requires parsing the target method
364 // signature, which we will be doing later on during the code flow analysis.
365 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
366
367 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
368 // or filled-new-array/range.
369 // - vA holds word count, vC holds index of first reg.
370 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
371
372 // Extract the relative offset from a branch instruction.
373 // Returns "false" on failure (e.g. this isn't a branch instruction).
374 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
375 bool* selfOkay);
376
377 /* Perform detailed code-flow analysis on a single method. */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700379
380 // Set the register types for the first instruction in the method based on the method signature.
381 // This has the side-effect of validating the signature.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700382 bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700383
384 /*
385 * Perform code flow on a method.
386 *
387 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
388 * instruction, process it (setting additional "changed" bits), and repeat until there are no
389 * more.
390 *
391 * v3 4.11.1.1
392 * - (N/A) operand stack is always the same size
393 * - operand stack [registers] contain the correct types of values
394 * - local variables [registers] contain the correct types of values
395 * - methods are invoked with the appropriate arguments
396 * - fields are assigned using values of appropriate types
397 * - opcodes have the correct type values in operand registers
398 * - there is never an uninitialized class instance in a local variable in code protected by an
399 * exception handler (operand stack is okay, because the operand stack is discarded when an
400 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
401 * register typing]
402 *
403 * v3 4.11.1.2
404 * - execution cannot fall off the end of the code
405 *
406 * (We also do many of the items described in the "static checks" sections, because it's easier to
407 * do them here.)
408 *
409 * We need an array of RegType values, one per register, for every instruction. If the method uses
410 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
411 * instruction. In theory this could become quite large -- up to several megabytes for a monster
412 * function.
413 *
414 * NOTE:
415 * The spec forbids backward branches when there's an uninitialized reference in a register. The
416 * idea is to prevent something like this:
417 * loop:
418 * move r1, r0
419 * new-instance r0, MyClass
420 * ...
421 * if-eq rN, loop // once
422 * initialize r0
423 *
424 * This leaves us with two different instances, both allocated by the same instruction, but only
425 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
426 * it by preventing backward branches. We achieve identical results without restricting code
427 * reordering by specifying that you can't execute the new-instance instruction if a register
428 * contains an uninitialized instance created by that same instruction.
429 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700431
432 /*
433 * Perform verification for a single instruction.
434 *
435 * This requires fully decoding the instruction to determine the effect it has on registers.
436 *
437 * Finds zero or more following instructions and sets the "changed" flag if execution at that
438 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
439 * addresses. Does not set or clear any other flags in "insn_flags_".
440 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700441 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700443
444 // Perform verification of a new array instruction
445 void VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 bool is_range)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700447 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700448
449 // Perform verification of an aget instruction. The destination register's type will be set to
450 // be that of component type of the array unless the array type is unknown, in which case a
451 // bottom type inferred from the type of instruction is used. is_primitive is false for an
452 // aget-object.
453 void VerifyAGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700455
456 // Perform verification of an aput instruction.
457 void VerifyAPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700459
460 // Lookup instance field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 mirror::Field* GetInstanceField(const RegType& obj_type, int field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700462 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700463
464 // Lookup static field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 mirror::Field* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700466
467 // Perform verification of an iget or sget instruction.
468 void VerifyISGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700469 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700470 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700471
472 // Perform verification of an iput or sput instruction.
473 void VerifyISPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700476
477 // Resolves a class based on an index and performs access checks to ensure the referrer can
478 // access the resolved class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700479 const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700481
482 /*
483 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
484 * address, determine the Join of all exceptions that can land here. Fails if no matching
485 * exception handler can be found or if the Join of exception types fails.
486 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700487 const RegType& GetCaughtExceptionType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489
490 /*
491 * Resolves a method based on an index and performs access checks to ensure
492 * the referrer can access the resolved method.
493 * Does not throw exceptions.
494 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 mirror::AbstractMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700497
498 /*
499 * Verify the arguments to a method. We're executing in "method", making
500 * a call to the method reference in vB.
501 *
502 * If this is a "direct" invoke, we allow calls to <init>. For calls to
503 * <init>, the first argument may be an uninitialized reference. Otherwise,
504 * calls to anything starting with '<' will be rejected, as will any
505 * uninitialized reference arguments.
506 *
507 * For non-static method calls, this will verify that the method call is
508 * appropriate for the "this" argument.
509 *
510 * The method reference is in vBBBB. The "is_range" parameter determines
511 * whether we use 0-4 "args" values or a range of registers defined by
512 * vAA and vCCCC.
513 *
514 * Widening conversions on integers and references are allowed, but
515 * narrowing conversions are not.
516 *
517 * Returns the resolved method on success, NULL on failure (with *failure
518 * set appropriately).
519 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 mirror::AbstractMethod* VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700521 MethodType method_type, bool is_range, bool is_super)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700522 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700523
524 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700525 * Verify that the target instruction is not "move-exception". It's important that the only way
526 * to execute a move-exception is as the first instruction of an exception handler.
527 * Returns "true" if all is well, "false" if the target instruction is move-exception.
528 */
529 bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
530
531 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700532 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
533 * next_insn, and set the changed flag on the target address if any of the registers were changed.
534 * Returns "false" if an error is encountered.
535 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700536 bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700537 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700538
Ian Rogersad0b3a32012-04-16 14:50:24 -0700539 // Is the method being verified a constructor?
540 bool IsConstructor() const {
541 return (method_access_flags_ & kAccConstructor) != 0;
542 }
543
544 // Is the method verified static?
545 bool IsStatic() const {
546 return (method_access_flags_ & kAccStatic) != 0;
547 }
548
549 // Return the register type for the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700550 const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700551
552 // Get a type representing the declaring class of the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700553 const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700554
Ian Rogers776ac1f2012-04-13 23:36:36 -0700555 /*
556 * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
557 * verification). For type-precise determination we have all the data we need, so we just need to
558 * encode it in some clever fashion.
559 * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
560 */
561 const std::vector<uint8_t>* GenerateGcMap();
562
563 // Verify that the GC map associated with method_ is well formed
564 void VerifyGcMap(const std::vector<uint8_t>& data);
565
566 // Compute sizes for GC map data
567 void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
568
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800569 InstructionFlags* CurrentInsnFlags();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700570
571 // All the GC maps that the verifier has created
Ian Rogers1212a022013-03-04 10:48:41 -0800572 typedef SafeMap<const CompilerDriver::MethodReference, const std::vector<uint8_t>*> DexGcMapTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700573 static Mutex* dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
574 static DexGcMapTable* dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
Ian Rogers1212a022013-03-04 10:48:41 -0800575 static void SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& dex_gc_map)
Ian Rogers0c7abda2012-09-19 13:33:42 -0700576 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700577
Ian Rogers1212a022013-03-04 10:48:41 -0800578 typedef std::set<CompilerDriver::ClassReference> RejectedClassesTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700579 static Mutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700580 static RejectedClassesTable* rejected_classes_;
581
Ian Rogers1212a022013-03-04 10:48:41 -0800582 static void AddRejectedClass(CompilerDriver::ClassReference ref)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700584
585 RegTypeCache reg_types_;
586
587 PcToRegisterLineTable reg_table_;
588
589 // Storage for the register status we're currently working on.
590 UniquePtr<RegisterLine> work_line_;
591
592 // The address of the instruction we're currently working on, note that this is in 2 byte
593 // quantities
594 uint32_t work_insn_idx_;
595
596 // Storage for the register status we're saving for later.
597 UniquePtr<RegisterLine> saved_line_;
598
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800599 uint32_t dex_method_idx_; // The method we're working on.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700600 // Its object representation if known.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 mirror::AbstractMethod* foo_method_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700602 uint32_t method_access_flags_; // Method's access flags.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700603 const DexFile* dex_file_; // The dex file containing the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604 // The dex_cache for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 mirror::DexCache* dex_cache_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700606 // The class loader for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800607 mirror::ClassLoader* class_loader_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608 uint32_t class_def_idx_; // The class def index of the declaring class of the method.
609 const DexFile::CodeItem* code_item_; // The code item containing the code for the method.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800610 // Instruction widths and flags, one entry per code unit.
611 UniquePtr<InstructionFlags[]> insn_flags_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700612
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700613 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
614 uint32_t interesting_dex_pc_;
615 // The container into which FindLocksAtDexPc should write the registers containing held locks,
616 // NULL if we're not doing FindLocksAtDexPc.
617 std::vector<uint32_t>* monitor_enter_dex_pcs_;
618
Ian Rogersad0b3a32012-04-16 14:50:24 -0700619 // The types of any error that occurs.
620 std::vector<VerifyError> failures_;
621 // Error messages associated with failures.
622 std::vector<std::ostringstream*> failure_messages_;
623 // Is there a pending hard failure?
624 bool have_pending_hard_failure_;
jeffhaofaf459e2012-08-31 15:32:47 -0700625 // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
626 // would fail at runtime throwing an exception. Such an instruction causes the following code
627 // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
628 // instructions that would hard fail the verification.
629 bool have_pending_runtime_throw_failure_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700630
Ian Rogersad0b3a32012-04-16 14:50:24 -0700631 // Info message log use primarily for verifier diagnostics.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700632 std::ostringstream info_messages_;
633
634 // The number of occurrences of specific opcodes.
635 size_t new_instance_count_;
636 size_t monitor_enter_count_;
Elliott Hughes80537bb2013-01-04 16:37:26 -0800637
638 const bool can_load_classes_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700639};
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700640std::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700641
642} // namespace verifier
643} // namespace art
644
645#endif // ART_SRC_VERIFIER_METHOD_VERIFIER_H_