blob: 273552db5e8a21e9703e3383887670c38daaf070 [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
25#include "casts.h"
26#include "compiler.h"
27#include "dex_file.h"
28#include "dex_instruction.h"
29#include "macros.h"
30#include "object.h"
31#include "reg_type.h"
32#include "reg_type_cache.h"
33#include "register_line.h"
34#include "safe_map.h"
35#include "stl_util.h"
36#include "UniquePtr.h"
37
38namespace art {
39
40struct ReferenceMap2Visitor;
41
42#if defined(ART_USE_LLVM_COMPILER)
43namespace compiler_llvm {
44 class InferredRegCategoryMap;
45} // namespace compiler_llvm
46#endif
47
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -070048#if defined(ART_USE_GREENLAND_COMPILER)
49namespace greenland {
50 class InferredRegCategoryMap;
51} // namespace greenland
52#endif
53
Ian Rogers776ac1f2012-04-13 23:36:36 -070054namespace verifier {
55
56class MethodVerifier;
57class InsnFlags;
58class PcToReferenceMap;
59
60/*
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 * "Direct" and "virtual" methods are stored independently. The type of call used to invoke the
62 * method determines which list we search, and whether we travel up into superclasses.
63 *
64 * (<clinit>, <init>, and methods declared "private" or "static" are stored in the "direct" list.
65 * All others are stored in the "virtual" list.)
66 */
67enum MethodType {
68 METHOD_UNKNOWN = 0,
69 METHOD_DIRECT, // <init>, private
70 METHOD_STATIC, // static
71 METHOD_VIRTUAL, // virtual, super
72 METHOD_INTERFACE // interface
73};
Ian Rogers2fc14272012-08-30 10:56:57 -070074std::ostream& operator<<(std::ostream& os, const MethodType& rhs);
Ian Rogers776ac1f2012-04-13 23:36:36 -070075
76/*
77 * An enumeration of problems that can turn up during verification.
78 * Both VERIFY_ERROR_BAD_CLASS_SOFT and VERIFY_ERROR_BAD_CLASS_HARD denote failures that cause
79 * the entire class to be rejected. However, VERIFY_ERROR_BAD_CLASS_SOFT denotes a soft failure
80 * that can potentially be corrected, and the verifier will try again at runtime.
81 * VERIFY_ERROR_BAD_CLASS_HARD denotes a hard failure that can't be corrected, and will cause
82 * the class to remain uncompiled. Other errors denote verification errors that cause bytecode
83 * to be rewritten to fail at runtime.
84 */
85enum VerifyError {
Ian Rogers776ac1f2012-04-13 23:36:36 -070086 VERIFY_ERROR_BAD_CLASS_HARD, // VerifyError; hard error that skips compilation.
87 VERIFY_ERROR_BAD_CLASS_SOFT, // VerifyError; soft error that verifies again at runtime.
88
89 VERIFY_ERROR_NO_CLASS, // NoClassDefFoundError.
90 VERIFY_ERROR_NO_FIELD, // NoSuchFieldError.
91 VERIFY_ERROR_NO_METHOD, // NoSuchMethodError.
92 VERIFY_ERROR_ACCESS_CLASS, // IllegalAccessError.
93 VERIFY_ERROR_ACCESS_FIELD, // IllegalAccessError.
94 VERIFY_ERROR_ACCESS_METHOD, // IllegalAccessError.
95 VERIFY_ERROR_CLASS_CHANGE, // IncompatibleClassChangeError.
96 VERIFY_ERROR_INSTANTIATION, // InstantiationError.
97};
98std::ostream& operator<<(std::ostream& os, const VerifyError& rhs);
99
100/*
101 * Identifies the type of reference in the instruction that generated the verify error
102 * (e.g. VERIFY_ERROR_ACCESS_CLASS could come from a method, field, or class reference).
103 *
104 * This must fit in two bits.
105 */
106enum VerifyErrorRefType {
107 VERIFY_ERROR_REF_CLASS = 0,
108 VERIFY_ERROR_REF_FIELD = 1,
109 VERIFY_ERROR_REF_METHOD = 2,
110};
111const int kVerifyErrorRefTypeShift = 6;
112
113// We don't need to store the register data for many instructions, because we either only need
114// it at branch points (for verification) or GC points and branches (for verification +
115// type-precise register analysis).
116enum RegisterTrackingMode {
117 kTrackRegsBranches,
118 kTrackRegsGcPoints,
119 kTrackRegsAll,
120};
121
122class PcToRegisterLineTable {
123 public:
124 PcToRegisterLineTable() {}
125 ~PcToRegisterLineTable() {
126 STLDeleteValues(&pc_to_register_line_);
127 }
128
129 // Initialize the RegisterTable. Every instruction address can have a different set of information
130 // about what's in which register, but for verification purposes we only need to store it at
131 // branch target addresses (because we merge into that).
132 void Init(RegisterTrackingMode mode, InsnFlags* flags, uint32_t insns_size,
133 uint16_t registers_size, MethodVerifier* verifier);
134
135 RegisterLine* GetLine(size_t idx) {
136 Table::iterator result = pc_to_register_line_.find(idx); // TODO: C++0x auto
137 if (result == pc_to_register_line_.end()) {
138 return NULL;
139 } else {
140 return result->second;
141 }
142 }
143
144 private:
145 typedef SafeMap<int32_t, RegisterLine*> Table;
146 // Map from a dex pc to the register status associated with it
147 Table pc_to_register_line_;
148};
149
150// The verifier
151class MethodVerifier {
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700152#if defined(ART_USE_LLVM_COMPILER)
153 typedef compiler_llvm::InferredRegCategoryMap InferredRegCategoryMap;
154#elif defined(ART_USE_GREENLAND_COMPILER)
155 typedef greenland::InferredRegCategoryMap InferredRegCategoryMap;
156#endif
Elliott Hughesa21039c2012-06-21 12:09:25 -0700157
Ian Rogers776ac1f2012-04-13 23:36:36 -0700158 public:
jeffhaof1e6b7c2012-06-05 18:33:30 -0700159 enum FailureKind {
160 kNoFailure,
161 kSoftFailure,
162 kHardFailure,
163 };
164
165 /* Verify a class. Returns "kNoFailure" on success. */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 static FailureKind VerifyClass(const Class* klass, std::string& error)
167 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700168 static FailureKind VerifyClass(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700169 ClassLoader* class_loader, uint32_t class_def_idx,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 std::string& error)
171 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700172
173 uint8_t EncodePcToReferenceMapData() const;
174
175 uint32_t DexFileVersion() const {
176 return dex_file_->GetVersion();
177 }
178
179 RegTypeCache* GetRegTypeCache() {
180 return &reg_types_;
181 }
182
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 // Log a verification failure.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700184 std::ostream& Fail(VerifyError error);
185
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 // Log for verification information.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700187 std::ostream& LogVerifyInfo() {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 return info_messages_ << "VFY: " << PrettyMethod(method_idx_, *dex_file_)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700189 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
190 }
191
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 // Dump the failures encountered by the verifier.
193 std::ostream& DumpFailures(std::ostream& os);
194
Ian Rogers776ac1f2012-04-13 23:36:36 -0700195 // Dump the state of the verifier, namely each instruction, what flags are set on it, register
196 // information
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700198
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 static const std::vector<uint8_t>* GetGcMap(Compiler::MethodReference ref)
200 LOCKS_EXCLUDED(gc_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700201
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700202 // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding
203 // to the locks held at 'dex_pc' in 'm'.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 static void FindLocksAtDexPc(Method* m, uint32_t dex_pc,
205 std::vector<uint32_t>& monitor_enter_dex_pcs)
206 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700207
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700208 static void Init();
209 static void Shutdown();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700210
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700211#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700212 static const InferredRegCategoryMap* GetInferredRegCategoryMap(Compiler::MethodReference ref)
213 LOCKS_EXCLUDED(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700214#endif
215
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700216 static bool IsClassRejected(Compiler::ClassReference ref)
217 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700218
219 private:
Ian Rogers776ac1f2012-04-13 23:36:36 -0700220 explicit MethodVerifier(const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700221 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 uint32_t method_idx, Method* method, uint32_t access_flags)
223 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700224
225 // 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 */
jeffhaof1e6b7c2012-06-05 18:33:30 -0700242 static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file, DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -0700243 ClassLoader* class_loader, uint32_t class_def_idx, const DexFile::CodeItem* code_item,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 Method* method, uint32_t method_access_flags)
245 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
246 static void VerifyMethodAndDump(Method* method)
247 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogerse1758fe2012-04-19 11:31:15 -0700248
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 // Run verification on the method. Returns true if verification completes and false if the input
250 // has an irrecoverable corruption.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 bool Verify() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700254
Ian Rogers776ac1f2012-04-13 23:36:36 -0700255 /*
256 * Compute the width of the instruction at each address in the instruction stream, and store it in
257 * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
258 * table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
259 *
260 * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
261 *
262 * Performs some static checks, notably:
263 * - opcode of first instruction begins at index 0
264 * - only documented instructions may appear
265 * - each instruction follows the last
266 * - last byte of last instruction is at (code_length-1)
267 *
268 * Logs an error and returns "false" on failure.
269 */
270 bool ComputeWidthsAndCountOps();
271
272 /*
273 * Set the "in try" flags for all instructions protected by "try" statements. Also sets the
274 * "branch target" flags for exception handlers.
275 *
276 * Call this after widths have been set in "insn_flags".
277 *
278 * Returns "false" if something in the exception table looks fishy, but we're expecting the
279 * exception table to be somewhat sane.
280 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700282
283 /*
284 * Perform static verification on all instructions in a method.
285 *
286 * Walks through instructions in a method calling VerifyInstruction on each.
287 */
288 bool VerifyInstructions();
289
290 /*
291 * Perform static verification on an instruction.
292 *
293 * As a side effect, this sets the "branch target" flags in InsnFlags.
294 *
295 * "(CF)" items are handled during code-flow analysis.
296 *
297 * v3 4.10.1
298 * - target of each jump and branch instruction must be valid
299 * - targets of switch statements must be valid
300 * - operands referencing constant pool entries must be valid
301 * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
302 * - (CF) operands of method invocation instructions must be valid
303 * - (CF) only invoke-direct can call a method starting with '<'
304 * - (CF) <clinit> must never be called explicitly
305 * - operands of instanceof, checkcast, new (and variants) must be valid
306 * - new-array[-type] limited to 255 dimensions
307 * - can't use "new" on an array class
308 * - (?) limit dimensions in multi-array creation
309 * - local variable load/store register values must be in valid range
310 *
311 * v3 4.11.1.2
312 * - branches must be within the bounds of the code array
313 * - targets of all control-flow instructions are the start of an instruction
314 * - register accesses fall within range of allocated registers
315 * - (N/A) access to constant pool must be of appropriate type
316 * - code does not end in the middle of an instruction
317 * - execution cannot fall off the end of the code
318 * - (earlier) for each exception handler, the "try" area must begin and
319 * end at the start of an instruction (end can be at the end of the code)
320 * - (earlier) for each exception handler, the handler must start at a valid
321 * instruction
322 */
323 bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
324
325 /* Ensure that the register index is valid for this code item. */
326 bool CheckRegisterIndex(uint32_t idx);
327
328 /* Ensure that the wide register index is valid for this code item. */
329 bool CheckWideRegisterIndex(uint32_t idx);
330
331 // Perform static checks on a field get or set instruction. All we do here is ensure that the
332 // field index is in the valid range.
333 bool CheckFieldIndex(uint32_t idx);
334
335 // Perform static checks on a method invocation instruction. All we do here is ensure that the
336 // method index is in the valid range.
337 bool CheckMethodIndex(uint32_t idx);
338
339 // Perform static checks on a "new-instance" instruction. Specifically, make sure the class
340 // reference isn't for an array class.
341 bool CheckNewInstance(uint32_t idx);
342
343 /* Ensure that the string index is in the valid range. */
344 bool CheckStringIndex(uint32_t idx);
345
346 // Perform static checks on an instruction that takes a class constant. Ensure that the class
347 // index is in the valid range.
348 bool CheckTypeIndex(uint32_t idx);
349
350 // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
351 // creating an array of arrays that causes the number of dimensions to exceed 255.
352 bool CheckNewArray(uint32_t idx);
353
354 // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
355 bool CheckArrayData(uint32_t cur_offset);
356
357 // Verify that the target of a branch instruction is valid. We don't expect code to jump directly
358 // into an exception handler, but it's valid to do so as long as the target isn't a
359 // "move-exception" instruction. We verify that in a later stage.
360 // The dex format forbids certain instructions from branching to themselves.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700361 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700362 bool CheckBranchTarget(uint32_t cur_offset);
363
364 // Verify a switch table. "cur_offset" is the offset of the switch instruction.
Elliott Hughes24edeb52012-06-18 15:29:46 -0700365 // Updates "insn_flags_", setting the "branch target" flag.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700366 bool CheckSwitchTargets(uint32_t cur_offset);
367
368 // Check the register indices used in a "vararg" instruction, such as invoke-virtual or
369 // filled-new-array.
370 // - vA holds word count (0-5), args[] have values.
371 // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
372 // takes a double is done with consecutive registers. This requires parsing the target method
373 // signature, which we will be doing later on during the code flow analysis.
374 bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]);
375
376 // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
377 // or filled-new-array/range.
378 // - vA holds word count, vC holds index of first reg.
379 bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC);
380
381 // Extract the relative offset from a branch instruction.
382 // Returns "false" on failure (e.g. this isn't a branch instruction).
383 bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
384 bool* selfOkay);
385
386 /* Perform detailed code-flow analysis on a single method. */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700388
389 // Set the register types for the first instruction in the method based on the method signature.
390 // This has the side-effect of validating the signature.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700392
393 /*
394 * Perform code flow on a method.
395 *
396 * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
397 * instruction, process it (setting additional "changed" bits), and repeat until there are no
398 * more.
399 *
400 * v3 4.11.1.1
401 * - (N/A) operand stack is always the same size
402 * - operand stack [registers] contain the correct types of values
403 * - local variables [registers] contain the correct types of values
404 * - methods are invoked with the appropriate arguments
405 * - fields are assigned using values of appropriate types
406 * - opcodes have the correct type values in operand registers
407 * - there is never an uninitialized class instance in a local variable in code protected by an
408 * exception handler (operand stack is okay, because the operand stack is discarded when an
409 * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
410 * register typing]
411 *
412 * v3 4.11.1.2
413 * - execution cannot fall off the end of the code
414 *
415 * (We also do many of the items described in the "static checks" sections, because it's easier to
416 * do them here.)
417 *
418 * We need an array of RegType values, one per register, for every instruction. If the method uses
419 * monitor-enter, we need extra data for every register, and a stack for every "interesting"
420 * instruction. In theory this could become quite large -- up to several megabytes for a monster
421 * function.
422 *
423 * NOTE:
424 * The spec forbids backward branches when there's an uninitialized reference in a register. The
425 * idea is to prevent something like this:
426 * loop:
427 * move r1, r0
428 * new-instance r0, MyClass
429 * ...
430 * if-eq rN, loop // once
431 * initialize r0
432 *
433 * This leaves us with two different instances, both allocated by the same instruction, but only
434 * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
435 * it by preventing backward branches. We achieve identical results without restricting code
436 * reordering by specifying that you can't execute the new-instance instruction if a register
437 * contains an uninitialized instance created by that same instruction.
438 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700440
441 /*
442 * Perform verification for a single instruction.
443 *
444 * This requires fully decoding the instruction to determine the effect it has on registers.
445 *
446 * Finds zero or more following instructions and sets the "changed" flag if execution at that
447 * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
448 * addresses. Does not set or clear any other flags in "insn_flags_".
449 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 bool CodeFlowVerifyInstruction(uint32_t* start_guess)
451 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700452
453 // Perform verification of a new array instruction
454 void VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700455 bool is_range)
456 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700457
458 // Perform verification of an aget instruction. The destination register's type will be set to
459 // be that of component type of the array unless the array type is unknown, in which case a
460 // bottom type inferred from the type of instruction is used. is_primitive is false for an
461 // aget-object.
462 void VerifyAGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 bool is_primitive) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700464
465 // Perform verification of an aput instruction.
466 void VerifyAPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700467 bool is_primitive) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700468
469 // Lookup instance field and fail for resolution violations
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 Field* GetInstanceField(const RegType& obj_type, int field_idx)
471 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700472
473 // Lookup static field and fail for resolution violations
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 Field* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700475
476 // Perform verification of an iget or sget instruction.
477 void VerifyISGet(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700478 bool is_primitive, bool is_static)
479 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700480
481 // Perform verification of an iput or sput instruction.
482 void VerifyISPut(const DecodedInstruction& insn, const RegType& insn_type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 bool is_primitive, bool is_static)
484 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485
486 // Resolves a class based on an index and performs access checks to ensure the referrer can
487 // access the resolved class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700488 const RegType& ResolveClassAndCheckAccess(uint32_t class_idx)
489 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700490
491 /*
492 * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
493 * address, determine the Join of all exceptions that can land here. Fails if no matching
494 * exception handler can be found or if the Join of exception types fails.
495 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 const RegType& GetCaughtExceptionType()
497 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700498
499 /*
500 * Resolves a method based on an index and performs access checks to ensure
501 * the referrer can access the resolved method.
502 * Does not throw exceptions.
503 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700504 Method* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
505 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700506
507 /*
508 * Verify the arguments to a method. We're executing in "method", making
509 * a call to the method reference in vB.
510 *
511 * If this is a "direct" invoke, we allow calls to <init>. For calls to
512 * <init>, the first argument may be an uninitialized reference. Otherwise,
513 * calls to anything starting with '<' will be rejected, as will any
514 * uninitialized reference arguments.
515 *
516 * For non-static method calls, this will verify that the method call is
517 * appropriate for the "this" argument.
518 *
519 * The method reference is in vBBBB. The "is_range" parameter determines
520 * whether we use 0-4 "args" values or a range of registers defined by
521 * vAA and vCCCC.
522 *
523 * Widening conversions on integers and references are allowed, but
524 * narrowing conversions are not.
525 *
526 * Returns the resolved method on success, NULL on failure (with *failure
527 * set appropriately).
528 */
529 Method* VerifyInvocationArgs(const DecodedInstruction& dec_insn,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 MethodType method_type, bool is_range, bool is_super)
531 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700532
533 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700534 * Verify that the target instruction is not "move-exception". It's important that the only way
535 * to execute a move-exception is as the first instruction of an exception handler.
536 * Returns "true" if all is well, "false" if the target instruction is move-exception.
537 */
538 bool CheckNotMoveException(const uint16_t* insns, int insn_idx);
539
540 /*
Ian Rogers776ac1f2012-04-13 23:36:36 -0700541 * Control can transfer to "next_insn". Merge the registers from merge_line into the table at
542 * next_insn, and set the changed flag on the target address if any of the registers were changed.
543 * Returns "false" if an error is encountered.
544 */
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700545 bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line)
546 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700547
Ian Rogersad0b3a32012-04-16 14:50:24 -0700548 // Is the method being verified a constructor?
549 bool IsConstructor() const {
550 return (method_access_flags_ & kAccConstructor) != 0;
551 }
552
553 // Is the method verified static?
554 bool IsStatic() const {
555 return (method_access_flags_ & kAccStatic) != 0;
556 }
557
558 // Return the register type for the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559 const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700560
561 // Get a type representing the declaring class of the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562 const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700563
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700564#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Ian Rogers776ac1f2012-04-13 23:36:36 -0700565 /*
566 * Generate the inferred register category for LLVM-based code generator.
567 * Returns a pointer to a two-dimension Class array, or NULL on failure.
568 */
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700569 const InferredRegCategoryMap* GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700570#endif
571
572 /*
573 * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
574 * verification). For type-precise determination we have all the data we need, so we just need to
575 * encode it in some clever fashion.
576 * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure.
577 */
578 const std::vector<uint8_t>* GenerateGcMap();
579
580 // Verify that the GC map associated with method_ is well formed
581 void VerifyGcMap(const std::vector<uint8_t>& data);
582
583 // Compute sizes for GC map data
584 void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc);
585
586 InsnFlags* CurrentInsnFlags();
587
588 // All the GC maps that the verifier has created
589 typedef SafeMap<const Compiler::MethodReference, const std::vector<uint8_t>*> GcMapTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700590 static Mutex* gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
591 static GcMapTable* gc_maps_ GUARDED_BY(gc_maps_lock_);
592 static void SetGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map)
593 LOCKS_EXCLUDED(gc_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700594
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700595 typedef std::set<Compiler::ClassReference> RejectedClassesTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700596 static Mutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700597 static RejectedClassesTable* rejected_classes_;
598
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700599#if defined(ART_USE_LLVM_COMPILER) || defined(ART_USE_GREENLAND_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -0700600 // All the inferred register category maps that the verifier has created.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700601 typedef SafeMap<const Compiler::MethodReference,
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -0700602 const InferredRegCategoryMap*> InferredRegCategoryMapTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700603 static Mutex* inferred_reg_category_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700604 static InferredRegCategoryMapTable* inferred_reg_category_maps_ GUARDED_BY(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700605 static void SetInferredRegCategoryMap(Compiler::MethodReference ref,
Shih-wei Liaocd05a622012-08-15 00:02:05 -0700606 const InferredRegCategoryMap& m)
607 LOCKS_EXCLUDED(inferred_reg_category_maps_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700608#endif
609
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700610 static void AddRejectedClass(Compiler::ClassReference ref)
611 LOCKS_EXCLUDED(rejected_classes_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700612
613 RegTypeCache reg_types_;
614
615 PcToRegisterLineTable reg_table_;
616
617 // Storage for the register status we're currently working on.
618 UniquePtr<RegisterLine> work_line_;
619
620 // The address of the instruction we're currently working on, note that this is in 2 byte
621 // quantities
622 uint32_t work_insn_idx_;
623
624 // Storage for the register status we're saving for later.
625 UniquePtr<RegisterLine> saved_line_;
626
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627 uint32_t method_idx_; // The method we're working on.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 // Its object representation if known.
629 Method* foo_method_ GUARDED_BY(GlobalSynchronization::mutator_lock_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 uint32_t method_access_flags_; // Method's access flags.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631 const DexFile* dex_file_; // The dex file containing the method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700632 // The dex_cache for the declaring class of the method.
633 DexCache* dex_cache_ GUARDED_BY(GlobalSynchronization::mutator_lock_);
634 // The class loader for the declaring class of the method.
635 ClassLoader* class_loader_ GUARDED_BY(GlobalSynchronization::mutator_lock_);
Ian Rogers776ac1f2012-04-13 23:36:36 -0700636 uint32_t class_def_idx_; // The class def index of the declaring class of the method.
637 const DexFile::CodeItem* code_item_; // The code item containing the code for the method.
638 UniquePtr<InsnFlags[]> insn_flags_; // Instruction widths and flags, one entry per code unit.
639
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700640 // The dex PC of a FindLocksAtDexPc request, -1 otherwise.
641 uint32_t interesting_dex_pc_;
642 // The container into which FindLocksAtDexPc should write the registers containing held locks,
643 // NULL if we're not doing FindLocksAtDexPc.
644 std::vector<uint32_t>* monitor_enter_dex_pcs_;
645
Ian Rogersad0b3a32012-04-16 14:50:24 -0700646 // The types of any error that occurs.
647 std::vector<VerifyError> failures_;
648 // Error messages associated with failures.
649 std::vector<std::ostringstream*> failure_messages_;
650 // Is there a pending hard failure?
651 bool have_pending_hard_failure_;
Ian Rogers776ac1f2012-04-13 23:36:36 -0700652
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 // Info message log use primarily for verifier diagnostics.
Ian Rogers776ac1f2012-04-13 23:36:36 -0700654 std::ostringstream info_messages_;
655
656 // The number of occurrences of specific opcodes.
657 size_t new_instance_count_;
658 size_t monitor_enter_count_;
659
660 friend struct art::ReferenceMap2Visitor; // for VerifyMethodAndDump
661};
662
663} // namespace verifier
664} // namespace art
665
666#endif // ART_SRC_VERIFIER_METHOD_VERIFIER_H_