Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [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 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 17 | #include "method_verifier-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 4a57d5f | 2019-04-03 12:31:18 -0700 | [diff] [blame] | 19 | #include <ostream> |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 20 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 25 | #include "base/aborting.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 26 | #include "base/enums.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "base/leb128.h" |
David Sehr | 9c4a015 | 2018-04-05 12:23:54 -0700 | [diff] [blame] | 28 | #include "base/indenter.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/logging.h" // For VLOG. |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 30 | #include "base/mutex-inl.h" |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 31 | #include "base/sdk_version.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 32 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 33 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | #include "base/time_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 35 | #include "base/utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 36 | #include "class_linker.h" |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 37 | #include "class_root.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 38 | #include "compiler_callbacks.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 39 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 40 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 41 | #include "dex/dex_file-inl.h" |
| 42 | #include "dex/dex_file_exception_helpers.h" |
| 43 | #include "dex/dex_instruction-inl.h" |
| 44 | #include "dex/dex_instruction_utils.h" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 45 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 47 | #include "handle_scope-inl.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 48 | #include "intern_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 50 | #include "mirror/class.h" |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 51 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 52 | #include "mirror/dex_cache-inl.h" |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 53 | #include "mirror/method_handle_impl.h" |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 54 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "mirror/object-inl.h" |
| 56 | #include "mirror/object_array-inl.h" |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 57 | #include "mirror/var_handle.h" |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 58 | #include "obj_ptr-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 59 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 60 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 61 | #include "runtime.h" |
Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 62 | #include "scoped_newline.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 63 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 64 | #include "stack.h" |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 65 | #include "vdex_file.h" |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 66 | #include "verifier/method_verifier.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 67 | #include "verifier_compiler_binding.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 68 | #include "verifier_deps.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 69 | |
| 70 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 71 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 72 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 73 | using android::base::StringPrintf; |
| 74 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 75 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 76 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 77 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator) |
| 78 | : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {} |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 79 | |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 80 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, |
| 81 | InstructionFlags* flags, |
| 82 | uint32_t insns_size, |
| 83 | uint16_t registers_size, |
| 84 | ScopedArenaAllocator& allocator, |
| 85 | RegTypeCache* reg_types) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 86 | DCHECK_GT(insns_size, 0U); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 87 | register_lines_.resize(insns_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 88 | for (uint32_t i = 0; i < insns_size; i++) { |
| 89 | bool interesting = false; |
| 90 | switch (mode) { |
| 91 | case kTrackRegsAll: |
| 92 | interesting = flags[i].IsOpcode(); |
| 93 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 94 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 95 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 96 | break; |
| 97 | case kTrackRegsBranches: |
| 98 | interesting = flags[i].IsBranchTarget(); |
| 99 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 100 | } |
| 101 | if (interesting) { |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 102 | register_lines_[i].reset(RegisterLine::Create(registers_size, allocator, reg_types)); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 107 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 108 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 109 | namespace impl { |
| 110 | namespace { |
| 111 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 112 | enum class CheckAccess { |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 113 | kNo, |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 114 | kOnResolvedClass, |
| 115 | kYes, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | enum class FieldAccessType { |
| 119 | kAccGet, |
| 120 | kAccPut |
| 121 | }; |
| 122 | |
Andreas Gampe | c658278 | 2019-07-10 15:06:18 -0700 | [diff] [blame] | 123 | // Instruction types that are not marked as throwing (because they normally would not), but for |
| 124 | // historical reasons may do so. These instructions cannot be marked kThrow as that would introduce |
| 125 | // a general flow that is unwanted. |
| 126 | // |
| 127 | // Note: Not implemented as Instruction::Flags value as that set is full and we'd need to increase |
| 128 | // the struct size (making it a non-power-of-two) for a single element. |
| 129 | // |
| 130 | // Note: This should eventually be removed. |
| 131 | constexpr bool IsCompatThrow(Instruction::Code opcode) { |
| 132 | return opcode == Instruction::Code::RETURN_OBJECT; |
| 133 | } |
| 134 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 135 | template <bool kVerifierDebug> |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 136 | class MethodVerifier final : public ::art::verifier::MethodVerifier { |
| 137 | public: |
| 138 | bool IsInstanceConstructor() const { |
| 139 | return IsConstructor() && !IsStatic(); |
| 140 | } |
| 141 | |
| 142 | const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 143 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 144 | DCHECK(!HasFailures()); |
| 145 | const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx); |
| 146 | DCHECK(!HasFailures()); |
| 147 | return result; |
| 148 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 149 | |
| 150 | void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_); |
| 151 | |
| 152 | private: |
| 153 | MethodVerifier(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 154 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 155 | ArenaPool* arena_pool, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 156 | const DexFile* dex_file, |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 157 | const dex::CodeItem* code_item, |
| 158 | uint32_t method_idx, |
| 159 | bool can_load_classes, |
| 160 | bool allow_thread_suspension, |
| 161 | bool allow_soft_failures, |
| 162 | bool aot_mode, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 163 | Handle<mirror::DexCache> dex_cache, |
| 164 | Handle<mirror::ClassLoader> class_loader, |
| 165 | const dex::ClassDef& class_def, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 166 | ArtMethod* method, |
| 167 | uint32_t access_flags, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 168 | bool need_precise_constants, |
| 169 | bool verify_to_dump, |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 170 | bool fill_register_lines, |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 171 | uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_) |
| 172 | : art::verifier::MethodVerifier(self, |
| 173 | class_linker, |
| 174 | arena_pool, |
| 175 | dex_file, |
| 176 | code_item, |
| 177 | method_idx, |
| 178 | can_load_classes, |
| 179 | allow_thread_suspension, |
| 180 | allow_soft_failures, |
| 181 | aot_mode), |
| 182 | method_being_verified_(method), |
| 183 | method_access_flags_(access_flags), |
| 184 | return_type_(nullptr), |
| 185 | dex_cache_(dex_cache), |
| 186 | class_loader_(class_loader), |
| 187 | class_def_(class_def), |
| 188 | declaring_class_(nullptr), |
| 189 | interesting_dex_pc_(-1), |
| 190 | monitor_enter_dex_pcs_(nullptr), |
| 191 | need_precise_constants_(need_precise_constants), |
| 192 | verify_to_dump_(verify_to_dump), |
| 193 | allow_thread_suspension_(allow_thread_suspension), |
| 194 | is_constructor_(false), |
| 195 | fill_register_lines_(fill_register_lines), |
| 196 | api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) { |
| 197 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 198 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 199 | void UninstantiableError(const char* descriptor) { |
| 200 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 201 | << "non-instantiable klass " << descriptor; |
| 202 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 203 | static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 204 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 205 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 206 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 207 | |
| 208 | // Is the method being verified a constructor? See the comment on the field. |
| 209 | bool IsConstructor() const { |
| 210 | return is_constructor_; |
| 211 | } |
| 212 | |
| 213 | // Is the method verified static? |
| 214 | bool IsStatic() const { |
| 215 | return (method_access_flags_ & kAccStatic) != 0; |
| 216 | } |
| 217 | |
| 218 | // Adds the given string to the beginning of the last failure message. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 219 | void PrependToLastFailMessage(std::string prepend) { |
| 220 | size_t failure_num = failure_messages_.size(); |
| 221 | DCHECK_NE(failure_num, 0U); |
| 222 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 223 | prepend += last_fail_message->str(); |
| 224 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
| 225 | delete last_fail_message; |
| 226 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 227 | |
| 228 | // Adds the given string to the end of the last failure message. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 229 | void AppendToLastFailMessage(const std::string& append) { |
| 230 | size_t failure_num = failure_messages_.size(); |
| 231 | DCHECK_NE(failure_num, 0U); |
| 232 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 233 | (*last_fail_message) << append; |
| 234 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 235 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 236 | /* |
| 237 | * Compute the width of the instruction at each address in the instruction stream, and store it in |
| 238 | * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch |
| 239 | * table data, are not touched (so the caller should probably initialize "insn_flags" to zero). |
| 240 | * |
| 241 | * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set. |
| 242 | * |
| 243 | * Performs some static checks, notably: |
| 244 | * - opcode of first instruction begins at index 0 |
| 245 | * - only documented instructions may appear |
| 246 | * - each instruction follows the last |
| 247 | * - last byte of last instruction is at (code_length-1) |
| 248 | * |
| 249 | * Logs an error and returns "false" on failure. |
| 250 | */ |
| 251 | bool ComputeWidthsAndCountOps(); |
| 252 | |
| 253 | /* |
| 254 | * Set the "in try" flags for all instructions protected by "try" statements. Also sets the |
| 255 | * "branch target" flags for exception handlers. |
| 256 | * |
| 257 | * Call this after widths have been set in "insn_flags". |
| 258 | * |
| 259 | * Returns "false" if something in the exception table looks fishy, but we're expecting the |
| 260 | * exception table to be somewhat sane. |
| 261 | */ |
| 262 | bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_); |
| 263 | |
| 264 | /* |
| 265 | * Perform static verification on all instructions in a method. |
| 266 | * |
| 267 | * Walks through instructions in a method calling VerifyInstruction on each. |
| 268 | */ |
| 269 | template <bool kAllowRuntimeOnlyInstructions> |
| 270 | bool VerifyInstructions(); |
| 271 | |
| 272 | /* |
| 273 | * Perform static verification on an instruction. |
| 274 | * |
| 275 | * As a side effect, this sets the "branch target" flags in InsnFlags. |
| 276 | * |
| 277 | * "(CF)" items are handled during code-flow analysis. |
| 278 | * |
| 279 | * v3 4.10.1 |
| 280 | * - target of each jump and branch instruction must be valid |
| 281 | * - targets of switch statements must be valid |
| 282 | * - operands referencing constant pool entries must be valid |
| 283 | * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid |
| 284 | * - (CF) operands of method invocation instructions must be valid |
| 285 | * - (CF) only invoke-direct can call a method starting with '<' |
| 286 | * - (CF) <clinit> must never be called explicitly |
| 287 | * - operands of instanceof, checkcast, new (and variants) must be valid |
| 288 | * - new-array[-type] limited to 255 dimensions |
| 289 | * - can't use "new" on an array class |
| 290 | * - (?) limit dimensions in multi-array creation |
| 291 | * - local variable load/store register values must be in valid range |
| 292 | * |
| 293 | * v3 4.11.1.2 |
| 294 | * - branches must be within the bounds of the code array |
| 295 | * - targets of all control-flow instructions are the start of an instruction |
| 296 | * - register accesses fall within range of allocated registers |
| 297 | * - (N/A) access to constant pool must be of appropriate type |
| 298 | * - code does not end in the middle of an instruction |
| 299 | * - execution cannot fall off the end of the code |
| 300 | * - (earlier) for each exception handler, the "try" area must begin and |
| 301 | * end at the start of an instruction (end can be at the end of the code) |
| 302 | * - (earlier) for each exception handler, the handler must start at a valid |
| 303 | * instruction |
| 304 | */ |
| 305 | template <bool kAllowRuntimeOnlyInstructions> |
| 306 | bool VerifyInstruction(const Instruction* inst, uint32_t code_offset); |
| 307 | |
| 308 | /* Ensure that the register index is valid for this code item. */ |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 309 | bool CheckRegisterIndex(uint32_t idx) { |
| 310 | if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) { |
| 311 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 312 | << code_item_accessor_.RegistersSize() << ")"; |
| 313 | return false; |
| 314 | } |
| 315 | return true; |
| 316 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 317 | |
| 318 | /* Ensure that the wide register index is valid for this code item. */ |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 319 | bool CheckWideRegisterIndex(uint32_t idx) { |
| 320 | if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) { |
| 321 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 322 | << "+1 >= " << code_item_accessor_.RegistersSize() << ")"; |
| 323 | return false; |
| 324 | } |
| 325 | return true; |
| 326 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 327 | |
| 328 | // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that |
| 329 | // the call site index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 330 | bool CheckCallSiteIndex(uint32_t idx) { |
| 331 | uint32_t limit = dex_file_->NumCallSiteIds(); |
| 332 | if (UNLIKELY(idx >= limit)) { |
| 333 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max " |
| 334 | << limit << ")"; |
| 335 | return false; |
| 336 | } |
| 337 | return true; |
| 338 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 339 | |
| 340 | // Perform static checks on a field Get or set instruction. All we do here is ensure that the |
| 341 | // field index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 342 | bool CheckFieldIndex(uint32_t idx) { |
| 343 | if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) { |
| 344 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 345 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
| 346 | return false; |
| 347 | } |
| 348 | return true; |
| 349 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 350 | |
| 351 | // Perform static checks on a method invocation instruction. All we do here is ensure that the |
| 352 | // method index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 353 | bool CheckMethodIndex(uint32_t idx) { |
| 354 | if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) { |
| 355 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 356 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
| 357 | return false; |
| 358 | } |
| 359 | return true; |
| 360 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 361 | |
| 362 | // Perform static checks on an instruction referencing a constant method handle. All we do here |
| 363 | // is ensure that the method index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 364 | bool CheckMethodHandleIndex(uint32_t idx) { |
| 365 | uint32_t limit = dex_file_->NumMethodHandles(); |
| 366 | if (UNLIKELY(idx >= limit)) { |
| 367 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max " |
| 368 | << limit << ")"; |
| 369 | return false; |
| 370 | } |
| 371 | return true; |
| 372 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 373 | |
| 374 | // Perform static checks on a "new-instance" instruction. Specifically, make sure the class |
| 375 | // reference isn't for an array class. |
| 376 | bool CheckNewInstance(dex::TypeIndex idx); |
| 377 | |
| 378 | // Perform static checks on a prototype indexing instruction. All we do here is ensure that the |
| 379 | // prototype index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 380 | bool CheckPrototypeIndex(uint32_t idx) { |
| 381 | if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) { |
| 382 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max " |
| 383 | << dex_file_->GetHeader().proto_ids_size_ << ")"; |
| 384 | return false; |
| 385 | } |
| 386 | return true; |
| 387 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 388 | |
| 389 | /* Ensure that the string index is in the valid range. */ |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 390 | bool CheckStringIndex(uint32_t idx) { |
| 391 | if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) { |
| 392 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 393 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
| 394 | return false; |
| 395 | } |
| 396 | return true; |
| 397 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 398 | |
| 399 | // Perform static checks on an instruction that takes a class constant. Ensure that the class |
| 400 | // index is in the valid range. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 401 | bool CheckTypeIndex(dex::TypeIndex idx) { |
| 402 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
| 403 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
| 404 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| 405 | return false; |
| 406 | } |
| 407 | return true; |
| 408 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 409 | |
| 410 | // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't |
| 411 | // creating an array of arrays that causes the number of dimensions to exceed 255. |
| 412 | bool CheckNewArray(dex::TypeIndex idx); |
| 413 | |
| 414 | // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction. |
| 415 | bool CheckArrayData(uint32_t cur_offset); |
| 416 | |
| 417 | // Verify that the target of a branch instruction is valid. We don't expect code to jump directly |
| 418 | // into an exception handler, but it's valid to do so as long as the target isn't a |
| 419 | // "move-exception" instruction. We verify that in a later stage. |
| 420 | // The dex format forbids certain instructions from branching to themselves. |
| 421 | // Updates "insn_flags_", setting the "branch target" flag. |
| 422 | bool CheckBranchTarget(uint32_t cur_offset); |
| 423 | |
| 424 | // Verify a switch table. "cur_offset" is the offset of the switch instruction. |
| 425 | // Updates "insn_flags_", setting the "branch target" flag. |
| 426 | bool CheckSwitchTargets(uint32_t cur_offset); |
| 427 | |
| 428 | // Check the register indices used in a "vararg" instruction, such as invoke-virtual or |
| 429 | // filled-new-array. |
| 430 | // - vA holds word count (0-5), args[] have values. |
| 431 | // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that |
| 432 | // takes a double is done with consecutive registers. This requires parsing the target method |
| 433 | // signature, which we will be doing later on during the code flow analysis. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 434 | bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
| 435 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
| 436 | for (uint32_t idx = 0; idx < vA; idx++) { |
| 437 | if (UNLIKELY(arg[idx] >= registers_size)) { |
| 438 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 439 | << ") in non-range invoke (>= " << registers_size << ")"; |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return true; |
| 445 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 446 | |
| 447 | // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range |
| 448 | // or filled-new-array/range. |
| 449 | // - vA holds word count, vC holds index of first reg. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 450 | bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
| 451 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
| 452 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 453 | // integer overflow when adding them here. |
| 454 | if (UNLIKELY(vA + vC > registers_size)) { |
| 455 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 456 | << " in range invoke (> " << registers_size << ")"; |
| 457 | return false; |
| 458 | } |
| 459 | return true; |
| 460 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 461 | |
| 462 | // Checks the method matches the expectations required to be signature polymorphic. |
| 463 | bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
| 464 | |
| 465 | // Checks the invoked receiver matches the expectations for signature polymorphic methods. |
| 466 | bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_); |
| 467 | |
| 468 | // Extract the relative offset from a branch instruction. |
| 469 | // Returns "false" on failure (e.g. this isn't a branch instruction). |
| 470 | bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 471 | bool* selfOkay); |
| 472 | |
| 473 | /* Perform detailed code-flow analysis on a single method. */ |
| 474 | bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_); |
| 475 | |
| 476 | // Set the register types for the first instruction in the method based on the method signature. |
| 477 | // This has the side-effect of validating the signature. |
| 478 | bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_); |
| 479 | |
| 480 | /* |
| 481 | * Perform code flow on a method. |
| 482 | * |
| 483 | * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first |
| 484 | * instruction, process it (setting additional "changed" bits), and repeat until there are no |
| 485 | * more. |
| 486 | * |
| 487 | * v3 4.11.1.1 |
| 488 | * - (N/A) operand stack is always the same size |
| 489 | * - operand stack [registers] contain the correct types of values |
| 490 | * - local variables [registers] contain the correct types of values |
| 491 | * - methods are invoked with the appropriate arguments |
| 492 | * - fields are assigned using values of appropriate types |
| 493 | * - opcodes have the correct type values in operand registers |
| 494 | * - there is never an uninitialized class instance in a local variable in code protected by an |
| 495 | * exception handler (operand stack is okay, because the operand stack is discarded when an |
| 496 | * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of |
| 497 | * register typing] |
| 498 | * |
| 499 | * v3 4.11.1.2 |
| 500 | * - execution cannot fall off the end of the code |
| 501 | * |
| 502 | * (We also do many of the items described in the "static checks" sections, because it's easier to |
| 503 | * do them here.) |
| 504 | * |
| 505 | * We need an array of RegType values, one per register, for every instruction. If the method uses |
| 506 | * monitor-enter, we need extra data for every register, and a stack for every "interesting" |
| 507 | * instruction. In theory this could become quite large -- up to several megabytes for a monster |
| 508 | * function. |
| 509 | * |
| 510 | * NOTE: |
| 511 | * The spec forbids backward branches when there's an uninitialized reference in a register. The |
| 512 | * idea is to prevent something like this: |
| 513 | * loop: |
| 514 | * move r1, r0 |
| 515 | * new-instance r0, MyClass |
| 516 | * ... |
| 517 | * if-eq rN, loop // once |
| 518 | * initialize r0 |
| 519 | * |
| 520 | * This leaves us with two different instances, both allocated by the same instruction, but only |
| 521 | * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around |
| 522 | * it by preventing backward branches. We achieve identical results without restricting code |
| 523 | * reordering by specifying that you can't execute the new-instance instruction if a register |
| 524 | * contains an uninitialized instance created by that same instruction. |
| 525 | */ |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 526 | template <bool kMonitorDexPCs> |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 527 | bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_); |
| 528 | |
| 529 | /* |
| 530 | * Perform verification for a single instruction. |
| 531 | * |
| 532 | * This requires fully decoding the instruction to determine the effect it has on registers. |
| 533 | * |
| 534 | * Finds zero or more following instructions and sets the "changed" flag if execution at that |
| 535 | * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target |
| 536 | * addresses. Does not set or clear any other flags in "insn_flags_". |
| 537 | */ |
| 538 | bool CodeFlowVerifyInstruction(uint32_t* start_guess) |
| 539 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 540 | |
| 541 | // Perform verification of a new array instruction |
| 542 | void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) |
| 543 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 544 | |
| 545 | // Helper to perform verification on puts of primitive type. |
| 546 | void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
| 547 | const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_); |
| 548 | |
| 549 | // Perform verification of an aget instruction. The destination register's type will be set to |
| 550 | // be that of component type of the array unless the array type is unknown, in which case a |
| 551 | // bottom type inferred from the type of instruction is used. is_primitive is false for an |
| 552 | // aget-object. |
| 553 | void VerifyAGet(const Instruction* inst, const RegType& insn_type, |
| 554 | bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_); |
| 555 | |
| 556 | // Perform verification of an aput instruction. |
| 557 | void VerifyAPut(const Instruction* inst, const RegType& insn_type, |
| 558 | bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_); |
| 559 | |
| 560 | // Lookup instance field and fail for resolution violations |
| 561 | ArtField* GetInstanceField(const RegType& obj_type, int field_idx) |
| 562 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 563 | |
| 564 | // Lookup static field and fail for resolution violations |
| 565 | ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_); |
| 566 | |
| 567 | // Perform verification of an iget/sget/iput/sput instruction. |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 568 | template <FieldAccessType kAccType> |
| 569 | void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 570 | bool is_primitive, bool is_static) |
| 571 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 572 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 573 | // Resolves a class based on an index and, if C is kYes, performs access checks to ensure |
| 574 | // the referrer can access the resolved class. |
| 575 | template <CheckAccess C> |
| 576 | const RegType& ResolveClass(dex::TypeIndex class_idx) |
| 577 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 578 | |
| 579 | /* |
| 580 | * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler |
| 581 | * address, determine the Join of all exceptions that can land here. Fails if no matching |
| 582 | * exception handler can be found or if the Join of exception types fails. |
| 583 | */ |
| 584 | const RegType& GetCaughtExceptionType() |
| 585 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 586 | |
| 587 | /* |
| 588 | * Resolves a method based on an index and performs access checks to ensure |
| 589 | * the referrer can access the resolved method. |
| 590 | * Does not throw exceptions. |
| 591 | */ |
| 592 | ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) |
| 593 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 594 | |
| 595 | /* |
| 596 | * Verify the arguments to a method. We're executing in "method", making |
| 597 | * a call to the method reference in vB. |
| 598 | * |
| 599 | * If this is a "direct" invoke, we allow calls to <init>. For calls to |
| 600 | * <init>, the first argument may be an uninitialized reference. Otherwise, |
| 601 | * calls to anything starting with '<' will be rejected, as will any |
| 602 | * uninitialized reference arguments. |
| 603 | * |
| 604 | * For non-static method calls, this will verify that the method call is |
| 605 | * appropriate for the "this" argument. |
| 606 | * |
| 607 | * The method reference is in vBBBB. The "is_range" parameter determines |
| 608 | * whether we use 0-4 "args" values or a range of registers defined by |
| 609 | * vAA and vCCCC. |
| 610 | * |
| 611 | * Widening conversions on integers and references are allowed, but |
| 612 | * narrowing conversions are not. |
| 613 | * |
| 614 | * Returns the resolved method on success, null on failure (with *failure |
| 615 | * set appropriately). |
| 616 | */ |
| 617 | ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range) |
| 618 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 619 | |
| 620 | // Similar checks to the above, but on the proto. Will be used when the method cannot be |
| 621 | // resolved. |
| 622 | void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type, |
| 623 | bool is_range) |
| 624 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 625 | |
| 626 | template <class T> |
| 627 | ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst, |
| 628 | MethodType method_type, bool is_range, |
| 629 | ArtMethod* res_method) |
| 630 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 631 | |
| 632 | /* |
| 633 | * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise. |
| 634 | */ |
| 635 | bool CheckCallSite(uint32_t call_site_idx); |
| 636 | |
| 637 | /* |
| 638 | * Verify that the target instruction is not "move-exception". It's important that the only way |
| 639 | * to execute a move-exception is as the first instruction of an exception handler. |
| 640 | * Returns "true" if all is well, "false" if the target instruction is move-exception. |
| 641 | */ |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 642 | bool CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
| 643 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
| 644 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
| 645 | return false; |
| 646 | } |
| 647 | return true; |
| 648 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 649 | |
| 650 | /* |
| 651 | * Verify that the target instruction is not "move-result". It is important that we cannot |
| 652 | * branch to move-result instructions, but we have to make this a distinct check instead of |
| 653 | * adding it to CheckNotMoveException, because it is legal to continue into "move-result" |
| 654 | * instructions - as long as the previous instruction was an invoke, which is checked elsewhere. |
| 655 | */ |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 656 | bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 657 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 658 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 659 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 660 | return false; |
| 661 | } |
| 662 | return true; |
| 663 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Verify that the target instruction is not "move-result" or "move-exception". This is to |
| 667 | * be used when checking branch and switch instructions, but not instructions that can |
| 668 | * continue. |
| 669 | */ |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 670 | bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 671 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 672 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * Control can transfer to "next_insn". Merge the registers from merge_line into the table at |
| 676 | * next_insn, and set the changed flag on the target address if any of the registers were changed. |
| 677 | * In the case of fall-through, update the merge line on a change as its the working line for the |
| 678 | * next instruction. |
| 679 | * Returns "false" if an error is encountered. |
| 680 | */ |
| 681 | bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line) |
| 682 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 683 | |
| 684 | // Return the register type for the method. |
| 685 | const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_); |
| 686 | |
| 687 | // Get a type representing the declaring class of the method. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 688 | const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 689 | if (declaring_class_ == nullptr) { |
| 690 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 691 | const char* descriptor |
| 692 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
| 693 | if (method_being_verified_ != nullptr) { |
| 694 | ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass(); |
| 695 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
| 696 | } else { |
| 697 | declaring_class_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| 698 | } |
| 699 | } |
| 700 | return *declaring_class_; |
| 701 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 702 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 703 | InstructionFlags* CurrentInsnFlags() { |
| 704 | return &GetModifiableInstructionFlags(work_insn_idx_); |
| 705 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 706 | |
| 707 | const RegType& DetermineCat1Constant(int32_t value, bool precise) |
| 708 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 709 | |
| 710 | // Try to create a register type from the given class. In case a precise type is requested, but |
| 711 | // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a |
| 712 | // non-precise reference will be returned. |
| 713 | // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is |
| 714 | // actually touched. |
| 715 | const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise) |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 716 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 717 | DCHECK(klass != nullptr); |
| 718 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 719 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 720 | << "non-instantiable klass " << descriptor; |
| 721 | precise = false; |
| 722 | } |
| 723 | return reg_types_.FromClass(descriptor, klass, precise); |
| 724 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 725 | |
| 726 | ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx); |
| 727 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 728 | ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) { |
| 729 | return insn_flags_[index]; |
| 730 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 731 | |
| 732 | // Returns the method index of an invoke instruction. |
| 733 | uint16_t GetMethodIdxOfInvoke(const Instruction* inst) |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 734 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 735 | switch (inst->Opcode()) { |
| 736 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
| 737 | case Instruction::INVOKE_VIRTUAL_QUICK: { |
| 738 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 739 | << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_; |
| 740 | DCHECK(method_being_verified_ != nullptr); |
| 741 | uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_); |
| 742 | CHECK_NE(method_idx, DexFile::kDexNoIndex16); |
| 743 | return method_idx; |
| 744 | } |
| 745 | default: { |
| 746 | return inst->VRegB(); |
| 747 | } |
| 748 | } |
| 749 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 750 | // Returns the field index of a field access instruction. |
| 751 | uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 752 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 753 | if (is_static) { |
| 754 | return inst->VRegB_21c(); |
| 755 | } else if (inst->IsQuickened()) { |
| 756 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
| 757 | DCHECK(method_being_verified_ != nullptr); |
| 758 | uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_); |
| 759 | CHECK_NE(field_idx, DexFile::kDexNoIndex16); |
| 760 | return field_idx; |
| 761 | } else { |
| 762 | return inst->VRegC_22c(); |
| 763 | } |
| 764 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 765 | |
| 766 | // Run verification on the method. Returns true if verification completes and false if the input |
| 767 | // has an irrecoverable corruption. |
| 768 | bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_); |
| 769 | |
| 770 | // Dump the failures encountered by the verifier. |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 771 | std::ostream& DumpFailures(std::ostream& os) { |
| 772 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Andreas Gampe | 4ec4d48 | 2019-08-01 10:00:23 -0700 | [diff] [blame] | 773 | for (const auto* stream : failure_messages_) { |
| 774 | os << stream->str() << "\n"; |
Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 775 | } |
| 776 | return os; |
| 777 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 778 | |
| 779 | // Dump the state of the verifier, namely each instruction, what flags are set on it, register |
| 780 | // information |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 781 | void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 782 | VariableIndentationOutputStream vios(&os); |
| 783 | Dump(&vios); |
| 784 | } |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 785 | void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_); |
| 786 | |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 787 | bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_); |
| 788 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 789 | ArtMethod* method_being_verified_; // Its ArtMethod representation if known. |
| 790 | const uint32_t method_access_flags_; // Method's access flags. |
| 791 | const RegType* return_type_; // Lazily computed return type of the method. |
| 792 | // The dex_cache for the declaring class of the method. |
| 793 | Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_); |
| 794 | // The class loader for the declaring class of the method. |
| 795 | Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_); |
| 796 | const dex::ClassDef& class_def_; // The class def of the declaring class of the method. |
| 797 | const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class. |
| 798 | |
| 799 | // The dex PC of a FindLocksAtDexPc request, -1 otherwise. |
| 800 | uint32_t interesting_dex_pc_; |
| 801 | // The container into which FindLocksAtDexPc should write the registers containing held locks, |
| 802 | // null if we're not doing FindLocksAtDexPc. |
| 803 | std::vector<DexLockInfo>* monitor_enter_dex_pcs_; |
| 804 | |
| 805 | |
| 806 | // An optimization where instead of generating unique RegTypes for constants we use imprecise |
| 807 | // constants that cover a range of constants. This isn't good enough for deoptimization that |
| 808 | // avoids loading from registers in the case of a constant as the dex instruction set lost the |
| 809 | // notion of whether a value should be in a floating point or general purpose register file. |
| 810 | const bool need_precise_constants_; |
| 811 | |
| 812 | // Indicates whether we verify to dump the info. In that case we accept quickened instructions |
| 813 | // even though we might detect to be a compiler. Should only be set when running |
| 814 | // VerifyMethodAndDump. |
| 815 | const bool verify_to_dump_; |
| 816 | |
| 817 | // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for |
| 818 | // thread dumping checkpoints since we may get thread suspension at an inopportune time due to |
| 819 | // FindLocksAtDexPC, resulting in deadlocks. |
| 820 | const bool allow_thread_suspension_; |
| 821 | |
| 822 | // Whether the method seems to be a constructor. Note that this field exists as we can't trust |
| 823 | // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>" |
| 824 | // correctly. |
| 825 | // |
| 826 | // Note: this flag is only valid once Verify() has started. |
| 827 | bool is_constructor_; |
| 828 | |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 829 | // Whether to attempt to fill all register lines for (ex) debugger use. |
| 830 | bool fill_register_lines_; |
| 831 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 832 | // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks. |
| 833 | // Instead, unset level should correspond to max(). |
| 834 | const uint32_t api_level_; |
| 835 | |
| 836 | friend class ::art::verifier::MethodVerifier; |
| 837 | |
| 838 | DISALLOW_COPY_AND_ASSIGN(MethodVerifier); |
| 839 | }; |
| 840 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 841 | // Note: returns true on failure. |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 842 | template <bool kVerifierDebug> |
| 843 | inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition, |
| 844 | const char* error_msg, |
| 845 | uint32_t work_insn_idx) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 846 | if (kIsDebugBuild) { |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 847 | // In a debug build, abort if the error condition is wrong. Only warn if |
| 848 | // we are already aborting (as this verification is likely run to print |
| 849 | // lock information). |
| 850 | if (LIKELY(gAborting == 0)) { |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 851 | DCHECK(condition) << error_msg << work_insn_idx << " " |
| 852 | << dex_file_->PrettyMethod(dex_method_idx_); |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 853 | } else { |
| 854 | if (!condition) { |
| 855 | LOG(ERROR) << error_msg << work_insn_idx; |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 856 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 857 | return true; |
| 858 | } |
| 859 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 860 | } else { |
| 861 | // In a non-debug build, just fail the class. |
| 862 | if (!condition) { |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 863 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 864 | return true; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | return false; |
| 869 | } |
| 870 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 871 | static bool IsLargeMethod(const CodeItemDataAccessor& accessor) { |
| 872 | if (!accessor.HasCodeItem()) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 873 | return false; |
| 874 | } |
| 875 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 876 | uint16_t registers_size = accessor.RegistersSize(); |
| 877 | uint32_t insns_size = accessor.InsnsSizeInCodeUnits(); |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 878 | |
| 879 | return registers_size * insns_size > 4*1024*1024; |
| 880 | } |
| 881 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 882 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 883 | void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 884 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 885 | CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 886 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 887 | // Quick check whether there are any monitor_enter instructions before verifying. |
| 888 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| 889 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 890 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 891 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 892 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 893 | // got what we wanted. |
| 894 | Verify(); |
| 895 | return; |
| 896 | } |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 897 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 900 | template <bool kVerifierDebug> |
| 901 | bool MethodVerifier<kVerifierDebug>::Verify() { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 902 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 903 | // the name. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 904 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 905 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 906 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 907 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 908 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 909 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 910 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 911 | if (!constructor_by_name) { |
| 912 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 913 | << "method is marked as constructor, but not named accordingly"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 914 | return false; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 915 | } |
| 916 | is_constructor_ = true; |
| 917 | } else if (constructor_by_name) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 918 | LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_) |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 919 | << " not marked as constructor."; |
| 920 | is_constructor_ = true; |
| 921 | } |
| 922 | // If it's a constructor, check whether IsStatic() matches the name. |
| 923 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 924 | if (kIsDebugBuild) { |
| 925 | if (IsConstructor()) { |
| 926 | if (IsStatic() ^ static_constructor_by_name) { |
| 927 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 928 | << "constructor name doesn't match static flag"; |
| 929 | return false; |
| 930 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 931 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 932 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 933 | |
| 934 | // Methods may only have one of public/protected/private. |
| 935 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 936 | if (kIsDebugBuild) { |
| 937 | size_t access_mod_count = |
| 938 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 939 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 940 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 941 | if (access_mod_count > 1) { |
| 942 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 943 | return false; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 948 | if (!code_item_accessor_.HasCodeItem()) { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 949 | // Only native or abstract methods may not have code. |
| 950 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 951 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 952 | return false; |
| 953 | } |
| 954 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 955 | // This should have been rejected by the dex file verifier. Only do in debug build. |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 956 | // Note: the above will also be rejected in the dex file verifier, starting in dex version 37. |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 957 | if (kIsDebugBuild) { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 958 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 959 | // Abstract methods are not allowed to have the following flags. |
| 960 | static constexpr uint32_t kForbidden = |
| 961 | kAccPrivate | |
| 962 | kAccStatic | |
| 963 | kAccFinal | |
| 964 | kAccNative | |
| 965 | kAccStrict | |
| 966 | kAccSynchronized; |
| 967 | if ((method_access_flags_ & kForbidden) != 0) { |
| 968 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 969 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 970 | return false; |
| 971 | } |
| 972 | } |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 973 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 974 | // Interface methods must be public and abstract (if default methods are disabled). |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 975 | uint32_t kRequired = kAccPublic; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 976 | if ((method_access_flags_ & kRequired) != kRequired) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 977 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 978 | return false; |
| 979 | } |
| 980 | // In addition to the above, interface methods must not be protected. |
| 981 | static constexpr uint32_t kForbidden = kAccProtected; |
| 982 | if ((method_access_flags_ & kForbidden) != 0) { |
| 983 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 984 | return false; |
| 985 | } |
| 986 | } |
| 987 | // We also don't allow constructors to be abstract or native. |
| 988 | if (IsConstructor()) { |
| 989 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 990 | return false; |
| 991 | } |
| 992 | } |
| 993 | return true; |
| 994 | } |
| 995 | |
| 996 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 997 | if (kIsDebugBuild) { |
| 998 | // When there's code, the method must not be native or abstract. |
| 999 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 1000 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 1004 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 1005 | // Interfaces may always have static initializers for their fields. If we are running with |
| 1006 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 1007 | // Otherwise that is the only type of method allowed. |
Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 1008 | if (!(IsConstructor() && IsStatic())) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 1009 | if (IsInstanceConstructor()) { |
| 1010 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 1011 | return false; |
| 1012 | } else if (method_access_flags_ & kAccFinal) { |
| 1013 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 1014 | return false; |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 1015 | } else { |
| 1016 | uint32_t access_flag_options = kAccPublic; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 1017 | if (dex_file_->SupportsDefaultMethods()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 1018 | access_flag_options |= kAccPrivate; |
| 1019 | } |
| 1020 | if (!(method_access_flags_ & access_flag_options)) { |
| 1021 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1022 | << "interfaces may not have protected or package-private members"; |
| 1023 | return false; |
| 1024 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 1025 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Instance constructors must not be synchronized. |
| 1030 | if (IsInstanceConstructor()) { |
| 1031 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 1032 | if ((method_access_flags_ & kForbidden) != 0) { |
| 1033 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 1034 | return false; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1040 | if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) { |
| 1041 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" |
| 1042 | << code_item_accessor_.InsSize() |
| 1043 | << " regs=" << code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1044 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1045 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 1046 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1047 | // Allocate and initialize an array to hold instruction data. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1048 | insn_flags_.reset(allocator_.AllocArray<InstructionFlags>( |
| 1049 | code_item_accessor_.InsnsSizeInCodeUnits())); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1050 | DCHECK(insn_flags_ != nullptr); |
| 1051 | std::uninitialized_fill_n(insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1052 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1053 | InstructionFlags()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1054 | // Run through the instructions and see if the width checks out. |
| 1055 | bool result = ComputeWidthsAndCountOps(); |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 1056 | bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1057 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 1058 | result = result && ScanTryCatchBlocks(); |
| 1059 | // Perform static instruction verification. |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1060 | result = result && (allow_runtime_only_instructions |
| 1061 | ? VerifyInstructions<true>() |
| 1062 | : VerifyInstructions<false>()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1063 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 1064 | result = result && VerifyCodeFlow(); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 1065 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 1066 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1069 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1070 | bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1071 | // We can't assume the instruction is well formed, handle the case where calculating the size |
| 1072 | // goes past the end of the code item. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1073 | SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end()); |
| 1074 | for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1075 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 1076 | SafeDexInstructionIterator next = it; |
| 1077 | ++next; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1078 | if (next.IsErrorState()) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | } |
| 1081 | Instruction::Code opcode = it->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1082 | switch (opcode) { |
| 1083 | case Instruction::APUT_OBJECT: |
| 1084 | case Instruction::CHECK_CAST: |
| 1085 | has_check_casts_ = true; |
| 1086 | break; |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1087 | default: |
| 1088 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1089 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1090 | GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1093 | if (it != code_item_accessor_.end()) { |
| 1094 | const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1095 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1096 | << it.DexPc() << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1097 | return false; |
| 1098 | } |
Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 1099 | DCHECK(GetInstructionFlags(0).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1100 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1101 | return true; |
| 1102 | } |
| 1103 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1104 | template <bool kVerifierDebug> |
| 1105 | bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1106 | const uint32_t tries_size = code_item_accessor_.TriesSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1107 | if (tries_size == 0) { |
| 1108 | return true; |
| 1109 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1110 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1111 | for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1112 | const uint32_t start = try_item.start_addr_; |
| 1113 | const uint32_t end = start + try_item.insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1114 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1115 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1116 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1117 | return false; |
| 1118 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1119 | if (!GetInstructionFlags(start).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1120 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1121 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1124 | DexInstructionIterator end_it(code_item_accessor_.Insns(), end); |
| 1125 | for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1126 | GetModifiableInstructionFlags(it.DexPc()).SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1129 | // Iterate over each of the handlers to verify target addresses. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1130 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 1131 | const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1132 | ClassLinker* linker = GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1133 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1134 | CatchHandlerIterator iterator(handlers_ptr); |
| 1135 | for (; iterator.HasNext(); iterator.Next()) { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 1136 | uint32_t dex_pc = iterator.GetHandlerAddress(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1137 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1138 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1139 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1140 | return false; |
| 1141 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1142 | if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) { |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 1143 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1144 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 1145 | return false; |
| 1146 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1147 | GetModifiableInstructionFlags(dex_pc).SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1148 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1149 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1150 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1151 | ObjPtr<mirror::Class> exception_type = |
| 1152 | linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1153 | if (exception_type == nullptr) { |
| 1154 | DCHECK(self_->IsExceptionPending()); |
| 1155 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1156 | } |
| 1157 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1158 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1159 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1160 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1161 | return true; |
| 1162 | } |
| 1163 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1164 | template <bool kVerifierDebug> |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1165 | template <bool kAllowRuntimeOnlyInstructions> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1166 | bool MethodVerifier<kVerifierDebug>::VerifyInstructions() { |
Andreas Gampe | 93adcb5 | 2019-06-06 20:16:07 -0700 | [diff] [blame] | 1167 | // Flag the start of the method as a branch target. |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1168 | GetModifiableInstructionFlags(0).SetBranchTarget(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1169 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1170 | const uint32_t dex_pc = inst.DexPc(); |
| 1171 | if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1172 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
Andreas Gampe | 93adcb5 | 2019-06-06 20:16:07 -0700 | [diff] [blame] | 1175 | // Flag some interesting instructions. |
| 1176 | if (inst->IsReturn()) { |
| 1177 | GetModifiableInstructionFlags(dex_pc).SetReturn(); |
| 1178 | } else if (inst->Opcode() == Instruction::CHECK_CAST) { |
| 1179 | // The dex-to-dex compiler wants type information to elide check-casts. |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1180 | GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1181 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1182 | } |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1186 | template <bool kVerifierDebug> |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1187 | template <bool kAllowRuntimeOnlyInstructions> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1188 | bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst, |
| 1189 | uint32_t code_offset) { |
Andreas Gampe | e05cc66 | 2017-05-15 10:17:30 -0700 | [diff] [blame] | 1190 | if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1191 | // Experimental instructions don't yet have verifier support implementation. |
| 1192 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 1193 | // with a virtual register that was created by an experimental instruction, |
| 1194 | // the data flow analysis will fail. |
| 1195 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 1196 | << "experimental instruction is not supported by verifier; skipping verification"; |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 1197 | flags_.have_pending_experimental_failure_ = true; |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1198 | return false; |
| 1199 | } |
| 1200 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1201 | bool result = true; |
| 1202 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1203 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1204 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1205 | break; |
| 1206 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1207 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1208 | break; |
| 1209 | } |
| 1210 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1211 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1212 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1213 | break; |
| 1214 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1215 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1216 | break; |
| 1217 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1218 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1219 | break; |
| 1220 | case Instruction::kVerifyRegBNewInstance: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1221 | result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1222 | break; |
| 1223 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1224 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1225 | break; |
| 1226 | case Instruction::kVerifyRegBType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1227 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1228 | break; |
| 1229 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1230 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1231 | break; |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1232 | case Instruction::kVerifyRegBCallSite: |
| 1233 | result = result && CheckCallSiteIndex(inst->VRegB()); |
| 1234 | break; |
| 1235 | case Instruction::kVerifyRegBMethodHandle: |
| 1236 | result = result && CheckMethodHandleIndex(inst->VRegB()); |
| 1237 | break; |
| 1238 | case Instruction::kVerifyRegBPrototype: |
| 1239 | result = result && CheckPrototypeIndex(inst->VRegB()); |
| 1240 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1241 | } |
| 1242 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1243 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1244 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1245 | break; |
| 1246 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1247 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1248 | break; |
| 1249 | case Instruction::kVerifyRegCNewArray: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1250 | result = result && CheckNewArray(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1251 | break; |
| 1252 | case Instruction::kVerifyRegCType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1253 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1254 | break; |
| 1255 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1256 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1259 | switch (inst->GetVerifyTypeArgumentH()) { |
| 1260 | case Instruction::kVerifyRegHPrototype: |
| 1261 | result = result && CheckPrototypeIndex(inst->VRegH()); |
| 1262 | break; |
| 1263 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1264 | switch (inst->GetVerifyExtraFlags()) { |
| 1265 | case Instruction::kVerifyArrayData: |
| 1266 | result = result && CheckArrayData(code_offset); |
| 1267 | break; |
| 1268 | case Instruction::kVerifyBranchTarget: |
| 1269 | result = result && CheckBranchTarget(code_offset); |
| 1270 | break; |
| 1271 | case Instruction::kVerifySwitchTargets: |
| 1272 | result = result && CheckSwitchTargets(code_offset); |
| 1273 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1274 | case Instruction::kVerifyVarArgNonZero: |
| 1275 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1276 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1277 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1278 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1279 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1280 | v_a > Instruction::kMaxVarArgRegs) { |
| 1281 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1282 | "non-range invoke"; |
| 1283 | return false; |
| 1284 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1285 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1286 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1287 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1288 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1289 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1290 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1291 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1292 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1293 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1294 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1295 | inst->VRegA() <= 0) { |
| 1296 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1297 | "range invoke"; |
| 1298 | return false; |
| 1299 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1300 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | break; |
| 1302 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1303 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1304 | result = false; |
| 1305 | break; |
| 1306 | } |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1307 | if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1308 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1309 | result = false; |
| 1310 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1311 | return result; |
| 1312 | } |
| 1313 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1314 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1315 | inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1316 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1317 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1318 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1319 | return false; |
| 1320 | } |
| 1321 | // We don't need the actual class, just a pointer to the class name. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1322 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1323 | if (UNLIKELY(descriptor[0] != 'L')) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1324 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1325 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1326 | } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) { |
Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1327 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1328 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1329 | Fail(VERIFY_ERROR_INSTANTIATION); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1330 | } |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1334 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1335 | bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1336 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1337 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1338 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1342 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1343 | const char* cp = descriptor; |
| 1344 | while (*cp++ == '[') { |
| 1345 | bracket_count++; |
| 1346 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1347 | if (UNLIKELY(bracket_count == 0)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1348 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1349 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1350 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1351 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1352 | } else if (UNLIKELY(bracket_count > 255)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1353 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1354 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1355 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | return true; |
| 1359 | } |
| 1360 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1361 | template <bool kVerifierDebug> |
| 1362 | bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1363 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
| 1364 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1365 | const uint16_t* array_data; |
| 1366 | int32_t array_data_offset; |
| 1367 | |
| 1368 | DCHECK_LT(cur_offset, insn_count); |
| 1369 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1370 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1371 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
| 1372 | cur_offset + array_data_offset + 2 >= insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1373 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1374 | << ", data offset " << array_data_offset |
| 1375 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1376 | return false; |
| 1377 | } |
| 1378 | /* offset to array data table is a relative branch-style offset */ |
| 1379 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1380 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1381 | if (UNLIKELY(!IsAligned<4>(array_data))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1382 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1383 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1384 | return false; |
| 1385 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1386 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1387 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1388 | if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1389 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1390 | << ", data offset " << array_data_offset |
| 1391 | << " not correctly visited, probably bad padding."; |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1395 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1396 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1397 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1398 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1399 | if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1400 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1401 | << ", data offset " << array_data_offset << ", end " |
| 1402 | << cur_offset + array_data_offset + table_size |
| 1403 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1404 | return false; |
| 1405 | } |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1409 | template <bool kVerifierDebug> |
| 1410 | bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1411 | int32_t offset; |
| 1412 | bool isConditional, selfOkay; |
| 1413 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1414 | return false; |
| 1415 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1416 | if (UNLIKELY(!selfOkay && offset == 0)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1417 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1418 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1419 | return false; |
| 1420 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1421 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1422 | // to have identical "wrap-around" behavior, but it's unwise to depend on that. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1423 | if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1424 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1425 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1428 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1429 | if (UNLIKELY(abs_offset < 0 || |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1430 | (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() || |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1431 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1432 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1433 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1434 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1435 | return false; |
| 1436 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1437 | GetModifiableInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1438 | return true; |
| 1439 | } |
| 1440 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1441 | template <bool kVerifierDebug> |
| 1442 | bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset, |
| 1443 | int32_t* pOffset, |
| 1444 | bool* pConditional, |
| 1445 | bool* selfOkay) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1446 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1447 | *pConditional = false; |
| 1448 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1449 | switch (*insns & 0xff) { |
| 1450 | case Instruction::GOTO: |
| 1451 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1452 | break; |
| 1453 | case Instruction::GOTO_32: |
| 1454 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1455 | *selfOkay = true; |
| 1456 | break; |
| 1457 | case Instruction::GOTO_16: |
| 1458 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1459 | break; |
| 1460 | case Instruction::IF_EQ: |
| 1461 | case Instruction::IF_NE: |
| 1462 | case Instruction::IF_LT: |
| 1463 | case Instruction::IF_GE: |
| 1464 | case Instruction::IF_GT: |
| 1465 | case Instruction::IF_LE: |
| 1466 | case Instruction::IF_EQZ: |
| 1467 | case Instruction::IF_NEZ: |
| 1468 | case Instruction::IF_LTZ: |
| 1469 | case Instruction::IF_GEZ: |
| 1470 | case Instruction::IF_GTZ: |
| 1471 | case Instruction::IF_LEZ: |
| 1472 | *pOffset = (int16_t) insns[1]; |
| 1473 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1474 | break; |
| 1475 | default: |
| 1476 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1477 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1478 | return true; |
| 1479 | } |
| 1480 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1481 | template <bool kVerifierDebug> |
| 1482 | bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1483 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1484 | DCHECK_LT(cur_offset, insn_count); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1485 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1486 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1487 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1488 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1489 | cur_offset + switch_offset + 2 > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1490 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1491 | << ", switch offset " << switch_offset |
| 1492 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1493 | return false; |
| 1494 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1495 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1496 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1497 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1498 | if (UNLIKELY(!IsAligned<4>(switch_insns))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1499 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1500 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1501 | return false; |
| 1502 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1503 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1504 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1505 | if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1506 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1507 | << ", switch offset " << switch_offset |
| 1508 | << " not correctly visited, probably bad padding."; |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1512 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1513 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1514 | uint32_t switch_count = switch_insns[1]; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1515 | int32_t targets_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1516 | uint16_t expected_signature; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1517 | if (is_packed_switch) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1518 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1519 | targets_offset = 4; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1520 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1521 | } else { |
| 1522 | /* 0=sig, 1=count, 2..count*2 = keys */ |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1523 | targets_offset = 2 + 2 * switch_count; |
| 1524 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1525 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1526 | uint32_t table_size = targets_offset + switch_count * 2; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1527 | if (UNLIKELY(switch_insns[0] != expected_signature)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1528 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1529 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1530 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1533 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1534 | if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1535 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1536 | << ", switch offset " << switch_offset |
| 1537 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1538 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1539 | return false; |
| 1540 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1541 | |
| 1542 | constexpr int32_t keys_offset = 2; |
| 1543 | if (switch_count > 1) { |
| 1544 | if (is_packed_switch) { |
| 1545 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1546 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1547 | int32_t max_first_key = |
| 1548 | std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1549 | if (UNLIKELY(first_key > max_first_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1550 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1551 | << ", switch_count=" << switch_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1554 | } else { |
| 1555 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1556 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1557 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1558 | int32_t key = |
| 1559 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1560 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1561 | if (UNLIKELY(key <= last_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1562 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1563 | << ", this=" << key; |
| 1564 | return false; |
| 1565 | } |
| 1566 | last_key = key; |
| 1567 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1570 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1571 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1572 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1573 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1574 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1575 | if (UNLIKELY(abs_offset < 0 || |
| 1576 | abs_offset >= static_cast<int32_t>(insn_count) || |
| 1577 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1578 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1579 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1580 | << reinterpret_cast<void*>(cur_offset) |
| 1581 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1582 | return false; |
| 1583 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1584 | GetModifiableInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1585 | } |
| 1586 | return true; |
| 1587 | } |
| 1588 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1589 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1590 | bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1591 | const uint16_t registers_size = code_item_accessor_.RegistersSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1592 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1593 | /* Create and initialize table holding register status */ |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 1594 | RegisterTrackingMode base_mode = IsAotMode() |
Andreas Gampe | 93adcb5 | 2019-06-06 20:16:07 -0700 | [diff] [blame] | 1595 | ? kTrackCompilerInterestPoints |
| 1596 | : kTrackRegsBranches; |
| 1597 | reg_table_.Init(fill_register_lines_ ? kTrackRegsAll : base_mode, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1598 | insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1599 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1600 | registers_size, |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 1601 | allocator_, |
| 1602 | GetRegTypeCache()); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1603 | |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 1604 | work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache())); |
| 1605 | saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache())); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1606 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1607 | /* Initialize register types of method arguments. */ |
| 1608 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1609 | DCHECK_NE(failures_.size(), 0U); |
| 1610 | std::string prepend("Bad signature in "); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1611 | prepend += dex_file_->PrettyMethod(dex_method_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1612 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1613 | return false; |
| 1614 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1615 | // We may have a runtime failure here, clear. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 1616 | flags_.have_pending_runtime_throw_failure_ = false; |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1617 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1618 | /* Perform code flow verification. */ |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1619 | bool res = LIKELY(monitor_enter_dex_pcs_ == nullptr) |
| 1620 | ? CodeFlowVerifyMethod</*kMonitorDexPCs=*/ false>() |
| 1621 | : CodeFlowVerifyMethod</*kMonitorDexPCs=*/ true>(); |
| 1622 | if (UNLIKELY(!res)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1623 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1624 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1625 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1626 | return true; |
| 1627 | } |
| 1628 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1629 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1630 | void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1631 | if (!code_item_accessor_.HasCodeItem()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1632 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1633 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1634 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1635 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1636 | vios->Stream() << "Register Types:\n"; |
| 1637 | ScopedIndentation indent1(vios); |
| 1638 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1639 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1640 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1641 | ScopedIndentation indent1(vios); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1642 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1643 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1644 | const size_t dex_pc = inst.DexPc(); |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1645 | |
| 1646 | // Might be asked to dump before the table is initialized. |
| 1647 | if (reg_table_.IsInitialized()) { |
| 1648 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1649 | if (reg_line != nullptr) { |
| 1650 | vios->Stream() << reg_line->Dump(this) << "\n"; |
| 1651 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1652 | } |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1653 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1654 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1655 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1656 | const bool kDumpHexOfInstruction = false; |
| 1657 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1658 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1659 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1660 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1661 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1664 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1665 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1666 | case 'I': |
| 1667 | case 'C': |
| 1668 | case 'S': |
| 1669 | case 'B': |
| 1670 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1671 | case 'F': |
| 1672 | case 'D': |
| 1673 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1674 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1675 | default: |
| 1676 | return false; |
| 1677 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1680 | template <bool kVerifierDebug> |
| 1681 | bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1682 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1683 | |
| 1684 | // Should have been verified earlier. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1685 | DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize()); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1686 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1687 | uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize(); |
| 1688 | size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1689 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1690 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1691 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1692 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1693 | if (expected_args == 0) { |
| 1694 | // Expect at least a receiver. |
| 1695 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1699 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1700 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1701 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1702 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1703 | if (IsConstructor()) { |
| 1704 | if (declaring_class.IsJavaLangObject()) { |
| 1705 | // "this" is implicitly initialized. |
| 1706 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1707 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1708 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1709 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1710 | this, |
| 1711 | arg_start + cur_arg, |
| 1712 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1713 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1714 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1715 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1716 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1717 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1720 | const dex::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1721 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1722 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1723 | |
| 1724 | for (; iterator.HasNext(); iterator.Next()) { |
| 1725 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1726 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1727 | LOG(FATAL) << "Null descriptor"; |
| 1728 | } |
| 1729 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1730 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1731 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1732 | return false; |
| 1733 | } |
| 1734 | switch (descriptor[0]) { |
| 1735 | case 'L': |
| 1736 | case '[': |
| 1737 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1738 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1739 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1740 | // can return without doing anything or call virtual methods). |
| 1741 | { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 1742 | // Note: don't check access. No error would be thrown for declaring or passing an |
| 1743 | // inaccessible class. Only actual accesses to fields or methods will. |
| 1744 | const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1745 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1746 | DCHECK(HasFailures()); |
| 1747 | return false; |
| 1748 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1749 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1750 | } |
| 1751 | break; |
| 1752 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1753 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1754 | break; |
| 1755 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1756 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1757 | break; |
| 1758 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1759 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1760 | break; |
| 1761 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1762 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1765 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1766 | break; |
| 1767 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1768 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1769 | break; |
| 1770 | case 'J': |
| 1771 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1772 | if (cur_arg + 1 >= expected_args) { |
| 1773 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1774 | << " args, found more (" << descriptor << ")"; |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1778 | const RegType* lo_half; |
| 1779 | const RegType* hi_half; |
| 1780 | if (descriptor[0] == 'J') { |
| 1781 | lo_half = ®_types_.LongLo(); |
| 1782 | hi_half = ®_types_.LongHi(); |
| 1783 | } else { |
| 1784 | lo_half = ®_types_.DoubleLo(); |
| 1785 | hi_half = ®_types_.DoubleHi(); |
| 1786 | } |
| 1787 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1788 | cur_arg++; |
| 1789 | break; |
| 1790 | } |
| 1791 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1792 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1793 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1794 | return false; |
| 1795 | } |
| 1796 | cur_arg++; |
| 1797 | } |
| 1798 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1799 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1800 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
| 1803 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1804 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1805 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1806 | bool result; |
| 1807 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1808 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1809 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1810 | size_t i = 0; |
| 1811 | do { |
| 1812 | i++; |
| 1813 | } while (descriptor[i] == '['); // process leading [ |
| 1814 | if (descriptor[i] == 'L') { // object array |
| 1815 | do { |
| 1816 | i++; // find closing ; |
| 1817 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1818 | result = descriptor[i] == ';'; |
| 1819 | } else { // primitive array |
| 1820 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1821 | } |
| 1822 | } else if (descriptor[0] == 'L') { |
| 1823 | // could be more thorough here, but shouldn't be required |
| 1824 | size_t i = 0; |
| 1825 | do { |
| 1826 | i++; |
| 1827 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1828 | result = descriptor[i] == ';'; |
| 1829 | } else { |
| 1830 | result = false; |
| 1831 | } |
| 1832 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1833 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1834 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1835 | } |
| 1836 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1839 | COLD_ATTR |
| 1840 | void HandleMonitorDexPcsWorkLine( |
| 1841 | std::vector<::art::verifier::MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs, |
| 1842 | RegisterLine* work_line) { |
| 1843 | monitor_enter_dex_pcs->clear(); // The new work line is more accurate than the previous one. |
| 1844 | |
| 1845 | std::map<uint32_t, ::art::verifier::MethodVerifier::DexLockInfo> depth_to_lock_info; |
| 1846 | auto collector = [&](uint32_t dex_reg, uint32_t depth) { |
| 1847 | auto insert_pair = depth_to_lock_info.emplace( |
| 1848 | depth, ::art::verifier::MethodVerifier::DexLockInfo(depth)); |
| 1849 | auto it = insert_pair.first; |
| 1850 | auto set_insert_pair = it->second.dex_registers.insert(dex_reg); |
| 1851 | DCHECK(set_insert_pair.second); |
| 1852 | }; |
| 1853 | work_line->IterateRegToLockDepths(collector); |
| 1854 | for (auto& pair : depth_to_lock_info) { |
| 1855 | monitor_enter_dex_pcs->push_back(pair.second); |
| 1856 | // Map depth to dex PC. |
Andreas Gampe | 4ec4d48 | 2019-08-01 10:00:23 -0700 | [diff] [blame] | 1857 | monitor_enter_dex_pcs->back().dex_pc = work_line->GetMonitorEnterDexPc(pair.second.dex_pc); |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1861 | template <bool kVerifierDebug> |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1862 | template <bool kMonitorDexPCs> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1863 | bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1864 | const uint16_t* insns = code_item_accessor_.Insns(); |
| 1865 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1866 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1867 | /* Begin by marking the first instruction as "changed". */ |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1868 | GetModifiableInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1869 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1870 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1871 | /* Continue until no instructions are marked "changed". */ |
| 1872 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1873 | if (allow_thread_suspension_) { |
| 1874 | self_->AllowThreadSuspension(); |
| 1875 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1876 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1877 | uint32_t insn_idx = start_guess; |
| 1878 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1879 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1880 | break; |
| 1881 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1882 | if (insn_idx == insns_size) { |
| 1883 | if (start_guess != 0) { |
| 1884 | /* try again, starting from the top */ |
| 1885 | start_guess = 0; |
| 1886 | continue; |
| 1887 | } else { |
| 1888 | /* all flags are clear */ |
| 1889 | break; |
| 1890 | } |
| 1891 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1892 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1893 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1894 | // "changed" flags, we need to load the set of registers from the table. |
| 1895 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1896 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1897 | // target. |
| 1898 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1899 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1900 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1901 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1902 | /* |
| 1903 | * Sanity check: retrieve the stored register line (assuming |
| 1904 | * a full table) and make sure it actually matches. |
| 1905 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1906 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1907 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1908 | if (work_line_->CompareLine(register_line) != 0) { |
Andreas Gampe | 4a57d5f | 2019-04-03 12:31:18 -0700 | [diff] [blame] | 1909 | Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1910 | LOG(FATAL_WITHOUT_ABORT) << info_messages_.str(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1911 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1912 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1913 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1914 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1915 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1916 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1917 | } |
Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1918 | |
| 1919 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1920 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1921 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1922 | // for a thread to be suspended). |
| 1923 | if (kMonitorDexPCs && UNLIKELY(work_insn_idx_ == interesting_dex_pc_)) { |
| 1924 | HandleMonitorDexPcsWorkLine(monitor_enter_dex_pcs_, work_line_.get()); |
| 1925 | } |
| 1926 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1927 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1928 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1929 | prepend += " failed to verify: "; |
| 1930 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1931 | return false; |
| 1932 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1933 | /* Clear "changed" and mark as visited. */ |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1934 | GetModifiableInstructionFlags(insn_idx).SetVisited(); |
| 1935 | GetModifiableInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1936 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1937 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1938 | if (kVerifierDebug) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1939 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1940 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1941 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1942 | * down the line, possibly in the verifier. |
| 1943 | * |
| 1944 | * If we've substituted "always throw" instructions into the stream, |
| 1945 | * we are almost certainly going to have some dead code. |
| 1946 | */ |
| 1947 | int dead_start = -1; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1948 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1949 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1950 | const uint32_t insn_idx = inst.DexPc(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1951 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1952 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1953 | * may or may not be preceded by a padding NOP (for alignment). |
| 1954 | */ |
| 1955 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1956 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1957 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1958 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1959 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1960 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1961 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1962 | GetModifiableInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1965 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1966 | if (dead_start < 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1967 | dead_start = insn_idx; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1968 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1969 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1970 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1971 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1972 | dead_start = -1; |
| 1973 | } |
| 1974 | } |
| 1975 | if (dead_start >= 0) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1976 | LogVerifyInfo() |
| 1977 | << "dead code " << reinterpret_cast<void*>(dead_start) |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1978 | << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1979 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1980 | // To dump the state of the verify after a method, do something like: |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1981 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1982 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1983 | // LOG(INFO) << info_messages_.str(); |
| 1984 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1985 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1986 | return true; |
| 1987 | } |
| 1988 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1989 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1990 | // is no such field. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1991 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1992 | const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1993 | DCHECK(class_def != nullptr); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 1994 | ClassAccessor accessor(dex_file, *class_def); |
| 1995 | for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) { |
| 1996 | if (field.IsFinal()) { |
| 1997 | return field.GetIndex(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1998 | } |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1999 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2000 | return dex::kDexNoIndex; |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2003 | // Setup a register line for the given return instruction. |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 2004 | template <bool kVerifierDebug> |
| 2005 | static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier, |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2006 | const Instruction* ret_inst, |
| 2007 | RegisterLine* line) { |
| 2008 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2009 | |
| 2010 | switch (opcode) { |
| 2011 | case Instruction::RETURN_VOID: |
| 2012 | case Instruction::RETURN_VOID_NO_BARRIER: |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2013 | if (verifier->IsInstanceConstructor()) { |
| 2014 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 2015 | line->CheckConstructorReturn(verifier); |
| 2016 | } |
| 2017 | line->MarkAllRegistersAsConflicts(verifier); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2018 | break; |
| 2019 | |
| 2020 | case Instruction::RETURN: |
| 2021 | case Instruction::RETURN_OBJECT: |
| 2022 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 2023 | break; |
| 2024 | |
| 2025 | case Instruction::RETURN_WIDE: |
| 2026 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 2027 | break; |
| 2028 | |
| 2029 | default: |
| 2030 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 2031 | UNREACHABLE(); |
| 2032 | } |
| 2033 | } |
| 2034 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 2035 | template <bool kVerifierDebug> |
| 2036 | bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2037 | /* |
| 2038 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2039 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2040 | * control to another statement: |
| 2041 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2042 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2043 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2044 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2046 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2047 | * throw an exception that is handled by an encompassing "try" |
| 2048 | * block. |
| 2049 | * |
| 2050 | * We can also return, in which case there is no successor instruction |
| 2051 | * from this point. |
| 2052 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2053 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2054 | */ |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2055 | const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2056 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2057 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2058 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2059 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2060 | bool just_set_result = false; |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 2061 | if (kVerifierDebug) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2062 | // Generate processing back trace to debug verifier |
Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 2063 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl |
| 2064 | << work_line_->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2065 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2066 | |
| 2067 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2068 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2069 | * can throw an exception, we will copy/merge this into the "catch" |
| 2070 | * address rather than work_line, because we don't want the result |
| 2071 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2072 | * a type) to be visible to the exception handler. |
| 2073 | */ |
Andreas Gampe | c658278 | 2019-07-10 15:06:18 -0700 | [diff] [blame] | 2074 | if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) && |
| 2075 | CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2076 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2077 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2078 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2079 | } |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 2080 | // Per-instruction flag, should not be set here. |
| 2081 | DCHECK(!flags_.have_pending_runtime_throw_failure_); |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2082 | bool exc_handler_unreachable = false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2083 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2084 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2085 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2086 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2087 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2088 | RegisterLineArenaUniquePtr branch_line; |
| 2089 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2090 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2091 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2092 | case Instruction::NOP: |
| 2093 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2094 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2095 | * a signature that looks like a NOP; if we see one of these in |
| 2096 | * the course of executing code then we have a problem. |
| 2097 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2098 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2100 | } |
| 2101 | break; |
| 2102 | |
| 2103 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2104 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2105 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2106 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2107 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2108 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2109 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2110 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2111 | break; |
| 2112 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2113 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2114 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2115 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2116 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2117 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2118 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2119 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2120 | break; |
| 2121 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2122 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2123 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2124 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2125 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2126 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2127 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2128 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2129 | break; |
| 2130 | |
| 2131 | /* |
| 2132 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2133 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2134 | * might want to hold the result in an actual CPU register, so the |
| 2135 | * Dalvik spec requires that these only appear immediately after an |
| 2136 | * invoke or filled-new-array. |
| 2137 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2138 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2139 | * redundant with the reset done below, but it can make the debug info |
| 2140 | * easier to read in some cases.) |
| 2141 | */ |
| 2142 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2143 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2144 | break; |
| 2145 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2146 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2147 | break; |
| 2148 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2149 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2150 | break; |
| 2151 | |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2152 | case Instruction::MOVE_EXCEPTION: |
| 2153 | if (!HandleMoveException(inst)) { |
| 2154 | exc_handler_unreachable = true; |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2155 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2156 | break; |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2157 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2158 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2159 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2160 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2161 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2162 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2163 | } |
| 2164 | break; |
| 2165 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2166 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2167 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2168 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2169 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2170 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2171 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2172 | } else { |
| 2173 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2174 | // Also, it may use integer values for boolean, byte, short, and character return types. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2175 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2176 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2177 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2178 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2179 | return_type.IsShort() || return_type.IsChar()) && |
| 2180 | src_type.IsInteger())); |
| 2181 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2182 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2183 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2184 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2185 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2186 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | break; |
| 2190 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2191 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2192 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2193 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2194 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2195 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2196 | } else { |
| 2197 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2198 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2199 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2200 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2201 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2202 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | break; |
| 2206 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2207 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2208 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2209 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2210 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2211 | } else { |
| 2212 | /* return_type is the *expected* return type, not register value */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2213 | DCHECK(!return_type.IsZeroOrNull()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2214 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2215 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2216 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2217 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2218 | // reference in vAA is an instance of the "return_type." |
| 2219 | if (reg_type.IsUndefined()) { |
| 2220 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2221 | } else if (reg_type.IsConflict()) { |
| 2222 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2223 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2224 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2225 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2226 | } else if (!reg_type.IsReferenceTypes()) { |
| 2227 | // We really do expect a reference here. |
| 2228 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2229 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2230 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2231 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
Andreas Gampe | bc919af | 2019-07-10 14:09:51 -0700 | [diff] [blame] | 2232 | Fail(api_level_ > 29u ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_NO_CLASS) |
| 2233 | << " can't resolve returned type '" << return_type << "' or '" << reg_type << "'"; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2234 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2235 | bool soft_error = false; |
| 2236 | // Check whether arrays are involved. They will show a valid class status, even |
| 2237 | // if their components are erroneous. |
| 2238 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2239 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2240 | if (soft_error) { |
| 2241 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2242 | << reg_type << " vs " << return_type; |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | if (!soft_error) { |
| 2247 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2248 | << "', but expected from declaration '" << return_type << "'"; |
| 2249 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2250 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | break; |
| 2255 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2256 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2257 | case Instruction::CONST_4: { |
| 2258 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2259 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2260 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2261 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2262 | } |
| 2263 | case Instruction::CONST_16: { |
| 2264 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2265 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2266 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2267 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2268 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2269 | case Instruction::CONST: { |
| 2270 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2271 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2272 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2273 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2274 | } |
| 2275 | case Instruction::CONST_HIGH16: { |
| 2276 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2277 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2278 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2279 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2280 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2281 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2282 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2283 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2284 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2285 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2286 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2287 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2288 | } |
| 2289 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2290 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2291 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2292 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2293 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2294 | break; |
| 2295 | } |
| 2296 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2297 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2298 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2299 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2300 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2301 | break; |
| 2302 | } |
| 2303 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2304 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2305 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2306 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2307 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2308 | break; |
| 2309 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2310 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2311 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2312 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2313 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2314 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2315 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2316 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2317 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2318 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2319 | // Get type from instruction if unresolved then we need an access check |
| 2320 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2321 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2322 | // Register holds class, ie its type is class, on error it will hold Conflict. |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2323 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2324 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2325 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2326 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2327 | } |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2328 | case Instruction::CONST_METHOD_HANDLE: |
| 2329 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2330 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle()); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2331 | break; |
| 2332 | case Instruction::CONST_METHOD_TYPE: |
| 2333 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2334 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType()); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2335 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2336 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2337 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2338 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2339 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2340 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2341 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2342 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2343 | prev_idx--; |
| 2344 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2345 | const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2346 | switch (prev_inst.Opcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2347 | case Instruction::MOVE_OBJECT: |
| 2348 | case Instruction::MOVE_OBJECT_16: |
| 2349 | case Instruction::MOVE_OBJECT_FROM16: |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2350 | if (prev_inst.VRegB() == inst->VRegA_11x()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2351 | // Redo the copy. This won't change the register types, but update the lock status |
| 2352 | // for the aliased register. |
| 2353 | work_line_->CopyRegister1(this, |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2354 | prev_inst.VRegA(), |
| 2355 | prev_inst.VRegB(), |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2356 | kTypeCategoryRef); |
| 2357 | } |
| 2358 | break; |
| 2359 | |
Alexey Grebenkin | ce75049 | 2018-05-31 23:42:20 +0300 | [diff] [blame] | 2360 | // Catch a case of register aliasing when two registers are linked to the same |
| 2361 | // java.lang.Class object via two consequent const-class instructions immediately |
| 2362 | // preceding monitor-enter called on one of those registers. |
| 2363 | case Instruction::CONST_CLASS: { |
| 2364 | // Get the second previous instruction. |
| 2365 | if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) { |
| 2366 | break; |
| 2367 | } |
| 2368 | prev_idx--; |
| 2369 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
| 2370 | prev_idx--; |
| 2371 | } |
| 2372 | const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx); |
| 2373 | |
| 2374 | // Match the pattern "const-class; const-class; monitor-enter;" |
| 2375 | if (prev2_inst.Opcode() != Instruction::CONST_CLASS) { |
| 2376 | break; |
| 2377 | } |
| 2378 | |
| 2379 | // Ensure both const-classes are called for the same type_idx. |
| 2380 | if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) { |
| 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | // Update the lock status for the aliased register. |
| 2385 | if (prev_inst.VRegA() == inst->VRegA_11x()) { |
| 2386 | work_line_->CopyRegister1(this, |
| 2387 | prev2_inst.VRegA(), |
| 2388 | inst->VRegA_11x(), |
| 2389 | kTypeCategoryRef); |
| 2390 | } else if (prev2_inst.VRegA() == inst->VRegA_11x()) { |
| 2391 | work_line_->CopyRegister1(this, |
| 2392 | prev_inst.VRegA(), |
| 2393 | inst->VRegA_11x(), |
| 2394 | kTypeCategoryRef); |
| 2395 | } |
| 2396 | break; |
| 2397 | } |
| 2398 | |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2399 | default: // Other instruction types ignored. |
| 2400 | break; |
| 2401 | } |
| 2402 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2403 | break; |
| 2404 | case Instruction::MONITOR_EXIT: |
| 2405 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2406 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2407 | * but when they do they act as if they succeeded and the PC is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2408 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2409 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2410 | * feature that Dalvik doesn't support.) |
| 2411 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2412 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2413 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2414 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2415 | * structured locking checks are working, the former would have |
| 2416 | * failed on the -enter instruction, and the latter is impossible. |
| 2417 | * |
| 2418 | * This is fortunate, because issue 3221411 prevents us from |
| 2419 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2420 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2421 | * some catch blocks (which will show up as "dead" code when |
| 2422 | * we skip them here); if we can't, then the code path could be |
| 2423 | * "live" so we still need to check it. |
| 2424 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2425 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2426 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2427 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2428 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2429 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2430 | /* |
| 2431 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2432 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2433 | * |
| 2434 | * If it fails, an exception is thrown, which we deal with later by ignoring the update to |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2435 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2436 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2437 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2438 | const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c()); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2439 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2440 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2441 | // If this is a primitive type, fail HARD. |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 2442 | ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2443 | type_idx, dex_cache_.Get(), class_loader_.Get()); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2444 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2445 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2446 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2447 | << GetDeclaringClass(); |
| 2448 | break; |
| 2449 | } |
| 2450 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2451 | DCHECK_NE(failures_.size(), 0U); |
| 2452 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2453 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2454 | inst->VRegA_22c(), |
| 2455 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2456 | } |
| 2457 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2458 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2459 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2460 | uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2461 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2462 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2463 | if (is_checkcast) { |
| 2464 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2465 | } else { |
| 2466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2467 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2468 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2469 | if (is_checkcast) { |
| 2470 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2471 | } else { |
| 2472 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2473 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2474 | } else if (orig_type.IsUninitializedTypes()) { |
| 2475 | if (is_checkcast) { |
| 2476 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2477 | << orig_type_reg; |
| 2478 | } else { |
| 2479 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2480 | << orig_type_reg; |
| 2481 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2482 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2483 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2484 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2485 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2486 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2487 | inst->VRegA_22c(), |
| 2488 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2489 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2490 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2491 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2492 | } |
| 2493 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2494 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2495 | if (res_type.IsReferenceTypes()) { |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2496 | if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) { |
| 2497 | // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2498 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2499 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2500 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2501 | inst->VRegA_12x(), |
| 2502 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2503 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2504 | } else { |
| 2505 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2506 | } |
| 2507 | break; |
| 2508 | } |
| 2509 | case Instruction::NEW_INSTANCE: { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2510 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2511 | if (res_type.IsConflict()) { |
| 2512 | DCHECK_NE(failures_.size(), 0U); |
| 2513 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2514 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2515 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2516 | // can't create an instance of an interface or abstract class */ |
| 2517 | if (!res_type.IsInstantiableTypes()) { |
| 2518 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2519 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2520 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2521 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2522 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2523 | // Any registers holding previous allocations from this address that have not yet been |
| 2524 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2525 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2526 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2527 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2528 | break; |
| 2529 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2530 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2531 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2532 | break; |
| 2533 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2534 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2535 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2536 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2537 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2538 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2539 | just_set_result = true; // Filled new array range sets result register |
| 2540 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2541 | case Instruction::CMPL_FLOAT: |
| 2542 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2543 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2544 | break; |
| 2545 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2546 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2547 | break; |
| 2548 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2549 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2550 | break; |
| 2551 | case Instruction::CMPL_DOUBLE: |
| 2552 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2553 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2554 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2555 | break; |
| 2556 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2557 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2558 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2559 | break; |
| 2560 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2561 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2562 | break; |
| 2563 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2564 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2565 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2566 | break; |
| 2567 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2568 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2569 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2570 | break; |
| 2571 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2572 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2573 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2574 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2575 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2576 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2577 | if (res_type.IsUninitializedTypes()) { |
| 2578 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2579 | } else if (!res_type.IsReferenceTypes()) { |
| 2580 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type; |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2581 | } else { |
| 2582 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2583 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2584 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2585 | } |
| 2586 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2587 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2588 | case Instruction::GOTO: |
| 2589 | case Instruction::GOTO_16: |
| 2590 | case Instruction::GOTO_32: |
| 2591 | /* no effect on or use of registers */ |
| 2592 | break; |
| 2593 | |
| 2594 | case Instruction::PACKED_SWITCH: |
| 2595 | case Instruction::SPARSE_SWITCH: |
| 2596 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2597 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2598 | break; |
| 2599 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2600 | case Instruction::FILL_ARRAY_DATA: { |
| 2601 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2602 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2603 | /* array_type can be null if the reg type is Zero */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2604 | if (!array_type.IsZeroOrNull()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2605 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2606 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2607 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2608 | } else if (array_type.IsUnresolvedTypes()) { |
| 2609 | // If it's an unresolved array type, it must be non-primitive. |
| 2610 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2611 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2612 | } else { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2613 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 2614 | class_loader_.Get()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2615 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2616 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2617 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2618 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2619 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2620 | // Now verify if the element width in the table matches the element width declared in |
| 2621 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2622 | const uint16_t* array_data = |
| 2623 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2624 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2625 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2626 | } else { |
| 2627 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2628 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2629 | // in the table and expected from the array class. |
| 2630 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2631 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2632 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2633 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2639 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2640 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2641 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2642 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2643 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2644 | bool mismatch = false; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2645 | if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2646 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2647 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2648 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2649 | } else { // both integral? |
| 2650 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2651 | } |
| 2652 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2653 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2654 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2655 | } |
| 2656 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2657 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2658 | case Instruction::IF_LT: |
| 2659 | case Instruction::IF_GE: |
| 2660 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2661 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2662 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2663 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2664 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2665 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2666 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2667 | } |
| 2668 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2669 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2670 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2671 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2672 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2673 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2674 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2675 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2676 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2677 | |
| 2678 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2679 | if (UNLIKELY(0 == work_insn_idx_)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2680 | break; |
| 2681 | } |
Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2682 | uint32_t instance_of_idx = work_insn_idx_ - 1; |
| 2683 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
| 2684 | instance_of_idx--; |
| 2685 | } |
| 2686 | // Dex index 0 must be an opcode. |
| 2687 | DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2688 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2689 | const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2690 | |
| 2691 | /* Check for peep-hole pattern of: |
| 2692 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2693 | * instance-of vX, vY, T; |
| 2694 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2695 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2696 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2697 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2698 | * and sharpen the type of vY to be type T. |
| 2699 | * Note, this pattern can't be if: |
| 2700 | * - if there are other branches to this branch, |
| 2701 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2702 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2703 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2704 | (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) && |
| 2705 | (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) && |
| 2706 | (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2707 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2708 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2709 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2710 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2711 | // and we don't want to disallow field and other operations on the object. If the value |
| 2712 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2713 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2714 | // type is assignable to the original then allow optimization. This check is performed to |
| 2715 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2716 | // interface from a class that would lose information relevant to field checks. |
Andreas Gampe | d78122b | 2019-07-10 14:06:53 -0700 | [diff] [blame] | 2717 | // |
| 2718 | // Note: do not do an access check. This may mark this with a runtime throw that actually |
| 2719 | // happens at the instanceof, not the branch (and branches aren't flagged to throw). |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2720 | const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c()); |
Andreas Gampe | d78122b | 2019-07-10 14:06:53 -0700 | [diff] [blame] | 2721 | const RegType& cast_type = ResolveClass<CheckAccess::kNo>( |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2722 | dex::TypeIndex(instance_of_inst.VRegC_22c())); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2723 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2724 | if (!orig_type.Equals(cast_type) && |
| 2725 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2726 | cast_type.HasClass() && // Could be conflict type, make sure it has a class. |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2727 | !cast_type.GetClass()->IsInterface() && |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2728 | (orig_type.IsZeroOrNull() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2729 | orig_type.IsStrictlyAssignableFrom( |
| 2730 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2731 | RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(), |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 2732 | allocator_, |
| 2733 | GetRegTypeCache()); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2734 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2735 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2736 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2737 | branch_line.reset(update_line); |
| 2738 | } |
| 2739 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2740 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2741 | instance_of_inst.VRegB_22c(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2742 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2743 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2744 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2745 | // register encoding space of instance-of, and propagate type information to the source |
| 2746 | // of the move-object. |
Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2747 | // Note: this is only valid if the move source was not clobbered. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2748 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2749 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2750 | move_idx--; |
| 2751 | } |
Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2752 | DCHECK(GetInstructionFlags(move_idx).IsOpcode()); |
Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2753 | auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type]( |
| 2754 | uint16_t move_src, |
| 2755 | uint16_t move_trg) |
| 2756 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2757 | if (move_trg == instance_of_inst.VRegB_22c() && |
| 2758 | move_src != instance_of_inst.VRegA_22c()) { |
| 2759 | update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type); |
| 2760 | } |
| 2761 | }; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2762 | const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2763 | switch (move_inst.Opcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2764 | case Instruction::MOVE_OBJECT: |
Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2765 | maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2766 | break; |
| 2767 | case Instruction::MOVE_OBJECT_FROM16: |
Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2768 | maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2769 | break; |
| 2770 | case Instruction::MOVE_OBJECT_16: |
Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2771 | maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2772 | break; |
| 2773 | default: |
| 2774 | break; |
| 2775 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2780 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2781 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2782 | case Instruction::IF_LTZ: |
| 2783 | case Instruction::IF_GEZ: |
| 2784 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2785 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2786 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2787 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2788 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2789 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2790 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2791 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2792 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2793 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2794 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2795 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2796 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2797 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2798 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2799 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2800 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2801 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2802 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2803 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2804 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2805 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2806 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2807 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2808 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2809 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2810 | break; |
| 2811 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2812 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2815 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2816 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2819 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2820 | break; |
| 2821 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2822 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2825 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2826 | break; |
| 2827 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2828 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2829 | break; |
| 2830 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2831 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2832 | break; |
| 2833 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2834 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2837 | case Instruction::IGET_BOOLEAN: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2838 | case Instruction::IGET_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2839 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2840 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2841 | case Instruction::IGET_BYTE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2842 | case Instruction::IGET_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2843 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2844 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2845 | case Instruction::IGET_CHAR: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2846 | case Instruction::IGET_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2847 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2848 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2849 | case Instruction::IGET_SHORT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2850 | case Instruction::IGET_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2851 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2852 | break; |
| 2853 | case Instruction::IGET: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2854 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2855 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2856 | break; |
| 2857 | case Instruction::IGET_WIDE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2858 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2859 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2860 | break; |
| 2861 | case Instruction::IGET_OBJECT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2862 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2863 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2864 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2865 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2866 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2867 | case Instruction::IPUT_BOOLEAN: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2868 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2869 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2870 | break; |
| 2871 | case Instruction::IPUT_BYTE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2872 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2873 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2874 | break; |
| 2875 | case Instruction::IPUT_CHAR: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2876 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2877 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2878 | break; |
| 2879 | case Instruction::IPUT_SHORT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2880 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2881 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2882 | break; |
| 2883 | case Instruction::IPUT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2884 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2885 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2886 | break; |
| 2887 | case Instruction::IPUT_WIDE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2888 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2889 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2890 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2891 | case Instruction::IPUT_OBJECT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2892 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2893 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2894 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2895 | break; |
| 2896 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2897 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2898 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2899 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2900 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2901 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2902 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2903 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2904 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2905 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2906 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2907 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2908 | break; |
| 2909 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2910 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2911 | break; |
| 2912 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2913 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2914 | break; |
| 2915 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2916 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2917 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2918 | break; |
| 2919 | |
| 2920 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2921 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2922 | break; |
| 2923 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2924 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2925 | break; |
| 2926 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2927 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2928 | break; |
| 2929 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2930 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2931 | break; |
| 2932 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2933 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2934 | break; |
| 2935 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2936 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2937 | break; |
| 2938 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2939 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2940 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2941 | break; |
| 2942 | |
| 2943 | case Instruction::INVOKE_VIRTUAL: |
| 2944 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2945 | case Instruction::INVOKE_SUPER: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2946 | case Instruction::INVOKE_SUPER_RANGE: |
| 2947 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2948 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2949 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2950 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE || |
| 2951 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2952 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2953 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2954 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2955 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2956 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2957 | if (called_method != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2958 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2959 | ? called_method->ResolveReturnType() |
| 2960 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2961 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2962 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
Vladimir Marko | 179b7c6 | 2019-03-22 13:38:57 +0000 | [diff] [blame] | 2963 | return_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2964 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2965 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2966 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2967 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | if (return_type == nullptr) { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2971 | uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2972 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2973 | dex::TypeIndex return_type_idx = |
| 2974 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2975 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2976 | return_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2977 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2978 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2979 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2980 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2981 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2982 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2983 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2984 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2985 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2986 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2987 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2988 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2989 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2990 | const char* return_type_descriptor; |
| 2991 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2992 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2993 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2994 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2995 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2996 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2997 | dex::TypeIndex return_type_idx = |
| 2998 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2999 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 3000 | } else { |
| 3001 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3002 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 3003 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 3004 | ? called_method->ResolveReturnType() |
| 3005 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3006 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3007 | return_type = &FromClass(return_type_descriptor, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3008 | return_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3009 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3010 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3011 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 3012 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3013 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3014 | } |
| 3015 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3016 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3017 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 3018 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 3019 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 3020 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 3021 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3022 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3023 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3024 | if (this_type.IsConflict()) // failure. |
| 3025 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3026 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3027 | /* no null refs allowed (?) */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3028 | if (this_type.IsZeroOrNull()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3029 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 3030 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 3031 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3032 | |
| 3033 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3034 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3035 | // TODO: re-enable constructor type verification |
| 3036 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3037 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3038 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 3039 | // break; |
| 3040 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3041 | |
| 3042 | /* arg must be an uninitialized reference */ |
| 3043 | if (!this_type.IsUninitializedTypes()) { |
| 3044 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 3045 | << this_type; |
| 3046 | break; |
| 3047 | } |
| 3048 | |
| 3049 | /* |
| 3050 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 3051 | * registers that have the same object instance in them, not just the "this" register. |
| 3052 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 3053 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3054 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3055 | if (return_type == nullptr) { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3056 | return_type = ®_types_.FromDescriptor(class_loader_.Get(), |
| 3057 | return_type_descriptor, |
| 3058 | false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3059 | } |
| 3060 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3061 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3062 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3063 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3064 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3065 | just_set_result = true; |
| 3066 | break; |
| 3067 | } |
| 3068 | case Instruction::INVOKE_STATIC: |
| 3069 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3070 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3071 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3072 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3073 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3074 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3075 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3076 | dex::TypeIndex return_type_idx = |
| 3077 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3078 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3079 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3080 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3081 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3082 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 3083 | descriptor, |
| 3084 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3085 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3086 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3087 | } else { |
| 3088 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3089 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3090 | just_set_result = true; |
| 3091 | } |
| 3092 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3093 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3094 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3095 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3096 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3097 | if (abs_method != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3098 | ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3099 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3100 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3101 | << abs_method->PrettyMethod() << "'"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3102 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3103 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3104 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3105 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3106 | * interface or Object (see comments in RegType::JoinClass). |
| 3107 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3108 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3109 | if (this_type.IsZeroOrNull()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3110 | /* null pointer always passes (and always fails at runtime) */ |
| 3111 | } else { |
| 3112 | if (this_type.IsUninitializedTypes()) { |
| 3113 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3114 | << this_type; |
| 3115 | break; |
| 3116 | } |
| 3117 | // In the past we have tried to assert that "called_interface" is assignable |
| 3118 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3119 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3120 | // implemented by "this_type". For example, two classes may implement the same |
| 3121 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3122 | // join will set "this_type" to the parent class and a test that this implements |
| 3123 | // the interface will incorrectly fail. |
| 3124 | } |
| 3125 | /* |
| 3126 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3127 | * the type information is in the abstract method, so we're good. |
| 3128 | */ |
| 3129 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3130 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3131 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3132 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3133 | dex::TypeIndex return_type_idx = |
| 3134 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3135 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3136 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3137 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3138 | } |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3139 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 3140 | descriptor, |
| 3141 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3142 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3143 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3144 | } else { |
| 3145 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3146 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3147 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3148 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3149 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3150 | case Instruction::INVOKE_POLYMORPHIC: |
| 3151 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3152 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3153 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3154 | if (called_method == nullptr) { |
| 3155 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3156 | if (failure_messages_.size() > 0) { |
| 3157 | std::string message = failure_messages_.back()->str(); |
| 3158 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3159 | } else { |
| 3160 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3161 | } |
| 3162 | break; |
| 3163 | } |
| 3164 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3165 | !CheckSignaturePolymorphicReceiver(inst)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 3166 | DCHECK(HasFailures()); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3167 | break; |
| 3168 | } |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 3169 | const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 3170 | const dex::ProtoIndex proto_idx(vRegH); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3171 | const char* return_descriptor = |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3172 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3173 | const RegType& return_type = |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3174 | reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3175 | if (!return_type.IsLowHalf()) { |
| 3176 | work_line_->SetResultRegisterType(this, return_type); |
| 3177 | } else { |
| 3178 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3179 | } |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3180 | just_set_result = true; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3181 | break; |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3182 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3183 | case Instruction::INVOKE_CUSTOM: |
| 3184 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3185 | // Verify registers based on method_type in the call site. |
| 3186 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3187 | |
| 3188 | // Step 1. Check the call site that produces the method handle for invocation |
| 3189 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3190 | if (!CheckCallSite(call_site_idx)) { |
| 3191 | DCHECK(HasFailures()); |
| 3192 | break; |
| 3193 | } |
| 3194 | |
| 3195 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3196 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3197 | // the first three arguments and CheckCallSite has checked the method handle type. |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 3198 | const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3199 | const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3200 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3201 | // Treat method as static as it has yet to be determined. |
| 3202 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3203 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3204 | |
| 3205 | // Step 3. Propagate return type information |
| 3206 | const RegType& return_type = |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3207 | reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3208 | if (!return_type.IsLowHalf()) { |
| 3209 | work_line_->SetResultRegisterType(this, return_type); |
| 3210 | } else { |
| 3211 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3212 | } |
| 3213 | just_set_result = true; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3214 | break; |
| 3215 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3216 | case Instruction::NEG_INT: |
| 3217 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3218 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3219 | break; |
| 3220 | case Instruction::NEG_LONG: |
| 3221 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3222 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3223 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3224 | break; |
| 3225 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3226 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3227 | break; |
| 3228 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3229 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3230 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3231 | break; |
| 3232 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3233 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3234 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3235 | break; |
| 3236 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3237 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3240 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3241 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3242 | break; |
| 3243 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3244 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3245 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3246 | break; |
| 3247 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3248 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3249 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3250 | break; |
| 3251 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3252 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3253 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3254 | break; |
| 3255 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3256 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3257 | break; |
| 3258 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3259 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3260 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3261 | break; |
| 3262 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3263 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3264 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3265 | break; |
| 3266 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3267 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3268 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3269 | break; |
| 3270 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3271 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3272 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3273 | break; |
| 3274 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3275 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3276 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3277 | break; |
| 3278 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3279 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3280 | break; |
| 3281 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3282 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3283 | break; |
| 3284 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3285 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3286 | break; |
| 3287 | |
| 3288 | case Instruction::ADD_INT: |
| 3289 | case Instruction::SUB_INT: |
| 3290 | case Instruction::MUL_INT: |
| 3291 | case Instruction::REM_INT: |
| 3292 | case Instruction::DIV_INT: |
| 3293 | case Instruction::SHL_INT: |
| 3294 | case Instruction::SHR_INT: |
| 3295 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3296 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3297 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3298 | break; |
| 3299 | case Instruction::AND_INT: |
| 3300 | case Instruction::OR_INT: |
| 3301 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3302 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3303 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3304 | break; |
| 3305 | case Instruction::ADD_LONG: |
| 3306 | case Instruction::SUB_LONG: |
| 3307 | case Instruction::MUL_LONG: |
| 3308 | case Instruction::DIV_LONG: |
| 3309 | case Instruction::REM_LONG: |
| 3310 | case Instruction::AND_LONG: |
| 3311 | case Instruction::OR_LONG: |
| 3312 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3313 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3314 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3315 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3316 | break; |
| 3317 | case Instruction::SHL_LONG: |
| 3318 | case Instruction::SHR_LONG: |
| 3319 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3320 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3321 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3322 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3323 | break; |
| 3324 | case Instruction::ADD_FLOAT: |
| 3325 | case Instruction::SUB_FLOAT: |
| 3326 | case Instruction::MUL_FLOAT: |
| 3327 | case Instruction::DIV_FLOAT: |
| 3328 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3329 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3330 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3331 | break; |
| 3332 | case Instruction::ADD_DOUBLE: |
| 3333 | case Instruction::SUB_DOUBLE: |
| 3334 | case Instruction::MUL_DOUBLE: |
| 3335 | case Instruction::DIV_DOUBLE: |
| 3336 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3337 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3338 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3339 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3340 | break; |
| 3341 | case Instruction::ADD_INT_2ADDR: |
| 3342 | case Instruction::SUB_INT_2ADDR: |
| 3343 | case Instruction::MUL_INT_2ADDR: |
| 3344 | case Instruction::REM_INT_2ADDR: |
| 3345 | case Instruction::SHL_INT_2ADDR: |
| 3346 | case Instruction::SHR_INT_2ADDR: |
| 3347 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3348 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3349 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3350 | break; |
| 3351 | case Instruction::AND_INT_2ADDR: |
| 3352 | case Instruction::OR_INT_2ADDR: |
| 3353 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3354 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3355 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3356 | break; |
| 3357 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3358 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3359 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3360 | break; |
| 3361 | case Instruction::ADD_LONG_2ADDR: |
| 3362 | case Instruction::SUB_LONG_2ADDR: |
| 3363 | case Instruction::MUL_LONG_2ADDR: |
| 3364 | case Instruction::DIV_LONG_2ADDR: |
| 3365 | case Instruction::REM_LONG_2ADDR: |
| 3366 | case Instruction::AND_LONG_2ADDR: |
| 3367 | case Instruction::OR_LONG_2ADDR: |
| 3368 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3369 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3370 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3371 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3372 | break; |
| 3373 | case Instruction::SHL_LONG_2ADDR: |
| 3374 | case Instruction::SHR_LONG_2ADDR: |
| 3375 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3376 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3377 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3378 | break; |
| 3379 | case Instruction::ADD_FLOAT_2ADDR: |
| 3380 | case Instruction::SUB_FLOAT_2ADDR: |
| 3381 | case Instruction::MUL_FLOAT_2ADDR: |
| 3382 | case Instruction::DIV_FLOAT_2ADDR: |
| 3383 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3384 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3385 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3386 | break; |
| 3387 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3388 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3389 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3390 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3391 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3392 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3393 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3394 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3395 | break; |
| 3396 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3397 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3398 | case Instruction::MUL_INT_LIT16: |
| 3399 | case Instruction::DIV_INT_LIT16: |
| 3400 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3401 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3402 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3403 | break; |
| 3404 | case Instruction::AND_INT_LIT16: |
| 3405 | case Instruction::OR_INT_LIT16: |
| 3406 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3407 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3408 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3409 | break; |
| 3410 | case Instruction::ADD_INT_LIT8: |
| 3411 | case Instruction::RSUB_INT_LIT8: |
| 3412 | case Instruction::MUL_INT_LIT8: |
| 3413 | case Instruction::DIV_INT_LIT8: |
| 3414 | case Instruction::REM_INT_LIT8: |
| 3415 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3416 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3417 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3418 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3419 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3420 | break; |
| 3421 | case Instruction::AND_INT_LIT8: |
| 3422 | case Instruction::OR_INT_LIT8: |
| 3423 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3424 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3425 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3426 | break; |
| 3427 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3428 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3429 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3430 | if (IsConstructor() && !IsStatic()) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3431 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3432 | if (declaring_class.IsUnresolvedReference()) { |
| 3433 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3434 | // manually over the underlying dex file. |
| 3435 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3436 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3437 | if (first_index != dex::kDexNoIndex) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3438 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3439 | << first_index; |
| 3440 | } |
| 3441 | break; |
| 3442 | } |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3443 | ObjPtr<mirror::Class> klass = declaring_class.GetClass(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3444 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3445 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3446 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3447 | << klass->GetInstanceField(i)->PrettyField(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3448 | break; |
| 3449 | } |
| 3450 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3451 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3452 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3453 | // quickened opcodes (otherwise this could be a fall-through). |
| 3454 | if (!IsConstructor()) { |
| 3455 | if (!GetMethodReturnType().IsConflict()) { |
| 3456 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3457 | } |
| 3458 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3459 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3460 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3461 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3462 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3463 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3464 | case Instruction::UNUSED_79: |
| 3465 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3467 | break; |
| 3468 | |
| 3469 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3470 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3471 | * complain if an instruction is missing (which is desirable). |
| 3472 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3473 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3474 | |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3475 | if (flags_.have_pending_hard_failure_) { |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 3476 | if (IsAotMode()) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3477 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3478 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3479 | LOG(ERROR) << "Pending failures:"; |
| 3480 | for (auto& error : failures_) { |
| 3481 | LOG(ERROR) << error; |
| 3482 | } |
| 3483 | for (auto& error_msg : failure_messages_) { |
| 3484 | LOG(ERROR) << error_msg->str(); |
| 3485 | } |
| 3486 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3487 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3488 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3489 | /* immediate failure, reject class */ |
| 3490 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3491 | return false; |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3492 | } else if (flags_.have_pending_runtime_throw_failure_) { |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3493 | LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw"; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3494 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3495 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3496 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3497 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3498 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3499 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3500 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3501 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3502 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3503 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3504 | */ |
| 3505 | if (!just_set_result) { |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 3506 | work_line_->SetResultTypeToUnknown(GetRegTypeCache()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3509 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3510 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3511 | * |
| 3512 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3513 | * state of the register when the branch is taken or not taken. For example, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3514 | * somebody could get a reference field, check it for zero, and if the |
| 3515 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3516 | * since the value is known to be zero. We do not currently account for |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3517 | * that, and will reject the code. |
| 3518 | * |
| 3519 | * TODO: avoid re-fetching the branch target |
| 3520 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3521 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3522 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3523 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3524 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3525 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3526 | return false; |
| 3527 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3528 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3529 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), |
| 3530 | work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3531 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3532 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3533 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3534 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3535 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3536 | return false; |
| 3537 | } |
| 3538 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3539 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3540 | return false; |
| 3541 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3542 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3546 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3547 | * |
| 3548 | * We've already verified that the table is structurally sound, so we |
| 3549 | * just need to walk through and tag the targets. |
| 3550 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3551 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3552 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3553 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3554 | int switch_count = switch_insns[1]; |
| 3555 | int offset_to_targets, targ; |
| 3556 | |
| 3557 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3558 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3559 | offset_to_targets = 4; |
| 3560 | } else { |
| 3561 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3562 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3563 | offset_to_targets = 2 + 2 * switch_count; |
| 3564 | } |
| 3565 | |
| 3566 | /* verify each switch target */ |
| 3567 | for (targ = 0; targ < switch_count; targ++) { |
| 3568 | int offset; |
| 3569 | uint32_t abs_offset; |
| 3570 | |
| 3571 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3572 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3573 | (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3574 | abs_offset = work_insn_idx_ + offset; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3575 | DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits()); |
| 3576 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3577 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3578 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3579 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3580 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3581 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3582 | } |
| 3583 | } |
| 3584 | |
| 3585 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3586 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3587 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3588 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3589 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3590 | bool has_catch_all_handler = false; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3591 | const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3592 | CHECK(try_item != nullptr); |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 3593 | CatchHandlerIterator iterator(code_item_accessor_, *try_item); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3594 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3595 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 3596 | ClassLinker* linker = GetClassLinker(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3597 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3598 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3599 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3600 | if (!handler_type_idx.IsValid()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3601 | has_catch_all_handler = true; |
| 3602 | } else { |
| 3603 | // It is also a catch-all if it is java.lang.Throwable. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3604 | ObjPtr<mirror::Class> klass = |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3605 | linker->ResolveType(handler_type_idx, dex_cache_, class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3606 | if (klass != nullptr) { |
Vladimir Marko | adbceb7 | 2018-05-29 14:34:14 +0100 | [diff] [blame] | 3607 | if (klass == GetClassRoot<mirror::Throwable>()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3608 | has_catch_all_handler = true; |
| 3609 | } |
| 3610 | } else { |
| 3611 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3612 | DCHECK(self_->IsExceptionPending()); |
| 3613 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3614 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3615 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3616 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3617 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3618 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3619 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3620 | */ |
Andreas Gampe | bfcca58 | 2019-04-19 12:01:55 -0700 | [diff] [blame] | 3621 | if (kVerifierDebug) { |
| 3622 | LogVerifyInfo() << "Updating exception handler 0x" |
| 3623 | << std::hex << iterator.GetHandlerAddress(); |
| 3624 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3625 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3626 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3627 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3631 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3632 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3633 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3634 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3635 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3636 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3637 | * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3638 | * it will do so before grabbing the lock). |
| 3639 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3640 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3641 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3642 | << "expected to be within a catch-all for an instruction where a monitor is held"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3643 | return false; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3648 | /* Handle "continue". Tag the next consecutive instruction. |
| 3649 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3650 | * because it changes work_line_ when performing peephole optimization |
| 3651 | * and this change should not be used in those cases. |
| 3652 | */ |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3653 | if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3654 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3655 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3656 | if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3657 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3658 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3659 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3660 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3661 | // next instruction isn't one. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3662 | if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3663 | return false; |
| 3664 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3665 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3666 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3667 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3668 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3669 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3670 | // For returns we only care about the operand to the return, all other registers are dead. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3671 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3672 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3673 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3674 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3675 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3676 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3677 | // needed. If the merge changes the state of the registers then the work line will be |
| 3678 | // updated. |
| 3679 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3680 | return false; |
| 3681 | } |
| 3682 | } else { |
| 3683 | /* |
| 3684 | * We're not recording register data for the next instruction, so we don't know what the |
| 3685 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3686 | */ |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3687 | GetModifiableInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3688 | } |
| 3689 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3690 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3691 | /* If we're returning from the method, make sure monitor stack is empty. */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3692 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3693 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3697 | * Update start_guess. Advance to the next instruction of that's |
| 3698 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3699 | * neither of those exists we're in a return or throw; leave start_guess |
| 3700 | * alone and let the caller sort it out. |
| 3701 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3702 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3703 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3704 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3705 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3706 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3707 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3710 | DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits()); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3711 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3712 | |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3713 | if (flags_.have_pending_runtime_throw_failure_) { |
| 3714 | flags_.have_any_pending_runtime_throw_failure_ = true; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3715 | // Reset the pending_runtime_throw flag now. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3716 | flags_.have_pending_runtime_throw_failure_ = false; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3719 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3720 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3721 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3722 | template <bool kVerifierDebug> |
| 3723 | template <CheckAccess C> |
| 3724 | const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 3725 | ClassLinker* linker = GetClassLinker(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3726 | ObjPtr<mirror::Class> klass = can_load_classes_ |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3727 | ? linker->ResolveType(class_idx, dex_cache_, class_loader_) |
| 3728 | : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3729 | if (can_load_classes_ && klass == nullptr) { |
| 3730 | DCHECK(self_->IsExceptionPending()); |
| 3731 | self_->ClearException(); |
| 3732 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3733 | const RegType* result = nullptr; |
Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3734 | if (klass != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3735 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3736 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3737 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3738 | UninstantiableError(descriptor); |
| 3739 | precise = false; |
| 3740 | } |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3741 | result = reg_types_.FindClass(klass, precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3742 | if (result == nullptr) { |
| 3743 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3744 | result = reg_types_.InsertClass(descriptor, klass, precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3745 | } |
| 3746 | } else { |
| 3747 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3748 | result = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3749 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3750 | DCHECK(result != nullptr); |
| 3751 | if (result->IsConflict()) { |
| 3752 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3753 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3754 | << "' in " << GetDeclaringClass(); |
| 3755 | return *result; |
| 3756 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3757 | |
| 3758 | // Record result of class resolution attempt. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3759 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3760 | |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3761 | // If requested, check if access is allowed. Unresolved types are included in this check, as the |
| 3762 | // interpreter only tests whether access is allowed when a class is not pre-verified and runs in |
| 3763 | // the access-checks interpreter. If result is primitive, skip the access check. |
| 3764 | // |
| 3765 | // Note: we do this for unresolved classes to trigger re-verification at runtime. |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3766 | if (C != CheckAccess::kNo && |
Andreas Gampe | dc39d32 | 2018-09-04 09:26:03 -0700 | [diff] [blame] | 3767 | result->IsNonZeroReferenceTypes() && |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3768 | ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) |
| 3769 | || !result->IsUnresolvedTypes())) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3770 | const RegType& referrer = GetDeclaringClass(); |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 3771 | if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) && |
| 3772 | !referrer.CanAccess(*result)) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3773 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3774 | << referrer << "' -> '" << *result << "'"; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3775 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3776 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3777 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3780 | template <bool kVerifierDebug> |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3781 | bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) { |
| 3782 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 3783 | // where one entrypoint to the catch block is not actually an exception path. |
| 3784 | if (work_insn_idx_ == 0) { |
| 3785 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 3786 | return true; |
| 3787 | } |
| 3788 | /* |
| 3789 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 3790 | * that as part of extracting the exception type from the catch block list. |
| 3791 | */ |
| 3792 | auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) -> |
| 3793 | std::pair<bool, const RegType*> { |
| 3794 | const RegType* common_super = nullptr; |
| 3795 | if (code_item_accessor_.TriesSize() != 0) { |
| 3796 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 3797 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3798 | const RegType* unresolved = nullptr; |
| 3799 | for (uint32_t i = 0; i < handlers_size; i++) { |
| 3800 | CatchHandlerIterator iterator(handlers_ptr); |
| 3801 | for (; iterator.HasNext(); iterator.Next()) { |
| 3802 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3803 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
| 3804 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3805 | } else { |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3806 | // Do access checks only on resolved exception classes. |
| 3807 | const RegType& exception = |
| 3808 | ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex()); |
| 3809 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
| 3810 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
| 3811 | if (exception.IsUnresolvedTypes()) { |
| 3812 | if (unresolved == nullptr) { |
| 3813 | unresolved = &exception; |
| 3814 | } else { |
| 3815 | unresolved = &unresolved->SafeMerge(exception, ®_types_, this); |
| 3816 | } |
| 3817 | } else { |
| 3818 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " |
| 3819 | << exception; |
| 3820 | return std::make_pair(true, ®_types_.Conflict()); |
| 3821 | } |
| 3822 | } else if (common_super == nullptr) { |
| 3823 | common_super = &exception; |
| 3824 | } else if (common_super->Equals(exception)) { |
| 3825 | // odd case, but nothing to do |
| 3826 | } else { |
| 3827 | common_super = &common_super->Merge(exception, ®_types_, this); |
| 3828 | if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3829 | *common_super, this), |
| 3830 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3831 | work_insn_idx_)) { |
| 3832 | break; |
| 3833 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3834 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | } |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3838 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3839 | } |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3840 | if (unresolved != nullptr) { |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 3841 | if (!IsAotMode() && common_super == nullptr) { |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3842 | // This is an unreachable handler. |
Andreas Gampe | 0a7d0b1 | 2019-06-04 10:42:29 -0700 | [diff] [blame] | 3843 | |
| 3844 | // We need to post a failure. The compiler currently does not handle unreachable |
| 3845 | // code correctly. |
Andreas Gampe | 0d87f99 | 2019-07-10 13:19:45 -0700 | [diff] [blame] | 3846 | Fail(VERIFY_ERROR_SKIP_COMPILER, /*pending_exc=*/ false) |
| 3847 | << "Unresolved catch handler, fail for compiler"; |
Andreas Gampe | 0a7d0b1 | 2019-06-04 10:42:29 -0700 | [diff] [blame] | 3848 | |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3849 | return std::make_pair(false, unresolved); |
| 3850 | } |
| 3851 | // Soft-fail, but do not handle this with a synthetic throw. |
Andreas Gampe | 0d87f99 | 2019-07-10 13:19:45 -0700 | [diff] [blame] | 3852 | Fail(VERIFY_ERROR_NO_CLASS, /*pending_exc=*/ false) << "Unresolved catch handler"; |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3853 | if (common_super != nullptr) { |
| 3854 | unresolved = &unresolved->Merge(*common_super, ®_types_, this); |
| 3855 | } |
| 3856 | return std::make_pair(true, unresolved); |
| 3857 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3858 | } |
Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3859 | if (common_super == nullptr) { |
| 3860 | /* no catch blocks, or no catches with classes we can find */ |
| 3861 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
| 3862 | return std::make_pair(true, ®_types_.Conflict()); |
| 3863 | } |
| 3864 | return std::make_pair(true, common_super); |
| 3865 | }; |
| 3866 | auto result = caught_exc_type_fn(); |
| 3867 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second); |
| 3868 | return result.first; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3871 | template <bool kVerifierDebug> |
| 3872 | ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3873 | uint32_t dex_method_idx, MethodType method_type) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3874 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3875 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3876 | if (klass_type.IsConflict()) { |
| 3877 | std::string append(" in attempt to access method "); |
| 3878 | append += dex_file_->GetMethodName(method_id); |
| 3879 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3880 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3881 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3882 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3883 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3884 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3885 | ObjPtr<mirror::Class> klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3886 | const RegType& referrer = GetDeclaringClass(); |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 3887 | ClassLinker* class_linker = GetClassLinker(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 3888 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3889 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3890 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3891 | if (res_method == nullptr) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 3892 | res_method = class_linker->FindResolvedMethod( |
| 3893 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3894 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3895 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3896 | // Record result of method resolution attempt. The klass resolution has recorded whether |
| 3897 | // the class is an interface or not and therefore the type of the lookup performed above. |
| 3898 | // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type. |
| 3899 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method); |
| 3900 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3901 | bool must_fail = false; |
| 3902 | // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method |
| 3903 | // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's |
| 3904 | // hard to see the differences. |
| 3905 | // If we don't have res_method here we must fail. Just use this bool to make sure of that with a |
| 3906 | // DCHECK. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3907 | if (res_method == nullptr) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3908 | must_fail = true; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3909 | // Try to find the method also with the other type for better error reporting below |
| 3910 | // but do not store such bogus lookup result in the DexCache or VerifierDeps. |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 3911 | res_method = class_linker->FindIncompatibleMethod( |
| 3912 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3913 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3914 | |
| 3915 | if (res_method == nullptr) { |
| 3916 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3917 | << klass->PrettyDescriptor() << "." |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3918 | << dex_file_->GetMethodName(method_id) << " " |
| 3919 | << dex_file_->GetMethodSignature(method_id); |
| 3920 | return nullptr; |
| 3921 | } |
| 3922 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3923 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3924 | // enforce them here. |
| 3925 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3926 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3927 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3928 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3929 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3930 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3931 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3932 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3933 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3934 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3935 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3936 | |
| 3937 | // Check that interface methods are static or match interface classes. |
| 3938 | // We only allow statics if we don't have default methods enabled. |
| 3939 | // |
| 3940 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3941 | // while this check implies an IncompatibleClassChangeError. |
| 3942 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3943 | // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3944 | // default methods are supported for the dex file), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3945 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3946 | method_type != METHOD_STATIC && |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3947 | (!dex_file_->SupportsDefaultMethods() || |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3948 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3949 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3950 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3951 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3952 | << " is in an interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3953 | return nullptr; |
| 3954 | } |
| 3955 | } else { |
| 3956 | if (method_type == METHOD_INTERFACE) { |
| 3957 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3958 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3959 | << " is in a non-interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3960 | return nullptr; |
| 3961 | } |
| 3962 | } |
| 3963 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3964 | // Check specifically for non-public object methods being provided for interface dispatch. This |
| 3965 | // can occur if we failed to find a method with FindInterfaceMethod but later find one with |
| 3966 | // FindClassMethod for error message use. |
| 3967 | if (method_type == METHOD_INTERFACE && |
| 3968 | res_method->GetDeclaringClass()->IsObjectClass() && |
| 3969 | !res_method->IsPublic()) { |
| 3970 | Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "." |
| 3971 | << dex_file_->GetMethodName(method_id) << " " |
| 3972 | << dex_file_->GetMethodSignature(method_id) << " resolved to " |
| 3973 | << "non-public object method " << res_method->PrettyMethod() << " " |
| 3974 | << "but non-public Object methods are excluded from interface " |
| 3975 | << "method resolution."; |
| 3976 | return nullptr; |
| 3977 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3978 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3979 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3980 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 3981 | << res_method->PrettyMethod() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3982 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3983 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3984 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3985 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3986 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3987 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3988 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3989 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3990 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3991 | // See if the method type implied by the invoke instruction matches the access flags for the |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3992 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 3993 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 3994 | // arguments. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3995 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3996 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3997 | ((method_type == METHOD_SUPER || |
| 3998 | method_type == METHOD_VIRTUAL || |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3999 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 4000 | ((method_type == METHOD_POLYMORPHIC) && |
| 4001 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4002 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4003 | "type of " << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4004 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4005 | } |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 4006 | // Make sure we weren't expecting to fail. |
| 4007 | DCHECK(!must_fail) << "invoke type (" << method_type << ")" |
| 4008 | << klass->PrettyDescriptor() << "." |
| 4009 | << dex_file_->GetMethodName(method_id) << " " |
| 4010 | << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to " |
| 4011 | << res_method->PrettyMethod() << " without error. Initially this method was " |
| 4012 | << "not found so we were expecting to fail for some reason."; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4013 | return res_method; |
| 4014 | } |
| 4015 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4016 | template <bool kVerifierDebug> |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4017 | template <class T> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4018 | ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4019 | T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) { |
Andreas Gampe | e383d23 | 2018-06-19 12:29:51 -0700 | [diff] [blame] | 4020 | DCHECK_EQ(!is_range, inst->HasVarArgs()); |
| 4021 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4022 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4023 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4024 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4025 | const size_t expected_args = inst->VRegA(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4026 | /* caught by static verifier */ |
| 4027 | DCHECK(is_range || expected_args <= 5); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4028 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4029 | if (expected_args > code_item_accessor_.OutsSize()) { |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4030 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4031 | << ") exceeds outsSize (" |
| 4032 | << code_item_accessor_.OutsSize() << ")"; |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4033 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4034 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4035 | |
| 4036 | /* |
| 4037 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4038 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4039 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4040 | */ |
| 4041 | if (method_type != METHOD_STATIC) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4042 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4043 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4044 | CHECK(flags_.have_pending_hard_failure_); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4045 | return nullptr; |
| 4046 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4047 | bool is_init = false; |
| 4048 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4049 | if (res_method) { |
| 4050 | if (!res_method->IsConstructor()) { |
| 4051 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4052 | return nullptr; |
| 4053 | } |
| 4054 | } else { |
| 4055 | // Check whether the name of the called method is "<init>" |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4056 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Jeff Hao | 0d08727 | 2014-08-04 14:47:17 -0700 | [diff] [blame] | 4057 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4058 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4059 | return nullptr; |
| 4060 | } |
| 4061 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4062 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4063 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4064 | const RegType& adjusted_type = is_init |
| 4065 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 4066 | : actual_arg_type; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4067 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4068 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 4069 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 4070 | // class. It would be wrong to use this for the type check (interface type checks are |
| 4071 | // postponed to runtime). |
| 4072 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 4073 | ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4074 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4075 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 4076 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4077 | } else { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4078 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4079 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4080 | res_method_class = ®_types_.FromDescriptor( |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4081 | class_loader_.Get(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4082 | dex_file_->StringByTypeIdx(class_idx), |
| 4083 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4084 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4085 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4086 | Fail(adjusted_type.IsUnresolvedTypes() |
| 4087 | ? VERIFY_ERROR_NO_CLASS |
| 4088 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 4089 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 4090 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4091 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4092 | // the compiler. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4093 | if (flags_.have_pending_hard_failure_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4094 | return nullptr; |
| 4095 | } |
| 4096 | } |
| 4097 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4100 | uint32_t arg[5]; |
| 4101 | if (!is_range) { |
| 4102 | inst->GetVarArgs(arg); |
| 4103 | } |
| 4104 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4105 | for ( ; it->HasNext(); it->Next()) { |
| 4106 | if (sig_registers >= expected_args) { |
| 4107 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4108 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4109 | return nullptr; |
| 4110 | } |
| 4111 | |
| 4112 | const char* param_descriptor = it->GetDescriptor(); |
| 4113 | |
| 4114 | if (param_descriptor == nullptr) { |
| 4115 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 4116 | "component"; |
| 4117 | return nullptr; |
| 4118 | } |
| 4119 | |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4120 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 4121 | param_descriptor, |
| 4122 | false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4123 | uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4124 | arg[sig_registers]; |
| 4125 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4126 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4127 | if (!src_type.IsIntegralTypes()) { |
| 4128 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4129 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4130 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4131 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4132 | } else { |
| 4133 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4134 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4135 | // the compiler. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4136 | if (flags_.have_pending_hard_failure_) { |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4137 | return nullptr; |
| 4138 | } |
| 4139 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4140 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4141 | // instructions not specifying register pairs by the first component, but require them |
| 4142 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4143 | // none, this will fail below. |
| 4144 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4145 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4146 | if (second_reg != get_reg + 1) { |
| 4147 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4148 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4149 | << second_reg << "."; |
| 4150 | return nullptr; |
| 4151 | } |
| 4152 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4156 | } |
| 4157 | if (expected_args != sig_registers) { |
| 4158 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4159 | " argument registers, method signature has " << sig_registers; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4160 | return nullptr; |
| 4161 | } |
| 4162 | return res_method; |
| 4163 | } |
| 4164 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4165 | template <bool kVerifierDebug> |
| 4166 | void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4167 | MethodType method_type, |
| 4168 | bool is_range) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4169 | // As the method may not have been resolved, make this static check against what we expect. |
| 4170 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4171 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4172 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4173 | DexFileParameterIterator it(*dex_file_, |
| 4174 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4175 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4178 | template <bool kVerifierDebug> |
| 4179 | bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) { |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4180 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4181 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4182 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4183 | return false; |
| 4184 | } |
| 4185 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4186 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
Alex Light | 1e52a07 | 2019-06-25 09:12:04 -0700 | [diff] [blame] | 4187 | // Check essential arguments are provided. The dex file verifier has verified indices of the |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4188 | // main values (method handle, name, method_type). |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4189 | static const size_t kRequiredArguments = 3; |
| 4190 | if (it.Size() < kRequiredArguments) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4191 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4192 | << " has too few arguments: " |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4193 | << it.Size() << " < " << kRequiredArguments; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4194 | return false; |
| 4195 | } |
| 4196 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4197 | std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] = |
| 4198 | { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() }, |
| 4199 | { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() }, |
| 4200 | { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() } |
| 4201 | }; |
| 4202 | uint32_t index[kRequiredArguments]; |
| 4203 | |
| 4204 | // Check arguments have expected types and are within permitted ranges. |
| 4205 | for (size_t i = 0; i < kRequiredArguments; ++i) { |
| 4206 | if (it.GetValueType() != type_and_max[i].first) { |
| 4207 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4208 | << " argument " << i << " has wrong type " |
| 4209 | << it.GetValueType() << "!=" << type_and_max[i].first; |
| 4210 | return false; |
| 4211 | } |
| 4212 | index[i] = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4213 | if (index[i] >= type_and_max[i].second) { |
| 4214 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4215 | << " argument " << i << " bad index " |
| 4216 | << index[i] << " >= " << type_and_max[i].second; |
| 4217 | return false; |
| 4218 | } |
| 4219 | it.Next(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 4220 | } |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4221 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4222 | // Check method handle kind is valid. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4223 | const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4224 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 4226 | << " argument 0 method handle type is not InvokeStatic: " |
| 4227 | << mh.method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4228 | return false; |
| 4229 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4230 | return true; |
| 4231 | } |
| 4232 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4233 | class MethodParamListDescriptorIterator { |
| 4234 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4235 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4236 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4237 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4238 | } |
| 4239 | |
| 4240 | bool HasNext() { |
| 4241 | return pos_ < params_size_; |
| 4242 | } |
| 4243 | |
| 4244 | void Next() { |
| 4245 | ++pos_; |
| 4246 | } |
| 4247 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4248 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4249 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4250 | } |
| 4251 | |
| 4252 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4253 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4254 | size_t pos_; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4255 | const dex::TypeList* params_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4256 | const size_t params_size_; |
| 4257 | }; |
| 4258 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4259 | template <bool kVerifierDebug> |
| 4260 | ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4261 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4262 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4263 | // we're making. |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4264 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4265 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4266 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4267 | // Check what we can statically. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4268 | if (!flags_.have_pending_hard_failure_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4269 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4270 | } |
| 4271 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4272 | } |
| 4273 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4274 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4275 | // has a vtable entry for the target method. Or the target is on a interface. |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4276 | if (method_type == METHOD_SUPER) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4277 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4278 | const RegType& reference_type = reg_types_.FromDescriptor( |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4279 | class_loader_.Get(), |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4280 | dex_file_->StringByTypeIdx(class_idx), |
| 4281 | false); |
| 4282 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4283 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4284 | return nullptr; |
| 4285 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4286 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4287 | if (!GetDeclaringClass().HasClass()) { |
| 4288 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4289 | << "interface invoke-super"; |
| 4290 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4291 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4292 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4293 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4294 | << " in method " |
| 4295 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4296 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4297 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4298 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4299 | } |
| 4300 | } else { |
| 4301 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4302 | if (super.IsUnresolvedTypes()) { |
| 4303 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4304 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4305 | << " to super " << res_method->PrettyMethod(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4306 | return nullptr; |
| 4307 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4308 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4309 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4310 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4311 | << dex_file_->PrettyMethod(dex_method_idx_) |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4312 | << " to super " << super |
| 4313 | << "." << res_method->GetName() |
| 4314 | << res_method->GetSignature(); |
| 4315 | return nullptr; |
| 4316 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4317 | } |
| 4318 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4319 | |
Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4320 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4321 | // Process the signature of the calling site that is invoking the method handle. |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 4322 | dex::ProtoIndex proto_idx(inst->VRegH()); |
| 4323 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4324 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4325 | } else { |
| 4326 | // Process the target method's signature. |
| 4327 | MethodParamListDescriptorIterator it(res_method); |
| 4328 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4329 | } |
| 4330 | } |
| 4331 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4332 | template <bool kVerifierDebug> |
| 4333 | bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 4334 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4335 | const char* method_name = method->GetName(); |
| 4336 | |
| 4337 | const char* expected_return_descriptor; |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4338 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots(); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4339 | if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4340 | expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4341 | } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4342 | expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name); |
| 4343 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4344 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4345 | << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4346 | return false; |
| 4347 | } |
| 4348 | |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4349 | if (expected_return_descriptor == nullptr) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4350 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4351 | << "Signature polymorphic method name invalid: " << method_name; |
| 4352 | return false; |
| 4353 | } |
| 4354 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4355 | const dex::TypeList* types = method->GetParameterTypeList(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4356 | if (types->Size() != 1) { |
| 4357 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4358 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4359 | return false; |
| 4360 | } |
| 4361 | |
| 4362 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4363 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4364 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4365 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4366 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4367 | return false; |
| 4368 | } |
| 4369 | |
| 4370 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4371 | if (strcmp(return_descriptor, expected_return_descriptor) != 0) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4372 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4373 | << "Signature polymorphic method has unexpected return type: " << return_descriptor |
| 4374 | << " != " << expected_return_descriptor; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4375 | return false; |
| 4376 | } |
| 4377 | |
| 4378 | return true; |
| 4379 | } |
| 4380 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4381 | template <bool kVerifierDebug> |
| 4382 | bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4383 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4384 | if (this_type.IsZeroOrNull()) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4385 | /* null pointer always passes (and always fails at run time) */ |
| 4386 | return true; |
| 4387 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4388 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4389 | << "invoke-polymorphic receiver is not a reference: " |
| 4390 | << this_type; |
| 4391 | return false; |
| 4392 | } else if (this_type.IsUninitializedReference()) { |
| 4393 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4394 | << "invoke-polymorphic receiver is uninitialized: " |
| 4395 | << this_type; |
| 4396 | return false; |
| 4397 | } else if (!this_type.HasClass()) { |
| 4398 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4399 | << "invoke-polymorphic receiver has no class: " |
| 4400 | << this_type; |
| 4401 | return false; |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4402 | } else { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4403 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots(); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4404 | if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) && |
| 4405 | !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) { |
| 4406 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4407 | << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: " |
| 4408 | << this_type; |
| 4409 | return false; |
| 4410 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4411 | } |
| 4412 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4413 | } |
| 4414 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4415 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4416 | void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst, |
| 4417 | bool is_filled, |
| 4418 | bool is_range) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4419 | dex::TypeIndex type_idx; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4420 | if (!is_filled) { |
| 4421 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4422 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4423 | } else if (!is_range) { |
| 4424 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4425 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4426 | } else { |
| 4427 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4428 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4429 | } |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4430 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4431 | if (res_type.IsConflict()) { // bad class |
| 4432 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4433 | } else { |
| 4434 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4435 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4436 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4437 | } else if (!is_filled) { |
| 4438 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4439 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4440 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4441 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4442 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4443 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4444 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4445 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4446 | // the list and fail. It's legal, if silly, for arg_count to be zero. |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4447 | const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4448 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4449 | uint32_t arg[5]; |
| 4450 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4451 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4452 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4453 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4454 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4455 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4456 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4457 | return; |
| 4458 | } |
| 4459 | } |
| 4460 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4461 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4462 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4467 | template <bool kVerifierDebug> |
| 4468 | void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst, |
| 4469 | const RegType& insn_type, |
| 4470 | bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4471 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4472 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4473 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4474 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4475 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4476 | if (array_type.IsZeroOrNull()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4477 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
Andreas Gampe | 52f205a | 2017-12-01 12:16:07 -0800 | [diff] [blame] | 4478 | // instruction type. |
| 4479 | if (!is_primitive) { |
| 4480 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null()); |
| 4481 | } else if (insn_type.IsInteger()) { |
| 4482 | // Pick a non-zero constant (to distinguish with null) that can fit in any primitive. |
| 4483 | // We cannot use 'insn_type' as it could be a float array or an int array. |
| 4484 | work_line_->SetRegisterType<LockOp::kClear>( |
| 4485 | this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_)); |
| 4486 | } else if (insn_type.IsCategory1Types()) { |
| 4487 | // Category 1 |
| 4488 | // The 'insn_type' is exactly the type we need. |
| 4489 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4490 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4491 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4492 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4493 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4494 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4495 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4496 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4497 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4498 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4499 | // Unresolved array types must be reference array types. |
| 4500 | if (is_primitive) { |
| 4501 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4502 | << " source for category 1 aget"; |
| 4503 | } else { |
| 4504 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4505 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4506 | // Approximate with java.lang.Object[]. |
| 4507 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4508 | inst->VRegA_23x(), |
| 4509 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4510 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4511 | } else { |
| 4512 | /* verify the class */ |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4513 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4514 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4515 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4516 | << " source for aget-object"; |
| 4517 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4518 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4519 | << " source for category 1 aget"; |
| 4520 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4521 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4522 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4523 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4524 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4525 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4526 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4527 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4528 | // doubles. |
| 4529 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4530 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4531 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4532 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4533 | component_type.HighHalf(®_types_)); |
| 4534 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | } |
| 4539 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4540 | template <bool kVerifierDebug> |
| 4541 | void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type, |
| 4542 | const RegType& insn_type, |
| 4543 | const uint32_t vregA) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4544 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4545 | bool instruction_compatible; |
| 4546 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4547 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4548 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4549 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4550 | value_compatible = value_type.IsIntegralTypes(); |
| 4551 | } else if (target_type.IsFloat()) { |
| 4552 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4553 | value_compatible = value_type.IsFloatTypes(); |
| 4554 | } else if (target_type.IsLong()) { |
| 4555 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4556 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4557 | // as target_type depends on the resolved type of the field. |
| 4558 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4559 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4560 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4561 | } else { |
| 4562 | value_compatible = false; |
| 4563 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4564 | } else if (target_type.IsDouble()) { |
| 4565 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4566 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4567 | // as target_type depends on the resolved type of the field. |
| 4568 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4569 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4570 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4571 | } else { |
| 4572 | value_compatible = false; |
| 4573 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4574 | } else { |
| 4575 | instruction_compatible = false; // reference with primitive store |
| 4576 | value_compatible = false; // unused |
| 4577 | } |
| 4578 | if (!instruction_compatible) { |
| 4579 | // This is a global failure rather than a class change failure as the instructions and |
| 4580 | // the descriptors for the type should have been consistent within the same file at |
| 4581 | // compile time. |
| 4582 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4583 | << "' but expected type '" << target_type << "'"; |
| 4584 | return; |
| 4585 | } |
| 4586 | if (!value_compatible) { |
| 4587 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4588 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4589 | return; |
| 4590 | } |
| 4591 | } |
| 4592 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4593 | template <bool kVerifierDebug> |
| 4594 | void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst, |
| 4595 | const RegType& insn_type, |
| 4596 | bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4597 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4598 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4599 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4600 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4601 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4602 | if (array_type.IsZeroOrNull()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4603 | // Null array type; this code path will fail at runtime. |
| 4604 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4605 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4606 | // and fits multiple register types. |
| 4607 | const RegType* modified_reg_type = &insn_type; |
| 4608 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4609 | (modified_reg_type == ®_types_.LongLo())) { |
| 4610 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4611 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4612 | if (modified_reg_type == ®_types_.Integer()) { |
| 4613 | if (&value_type == ®_types_.Float()) { |
| 4614 | modified_reg_type = &value_type; |
| 4615 | } |
| 4616 | } else { |
| 4617 | if (&value_type == ®_types_.DoubleLo()) { |
| 4618 | modified_reg_type = &value_type; |
| 4619 | } |
| 4620 | } |
| 4621 | } |
| 4622 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4623 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4624 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4625 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4626 | // Unresolved array types must be reference array types. |
| 4627 | if (is_primitive) { |
| 4628 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4629 | << "' but unresolved type '" << array_type << "'"; |
| 4630 | } else { |
| 4631 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4632 | << " because of missing class"; |
| 4633 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4634 | } else { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4635 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4636 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4637 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4638 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4639 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4640 | if (!component_type.IsReferenceTypes()) { |
| 4641 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4642 | << " source for aput-object"; |
| 4643 | } else { |
| 4644 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4645 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4646 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4647 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4648 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4649 | } |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4654 | template <bool kVerifierDebug> |
| 4655 | ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4656 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4657 | // Check access to class |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4658 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4659 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4660 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4661 | field_idx, dex_file_->GetFieldName(field_id), |
| 4662 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4663 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4664 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4665 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4666 | // Accessibility checks depend on resolved fields. |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4667 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4668 | !failures_.empty() || |
| 4669 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4670 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4671 | return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime. |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4672 | } |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4673 | ClassLinker* class_linker = GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4674 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4675 | |
| 4676 | // Record result of the field resolution attempt. |
| 4677 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4678 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4679 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4680 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4681 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4682 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4683 | DCHECK(self_->IsExceptionPending()); |
| 4684 | self_->ClearException(); |
| 4685 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4686 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4687 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4688 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4689 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4690 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4691 | } else if (!field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4692 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4693 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4694 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4695 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4698 | template <bool kVerifierDebug> |
| 4699 | ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) { |
Andreas Gampe | b34981b | 2019-05-06 13:00:40 -0700 | [diff] [blame] | 4700 | if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) { |
| 4701 | // Trying to read a field from something that isn't a reference. |
| 4702 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4703 | << "non-reference type " << obj_type; |
| 4704 | return nullptr; |
| 4705 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4706 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4707 | // Check access to class. |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4708 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4709 | if (klass_type.IsConflict()) { |
| 4710 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4711 | field_idx, dex_file_->GetFieldName(field_id), |
| 4712 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4713 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4714 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4715 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4716 | // Accessibility checks depend on resolved fields. |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4717 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4718 | !failures_.empty() || |
| 4719 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4720 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4721 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4722 | } |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4723 | ClassLinker* class_linker = GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4724 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4725 | |
| 4726 | // Record result of the field resolution attempt. |
| 4727 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4728 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4729 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4730 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4731 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4732 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4733 | DCHECK(self_->IsExceptionPending()); |
| 4734 | self_->ClearException(); |
| 4735 | return nullptr; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4736 | } else if (obj_type.IsZeroOrNull()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4737 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4738 | // Fall through into a few last soft failure checks below. |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4739 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4740 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4741 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4742 | const RegType& field_klass = |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4743 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4744 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4745 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4746 | // the field is declared in this class. |
| 4747 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4748 | // appear in constructors. |
| 4749 | if (!obj_type.IsUninitializedThisReference() || |
| 4750 | !IsConstructor() || |
| 4751 | !field_klass.Equals(GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4752 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4753 | << " of a not fully initialized object within the context" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4754 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4755 | return nullptr; |
| 4756 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4757 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4758 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4759 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4760 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4761 | VerifyError type; |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 4762 | bool is_aot = IsAotMode(); |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4763 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4764 | // Compiler & unresolved types involved, retry at runtime. |
| 4765 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4766 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4767 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4768 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4769 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4770 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4771 | Fail(type) << "cannot access instance field " << field->PrettyField() |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4772 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4773 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4774 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4775 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4776 | |
| 4777 | // Few last soft failure checks. |
| 4778 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4779 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4780 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4781 | << " from " << GetDeclaringClass(); |
| 4782 | return nullptr; |
| 4783 | } else if (field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4784 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4785 | << " to not be static"; |
| 4786 | return nullptr; |
| 4787 | } |
| 4788 | |
| 4789 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4790 | } |
| 4791 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4792 | template <bool kVerifierDebug> |
| 4793 | template <FieldAccessType kAccType> |
| 4794 | void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst, |
| 4795 | const RegType& insn_type, |
| 4796 | bool is_primitive, |
| 4797 | bool is_static) { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4798 | uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4799 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4800 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4801 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4802 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4803 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4804 | |
| 4805 | // One is not allowed to access fields on uninitialized references, except to write to |
| 4806 | // fields in the constructor (before calling another constructor). |
| 4807 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 4808 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 4809 | // checks at the same time that we're verifying a constructor). |
| 4810 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 4811 | object_type.IsUninitializedThisReference(); |
| 4812 | const RegType& adjusted_type = should_adjust |
| 4813 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 4814 | : object_type; |
| 4815 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4816 | if (UNLIKELY(flags_.have_pending_hard_failure_)) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4817 | return; |
| 4818 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4819 | if (should_adjust) { |
| 4820 | if (field == nullptr) { |
| 4821 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 4822 | << "to the superclass being initialized in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4823 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4824 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4825 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4826 | << field->PrettyField() << " of a not fully initialized " |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4827 | << "object within the context of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4828 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4829 | return; |
| 4830 | } |
| 4831 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4832 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4833 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4834 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4835 | if (kAccType == FieldAccessType::kAccPut) { |
| 4836 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4837 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4838 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 4839 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4840 | } |
| 4841 | } |
| 4842 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4843 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 4844 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4845 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4846 | field_type = &FromClass(field->GetTypeDescriptor(), |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4847 | field_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4848 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4849 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4850 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4851 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4852 | } |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4853 | } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) { |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4854 | // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to |
| 4855 | // redo verification at runtime as the field may be final, unless the field id shows it's in |
| 4856 | // the same class. |
| 4857 | // |
| 4858 | // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an |
| 4859 | // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class |
| 4860 | // to the access-checks interpreter. |
| 4861 | // |
| 4862 | // Note: see b/34966607. This and above may be changed in the future. |
| 4863 | if (kAccType == FieldAccessType::kAccPut) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4864 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4865 | const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4866 | const RegType* field_class_type = ®_types_.FromDescriptor(class_loader_.Get(), |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4867 | field_class_descriptor, |
| 4868 | false); |
| 4869 | if (!field_class_type->Equals(GetDeclaringClass())) { |
| 4870 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of " |
| 4871 | << field_class_descriptor |
| 4872 | << "." |
| 4873 | << dex_file_->GetFieldName(field_id) |
| 4874 | << " from other class " |
| 4875 | << GetDeclaringClass(); |
| 4876 | } |
| 4877 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4878 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4879 | if (field_type == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4880 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4881 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4882 | field_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4883 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4884 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4885 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4886 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4887 | "Unexpected third access type"); |
| 4888 | if (kAccType == FieldAccessType::kAccPut) { |
| 4889 | // sput or iput. |
| 4890 | if (is_primitive) { |
| 4891 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4892 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4893 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4894 | // If the field type is not a reference, this is a global failure rather than |
| 4895 | // a class change failure as the instructions and the descriptors for the type |
| 4896 | // should have been consistent within the same file at compile time. |
| 4897 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4898 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4899 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4900 | << " to be compatible with type '" << insn_type |
| 4901 | << "' but found type '" << *field_type |
| 4902 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4903 | return; |
| 4904 | } |
| 4905 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4906 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4907 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4908 | // sget or iget. |
| 4909 | if (is_primitive) { |
| 4910 | if (field_type->Equals(insn_type) || |
| 4911 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4912 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4913 | // expected that read is of the correct primitive type or that int reads are reading |
| 4914 | // floats or long reads are reading doubles |
| 4915 | } else { |
| 4916 | // This is a global failure rather than a class change failure as the instructions and |
| 4917 | // the descriptors for the type should have been consistent within the same file at |
| 4918 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4919 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4920 | << " to be of type '" << insn_type |
| 4921 | << "' but found type '" << *field_type << "' in get"; |
| 4922 | return; |
| 4923 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4924 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4925 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4926 | // If the field type is not a reference, this is a global failure rather than |
| 4927 | // a class change failure as the instructions and the descriptors for the type |
| 4928 | // should have been consistent within the same file at compile time. |
| 4929 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4930 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4931 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4932 | << " to be compatible with type '" << insn_type |
| 4933 | << "' but found type '" << *field_type |
| 4934 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4935 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4936 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4937 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4938 | return; |
| 4939 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4940 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4941 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4942 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4943 | } else { |
| 4944 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4945 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4946 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4947 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4951 | template <bool kVerifierDebug> |
| 4952 | bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn, |
| 4953 | RegisterLine* merge_line, |
| 4954 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4955 | bool changed = true; |
| 4956 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4957 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4958 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4959 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4960 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4961 | * only way a register can transition out of "unknown", so this is not just an optimization.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4962 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4963 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4964 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4965 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4966 | merge_line->VerifyMonitorStackEmpty(this); |
| 4967 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4968 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4969 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4970 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4971 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4972 | // Directly bail if a hard failure was found. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4973 | if (flags_.have_pending_hard_failure_) { |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 4974 | return false; |
| 4975 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4976 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4977 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 4978 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4979 | if (kVerifierDebug) { |
Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 4980 | copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache())); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4981 | copy->CopyFromLine(target_line); |
| 4982 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4983 | changed = target_line->MergeRegisters(this, merge_line); |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4984 | if (flags_.have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4985 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4986 | } |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4987 | if (kVerifierDebug && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4988 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4989 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4990 | << copy->Dump(this) << " MERGE\n" |
| 4991 | << merge_line->Dump(this) << " ==\n" |
Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 4992 | << target_line->Dump(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4993 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4994 | if (update_merge_line && changed) { |
| 4995 | merge_line->CopyFromLine(target_line); |
| 4996 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4997 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4998 | if (changed) { |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4999 | GetModifiableInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5000 | } |
| 5001 | return true; |
| 5002 | } |
| 5003 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5004 | template <bool kVerifierDebug> |
| 5005 | const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5006 | if (return_type_ == nullptr) { |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 5007 | if (method_being_verified_ != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5008 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 5009 | ? method_being_verified_->ResolveReturnType() |
| 5010 | : method_being_verified_->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5011 | if (return_type_class != nullptr) { |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 5012 | return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(), |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 5013 | return_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5014 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5015 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5016 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5017 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5018 | } |
| 5019 | } |
| 5020 | if (return_type_ == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5021 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 5022 | const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5023 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5024 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 5025 | return_type_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5026 | } |
| 5027 | } |
| 5028 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5029 | } |
| 5030 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5031 | template <bool kVerifierDebug> |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5032 | const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5033 | if (precise) { |
| 5034 | // Precise constant type. |
| 5035 | return reg_types_.FromCat1Const(value, true); |
| 5036 | } else { |
| 5037 | // Imprecise constant type. |
| 5038 | if (value < -32768) { |
| 5039 | return reg_types_.IntConstant(); |
| 5040 | } else if (value < -128) { |
| 5041 | return reg_types_.ShortConstant(); |
| 5042 | } else if (value < 0) { |
| 5043 | return reg_types_.ByteConstant(); |
| 5044 | } else if (value == 0) { |
| 5045 | return reg_types_.Zero(); |
| 5046 | } else if (value == 1) { |
| 5047 | return reg_types_.One(); |
| 5048 | } else if (value < 128) { |
| 5049 | return reg_types_.PosByteConstant(); |
| 5050 | } else if (value < 32768) { |
| 5051 | return reg_types_.PosShortConstant(); |
| 5052 | } else if (value < 65536) { |
| 5053 | return reg_types_.CharConstant(); |
| 5054 | } else { |
| 5055 | return reg_types_.IntConstant(); |
| 5056 | } |
| 5057 | } |
| 5058 | } |
| 5059 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5060 | } // namespace |
| 5061 | } // namespace impl |
| 5062 | |
| 5063 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5064 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5065 | ArenaPool* arena_pool, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5066 | const DexFile* dex_file, |
| 5067 | const dex::CodeItem* code_item, |
| 5068 | uint32_t dex_method_idx, |
| 5069 | bool can_load_classes, |
| 5070 | bool allow_thread_suspension, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5071 | bool allow_soft_failures, |
| 5072 | bool aot_mode) |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5073 | : self_(self), |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5074 | arena_stack_(arena_pool), |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5075 | allocator_(&arena_stack_), |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5076 | reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension), |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5077 | reg_table_(allocator_), |
| 5078 | work_insn_idx_(dex::kDexNoIndex), |
| 5079 | dex_method_idx_(dex_method_idx), |
| 5080 | dex_file_(dex_file), |
| 5081 | code_item_accessor_(*dex_file, code_item), |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5082 | // TODO: make it designated initialization when we compile as C++20. |
| 5083 | flags_({false, false, false, false, aot_mode}), |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5084 | encountered_failure_types_(0), |
| 5085 | can_load_classes_(can_load_classes), |
| 5086 | allow_soft_failures_(allow_soft_failures), |
| 5087 | has_check_casts_(false), |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5088 | class_linker_(class_linker), |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5089 | link_(nullptr) { |
| 5090 | self->PushVerifier(this); |
| 5091 | } |
| 5092 | |
| 5093 | MethodVerifier::~MethodVerifier() { |
| 5094 | Thread::Current()->PopVerifier(this); |
| 5095 | STLDeleteElements(&failure_messages_); |
| 5096 | } |
| 5097 | |
| 5098 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5099 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5100 | ArenaPool* arena_pool, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5101 | uint32_t method_idx, |
| 5102 | const DexFile* dex_file, |
| 5103 | Handle<mirror::DexCache> dex_cache, |
| 5104 | Handle<mirror::ClassLoader> class_loader, |
| 5105 | const dex::ClassDef& class_def, |
| 5106 | const dex::CodeItem* code_item, |
| 5107 | ArtMethod* method, |
| 5108 | uint32_t method_access_flags, |
| 5109 | CompilerCallbacks* callbacks, |
| 5110 | bool allow_soft_failures, |
| 5111 | HardFailLogMode log_level, |
| 5112 | bool need_precise_constants, |
| 5113 | uint32_t api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5114 | bool aot_mode, |
Alex Light | db55a11 | 2019-10-17 10:32:47 -0700 | [diff] [blame^] | 5115 | bool allow_suspension, |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5116 | std::string* hard_failure_msg) { |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5117 | if (VLOG_IS_ON(verifier_debug)) { |
| 5118 | return VerifyMethod<true>(self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5119 | class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5120 | arena_pool, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5121 | method_idx, |
| 5122 | dex_file, |
| 5123 | dex_cache, |
| 5124 | class_loader, |
| 5125 | class_def, |
| 5126 | code_item, |
| 5127 | method, |
| 5128 | method_access_flags, |
| 5129 | callbacks, |
| 5130 | allow_soft_failures, |
| 5131 | log_level, |
| 5132 | need_precise_constants, |
| 5133 | api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5134 | aot_mode, |
Alex Light | db55a11 | 2019-10-17 10:32:47 -0700 | [diff] [blame^] | 5135 | allow_suspension, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5136 | hard_failure_msg); |
| 5137 | } else { |
| 5138 | return VerifyMethod<false>(self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5139 | class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5140 | arena_pool, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5141 | method_idx, |
| 5142 | dex_file, |
| 5143 | dex_cache, |
| 5144 | class_loader, |
| 5145 | class_def, |
| 5146 | code_item, |
| 5147 | method, |
| 5148 | method_access_flags, |
| 5149 | callbacks, |
| 5150 | allow_soft_failures, |
| 5151 | log_level, |
| 5152 | need_precise_constants, |
| 5153 | api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5154 | aot_mode, |
Alex Light | db55a11 | 2019-10-17 10:32:47 -0700 | [diff] [blame^] | 5155 | allow_suspension, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5156 | hard_failure_msg); |
| 5157 | } |
| 5158 | } |
| 5159 | |
| 5160 | template <bool kVerifierDebug> |
| 5161 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5162 | ClassLinker* class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5163 | ArenaPool* arena_pool, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5164 | uint32_t method_idx, |
| 5165 | const DexFile* dex_file, |
| 5166 | Handle<mirror::DexCache> dex_cache, |
| 5167 | Handle<mirror::ClassLoader> class_loader, |
| 5168 | const dex::ClassDef& class_def, |
| 5169 | const dex::CodeItem* code_item, |
| 5170 | ArtMethod* method, |
| 5171 | uint32_t method_access_flags, |
| 5172 | CompilerCallbacks* callbacks, |
| 5173 | bool allow_soft_failures, |
| 5174 | HardFailLogMode log_level, |
| 5175 | bool need_precise_constants, |
| 5176 | uint32_t api_level, |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5177 | bool aot_mode, |
Alex Light | db55a11 | 2019-10-17 10:32:47 -0700 | [diff] [blame^] | 5178 | bool allow_suspension, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5179 | std::string* hard_failure_msg) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5180 | MethodVerifier::FailureData result; |
| 5181 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
| 5182 | |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5183 | impl::MethodVerifier<kVerifierDebug> verifier(self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5184 | class_linker, |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5185 | arena_pool, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5186 | dex_file, |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5187 | code_item, |
| 5188 | method_idx, |
Alex Light | db55a11 | 2019-10-17 10:32:47 -0700 | [diff] [blame^] | 5189 | /* can_load_classes= */ allow_suspension, |
| 5190 | /* allow_thread_suspension= */ allow_suspension, |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5191 | allow_soft_failures, |
| 5192 | aot_mode, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5193 | dex_cache, |
| 5194 | class_loader, |
| 5195 | class_def, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5196 | method, |
| 5197 | method_access_flags, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5198 | need_precise_constants, |
| 5199 | /* verify to dump */ false, |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5200 | /* fill_register_lines= */ false, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5201 | api_level); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5202 | if (verifier.Verify()) { |
| 5203 | // Verification completed, however failures may be pending that didn't cause the verification |
| 5204 | // to hard fail. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5205 | CHECK(!verifier.flags_.have_pending_hard_failure_); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5206 | |
| 5207 | if (code_item != nullptr && callbacks != nullptr) { |
| 5208 | // Let the interested party know that the method was verified. |
| 5209 | callbacks->MethodVerified(&verifier); |
| 5210 | } |
| 5211 | |
| 5212 | if (verifier.failures_.size() != 0) { |
| 5213 | if (VLOG_IS_ON(verifier)) { |
| 5214 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
| 5215 | << dex_file->PrettyMethod(method_idx) << "\n"); |
| 5216 | } |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5217 | if (kVerifierDebug) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5218 | LOG(INFO) << verifier.info_messages_.str(); |
| 5219 | verifier.Dump(LOG_STREAM(INFO)); |
| 5220 | } |
| 5221 | result.kind = FailureKind::kSoftFailure; |
| 5222 | if (method != nullptr && |
| 5223 | !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) { |
| 5224 | method->SetDontCompile(); |
| 5225 | } |
| 5226 | } |
| 5227 | if (method != nullptr) { |
| 5228 | if (verifier.HasInstructionThatWillThrow()) { |
| 5229 | method->SetDontCompile(); |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5230 | if (aot_mode && (callbacks != nullptr) && !callbacks->IsBootImage()) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5231 | // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger |
| 5232 | // re-verification at runtime. |
| 5233 | // The dead code after the throw is not verified and might be invalid. This may cause |
| 5234 | // the JIT compiler to crash since it assumes that all the code is valid. |
| 5235 | // |
| 5236 | // There's a strong assumption that the entire boot image is verified and all its dex |
| 5237 | // code is valid (even the dead and unverified one). As such this is done only for apps. |
| 5238 | // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are |
| 5239 | // fully verified). |
| 5240 | result.kind = FailureKind::kSoftFailure; |
| 5241 | } |
| 5242 | } |
| 5243 | if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
| 5244 | method->SetMustCountLocks(); |
| 5245 | } |
| 5246 | } |
| 5247 | } else { |
| 5248 | // Bad method data. |
| 5249 | CHECK_NE(verifier.failures_.size(), 0U); |
| 5250 | |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5251 | if (UNLIKELY(verifier.flags_.have_pending_experimental_failure_)) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5252 | // Failed due to being forced into interpreter. This is ok because |
| 5253 | // we just want to skip verification. |
| 5254 | result.kind = FailureKind::kSoftFailure; |
| 5255 | } else { |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5256 | CHECK(verifier.flags_.have_pending_hard_failure_); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5257 | if (VLOG_IS_ON(verifier)) { |
| 5258 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
| 5259 | } |
| 5260 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 5261 | LogSeverity severity; |
| 5262 | switch (log_level) { |
| 5263 | case HardFailLogMode::kLogVerbose: |
| 5264 | severity = LogSeverity::VERBOSE; |
| 5265 | break; |
| 5266 | case HardFailLogMode::kLogWarning: |
| 5267 | severity = LogSeverity::WARNING; |
| 5268 | break; |
| 5269 | case HardFailLogMode::kLogInternalFatal: |
| 5270 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
| 5271 | break; |
| 5272 | default: |
| 5273 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 5274 | UNREACHABLE(); |
| 5275 | } |
| 5276 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
| 5277 | << dex_file->PrettyMethod(method_idx) |
| 5278 | << "\n"); |
| 5279 | } |
| 5280 | if (hard_failure_msg != nullptr) { |
| 5281 | CHECK(!verifier.failure_messages_.empty()); |
| 5282 | *hard_failure_msg = |
| 5283 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 5284 | } |
| 5285 | result.kind = FailureKind::kHardFailure; |
| 5286 | |
| 5287 | if (callbacks != nullptr) { |
| 5288 | // Let the interested party know that we failed the class. |
| 5289 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
| 5290 | callbacks->ClassRejected(ref); |
| 5291 | } |
| 5292 | } |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5293 | if (kVerifierDebug || VLOG_IS_ON(verifier)) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5294 | LOG(ERROR) << verifier.info_messages_.str(); |
| 5295 | verifier.Dump(LOG_STREAM(ERROR)); |
| 5296 | } |
| 5297 | // Under verifier-debug, dump the complete log into the error message. |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5298 | if (kVerifierDebug && hard_failure_msg != nullptr) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5299 | hard_failure_msg->append("\n"); |
| 5300 | hard_failure_msg->append(verifier.info_messages_.str()); |
| 5301 | hard_failure_msg->append("\n"); |
| 5302 | std::ostringstream oss; |
| 5303 | verifier.Dump(oss); |
| 5304 | hard_failure_msg->append(oss.str()); |
| 5305 | } |
| 5306 | } |
| 5307 | if (kTimeVerifyMethod) { |
| 5308 | uint64_t duration_ns = NanoTime() - start_ns; |
| 5309 | if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) { |
Andreas Gampe | efdd1b0 | 2019-05-07 12:30:10 -0700 | [diff] [blame] | 5310 | double bytecodes_per_second = |
| 5311 | verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5312 | LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx) |
| 5313 | << " took " << PrettyDuration(duration_ns) |
Andreas Gampe | efdd1b0 | 2019-05-07 12:30:10 -0700 | [diff] [blame] | 5314 | << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "") |
Andreas Gampe | d91f841 | 2019-06-13 10:11:14 -0700 | [diff] [blame] | 5315 | << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)" |
| 5316 | << " (" << verifier.allocator_.ApproximatePeakBytes() |
| 5317 | << "B approximate peak alloc)"; |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5318 | } |
| 5319 | } |
| 5320 | result.types = verifier.encountered_failure_types_; |
| 5321 | return result; |
| 5322 | } |
| 5323 | |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5324 | MethodVerifier* MethodVerifier::CalculateVerificationInfo( |
| 5325 | Thread* self, |
| 5326 | ArtMethod* method, |
| 5327 | Handle<mirror::DexCache> dex_cache, |
| 5328 | Handle<mirror::ClassLoader> class_loader) { |
| 5329 | std::unique_ptr<impl::MethodVerifier<false>> verifier( |
| 5330 | new impl::MethodVerifier<false>(self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5331 | Runtime::Current()->GetClassLinker(), |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5332 | Runtime::Current()->GetArenaPool(), |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5333 | method->GetDexFile(), |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5334 | method->GetCodeItem(), |
| 5335 | method->GetDexMethodIndex(), |
| 5336 | /* can_load_classes= */ false, |
| 5337 | /* allow_thread_suspension= */ false, |
| 5338 | /* allow_soft_failures= */ true, |
| 5339 | Runtime::Current()->IsAotCompiler(), |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5340 | dex_cache, |
| 5341 | class_loader, |
| 5342 | *method->GetDeclaringClass()->GetClassDef(), |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5343 | method, |
| 5344 | method->GetAccessFlags(), |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5345 | /* need_precise_constants= */ true, |
| 5346 | /* verify_to_dump= */ false, |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5347 | /* fill_register_lines= */ true, |
Alex Light | af52cbe | 2019-05-22 15:05:09 -0700 | [diff] [blame] | 5348 | // Just use the verifier at the current skd-version. |
| 5349 | // This might affect what soft-verifier errors are reported. |
| 5350 | // Callers can then filter out relevant errors if needed. |
| 5351 | Runtime::Current()->GetTargetSdkVersion())); |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5352 | verifier->Verify(); |
| 5353 | if (VLOG_IS_ON(verifier)) { |
| 5354 | verifier->DumpFailures(VLOG_STREAM(verifier)); |
| 5355 | VLOG(verifier) << verifier->info_messages_.str(); |
| 5356 | verifier->Dump(VLOG_STREAM(verifier)); |
| 5357 | } |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5358 | if (verifier->flags_.have_pending_hard_failure_) { |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5359 | return nullptr; |
| 5360 | } else { |
| 5361 | return verifier.release(); |
| 5362 | } |
| 5363 | } |
| 5364 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5365 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 5366 | VariableIndentationOutputStream* vios, |
| 5367 | uint32_t dex_method_idx, |
| 5368 | const DexFile* dex_file, |
| 5369 | Handle<mirror::DexCache> dex_cache, |
| 5370 | Handle<mirror::ClassLoader> class_loader, |
| 5371 | const dex::ClassDef& class_def, |
| 5372 | const dex::CodeItem* code_item, |
| 5373 | ArtMethod* method, |
| 5374 | uint32_t method_access_flags, |
| 5375 | uint32_t api_level) { |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5376 | impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>( |
| 5377 | self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5378 | Runtime::Current()->GetClassLinker(), |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5379 | Runtime::Current()->GetArenaPool(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5380 | dex_file, |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5381 | code_item, |
| 5382 | dex_method_idx, |
| 5383 | /* can_load_classes= */ true, |
| 5384 | /* allow_thread_suspension= */ true, |
| 5385 | /* allow_soft_failures= */ true, |
| 5386 | Runtime::Current()->IsAotCompiler(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5387 | dex_cache, |
| 5388 | class_loader, |
| 5389 | class_def, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5390 | method, |
| 5391 | method_access_flags, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5392 | /* need_precise_constants= */ true, |
| 5393 | /* verify_to_dump= */ true, |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5394 | /* fill_register_lines= */ false, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5395 | api_level); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5396 | verifier->Verify(); |
| 5397 | verifier->DumpFailures(vios->Stream()); |
| 5398 | vios->Stream() << verifier->info_messages_.str(); |
| 5399 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 5400 | // and querying any info is dangerous/can abort. |
Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5401 | if (verifier->flags_.have_pending_hard_failure_) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5402 | delete verifier; |
| 5403 | return nullptr; |
| 5404 | } else { |
| 5405 | verifier->Dump(vios); |
| 5406 | return verifier; |
| 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | void MethodVerifier::FindLocksAtDexPc( |
| 5411 | ArtMethod* m, |
| 5412 | uint32_t dex_pc, |
| 5413 | std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs, |
| 5414 | uint32_t api_level) { |
| 5415 | StackHandleScope<2> hs(Thread::Current()); |
| 5416 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 5417 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5418 | impl::MethodVerifier<false> verifier(hs.Self(), |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5419 | Runtime::Current()->GetClassLinker(), |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5420 | Runtime::Current()->GetArenaPool(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5421 | m->GetDexFile(), |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5422 | m->GetCodeItem(), |
| 5423 | m->GetDexMethodIndex(), |
| 5424 | /* can_load_classes= */ false, |
| 5425 | /* allow_thread_suspension= */ false, |
| 5426 | /* allow_soft_failures= */ true, |
| 5427 | Runtime::Current()->IsAotCompiler(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5428 | dex_cache, |
| 5429 | class_loader, |
| 5430 | m->GetClassDef(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5431 | m, |
| 5432 | m->GetAccessFlags(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5433 | /* need_precise_constants= */ false, |
| 5434 | /* verify_to_dump= */ false, |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5435 | /* fill_register_lines= */ false, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5436 | api_level); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5437 | verifier.interesting_dex_pc_ = dex_pc; |
| 5438 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
| 5439 | verifier.FindLocksAtDexPc(); |
| 5440 | } |
| 5441 | |
| 5442 | MethodVerifier* MethodVerifier::CreateVerifier(Thread* self, |
| 5443 | const DexFile* dex_file, |
| 5444 | Handle<mirror::DexCache> dex_cache, |
| 5445 | Handle<mirror::ClassLoader> class_loader, |
| 5446 | const dex::ClassDef& class_def, |
| 5447 | const dex::CodeItem* code_item, |
| 5448 | uint32_t method_idx, |
| 5449 | ArtMethod* method, |
| 5450 | uint32_t access_flags, |
| 5451 | bool can_load_classes, |
| 5452 | bool allow_soft_failures, |
| 5453 | bool need_precise_constants, |
| 5454 | bool verify_to_dump, |
| 5455 | bool allow_thread_suspension, |
| 5456 | uint32_t api_level) { |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5457 | return new impl::MethodVerifier<false>(self, |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5458 | Runtime::Current()->GetClassLinker(), |
Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5459 | Runtime::Current()->GetArenaPool(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5460 | dex_file, |
Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5461 | code_item, |
| 5462 | method_idx, |
| 5463 | can_load_classes, |
| 5464 | allow_thread_suspension, |
| 5465 | allow_soft_failures, |
| 5466 | Runtime::Current()->IsAotCompiler(), |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5467 | dex_cache, |
| 5468 | class_loader, |
| 5469 | class_def, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5470 | method, |
| 5471 | access_flags, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5472 | need_precise_constants, |
| 5473 | verify_to_dump, |
Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5474 | /* fill_register_lines= */ false, |
Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5475 | api_level); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5476 | } |
| 5477 | |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5478 | void MethodVerifier::Init(ClassLinker* class_linker) { |
| 5479 | art::verifier::RegTypeCache::Init(class_linker); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5480 | } |
| 5481 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5482 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5483 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5484 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5485 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5486 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5487 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5488 | } |
| 5489 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5490 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5491 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5494 | std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) { |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5495 | // Mark the error type as encountered. |
| 5496 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
| 5497 | |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5498 | if (pending_exc) { |
| 5499 | switch (error) { |
| 5500 | case VERIFY_ERROR_NO_CLASS: |
| 5501 | case VERIFY_ERROR_NO_FIELD: |
| 5502 | case VERIFY_ERROR_NO_METHOD: |
| 5503 | case VERIFY_ERROR_ACCESS_CLASS: |
| 5504 | case VERIFY_ERROR_ACCESS_FIELD: |
| 5505 | case VERIFY_ERROR_ACCESS_METHOD: |
| 5506 | case VERIFY_ERROR_INSTANTIATION: |
| 5507 | case VERIFY_ERROR_CLASS_CHANGE: |
| 5508 | case VERIFY_ERROR_FORCE_INTERPRETER: |
| 5509 | case VERIFY_ERROR_LOCKING: |
Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5510 | if (IsAotMode() || !can_load_classes_) { |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5511 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 5512 | // class change and instantiation errors into soft verification errors so that we |
| 5513 | // re-verify at runtime. We may fail to find or to agree on access because of not yet |
| 5514 | // available class loaders, or class loaders that will differ at runtime. In these cases, |
| 5515 | // we don't want to affect the soundness of the code being compiled. Instead, the |
| 5516 | // generated code runs "slow paths" that dynamically perform the verification and cause |
| 5517 | // the behavior to be that akin to an interpreter. |
| 5518 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 5519 | } else { |
| 5520 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 5521 | // method to be executed using the interpreter with checks. |
| 5522 | flags_.have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | 16a08f6 | 2019-07-18 10:59:08 -0700 | [diff] [blame] | 5523 | } |
| 5524 | // How to handle runtime failures for instructions that are not flagged kThrow. |
| 5525 | // |
| 5526 | // The verifier may fail before we touch any instruction, for the signature of a method. So |
| 5527 | // add a check. |
| 5528 | if (work_insn_idx_ < dex::kDexNoIndex) { |
| 5529 | const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_); |
| 5530 | Instruction::Code opcode = inst.Opcode(); |
| 5531 | if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 && |
| 5532 | !impl::IsCompatThrow(opcode) && |
| 5533 | GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | d84794d | 2019-07-18 13:40:03 -0700 | [diff] [blame] | 5534 | if (Runtime::Current()->IsVerifierMissingKThrowFatal()) { |
| 5535 | LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode; |
| 5536 | UNREACHABLE(); |
| 5537 | } |
Andreas Gampe | 16a08f6 | 2019-07-18 10:59:08 -0700 | [diff] [blame] | 5538 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise |
| 5539 | // we'll try to merge garbage. |
| 5540 | // Note: this assumes that Fail is called before we do any work_line modifications. |
| 5541 | saved_line_->CopyFromLine(work_line_.get()); |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5542 | } |
| 5543 | } |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5544 | break; |
Andreas Gampe | 7a82acc | 2019-07-01 13:54:21 -0700 | [diff] [blame] | 5545 | |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5546 | // Indication that verification should be retried at runtime. |
| 5547 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
| 5548 | if (!allow_soft_failures_) { |
| 5549 | flags_.have_pending_hard_failure_ = true; |
| 5550 | } |
| 5551 | break; |
| 5552 | |
| 5553 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 5554 | // marked as rejected to prevent it from being compiled. |
| 5555 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5556 | flags_.have_pending_hard_failure_ = true; |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5557 | break; |
Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5558 | } |
Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5559 | |
Andreas Gampe | 0d87f99 | 2019-07-10 13:19:45 -0700 | [diff] [blame] | 5560 | case VERIFY_ERROR_SKIP_COMPILER: |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5561 | // Nothing to do, just remember the failure type. |
| 5562 | break; |
Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5563 | } |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5564 | } else if (kIsDebugBuild) { |
| 5565 | CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT); |
| 5566 | CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD); |
Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5567 | } |
Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5568 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5569 | failures_.push_back(error); |
| 5570 | std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(), |
| 5571 | work_insn_idx_)); |
| 5572 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
| 5573 | failure_messages_.push_back(failure_message); |
| 5574 | return *failure_message; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5575 | } |
| 5576 | |
Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5577 | ScopedNewLine MethodVerifier::LogVerifyInfo() { |
| 5578 | ScopedNewLine ret{info_messages_}; |
| 5579 | ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_) |
| 5580 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 5581 | return ret; |
| 5582 | } |
| 5583 | |
| 5584 | static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) { |
| 5585 | static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure |
| 5586 | && FailureKind::kSoftFailure < FailureKind::kHardFailure, |
| 5587 | "Unexpected FailureKind order"); |
| 5588 | return std::max(fk1, fk2); |
| 5589 | } |
| 5590 | |
| 5591 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 5592 | kind = FailureKindMax(kind, fd.kind); |
| 5593 | types |= fd.types; |
Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 5594 | } |
| 5595 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5596 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5597 | } // namespace art |