blob: a36a1f93dd047d89fd5e8629e8347ba7dad441a1 [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
20#include <deque>
21#include <limits>
22#include <set>
23#include <vector>
24
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/casts.h"
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070028#include "compiler.h"
29#include "dex_file.h"
30#include "dex_instruction.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070032#include "reg_type.h"
33#include "reg_type_cache.h"
34#include "register_line.h"
35#include "safe_map.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070036#include "UniquePtr.h"
37
38namespace art {
39
40struct ReferenceMap2Visitor;
41
buzbeec531cef2012-10-18 07:09:20 -070042#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070043namespace greenland {
Ian Rogers776ac1f2012-04-13 23:36:36 -070044 class InferredRegCategoryMap;
TDYa12789f96052012-07-12 20:49:53 -070045} // namespace greenland
Ian Rogers776ac1f2012-04-13 23:36:36 -070046#endif
47
48namespace verifier {
49
50class MethodVerifier;
51class InsnFlags;
Ian Rogers46c6bb22012-09-18 13:47:36 -070052class DexPcToReferenceMap;
Ian Rogers776ac1f2012-04-13 23:36:36 -070053
54/*
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
56 * method determines which list we search, and whether we travel up into superclasses.
57 *
58 * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
59 * All others are stored in the "virtual" list.)
60 */
61enum MethodType {
62 METHOD_UNKNOWN = 0,
63 METHOD_DIRECT, // <init>, private
64 METHOD_STATIC, // static
65 METHOD_VIRTUAL, // virtual, super
66 METHOD_INTERFACE // interface
67};
Ian Rogers2fc14272012-08-30 10:56:57 -070068std::ostream& operator<<(std::ostream& os, const MethodType& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -070069
70/*
71 * An enumeration of problems that can turn up during verification.
72 * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
73 * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
74 * that can potentially be corrected, and the verifier will try again at runtime.
75 * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
76 * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
77 * to be rewritten to fail at runtime.
78 */
79enum VerifyError {
Ian Rogers776ac1f2012-04-13 23:36:36 -070080 VERIFY_ERROR_BAD_CLASS_HARD, // VerifyError; hard error that skips compilation.
81 VERIFY_ERROR_BAD_CLASS_SOFT, // VerifyError; soft error that verifies again at runtime.
82
83 VERIFY_ERROR_NO_CLASS, // NoClassDefFoundError.
84 VERIFY_ERROR_NO_FIELD, // NoSuchFieldError.
85 VERIFY_ERROR_NO_METHOD, // NoSuchMethodError.
86 VERIFY_ERROR_ACCESS_CLASS, // IllegalAccessError.
87 VERIFY_ERROR_ACCESS_FIELD, // IllegalAccessError.
88 VERIFY_ERROR_ACCESS_METHOD, // IllegalAccessError.
89 VERIFY_ERROR_CLASS_CHANGE, // IncompatibleClassChangeError.
90 VERIFY_ERROR_INSTANTIATION, // InstantiationError.
91};
92std::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
93
94/*
95 * Identifies the type of reference in the instruction that generated the verify error
96 * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference).
97 *
98 * This must fit in two bits.
99 */
100enum VerifyErrorRefType {
101 VERIFY_ERROR_REF_CLASS = 0,
102 VERIFY_ERROR_REF_FIELD = 1,
103 VERIFY_ERROR_REF_METHOD = 2,
104};
105const int kVerifyErrorRefTypeShift = 6;
106
107// We don't need to store the register data for many instructions, because we either only need
108// it at branch points (for verification) or GC points and branches (for verification +
109// type-precise register analysis).
110enum RegisterTrackingMode {
111 kTrackRegsBranches,
112 kTrackRegsGcPoints,
113 kTrackRegsAll,
114};
115
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800116// A mapping from a dex pc to the register line statuses as they are immediately prior to the
117// execution of that instruction.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700118class PcToRegisterLineTable {
119 public:
120 PcToRegisterLineTable() {}
121 ~PcToRegisterLineTable() {
122 STLDeleteValues(&pc_to_register_line_);
123 }
124
125 // Initialize the RegisterTable. Every instruction address can have a different set of information
126 // about what's in which register, but for verification purposes we only need to store it at
127 // branch target addresses (because we merge into that).
128 void Init(RegisterTrackingMode mode, InsnFlags* flags, uint32_t insns_size,
129 uint16_t registers_size, MethodVerifier* verifier);
130
131 RegisterLine* GetLine(size_t idx) {
132 Table::iterator result = pc_to_register_line_.find(idx); // TODO: C++0x auto
133 if (result == pc_to_register_line_.end()) {
134 return NULL;
135 } else {
136 return result->second;
137 }
138 }
139
140 private:
141 typedef SafeMap<int32_t, RegisterLine*> Table;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700142 Table pc_to_register_line_;
143};
144
145// The verifier
146class MethodVerifier {
buzbeec531cef2012-10-18 07:09:20 -0700147#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -0700148 typedef greenland::InferredRegCategoryMap InferredRegCategoryMap;
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700149#endif
Elliott Hughesa21039c2012-06-21 12:09:25 -0700150
Ian Rogers776ac1f2012-04-13 23:36:36 -0700151 public:
jeffhaof1e6b7c2012-06-05 18:33:30 -0700152 enum FailureKind {
153 kNoFailure,
154 kSoftFailure,
155 kHardFailure,
156 };
157
158 /* Verify a class. Returns "kNoFailure" on success. */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 static FailureKind VerifyClass(const mirror::Class* klass, std::string& error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 static FailureKind VerifyClass(const DexFile* dex_file, mirror::DexCache* dex_cache,
162 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 std::string& error)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700164 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700165
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800166 static void VerifyMethodAndDump(std::ostream& os, uint32_t method_idx, const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 mirror::DexCache* dex_cache, mirror::ClassLoader* class_loader,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800168 uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 mirror::AbstractMethod* method, uint32_t method_access_flags)
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
171
172 static std::vector<int32_t> DescribeVRegs(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 const DexFile* dex_file, mirror::DexCache* dex_cache,
174 mirror::ClassLoader* class_loader,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800175 uint32_t class_def_idx,
176 const DexFile::CodeItem* code_item,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800178 uint32_t method_access_flags, uint32_t dex_pc)
Ian Rogers08254272012-10-23 17:49:23 -0700179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
180
Ian Rogers776ac1f2012-04-13 23:36:36 -0700181 uint8_t EncodePcToReferenceMapData() const;
182
183 uint32_t DexFileVersion() const {
184 return dex_file_->GetVersion();
185 }
186
187 RegTypeCache* GetRegTypeCache() {
188 return &reg_types_;
189 }
190
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 // Log a verification failure.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700192 std::ostream& Fail(VerifyError error);
193
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 // Log for verification information.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700195 std::ostream& LogVerifyInfo() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800196 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700197 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
198 }
199
Ian Rogersad0b3a32012-04-16 14:50:24 -0700200 // Dump the failures encountered by the verifier.
201 std::ostream& DumpFailures(std::ostream& os);
202
Ian Rogers776ac1f2012-04-13 23:36:36 -0700203 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
204 // information
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700206
Ian Rogers0c7abda2012-09-19 13:33:42 -0700207 static const std::vector<uint8_t>* GetDexGcMap(Compiler::MethodReference ref)
208 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700209
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700210 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
211 // to the locks held at 'dex_pc' in 'm'.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 static void FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 std::vector<uint32_t>& monitor_enter_dex_pcs)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700215
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700216 static void Init();
217 static void Shutdown();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700218
buzbeec531cef2012-10-18 07:09:20 -0700219#if defined(ART_USE_LLVM_COMPILER)
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700220 static const InferredRegCategoryMap* GetInferredRegCategoryMap(Compiler::MethodReference ref)
221 LOCKS_EXCLUDED(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700222#endif
223
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 static bool IsClassRejected(Compiler::ClassReference ref)
225 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700226
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800227 bool CanLoadClasses() const {
228 return can_load_classes_;
229 }
230
Ian Rogers776ac1f2012-04-13 23:36:36 -0700231 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 explicit MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
233 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
234 const DexFile::CodeItem* code_item,
235 uint32_t method_idx, mirror::AbstractMethod* method, uint32_t access_flags,
236 bool can_load_classes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700238
239 // Adds the given string to the beginning of the last failure message.
240 void PrependToLastFailMessage(std::string);
241
242 // Adds the given string to the end of the last failure message.
243 void AppendToLastFailMessage(std::string);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700244
245 /*
246 * Perform verification on a single method.
247 *
248 * We do this in three passes:
249 * (1) Walk through all code units, determining instruction locations,
250 * widths, and other characteristics.
251 * (2) Walk through all code units, performing static checks on
252 * operands.
253 * (3) Iterate through the method, checking type safety and looking
254 * for code flow problems.
Ian Rogerse1758fe2012-04-19 11:31:15 -0700255 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file,
257 mirror::DexCache* dex_cache,
258 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
259 const DexFile::CodeItem* code_item,
260 mirror::AbstractMethod* method, uint32_t method_access_flags)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogerse1758fe2012-04-19 11:31:15 -0700262
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263 // Run verification on the method. Returns true if verification completes and false if the input
264 // has an irrecoverable corruption.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700266
Ian Rogersb726dcb2012-09-05 08:57:23 -0700267 void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700268
Ian Rogers776ac1f2012-04-13 23:36:36 -0700269 /*
270 * Compute the width of the instruction at each address in the instruction stream, and store it in
271 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
272 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
273 *
274 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
275 *
276 * Performs some static checks, notably:
277 * - opcode of first instruction begins at index 0
278 * - only documented instructions may appear
279 * - each instruction follows the last
280 * - last byte of last instruction is at (code_length-1)
281 *
282 * Logs an error and returns "false" on failure.
283 */
284 bool ComputeWidthsAndCountOps();
285
286 /*
287 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
288 * "branch target" flags for exception handlers.
289 *
290 * Call this after widths have been set in "insn_flags".
291 *
292 * Returns "false" if something in the exception table looks fishy, but we're expecting the
293 * exception table to be somewhat sane.
294 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700295 bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700296
297 /*
298 * Perform static verification on all instructions in a method.
299 *
300 * Walks through instructions in a method calling VerifyInstruction on each.
301 */
302 bool VerifyInstructions();
303
304 /*
305 * Perform static verification on an instruction.
306 *
307 * As a side effect, this sets the "branch target" flags in InsnFlags.
308 *
309 * "(CF)" items are handled during code-flow analysis.
310 *
311 * v3 4.10.1
312 * - target of each jump and branch instruction must be valid
313 * - targets of switch statements must be valid
314 * - operands referencing constant pool entries must be valid
315 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
316 * - (CF) operands of method invocation instructions must be valid
317 * - (CF) only invoke-direct can call a method starting with '<'
318 * - (CF) <clinit> must never be called explicitly
319 * - operands of instanceof, checkcast, new (and variants) must be valid
320 * - new-array[-type] limited to 255 dimensions
321 * - can't use "new" on an array class
322 * - (?) limit dimensions in multi-array creation
323 * - local variable load/store register values must be in valid range
324 *
325 * v3 4.11.1.2
326 * - branches must be within the bounds of the code array
327 * - targets of all control-flow instructions are the start of an instruction
328 * - register accesses fall within range of allocated registers
329 * - (N/A) access to constant pool must be of appropriate type
330 * - code does not end in the middle of an instruction
331 * - execution cannot fall off the end of the code
332 * - (earlier) for each exception handler, the "try" area must begin and
333 * end at the start of an instruction (end can be at the end of the code)
334 * - (earlier) for each exception handler, the handler must start at a valid
335 * instruction
336 */
337 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
338
339 /* Ensure that the register index is valid for this code item. */
340 bool CheckRegisterIndex(uint32_t idx);
341
342 /* Ensure that the wide register index is valid for this code item. */
343 bool CheckWideRegisterIndex(uint32_t idx);
344
345 // Perform static checks on a field get or set instruction. All we do here is ensure that the
346 // field index is in the valid range.
347 bool CheckFieldIndex(uint32_t idx);
348
349 // Perform static checks on a method invocation instruction. All we do here is ensure that the
350 // method index is in the valid range.
351 bool CheckMethodIndex(uint32_t idx);
352
353 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
354 // reference isn't for an array class.
355 bool CheckNewInstance(uint32_t idx);
356
357 /* Ensure that the string index is in the valid range. */
358 bool CheckStringIndex(uint32_t idx);
359
360 // Perform static checks on an instruction that takes a class constant. Ensure that the class
361 // index is in the valid range.
362 bool CheckTypeIndex(uint32_t idx);
363
364 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
365 // creating an array of arrays that causes the number of dimensions to exceed 255.
366 bool CheckNewArray(uint32_t idx);
367
368 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
369 bool CheckArrayData(uint32_t cur_offset);
370
371 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
372 // into an exception handler, but it's valid to do so as long as the target isn't a
373 // "move-exception" instruction. We verify that in a later stage.
374 // The dex format forbids certain instructions from branching to themselves.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700375 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700376 bool CheckBranchTarget(uint32_t cur_offset);
377
378 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700379 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700380 bool CheckSwitchTargets(uint32_t cur_offset);
381
382 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
383 // filled-new-array.
384 // - vA holds word count (0-5), args[] have values.
385 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
386 // takes a double is done with consecutive registers. This requires parsing the target method
387 // signature, which we will be doing later on during the code flow analysis.
388 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
389
390 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
391 // or filled-new-array/range.
392 // - vA holds word count, vC holds index of first reg.
393 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
394
395 // Extract the relative offset from a branch instruction.
396 // Returns "false" on failure (e.g. this isn't a branch instruction).
397 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
398 bool* selfOkay);
399
400 /* Perform detailed code-flow analysis on a single method. */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700401 bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700402
403 // Set the register types for the first instruction in the method based on the method signature.
404 // This has the side-effect of validating the signature.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700405 bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700406
407 /*
408 * Perform code flow on a method.
409 *
410 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
411 * instruction, process it (setting additional "changed" bits), and repeat until there are no
412 * more.
413 *
414 * v3 4.11.1.1
415 * - (N/A) operand stack is always the same size
416 * - operand stack [registers] contain the correct types of values
417 * - local variables [registers] contain the correct types of values
418 * - methods are invoked with the appropriate arguments
419 * - fields are assigned using values of appropriate types
420 * - opcodes have the correct type values in operand registers
421 * - there is never an uninitialized class instance in a local variable in code protected by an
422 * exception handler (operand stack is okay, because the operand stack is discarded when an
423 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
424 * register typing]
425 *
426 * v3 4.11.1.2
427 * - execution cannot fall off the end of the code
428 *
429 * (We also do many of the items described in the "static checks" sections, because it's easier to
430 * do them here.)
431 *
432 * We need an array of RegType values, one per register, for every instruction. If the method uses
433 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
434 * instruction. In theory this could become quite large -- up to several megabytes for a monster
435 * function.
436 *
437 * NOTE:
438 * The spec forbids backward branches when there's an uninitialized reference in a register. The
439 * idea is to prevent something like this:
440 * loop:
441 * move r1, r0
442 * new-instance r0, MyClass
443 * ...
444 * if-eq rN, loop // once
445 * initialize r0
446 *
447 * This leaves us with two different instances, both allocated by the same instruction, but only
448 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
449 * it by preventing backward branches. We achieve identical results without restricting code
450 * reordering by specifying that you can't execute the new-instance instruction if a register
451 * contains an uninitialized instance created by that same instruction.
452 */
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700454
455 /*
456 * Perform verification for a single instruction.
457 *
458 * This requires fully decoding the instruction to determine the effect it has on registers.
459 *
460 * Finds zero or more following instructions and sets the "changed" flag if execution at that
461 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
462 * addresses. Does not set or clear any other flags in "insn_flags_".
463 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700466
467 // Perform verification of a new array instruction
468 void VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700469 bool is_range)
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 aget instruction. The destination register's type will be set to
473 // be that of component type of the array unless the array type is unknown, in which case a
474 // bottom type inferred from the type of instruction is used. is_primitive is false for an
475 // aget-object.
476 void VerifyAGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700477 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700478
479 // Perform verification of an aput instruction.
480 void VerifyAPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700482
483 // Lookup instance field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800484 mirror::Field* GetInstanceField(const RegType& obj_type, int field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700486
487 // Lookup static field and fail for resolution violations
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 mirror::Field* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489
490 // Perform verification of an iget or sget instruction.
491 void VerifyISGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700492 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700494
495 // Perform verification of an iput or sput instruction.
496 void VerifyISPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700497 bool is_primitive, bool is_static)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700499
500 // Resolves a class based on an index and performs access checks to ensure the referrer can
501 // access the resolved class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700502 const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700503 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700504
505 /*
506 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
507 * address, determine the Join of all exceptions that can land here. Fails if no matching
508 * exception handler can be found or if the Join of exception types fails.
509 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700510 const RegType& GetCaughtExceptionType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700512
513 /*
514 * Resolves a method based on an index and performs access checks to ensure
515 * the referrer can access the resolved method.
516 * Does not throw exceptions.
517 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 mirror::AbstractMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700520
521 /*
522 * Verify the arguments to a method. We're executing in "method", making
523 * a call to the method reference in vB.
524 *
525 * If this is a "direct" invoke, we allow calls to <init>. For calls to
526 * <init>, the first argument may be an uninitialized reference. Otherwise,
527 * calls to anything starting with '<' will be rejected, as will any
528 * uninitialized reference arguments.
529 *
530 * For non-static method calls, this will verify that the method call is
531 * appropriate for the "this" argument.
532 *
533 * The method reference is in vBBBB. The "is_range" parameter determines
534 * whether we use 0-4 "args" values or a range of registers defined by
535 * vAA and vCCCC.
536 *
537 * Widening conversions on integers and references are allowed, but
538 * narrowing conversions are not.
539 *
540 * Returns the resolved method on success, NULL on failure (with *failure
541 * set appropriately).
542 */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 mirror::AbstractMethod* VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700544 MethodType method_type, bool is_range, bool is_super)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700546
547 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700548 * Verify that the target instruction is not "move-exception". It's important that the only way
549 * to execute a move-exception is as the first instruction of an exception handler.
550 * Returns "true" if all is well, "false" if the target instruction is move-exception.
551 */
552 bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
553
554 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700555 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
556 * next_insn, and set the changed flag on the target address if any of the registers were changed.
557 * Returns "false" if an error is encountered.
558 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559 bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700560 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700561
Ian Rogersad0b3a32012-04-16 14:50:24 -0700562 // Is the method being verified a constructor?
563 bool IsConstructor() const {
564 return (method_access_flags_ & kAccConstructor) != 0;
565 }
566
567 // Is the method verified static?
568 bool IsStatic() const {
569 return (method_access_flags_ & kAccStatic) != 0;
570 }
571
572 // Return the register type for the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700573 const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700574
575 // Get a type representing the declaring class of the method.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700576 const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577
buzbeec531cef2012-10-18 07:09:20 -0700578#if defined(ART_USE_LLVM_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700579 /*
580 * Generate the inferred register category for LLVM-based code generator.
581 * Returns a pointer to a two-dimension Class array, or NULL on failure.
582 */
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700583 const InferredRegCategoryMap* GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700584#endif
585
586 /*
587 * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
588 * verification). For type-precise determination we have all the data we need, so we just need to
589 * encode it in some clever fashion.
590 * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
591 */
592 const std::vector<uint8_t>* GenerateGcMap();
593
594 // Verify that the GC map associated with method_ is well formed
595 void VerifyGcMap(const std::vector<uint8_t>& data);
596
597 // Compute sizes for GC map data
598 void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
599
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800600 // Describe VRegs at the given dex pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 std::vector<int32_t> DescribeVRegs(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800602
Ian Rogers776ac1f2012-04-13 23:36:36 -0700603 InsnFlags* CurrentInsnFlags();
604
605 // All the GC maps that the verifier has created
Ian Rogers0c7abda2012-09-19 13:33:42 -0700606 typedef SafeMap<const Compiler::MethodReference, const std::vector<uint8_t>*> DexGcMapTable;
607 static Mutex* dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
608 static DexGcMapTable* dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_);
609 static void SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& dex_gc_map)
610 LOCKS_EXCLUDED(dex_gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700611
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700612 typedef std::set<Compiler::ClassReference> RejectedClassesTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 static Mutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700614 static RejectedClassesTable* rejected_classes_;
615
buzbeec531cef2012-10-18 07:09:20 -0700616#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700617 // All the inferred register category maps that the verifier has created.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700618 typedef SafeMap<const Compiler::MethodReference,
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700619 const InferredRegCategoryMap*> InferredRegCategoryMapTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700620 static Mutex* inferred_reg_category_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700621 static InferredRegCategoryMapTable* inferred_reg_category_maps_ GUARDED_BY(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700622 static void SetInferredRegCategoryMap(Compiler::MethodReference ref,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700623 const InferredRegCategoryMap& m)
624 LOCKS_EXCLUDED(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700625#endif
626
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 static void AddRejectedClass(Compiler::ClassReference ref)
628 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700629
630 RegTypeCache reg_types_;
631
632 PcToRegisterLineTable reg_table_;
633
634 // Storage for the register status we're currently working on.
635 UniquePtr<RegisterLine> work_line_;
636
637 // The address of the instruction we're currently working on, note that this is in 2 byte
638 // quantities
639 uint32_t work_insn_idx_;
640
641 // Storage for the register status we're saving for later.
642 UniquePtr<RegisterLine> saved_line_;
643
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800644 uint32_t dex_method_idx_; // The method we're working on.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700645 // Its object representation if known.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800646 mirror::AbstractMethod* foo_method_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700647 uint32_t method_access_flags_; // Method's access flags.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700648 const DexFile* dex_file_; // The dex file containing the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 // The dex_cache for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800650 mirror::DexCache* dex_cache_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700651 // The class loader for the declaring class of the method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 mirror::ClassLoader* class_loader_ GUARDED_BY(Locks::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700653 uint32_t class_def_idx_; // The class def index of the declaring class of the method.
654 const DexFile::CodeItem* code_item_; // The code item containing the code for the method.
655 UniquePtr<InsnFlags[]> insn_flags_; // Instruction widths and flags, one entry per code unit.
656
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700657 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
658 uint32_t interesting_dex_pc_;
659 // The container into which FindLocksAtDexPc should write the registers containing held locks,
660 // NULL if we're not doing FindLocksAtDexPc.
661 std::vector<uint32_t>* monitor_enter_dex_pcs_;
662
Ian Rogersad0b3a32012-04-16 14:50:24 -0700663 // The types of any error that occurs.
664 std::vector<VerifyError> failures_;
665 // Error messages associated with failures.
666 std::vector<std::ostringstream*> failure_messages_;
667 // Is there a pending hard failure?
668 bool have_pending_hard_failure_;
jeffhaofaf459e2012-08-31 15:32:47 -0700669 // Is there a pending runtime throw failure? A runtime throw failure is when an instruction
670 // would fail at runtime throwing an exception. Such an instruction causes the following code
671 // to be unreachable. This is set by Fail and used to ensure we don't process unreachable
672 // instructions that would hard fail the verification.
673 bool have_pending_runtime_throw_failure_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700674
Ian Rogersad0b3a32012-04-16 14:50:24 -0700675 // Info message log use primarily for verifier diagnostics.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700676 std::ostringstream info_messages_;
677
678 // The number of occurrences of specific opcodes.
679 size_t new_instance_count_;
680 size_t monitor_enter_count_;
Elliott Hughes80537bb2013-01-04 16:37:26 -0800681
682 const bool can_load_classes_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700683};
jeffhaoe4f0b2a2012-08-30 11:18:57 -0700684std::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700685
686} // namespace verifier
687} // namespace art
688
689#endif // ART_SRC_VERIFIER_METHOD_VERIFIER_H_