Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 20 | #include <set> |
| 21 | #include <vector> |
| 22 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 23 | #include "base/casts.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 26 | #include "compiler/driver/compiler_driver.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 27 | #include "dex_file.h" |
| 28 | #include "dex_instruction.h" |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 29 | #include "instruction_flags.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/object.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 31 | #include "reg_type.h" |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 32 | #include "reg_type_cache-inl.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 33 | #include "register_line.h" |
| 34 | #include "safe_map.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 35 | #include "UniquePtr.h" |
| 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | struct ReferenceMap2Visitor; |
| 40 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 41 | namespace verifier { |
| 42 | |
| 43 | class MethodVerifier; |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 44 | class DexPcToReferenceMap; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 47 | * "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 | */ |
| 53 | enum 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 Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 60 | std::ostream& operator<<(std::ostream& os, const MethodType& rhs); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 61 | |
| 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 | */ |
| 71 | enum VerifyError { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 72 | 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 | }; |
| 84 | std::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 | */ |
| 92 | enum VerifyErrorRefType { |
| 93 | VERIFY_ERROR_REF_CLASS = 0, |
| 94 | VERIFY_ERROR_REF_FIELD = 1, |
| 95 | VERIFY_ERROR_REF_METHOD = 2, |
| 96 | }; |
| 97 | const 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). |
| 102 | enum RegisterTrackingMode { |
| 103 | kTrackRegsBranches, |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 104 | kTrackCompilerInterestPoints, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 105 | kTrackRegsAll, |
| 106 | }; |
| 107 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 108 | // A mapping from a dex pc to the register line statuses as they are immediately prior to the |
| 109 | // execution of that instruction. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 110 | class 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 Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 120 | void Init(RegisterTrackingMode mode, InstructionFlags* flags, uint32_t insns_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 121 | 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 Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 134 | Table pc_to_register_line_; |
| 135 | }; |
| 136 | |
| 137 | // The verifier |
| 138 | class MethodVerifier { |
| 139 | public: |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 140 | enum FailureKind { |
| 141 | kNoFailure, |
| 142 | kSoftFailure, |
| 143 | kHardFailure, |
| 144 | }; |
| 145 | |
| 146 | /* Verify a class. Returns "kNoFailure" on success. */ |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 147 | static FailureKind VerifyClass(const mirror::Class* klass, std::string& error, |
| 148 | bool allow_soft_failures) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 149 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 150 | static FailureKind VerifyClass(const DexFile* dex_file, mirror::DexCache* dex_cache, |
| 151 | mirror::ClassLoader* class_loader, uint32_t class_def_idx, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 152 | std::string& error, bool allow_soft_failures) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 153 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 154 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 155 | static void VerifyMethodAndDump(std::ostream& os, uint32_t method_idx, const DexFile* dex_file, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 156 | mirror::DexCache* dex_cache, mirror::ClassLoader* class_loader, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 157 | uint32_t class_def_idx, const DexFile::CodeItem* code_item, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 158 | mirror::AbstractMethod* method, uint32_t method_access_flags) |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 159 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 160 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 161 | uint8_t EncodePcToReferenceMapData() const; |
| 162 | |
| 163 | uint32_t DexFileVersion() const { |
| 164 | return dex_file_->GetVersion(); |
| 165 | } |
| 166 | |
| 167 | RegTypeCache* GetRegTypeCache() { |
| 168 | return ®_types_; |
| 169 | } |
| 170 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 171 | // Log a verification failure. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 172 | std::ostream& Fail(VerifyError error); |
| 173 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 174 | // Log for verification information. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 175 | std::ostream& LogVerifyInfo() { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 176 | return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 177 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 178 | } |
| 179 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 180 | // Dump the failures encountered by the verifier. |
| 181 | std::ostream& DumpFailures(std::ostream& os); |
| 182 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 183 | // Dump the state of the verifier, namely each instruction, what flags are set on it, register |
| 184 | // information |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 185 | void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 186 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 187 | static const std::vector<uint8_t>* GetDexGcMap(CompilerDriver::MethodReference ref) |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 188 | LOCKS_EXCLUDED(dex_gc_maps_lock_); |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 189 | |
Ian Rogers | e3cd2f0 | 2013-05-24 15:32:56 -0700 | [diff] [blame] | 190 | static const CompilerDriver::MethodReference* GetDevirtMap(const CompilerDriver::MethodReference& ref, |
| 191 | uint32_t dex_pc) |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 192 | LOCKS_EXCLUDED(devirt_maps_lock_); |
| 193 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 194 | // Returns true if the cast can statically be verified to be redundant |
| 195 | // by using the check-cast elision peephole optimization in the verifier |
| 196 | static bool IsSafeCast(CompilerDriver::MethodReference ref, uint32_t pc) |
| 197 | LOCKS_EXCLUDED(safecast_map_lock_); |
| 198 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 199 | // Fills 'monitor_enter_dex_pcs' with the dex pcs of the monitor-enter instructions corresponding |
| 200 | // to the locks held at 'dex_pc' in 'm'. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 201 | static void FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 202 | std::vector<uint32_t>& monitor_enter_dex_pcs) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 203 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 204 | |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 205 | static void Init() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 206 | static void Shutdown(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 207 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 208 | static bool IsClassRejected(CompilerDriver::ClassReference ref) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | LOCKS_EXCLUDED(rejected_classes_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 210 | |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 211 | bool CanLoadClasses() const { |
| 212 | return can_load_classes_; |
| 213 | } |
| 214 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 215 | MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache, |
| 216 | mirror::ClassLoader* class_loader, uint32_t class_def_idx, |
| 217 | const DexFile::CodeItem* code_item, |
| 218 | uint32_t method_idx, mirror::AbstractMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 219 | uint32_t access_flags, bool can_load_classes, bool allow_soft_failures) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 220 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 222 | // Run verification on the method. Returns true if verification completes and false if the input |
| 223 | // has an irrecoverable corruption. |
| 224 | bool Verify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 225 | |
| 226 | // Describe VRegs at the given dex pc. |
| 227 | std::vector<int32_t> DescribeVRegs(uint32_t dex_pc); |
| 228 | |
| 229 | private: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 230 | // Adds the given string to the beginning of the last failure message. |
| 231 | void PrependToLastFailMessage(std::string); |
| 232 | |
| 233 | // Adds the given string to the end of the last failure message. |
| 234 | void AppendToLastFailMessage(std::string); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * Perform verification on a single method. |
| 238 | * |
| 239 | * We do this in three passes: |
| 240 | * (1) Walk through all code units, determining instruction locations, |
| 241 | * widths, and other characteristics. |
| 242 | * (2) Walk through all code units, performing static checks on |
| 243 | * operands. |
| 244 | * (3) Iterate through the method, checking type safety and looking |
| 245 | * for code flow problems. |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 246 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 247 | static FailureKind VerifyMethod(uint32_t method_idx, const DexFile* dex_file, |
| 248 | mirror::DexCache* dex_cache, |
| 249 | mirror::ClassLoader* class_loader, uint32_t class_def_idx, |
| 250 | const DexFile::CodeItem* code_item, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 251 | mirror::AbstractMethod* method, uint32_t method_access_flags, |
| 252 | bool allow_soft_failures) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 253 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 254 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 255 | void FindLocksAtDexPc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 256 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 257 | /* |
| 258 | * Compute the width of the instruction at each address in the instruction stream, and store it in |
| 259 | * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch |
| 260 | * table data, are not touched (so the caller should probably initialize "insn_flags" to zero). |
| 261 | * |
| 262 | * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set. |
| 263 | * |
| 264 | * Performs some static checks, notably: |
| 265 | * - opcode of first instruction begins at index 0 |
| 266 | * - only documented instructions may appear |
| 267 | * - each instruction follows the last |
| 268 | * - last byte of last instruction is at (code_length-1) |
| 269 | * |
| 270 | * Logs an error and returns "false" on failure. |
| 271 | */ |
| 272 | bool ComputeWidthsAndCountOps(); |
| 273 | |
| 274 | /* |
| 275 | * Set the "in try" flags for all instructions protected by "try" statements. Also sets the |
| 276 | * "branch target" flags for exception handlers. |
| 277 | * |
| 278 | * Call this after widths have been set in "insn_flags". |
| 279 | * |
| 280 | * Returns "false" if something in the exception table looks fishy, but we're expecting the |
| 281 | * exception table to be somewhat sane. |
| 282 | */ |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 283 | bool ScanTryCatchBlocks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * Perform static verification on all instructions in a method. |
| 287 | * |
| 288 | * Walks through instructions in a method calling VerifyInstruction on each. |
| 289 | */ |
| 290 | bool VerifyInstructions(); |
| 291 | |
| 292 | /* |
| 293 | * Perform static verification on an instruction. |
| 294 | * |
| 295 | * As a side effect, this sets the "branch target" flags in InsnFlags. |
| 296 | * |
| 297 | * "(CF)" items are handled during code-flow analysis. |
| 298 | * |
| 299 | * v3 4.10.1 |
| 300 | * - target of each jump and branch instruction must be valid |
| 301 | * - targets of switch statements must be valid |
| 302 | * - operands referencing constant pool entries must be valid |
| 303 | * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid |
| 304 | * - (CF) operands of method invocation instructions must be valid |
| 305 | * - (CF) only invoke-direct can call a method starting with '<' |
| 306 | * - (CF) <clinit> must never be called explicitly |
| 307 | * - operands of instanceof, checkcast, new (and variants) must be valid |
| 308 | * - new-array[-type] limited to 255 dimensions |
| 309 | * - can't use "new" on an array class |
| 310 | * - (?) limit dimensions in multi-array creation |
| 311 | * - local variable load/store register values must be in valid range |
| 312 | * |
| 313 | * v3 4.11.1.2 |
| 314 | * - branches must be within the bounds of the code array |
| 315 | * - targets of all control-flow instructions are the start of an instruction |
| 316 | * - register accesses fall within range of allocated registers |
| 317 | * - (N/A) access to constant pool must be of appropriate type |
| 318 | * - code does not end in the middle of an instruction |
| 319 | * - execution cannot fall off the end of the code |
| 320 | * - (earlier) for each exception handler, the "try" area must begin and |
| 321 | * end at the start of an instruction (end can be at the end of the code) |
| 322 | * - (earlier) for each exception handler, the handler must start at a valid |
| 323 | * instruction |
| 324 | */ |
| 325 | bool VerifyInstruction(const Instruction* inst, uint32_t code_offset); |
| 326 | |
| 327 | /* Ensure that the register index is valid for this code item. */ |
| 328 | bool CheckRegisterIndex(uint32_t idx); |
| 329 | |
| 330 | /* Ensure that the wide register index is valid for this code item. */ |
| 331 | bool CheckWideRegisterIndex(uint32_t idx); |
| 332 | |
| 333 | // Perform static checks on a field get or set instruction. All we do here is ensure that the |
| 334 | // field index is in the valid range. |
| 335 | bool CheckFieldIndex(uint32_t idx); |
| 336 | |
| 337 | // Perform static checks on a method invocation instruction. All we do here is ensure that the |
| 338 | // method index is in the valid range. |
| 339 | bool CheckMethodIndex(uint32_t idx); |
| 340 | |
| 341 | // Perform static checks on a "new-instance" instruction. Specifically, make sure the class |
| 342 | // reference isn't for an array class. |
| 343 | bool CheckNewInstance(uint32_t idx); |
| 344 | |
| 345 | /* Ensure that the string index is in the valid range. */ |
| 346 | bool CheckStringIndex(uint32_t idx); |
| 347 | |
| 348 | // Perform static checks on an instruction that takes a class constant. Ensure that the class |
| 349 | // index is in the valid range. |
| 350 | bool CheckTypeIndex(uint32_t idx); |
| 351 | |
| 352 | // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't |
| 353 | // creating an array of arrays that causes the number of dimensions to exceed 255. |
| 354 | bool CheckNewArray(uint32_t idx); |
| 355 | |
| 356 | // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction. |
| 357 | bool CheckArrayData(uint32_t cur_offset); |
| 358 | |
| 359 | // Verify that the target of a branch instruction is valid. We don't expect code to jump directly |
| 360 | // into an exception handler, but it's valid to do so as long as the target isn't a |
| 361 | // "move-exception" instruction. We verify that in a later stage. |
| 362 | // The dex format forbids certain instructions from branching to themselves. |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 363 | // Updates "insn_flags_", setting the "branch target" flag. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 364 | bool CheckBranchTarget(uint32_t cur_offset); |
| 365 | |
| 366 | // Verify a switch table. "cur_offset" is the offset of the switch instruction. |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 367 | // Updates "insn_flags_", setting the "branch target" flag. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 368 | bool CheckSwitchTargets(uint32_t cur_offset); |
| 369 | |
| 370 | // Check the register indices used in a "vararg" instruction, such as invoke-virtual or |
| 371 | // filled-new-array. |
| 372 | // - vA holds word count (0-5), args[] have values. |
| 373 | // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that |
| 374 | // takes a double is done with consecutive registers. This requires parsing the target method |
| 375 | // signature, which we will be doing later on during the code flow analysis. |
| 376 | bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]); |
| 377 | |
| 378 | // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range |
| 379 | // or filled-new-array/range. |
| 380 | // - vA holds word count, vC holds index of first reg. |
| 381 | bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC); |
| 382 | |
| 383 | // Extract the relative offset from a branch instruction. |
| 384 | // Returns "false" on failure (e.g. this isn't a branch instruction). |
| 385 | bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 386 | bool* selfOkay); |
| 387 | |
| 388 | /* Perform detailed code-flow analysis on a single method. */ |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 389 | bool VerifyCodeFlow() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 390 | |
| 391 | // Set the register types for the first instruction in the method based on the method signature. |
| 392 | // This has the side-effect of validating the signature. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 393 | bool SetTypesFromSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * Perform code flow on a method. |
| 397 | * |
| 398 | * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first |
| 399 | * instruction, process it (setting additional "changed" bits), and repeat until there are no |
| 400 | * more. |
| 401 | * |
| 402 | * v3 4.11.1.1 |
| 403 | * - (N/A) operand stack is always the same size |
| 404 | * - operand stack [registers] contain the correct types of values |
| 405 | * - local variables [registers] contain the correct types of values |
| 406 | * - methods are invoked with the appropriate arguments |
| 407 | * - fields are assigned using values of appropriate types |
| 408 | * - opcodes have the correct type values in operand registers |
| 409 | * - there is never an uninitialized class instance in a local variable in code protected by an |
| 410 | * exception handler (operand stack is okay, because the operand stack is discarded when an |
| 411 | * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of |
| 412 | * register typing] |
| 413 | * |
| 414 | * v3 4.11.1.2 |
| 415 | * - execution cannot fall off the end of the code |
| 416 | * |
| 417 | * (We also do many of the items described in the "static checks" sections, because it's easier to |
| 418 | * do them here.) |
| 419 | * |
| 420 | * We need an array of RegType values, one per register, for every instruction. If the method uses |
| 421 | * monitor-enter, we need extra data for every register, and a stack for every "interesting" |
| 422 | * instruction. In theory this could become quite large -- up to several megabytes for a monster |
| 423 | * function. |
| 424 | * |
| 425 | * NOTE: |
| 426 | * The spec forbids backward branches when there's an uninitialized reference in a register. The |
| 427 | * idea is to prevent something like this: |
| 428 | * loop: |
| 429 | * move r1, r0 |
| 430 | * new-instance r0, MyClass |
| 431 | * ... |
| 432 | * if-eq rN, loop // once |
| 433 | * initialize r0 |
| 434 | * |
| 435 | * This leaves us with two different instances, both allocated by the same instruction, but only |
| 436 | * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around |
| 437 | * it by preventing backward branches. We achieve identical results without restricting code |
| 438 | * reordering by specifying that you can't execute the new-instance instruction if a register |
| 439 | * contains an uninitialized instance created by that same instruction. |
| 440 | */ |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 441 | bool CodeFlowVerifyMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Perform verification for a single instruction. |
| 445 | * |
| 446 | * This requires fully decoding the instruction to determine the effect it has on registers. |
| 447 | * |
| 448 | * Finds zero or more following instructions and sets the "changed" flag if execution at that |
| 449 | * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target |
| 450 | * addresses. Does not set or clear any other flags in "insn_flags_". |
| 451 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 452 | bool CodeFlowVerifyInstruction(uint32_t* start_guess) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 453 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 454 | |
| 455 | // Perform verification of a new array instruction |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 456 | void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 457 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 458 | |
| 459 | // Perform verification of an aget instruction. The destination register's type will be set to |
| 460 | // be that of component type of the array unless the array type is unknown, in which case a |
| 461 | // bottom type inferred from the type of instruction is used. is_primitive is false for an |
| 462 | // aget-object. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 463 | void VerifyAGet(const Instruction* inst, const RegType& insn_type, |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 464 | bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 465 | |
| 466 | // Perform verification of an aput instruction. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 467 | void VerifyAPut(const Instruction* inst, const RegType& insn_type, |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 468 | bool is_primitive) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 469 | |
| 470 | // Lookup instance field and fail for resolution violations |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 471 | mirror::Field* GetInstanceField(const RegType& obj_type, int field_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 472 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 473 | |
| 474 | // Lookup static field and fail for resolution violations |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 475 | mirror::Field* GetStaticField(int field_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 476 | |
| 477 | // Perform verification of an iget or sget instruction. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 478 | void VerifyISGet(const Instruction* inst, const RegType& insn_type, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 479 | bool is_primitive, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 480 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 481 | |
| 482 | // Perform verification of an iput or sput instruction. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 483 | void VerifyISPut(const Instruction* inst, const RegType& insn_type, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 484 | bool is_primitive, bool is_static) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 485 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 486 | |
| 487 | // Resolves a class based on an index and performs access checks to ensure the referrer can |
| 488 | // access the resolved class. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 489 | const RegType& ResolveClassAndCheckAccess(uint32_t class_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 490 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler |
| 494 | * address, determine the Join of all exceptions that can land here. Fails if no matching |
| 495 | * exception handler can be found or if the Join of exception types fails. |
| 496 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 497 | const RegType& GetCaughtExceptionType() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 498 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | * Resolves a method based on an index and performs access checks to ensure |
| 502 | * the referrer can access the resolved method. |
| 503 | * Does not throw exceptions. |
| 504 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 505 | mirror::AbstractMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 506 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * Verify the arguments to a method. We're executing in "method", making |
| 510 | * a call to the method reference in vB. |
| 511 | * |
| 512 | * If this is a "direct" invoke, we allow calls to <init>. For calls to |
| 513 | * <init>, the first argument may be an uninitialized reference. Otherwise, |
| 514 | * calls to anything starting with '<' will be rejected, as will any |
| 515 | * uninitialized reference arguments. |
| 516 | * |
| 517 | * For non-static method calls, this will verify that the method call is |
| 518 | * appropriate for the "this" argument. |
| 519 | * |
| 520 | * The method reference is in vBBBB. The "is_range" parameter determines |
| 521 | * whether we use 0-4 "args" values or a range of registers defined by |
| 522 | * vAA and vCCCC. |
| 523 | * |
| 524 | * Widening conversions on integers and references are allowed, but |
| 525 | * narrowing conversions are not. |
| 526 | * |
| 527 | * Returns the resolved method on success, NULL on failure (with *failure |
| 528 | * set appropriately). |
| 529 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 530 | mirror::AbstractMethod* VerifyInvocationArgs(const Instruction* inst, |
| 531 | MethodType method_type, |
| 532 | bool is_range, bool is_super) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 533 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 534 | |
| 535 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 536 | * Verify that the target instruction is not "move-exception". It's important that the only way |
| 537 | * to execute a move-exception is as the first instruction of an exception handler. |
| 538 | * Returns "true" if all is well, "false" if the target instruction is move-exception. |
| 539 | */ |
| 540 | bool CheckNotMoveException(const uint16_t* insns, int insn_idx); |
| 541 | |
| 542 | /* |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 543 | * Control can transfer to "next_insn". Merge the registers from merge_line into the table at |
| 544 | * next_insn, and set the changed flag on the target address if any of the registers were changed. |
| 545 | * Returns "false" if an error is encountered. |
| 546 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 547 | bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 548 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 549 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 550 | // Is the method being verified a constructor? |
| 551 | bool IsConstructor() const { |
| 552 | return (method_access_flags_ & kAccConstructor) != 0; |
| 553 | } |
| 554 | |
| 555 | // Is the method verified static? |
| 556 | bool IsStatic() const { |
| 557 | return (method_access_flags_ & kAccStatic) != 0; |
| 558 | } |
| 559 | |
| 560 | // Return the register type for the method. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 561 | const RegType& GetMethodReturnType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 562 | |
| 563 | // Get a type representing the declaring class of the method. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 564 | const RegType& GetDeclaringClass() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 565 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 566 | /* |
| 567 | * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of |
| 568 | * verification). For type-precise determination we have all the data we need, so we just need to |
| 569 | * encode it in some clever fashion. |
| 570 | * Returns a pointer to a newly-allocated RegisterMap, or NULL on failure. |
| 571 | */ |
| 572 | const std::vector<uint8_t>* GenerateGcMap(); |
| 573 | |
| 574 | // Verify that the GC map associated with method_ is well formed |
| 575 | void VerifyGcMap(const std::vector<uint8_t>& data); |
| 576 | |
| 577 | // Compute sizes for GC map data |
| 578 | void ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, size_t* log2_max_gc_pc); |
| 579 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 580 | InstructionFlags* CurrentInsnFlags(); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 581 | |
| 582 | // All the GC maps that the verifier has created |
Ian Rogers | e3cd2f0 | 2013-05-24 15:32:56 -0700 | [diff] [blame] | 583 | typedef SafeMap<const CompilerDriver::MethodReference, const std::vector<uint8_t>*, |
| 584 | CompilerDriver::MethodReferenceComparator> DexGcMapTable; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 585 | static ReaderWriterMutex* dex_gc_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 586 | static DexGcMapTable* dex_gc_maps_ GUARDED_BY(dex_gc_maps_lock_); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 587 | static void SetDexGcMap(CompilerDriver::MethodReference ref, const std::vector<uint8_t>& dex_gc_map) |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 588 | LOCKS_EXCLUDED(dex_gc_maps_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 589 | |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 590 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 591 | // Cast elision types. |
| 592 | typedef std::set<uint32_t> MethodSafeCastSet; |
| 593 | typedef SafeMap<const CompilerDriver::MethodReference, const MethodSafeCastSet*, |
| 594 | CompilerDriver::MethodReferenceComparator> SafeCastMap; |
| 595 | MethodVerifier::MethodSafeCastSet* GenerateSafeCastSet() |
| 596 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 597 | static void SetSafeCastMap(CompilerDriver::MethodReference ref, const MethodSafeCastSet* mscs); |
| 598 | LOCKS_EXCLUDED(safecast_map_lock_); |
| 599 | static Mutex* safecast_map_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 600 | static SafeCastMap* safecast_map_ GUARDED_BY(safecast_map_lock_); |
| 601 | |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 602 | // Devirtualization map. |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 603 | typedef SafeMap<const uint32_t, CompilerDriver::MethodReference> PcToConcreteMethodMap; |
| 604 | typedef SafeMap<const CompilerDriver::MethodReference, const PcToConcreteMethodMap*, |
Ian Rogers | e3cd2f0 | 2013-05-24 15:32:56 -0700 | [diff] [blame] | 605 | CompilerDriver::MethodReferenceComparator> DevirtualizationMapTable; |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 606 | MethodVerifier::PcToConcreteMethodMap* GenerateDevirtMap() |
Ian Rogers | 33e9566 | 2013-05-20 20:29:14 -0700 | [diff] [blame] | 607 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 608 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 609 | static ReaderWriterMutex* devirt_maps_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 610 | static DevirtualizationMapTable* devirt_maps_ GUARDED_BY(devirt_maps_lock_); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 611 | static void SetDevirtMap(CompilerDriver::MethodReference ref, |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 612 | const PcToConcreteMethodMap* pc_method_map) |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 613 | LOCKS_EXCLUDED(devirt_maps_lock_); |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 614 | typedef std::set<CompilerDriver::ClassReference> RejectedClassesTable; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 615 | static Mutex* rejected_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 616 | static RejectedClassesTable* rejected_classes_; |
| 617 | |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 618 | static void AddRejectedClass(CompilerDriver::ClassReference ref) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 619 | LOCKS_EXCLUDED(rejected_classes_lock_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 620 | |
| 621 | RegTypeCache reg_types_; |
| 622 | |
| 623 | PcToRegisterLineTable reg_table_; |
| 624 | |
| 625 | // Storage for the register status we're currently working on. |
| 626 | UniquePtr<RegisterLine> work_line_; |
| 627 | |
| 628 | // The address of the instruction we're currently working on, note that this is in 2 byte |
| 629 | // quantities |
| 630 | uint32_t work_insn_idx_; |
| 631 | |
| 632 | // Storage for the register status we're saving for later. |
| 633 | UniquePtr<RegisterLine> saved_line_; |
| 634 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 635 | const uint32_t dex_method_idx_; // The method we're working on. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 636 | // Its object representation if known. |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 637 | mirror::AbstractMethod* mirror_method_ GUARDED_BY(Locks::mutator_lock_); |
| 638 | const uint32_t method_access_flags_; // Method's access flags. |
| 639 | const DexFile* const dex_file_; // The dex file containing the method. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 640 | // The dex_cache for the declaring class of the method. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 641 | mirror::DexCache* dex_cache_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 642 | // The class loader for the declaring class of the method. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 643 | mirror::ClassLoader* class_loader_ GUARDED_BY(Locks::mutator_lock_); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 644 | const uint32_t class_def_idx_; // The class def index of the declaring class of the method. |
| 645 | const DexFile::CodeItem* const code_item_; // The code item containing the code for the method. |
| 646 | const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 647 | // Instruction widths and flags, one entry per code unit. |
| 648 | UniquePtr<InstructionFlags[]> insn_flags_; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 649 | // The dex PC of a FindLocksAtDexPc request, -1 otherwise. |
| 650 | uint32_t interesting_dex_pc_; |
| 651 | // The container into which FindLocksAtDexPc should write the registers containing held locks, |
| 652 | // NULL if we're not doing FindLocksAtDexPc. |
| 653 | std::vector<uint32_t>* monitor_enter_dex_pcs_; |
| 654 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 655 | // The types of any error that occurs. |
| 656 | std::vector<VerifyError> failures_; |
| 657 | // Error messages associated with failures. |
| 658 | std::vector<std::ostringstream*> failure_messages_; |
| 659 | // Is there a pending hard failure? |
| 660 | bool have_pending_hard_failure_; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 661 | // Is there a pending runtime throw failure? A runtime throw failure is when an instruction |
| 662 | // would fail at runtime throwing an exception. Such an instruction causes the following code |
| 663 | // to be unreachable. This is set by Fail and used to ensure we don't process unreachable |
| 664 | // instructions that would hard fail the verification. |
| 665 | bool have_pending_runtime_throw_failure_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 666 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 667 | // Info message log use primarily for verifier diagnostics. |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 668 | std::ostringstream info_messages_; |
| 669 | |
| 670 | // The number of occurrences of specific opcodes. |
| 671 | size_t new_instance_count_; |
| 672 | size_t monitor_enter_count_; |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 673 | |
| 674 | const bool can_load_classes_; |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 675 | |
| 676 | // Converts soft failures to hard failures when false. Only false when the compiler isn't |
| 677 | // running and the verifier is called from the class linker. |
| 678 | const bool allow_soft_failures_; |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 679 | }; |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 680 | std::ostream& operator<<(std::ostream& os, const MethodVerifier::FailureKind& rhs); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 681 | |
| 682 | } // namespace verifier |
| 683 | } // namespace art |
| 684 | |
| 685 | #endif // ART_SRC_VERIFIER_METHOD_VERIFIER_H_ |