| 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 | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 37 | #include "class_root-inl.h" |
| Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 38 | #include "dex/class_accessor-inl.h" |
| David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 39 | #include "dex/descriptors_names.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 40 | #include "dex/dex_file-inl.h" |
| 41 | #include "dex/dex_file_exception_helpers.h" |
| 42 | #include "dex/dex_instruction-inl.h" |
| 43 | #include "dex/dex_instruction_utils.h" |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 44 | #include "experimental_flags.h" |
| Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 45 | #include "gc/accounting/card_table-inl.h" |
| Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 46 | #include "handle_scope-inl.h" |
| Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 47 | #include "intern_table.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "mirror/class-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 49 | #include "mirror/class.h" |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 50 | #include "mirror/class_loader.h" |
| Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 51 | #include "mirror/dex_cache-inl.h" |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 52 | #include "mirror/method_handle_impl.h" |
| Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 53 | #include "mirror/method_type.h" |
| Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | #include "mirror/object-inl.h" |
| 55 | #include "mirror/object_array-inl.h" |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 56 | #include "mirror/var_handle.h" |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 57 | #include "obj_ptr-inl.h" |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 58 | #include "reg_type-inl.h" |
| Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 59 | #include "register_line-inl.h" |
| Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 60 | #include "runtime.h" |
| Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 61 | #include "scoped_newline.h" |
| Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 62 | #include "scoped_thread_state_change-inl.h" |
| Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 63 | #include "stack.h" |
| Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 64 | #include "vdex_file.h" |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 65 | #include "verifier/method_verifier.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 66 | #include "verifier_deps.h" |
| Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 67 | |
| 68 | namespace art { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 69 | namespace verifier { |
| Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 70 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 71 | using android::base::StringPrintf; |
| 72 | |
| Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 73 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
| Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 74 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 75 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator) |
| 76 | : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {} |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 77 | |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 78 | void PcToRegisterLineTable::Init(InstructionFlags* flags, |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 79 | uint32_t insns_size, |
| 80 | uint16_t registers_size, |
| 81 | ScopedArenaAllocator& allocator, |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 82 | RegTypeCache* reg_types, |
| 83 | uint32_t interesting_dex_pc) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 84 | DCHECK_GT(insns_size, 0U); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 85 | register_lines_.resize(insns_size); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 86 | for (uint32_t i = 0; i < insns_size; i++) { |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 87 | if ((i == interesting_dex_pc) || flags[i].IsBranchTarget()) { |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 88 | register_lines_[i].reset(RegisterLine::Create(registers_size, allocator, reg_types)); |
| Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 93 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 94 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 95 | namespace impl { |
| 96 | namespace { |
| 97 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 98 | enum class CheckAccess { |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 99 | kNo, |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 100 | kOnResolvedClass, |
| 101 | kYes, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | enum class FieldAccessType { |
| 105 | kAccGet, |
| 106 | kAccPut |
| 107 | }; |
| 108 | |
| Andreas Gampe | c658278 | 2019-07-10 15:06:18 -0700 | [diff] [blame] | 109 | // Instruction types that are not marked as throwing (because they normally would not), but for |
| 110 | // historical reasons may do so. These instructions cannot be marked kThrow as that would introduce |
| 111 | // a general flow that is unwanted. |
| 112 | // |
| 113 | // Note: Not implemented as Instruction::Flags value as that set is full and we'd need to increase |
| 114 | // the struct size (making it a non-power-of-two) for a single element. |
| 115 | // |
| 116 | // Note: This should eventually be removed. |
| 117 | constexpr bool IsCompatThrow(Instruction::Code opcode) { |
| Mathieu Chartier | e8ed866 | 2020-03-13 13:55:44 -0700 | [diff] [blame] | 118 | return opcode == Instruction::Code::RETURN_OBJECT || opcode == Instruction::Code::MOVE_EXCEPTION; |
| Andreas Gampe | c658278 | 2019-07-10 15:06:18 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 121 | template <bool kVerifierDebug> |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 122 | class MethodVerifier final : public ::art::verifier::MethodVerifier { |
| 123 | public: |
| 124 | bool IsInstanceConstructor() const { |
| 125 | return IsConstructor() && !IsStatic(); |
| 126 | } |
| 127 | |
| 128 | const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 129 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 130 | DCHECK(!HasFailures()); |
| 131 | const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx); |
| 132 | DCHECK(!HasFailures()); |
| 133 | return result; |
| 134 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 135 | |
| 136 | void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_); |
| 137 | |
| 138 | private: |
| 139 | MethodVerifier(Thread* self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 140 | ClassLinker* class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 141 | ArenaPool* arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 142 | VerifierDeps* verifier_deps, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 143 | const DexFile* dex_file, |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 144 | const dex::CodeItem* code_item, |
| 145 | uint32_t method_idx, |
| 146 | bool can_load_classes, |
| 147 | bool allow_thread_suspension, |
| 148 | bool allow_soft_failures, |
| 149 | bool aot_mode, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 150 | Handle<mirror::DexCache> dex_cache, |
| 151 | Handle<mirror::ClassLoader> class_loader, |
| 152 | const dex::ClassDef& class_def, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 153 | uint32_t access_flags, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 154 | bool verify_to_dump, |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 155 | uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_) |
| 156 | : art::verifier::MethodVerifier(self, |
| 157 | class_linker, |
| 158 | arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 159 | verifier_deps, |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 160 | dex_file, |
| Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 161 | class_def, |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 162 | code_item, |
| 163 | method_idx, |
| 164 | can_load_classes, |
| 165 | allow_thread_suspension, |
| 166 | allow_soft_failures, |
| 167 | aot_mode), |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 168 | method_access_flags_(access_flags), |
| 169 | return_type_(nullptr), |
| 170 | dex_cache_(dex_cache), |
| 171 | class_loader_(class_loader), |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 172 | declaring_class_(nullptr), |
| 173 | interesting_dex_pc_(-1), |
| 174 | monitor_enter_dex_pcs_(nullptr), |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 175 | verify_to_dump_(verify_to_dump), |
| 176 | allow_thread_suspension_(allow_thread_suspension), |
| 177 | is_constructor_(false), |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 178 | api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) { |
| 179 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 180 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 181 | void UninstantiableError(const char* descriptor) { |
| 182 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 183 | << "non-instantiable klass " << descriptor; |
| 184 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 185 | static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 186 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 187 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 188 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 189 | |
| 190 | // Is the method being verified a constructor? See the comment on the field. |
| 191 | bool IsConstructor() const { |
| 192 | return is_constructor_; |
| 193 | } |
| 194 | |
| 195 | // Is the method verified static? |
| 196 | bool IsStatic() const { |
| 197 | return (method_access_flags_ & kAccStatic) != 0; |
| 198 | } |
| 199 | |
| 200 | // Adds the given string to the beginning of the last failure message. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 201 | void PrependToLastFailMessage(std::string prepend) { |
| 202 | size_t failure_num = failure_messages_.size(); |
| 203 | DCHECK_NE(failure_num, 0U); |
| 204 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 205 | prepend += last_fail_message->str(); |
| 206 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
| 207 | delete last_fail_message; |
| 208 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 209 | |
| 210 | // Adds the given string to the end of the last failure message. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 211 | void AppendToLastFailMessage(const std::string& append) { |
| 212 | size_t failure_num = failure_messages_.size(); |
| 213 | DCHECK_NE(failure_num, 0U); |
| 214 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 215 | (*last_fail_message) << append; |
| 216 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 217 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 218 | /* |
| 219 | * Compute the width of the instruction at each address in the instruction stream, and store it in |
| 220 | * insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch |
| 221 | * table data, are not touched (so the caller should probably initialize "insn_flags" to zero). |
| 222 | * |
| 223 | * The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set. |
| 224 | * |
| 225 | * Performs some static checks, notably: |
| 226 | * - opcode of first instruction begins at index 0 |
| 227 | * - only documented instructions may appear |
| 228 | * - each instruction follows the last |
| 229 | * - last byte of last instruction is at (code_length-1) |
| 230 | * |
| 231 | * Logs an error and returns "false" on failure. |
| 232 | */ |
| 233 | bool ComputeWidthsAndCountOps(); |
| 234 | |
| 235 | /* |
| 236 | * Set the "in try" flags for all instructions protected by "try" statements. Also sets the |
| 237 | * "branch target" flags for exception handlers. |
| 238 | * |
| 239 | * Call this after widths have been set in "insn_flags". |
| 240 | * |
| 241 | * Returns "false" if something in the exception table looks fishy, but we're expecting the |
| Orion Hodson | aacf977 | 2020-07-22 21:51:00 +0100 | [diff] [blame] | 242 | * exception table to be valid. |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 243 | */ |
| 244 | bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_); |
| 245 | |
| 246 | /* |
| 247 | * Perform static verification on all instructions in a method. |
| 248 | * |
| 249 | * Walks through instructions in a method calling VerifyInstruction on each. |
| 250 | */ |
| 251 | template <bool kAllowRuntimeOnlyInstructions> |
| 252 | bool VerifyInstructions(); |
| 253 | |
| 254 | /* |
| 255 | * Perform static verification on an instruction. |
| 256 | * |
| 257 | * As a side effect, this sets the "branch target" flags in InsnFlags. |
| 258 | * |
| 259 | * "(CF)" items are handled during code-flow analysis. |
| 260 | * |
| 261 | * v3 4.10.1 |
| 262 | * - target of each jump and branch instruction must be valid |
| 263 | * - targets of switch statements must be valid |
| 264 | * - operands referencing constant pool entries must be valid |
| 265 | * - (CF) operands of getfield, putfield, getstatic, putstatic must be valid |
| 266 | * - (CF) operands of method invocation instructions must be valid |
| 267 | * - (CF) only invoke-direct can call a method starting with '<' |
| 268 | * - (CF) <clinit> must never be called explicitly |
| 269 | * - operands of instanceof, checkcast, new (and variants) must be valid |
| 270 | * - new-array[-type] limited to 255 dimensions |
| 271 | * - can't use "new" on an array class |
| 272 | * - (?) limit dimensions in multi-array creation |
| 273 | * - local variable load/store register values must be in valid range |
| 274 | * |
| 275 | * v3 4.11.1.2 |
| 276 | * - branches must be within the bounds of the code array |
| 277 | * - targets of all control-flow instructions are the start of an instruction |
| 278 | * - register accesses fall within range of allocated registers |
| 279 | * - (N/A) access to constant pool must be of appropriate type |
| 280 | * - code does not end in the middle of an instruction |
| 281 | * - execution cannot fall off the end of the code |
| 282 | * - (earlier) for each exception handler, the "try" area must begin and |
| 283 | * end at the start of an instruction (end can be at the end of the code) |
| 284 | * - (earlier) for each exception handler, the handler must start at a valid |
| 285 | * instruction |
| 286 | */ |
| 287 | template <bool kAllowRuntimeOnlyInstructions> |
| 288 | bool VerifyInstruction(const Instruction* inst, uint32_t code_offset); |
| 289 | |
| 290 | /* Ensure that the register index is valid for this code item. */ |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 291 | bool CheckRegisterIndex(uint32_t idx) { |
| 292 | if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) { |
| 293 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 294 | << code_item_accessor_.RegistersSize() << ")"; |
| 295 | return false; |
| 296 | } |
| 297 | return true; |
| 298 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 299 | |
| 300 | /* Ensure that the wide register index is valid for this code item. */ |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 301 | bool CheckWideRegisterIndex(uint32_t idx) { |
| 302 | if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) { |
| 303 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 304 | << "+1 >= " << code_item_accessor_.RegistersSize() << ")"; |
| 305 | return false; |
| 306 | } |
| 307 | return true; |
| 308 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 309 | |
| 310 | // Perform static checks on an instruction referencing a CallSite. All we do here is ensure that |
| 311 | // the call site index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 312 | bool CheckCallSiteIndex(uint32_t idx) { |
| 313 | uint32_t limit = dex_file_->NumCallSiteIds(); |
| 314 | if (UNLIKELY(idx >= limit)) { |
| 315 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max " |
| 316 | << limit << ")"; |
| 317 | return false; |
| 318 | } |
| 319 | return true; |
| 320 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 321 | |
| 322 | // Perform static checks on a field Get or set instruction. All we do here is ensure that the |
| 323 | // field index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 324 | bool CheckFieldIndex(uint32_t idx) { |
| 325 | if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) { |
| 326 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 327 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
| 328 | return false; |
| 329 | } |
| 330 | return true; |
| 331 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 332 | |
| 333 | // Perform static checks on a method invocation instruction. All we do here is ensure that the |
| 334 | // method index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 335 | bool CheckMethodIndex(uint32_t idx) { |
| 336 | if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) { |
| 337 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 338 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
| 339 | return false; |
| 340 | } |
| 341 | return true; |
| 342 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 343 | |
| 344 | // Perform static checks on an instruction referencing a constant method handle. All we do here |
| 345 | // is ensure that the method index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 346 | bool CheckMethodHandleIndex(uint32_t idx) { |
| 347 | uint32_t limit = dex_file_->NumMethodHandles(); |
| 348 | if (UNLIKELY(idx >= limit)) { |
| 349 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max " |
| 350 | << limit << ")"; |
| 351 | return false; |
| 352 | } |
| 353 | return true; |
| 354 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 355 | |
| 356 | // Perform static checks on a "new-instance" instruction. Specifically, make sure the class |
| 357 | // reference isn't for an array class. |
| 358 | bool CheckNewInstance(dex::TypeIndex idx); |
| 359 | |
| 360 | // Perform static checks on a prototype indexing instruction. All we do here is ensure that the |
| 361 | // prototype index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 362 | bool CheckPrototypeIndex(uint32_t idx) { |
| 363 | if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) { |
| 364 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max " |
| 365 | << dex_file_->GetHeader().proto_ids_size_ << ")"; |
| 366 | return false; |
| 367 | } |
| 368 | return true; |
| 369 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 370 | |
| 371 | /* Ensure that the string index is in the valid range. */ |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 372 | bool CheckStringIndex(uint32_t idx) { |
| 373 | if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) { |
| 374 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 375 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
| 376 | return false; |
| 377 | } |
| 378 | return true; |
| 379 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 380 | |
| 381 | // Perform static checks on an instruction that takes a class constant. Ensure that the class |
| 382 | // index is in the valid range. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 383 | bool CheckTypeIndex(dex::TypeIndex idx) { |
| 384 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
| 385 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
| 386 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| 387 | return false; |
| 388 | } |
| 389 | return true; |
| 390 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 391 | |
| 392 | // Perform static checks on a "new-array" instruction. Specifically, make sure they aren't |
| 393 | // creating an array of arrays that causes the number of dimensions to exceed 255. |
| 394 | bool CheckNewArray(dex::TypeIndex idx); |
| 395 | |
| 396 | // Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction. |
| 397 | bool CheckArrayData(uint32_t cur_offset); |
| 398 | |
| 399 | // Verify that the target of a branch instruction is valid. We don't expect code to jump directly |
| 400 | // into an exception handler, but it's valid to do so as long as the target isn't a |
| 401 | // "move-exception" instruction. We verify that in a later stage. |
| 402 | // The dex format forbids certain instructions from branching to themselves. |
| 403 | // Updates "insn_flags_", setting the "branch target" flag. |
| 404 | bool CheckBranchTarget(uint32_t cur_offset); |
| 405 | |
| 406 | // Verify a switch table. "cur_offset" is the offset of the switch instruction. |
| 407 | // Updates "insn_flags_", setting the "branch target" flag. |
| 408 | bool CheckSwitchTargets(uint32_t cur_offset); |
| 409 | |
| 410 | // Check the register indices used in a "vararg" instruction, such as invoke-virtual or |
| 411 | // filled-new-array. |
| 412 | // - vA holds word count (0-5), args[] have values. |
| 413 | // There are some tests we don't do here, e.g. we don't try to verify that invoking a method that |
| 414 | // takes a double is done with consecutive registers. This requires parsing the target method |
| 415 | // 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] | 416 | bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
| 417 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
| 418 | for (uint32_t idx = 0; idx < vA; idx++) { |
| 419 | if (UNLIKELY(arg[idx] >= registers_size)) { |
| 420 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 421 | << ") in non-range invoke (>= " << registers_size << ")"; |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 428 | |
| 429 | // Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range |
| 430 | // or filled-new-array/range. |
| 431 | // - vA holds word count, vC holds index of first reg. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 432 | bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
| 433 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
| 434 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 435 | // integer overflow when adding them here. |
| 436 | if (UNLIKELY(vA + vC > registers_size)) { |
| 437 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 438 | << " in range invoke (> " << registers_size << ")"; |
| 439 | return false; |
| 440 | } |
| 441 | return true; |
| 442 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 443 | |
| 444 | // Checks the method matches the expectations required to be signature polymorphic. |
| 445 | bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
| 446 | |
| 447 | // Checks the invoked receiver matches the expectations for signature polymorphic methods. |
| 448 | bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_); |
| 449 | |
| 450 | // Extract the relative offset from a branch instruction. |
| 451 | // Returns "false" on failure (e.g. this isn't a branch instruction). |
| 452 | bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
| 453 | bool* selfOkay); |
| 454 | |
| 455 | /* Perform detailed code-flow analysis on a single method. */ |
| 456 | bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_); |
| 457 | |
| 458 | // Set the register types for the first instruction in the method based on the method signature. |
| 459 | // This has the side-effect of validating the signature. |
| 460 | bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_); |
| 461 | |
| 462 | /* |
| 463 | * Perform code flow on a method. |
| 464 | * |
| 465 | * The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first |
| 466 | * instruction, process it (setting additional "changed" bits), and repeat until there are no |
| 467 | * more. |
| 468 | * |
| 469 | * v3 4.11.1.1 |
| 470 | * - (N/A) operand stack is always the same size |
| 471 | * - operand stack [registers] contain the correct types of values |
| 472 | * - local variables [registers] contain the correct types of values |
| 473 | * - methods are invoked with the appropriate arguments |
| 474 | * - fields are assigned using values of appropriate types |
| 475 | * - opcodes have the correct type values in operand registers |
| 476 | * - there is never an uninitialized class instance in a local variable in code protected by an |
| 477 | * exception handler (operand stack is okay, because the operand stack is discarded when an |
| 478 | * exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of |
| 479 | * register typing] |
| 480 | * |
| 481 | * v3 4.11.1.2 |
| 482 | * - execution cannot fall off the end of the code |
| 483 | * |
| 484 | * (We also do many of the items described in the "static checks" sections, because it's easier to |
| 485 | * do them here.) |
| 486 | * |
| 487 | * We need an array of RegType values, one per register, for every instruction. If the method uses |
| 488 | * monitor-enter, we need extra data for every register, and a stack for every "interesting" |
| 489 | * instruction. In theory this could become quite large -- up to several megabytes for a monster |
| 490 | * function. |
| 491 | * |
| 492 | * NOTE: |
| 493 | * The spec forbids backward branches when there's an uninitialized reference in a register. The |
| 494 | * idea is to prevent something like this: |
| 495 | * loop: |
| 496 | * move r1, r0 |
| 497 | * new-instance r0, MyClass |
| 498 | * ... |
| 499 | * if-eq rN, loop // once |
| 500 | * initialize r0 |
| 501 | * |
| 502 | * This leaves us with two different instances, both allocated by the same instruction, but only |
| 503 | * one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around |
| 504 | * it by preventing backward branches. We achieve identical results without restricting code |
| 505 | * reordering by specifying that you can't execute the new-instance instruction if a register |
| 506 | * contains an uninitialized instance created by that same instruction. |
| 507 | */ |
| Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 508 | template <bool kMonitorDexPCs> |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 509 | bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_); |
| 510 | |
| 511 | /* |
| 512 | * Perform verification for a single instruction. |
| 513 | * |
| 514 | * This requires fully decoding the instruction to determine the effect it has on registers. |
| 515 | * |
| 516 | * Finds zero or more following instructions and sets the "changed" flag if execution at that |
| 517 | * point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target |
| 518 | * addresses. Does not set or clear any other flags in "insn_flags_". |
| 519 | */ |
| 520 | bool CodeFlowVerifyInstruction(uint32_t* start_guess) |
| 521 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 522 | |
| 523 | // Perform verification of a new array instruction |
| 524 | void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) |
| 525 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 526 | |
| 527 | // Helper to perform verification on puts of primitive type. |
| 528 | void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
| 529 | const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_); |
| 530 | |
| 531 | // Perform verification of an aget instruction. The destination register's type will be set to |
| 532 | // be that of component type of the array unless the array type is unknown, in which case a |
| 533 | // bottom type inferred from the type of instruction is used. is_primitive is false for an |
| 534 | // aget-object. |
| 535 | void VerifyAGet(const Instruction* inst, const RegType& insn_type, |
| 536 | bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_); |
| 537 | |
| 538 | // Perform verification of an aput instruction. |
| 539 | void VerifyAPut(const Instruction* inst, const RegType& insn_type, |
| 540 | bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_); |
| 541 | |
| 542 | // Lookup instance field and fail for resolution violations |
| 543 | ArtField* GetInstanceField(const RegType& obj_type, int field_idx) |
| 544 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 545 | |
| 546 | // Lookup static field and fail for resolution violations |
| 547 | ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_); |
| 548 | |
| 549 | // Perform verification of an iget/sget/iput/sput instruction. |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 550 | template <FieldAccessType kAccType> |
| 551 | void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 552 | bool is_primitive, bool is_static) |
| 553 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 554 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 555 | // Resolves a class based on an index and, if C is kYes, performs access checks to ensure |
| 556 | // the referrer can access the resolved class. |
| 557 | template <CheckAccess C> |
| 558 | const RegType& ResolveClass(dex::TypeIndex class_idx) |
| 559 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 560 | |
| 561 | /* |
| 562 | * For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler |
| 563 | * address, determine the Join of all exceptions that can land here. Fails if no matching |
| 564 | * exception handler can be found or if the Join of exception types fails. |
| 565 | */ |
| 566 | const RegType& GetCaughtExceptionType() |
| 567 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 568 | |
| 569 | /* |
| 570 | * Resolves a method based on an index and performs access checks to ensure |
| 571 | * the referrer can access the resolved method. |
| 572 | * Does not throw exceptions. |
| 573 | */ |
| 574 | ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type) |
| 575 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 576 | |
| 577 | /* |
| 578 | * Verify the arguments to a method. We're executing in "method", making |
| 579 | * a call to the method reference in vB. |
| 580 | * |
| 581 | * If this is a "direct" invoke, we allow calls to <init>. For calls to |
| 582 | * <init>, the first argument may be an uninitialized reference. Otherwise, |
| 583 | * calls to anything starting with '<' will be rejected, as will any |
| 584 | * uninitialized reference arguments. |
| 585 | * |
| 586 | * For non-static method calls, this will verify that the method call is |
| 587 | * appropriate for the "this" argument. |
| 588 | * |
| 589 | * The method reference is in vBBBB. The "is_range" parameter determines |
| 590 | * whether we use 0-4 "args" values or a range of registers defined by |
| 591 | * vAA and vCCCC. |
| 592 | * |
| 593 | * Widening conversions on integers and references are allowed, but |
| 594 | * narrowing conversions are not. |
| 595 | * |
| 596 | * Returns the resolved method on success, null on failure (with *failure |
| 597 | * set appropriately). |
| 598 | */ |
| 599 | ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range) |
| 600 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 601 | |
| 602 | // Similar checks to the above, but on the proto. Will be used when the method cannot be |
| 603 | // resolved. |
| 604 | void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type, |
| 605 | bool is_range) |
| 606 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 607 | |
| 608 | template <class T> |
| 609 | ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst, |
| 610 | MethodType method_type, bool is_range, |
| 611 | ArtMethod* res_method) |
| 612 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 613 | |
| 614 | /* |
| 615 | * Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise. |
| 616 | */ |
| 617 | bool CheckCallSite(uint32_t call_site_idx); |
| 618 | |
| 619 | /* |
| 620 | * Verify that the target instruction is not "move-exception". It's important that the only way |
| 621 | * to execute a move-exception is as the first instruction of an exception handler. |
| 622 | * Returns "true" if all is well, "false" if the target instruction is move-exception. |
| 623 | */ |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 624 | bool CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
| 625 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
| 626 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
| 627 | return false; |
| 628 | } |
| 629 | return true; |
| 630 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 631 | |
| 632 | /* |
| 633 | * Verify that the target instruction is not "move-result". It is important that we cannot |
| 634 | * branch to move-result instructions, but we have to make this a distinct check instead of |
| 635 | * adding it to CheckNotMoveException, because it is legal to continue into "move-result" |
| 636 | * instructions - as long as the previous instruction was an invoke, which is checked elsewhere. |
| 637 | */ |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 638 | bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 639 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 640 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 641 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 642 | return false; |
| 643 | } |
| 644 | return true; |
| 645 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 646 | |
| 647 | /* |
| 648 | * Verify that the target instruction is not "move-result" or "move-exception". This is to |
| 649 | * be used when checking branch and switch instructions, but not instructions that can |
| 650 | * continue. |
| 651 | */ |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 652 | bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 653 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 654 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Control can transfer to "next_insn". Merge the registers from merge_line into the table at |
| 658 | * next_insn, and set the changed flag on the target address if any of the registers were changed. |
| 659 | * In the case of fall-through, update the merge line on a change as its the working line for the |
| 660 | * next instruction. |
| 661 | * Returns "false" if an error is encountered. |
| 662 | */ |
| 663 | bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line) |
| 664 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 665 | |
| 666 | // Return the register type for the method. |
| 667 | const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_); |
| 668 | |
| 669 | // Get a type representing the declaring class of the method. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 670 | const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 671 | if (declaring_class_ == nullptr) { |
| 672 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 673 | const char* descriptor |
| 674 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 675 | declaring_class_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 676 | } |
| 677 | return *declaring_class_; |
| 678 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 679 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 680 | InstructionFlags* CurrentInsnFlags() { |
| 681 | return &GetModifiableInstructionFlags(work_insn_idx_); |
| 682 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 683 | |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 684 | const RegType& DetermineCat1Constant(int32_t value) |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 685 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 686 | |
| 687 | // Try to create a register type from the given class. In case a precise type is requested, but |
| 688 | // the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a |
| 689 | // non-precise reference will be returned. |
| 690 | // Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is |
| 691 | // actually touched. |
| 692 | const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise) |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 693 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 694 | DCHECK(klass != nullptr); |
| 695 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 696 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 697 | << "non-instantiable klass " << descriptor; |
| 698 | precise = false; |
| 699 | } |
| 700 | return reg_types_.FromClass(descriptor, klass, precise); |
| 701 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 702 | |
| 703 | ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx); |
| 704 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 705 | ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) { |
| 706 | return insn_flags_[index]; |
| 707 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 708 | |
| 709 | // Returns the method index of an invoke instruction. |
| 710 | uint16_t GetMethodIdxOfInvoke(const Instruction* inst) |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 711 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Nicolas Geoffray | 4924ea9 | 2021-03-23 08:25:31 +0000 | [diff] [blame] | 712 | return inst->VRegB(); |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 713 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 714 | // Returns the field index of a field access instruction. |
| 715 | uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 716 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 717 | if (is_static) { |
| 718 | return inst->VRegB_21c(); |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 719 | } else { |
| 720 | return inst->VRegC_22c(); |
| 721 | } |
| 722 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 723 | |
| 724 | // Run verification on the method. Returns true if verification completes and false if the input |
| 725 | // has an irrecoverable corruption. |
| 726 | bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_); |
| 727 | |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 728 | // For app-compatibility, code after a runtime throw is treated as dead code |
| 729 | // for apps targeting <= S. |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 730 | // Returns whether the current instruction was marked as throwing. |
| 731 | bool PotentiallyMarkRuntimeThrow() override; |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 732 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 733 | // Dump the failures encountered by the verifier. |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 734 | std::ostream& DumpFailures(std::ostream& os) { |
| 735 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
| Andreas Gampe | 4ec4d48 | 2019-08-01 10:00:23 -0700 | [diff] [blame] | 736 | for (const auto* stream : failure_messages_) { |
| 737 | os << stream->str() << "\n"; |
| Andreas Gampe | 86bf068 | 2019-07-29 11:22:44 -0700 | [diff] [blame] | 738 | } |
| 739 | return os; |
| 740 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 741 | |
| 742 | // Dump the state of the verifier, namely each instruction, what flags are set on it, register |
| 743 | // information |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 744 | void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 745 | VariableIndentationOutputStream vios(&os); |
| 746 | Dump(&vios); |
| 747 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 748 | void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_); |
| 749 | |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 750 | bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_); |
| 751 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 752 | const uint32_t method_access_flags_; // Method's access flags. |
| 753 | const RegType* return_type_; // Lazily computed return type of the method. |
| 754 | // The dex_cache for the declaring class of the method. |
| 755 | Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_); |
| 756 | // The class loader for the declaring class of the method. |
| 757 | Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 758 | const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class. |
| 759 | |
| 760 | // The dex PC of a FindLocksAtDexPc request, -1 otherwise. |
| 761 | uint32_t interesting_dex_pc_; |
| 762 | // The container into which FindLocksAtDexPc should write the registers containing held locks, |
| 763 | // null if we're not doing FindLocksAtDexPc. |
| 764 | std::vector<DexLockInfo>* monitor_enter_dex_pcs_; |
| 765 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 766 | // Indicates whether we verify to dump the info. In that case we accept quickened instructions |
| 767 | // even though we might detect to be a compiler. Should only be set when running |
| 768 | // VerifyMethodAndDump. |
| 769 | const bool verify_to_dump_; |
| 770 | |
| 771 | // Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for |
| 772 | // thread dumping checkpoints since we may get thread suspension at an inopportune time due to |
| 773 | // FindLocksAtDexPC, resulting in deadlocks. |
| 774 | const bool allow_thread_suspension_; |
| 775 | |
| 776 | // Whether the method seems to be a constructor. Note that this field exists as we can't trust |
| 777 | // the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>" |
| 778 | // correctly. |
| 779 | // |
| 780 | // Note: this flag is only valid once Verify() has started. |
| 781 | bool is_constructor_; |
| 782 | |
| 783 | // API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks. |
| 784 | // Instead, unset level should correspond to max(). |
| 785 | const uint32_t api_level_; |
| 786 | |
| 787 | friend class ::art::verifier::MethodVerifier; |
| 788 | |
| 789 | DISALLOW_COPY_AND_ASSIGN(MethodVerifier); |
| 790 | }; |
| 791 | |
| Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 792 | // Note: returns true on failure. |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 793 | template <bool kVerifierDebug> |
| 794 | inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition, |
| 795 | const char* error_msg, |
| 796 | uint32_t work_insn_idx) { |
| Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 797 | if (kIsDebugBuild) { |
| Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 798 | // In a debug build, abort if the error condition is wrong. Only warn if |
| 799 | // we are already aborting (as this verification is likely run to print |
| 800 | // lock information). |
| 801 | if (LIKELY(gAborting == 0)) { |
| Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 802 | DCHECK(condition) << error_msg << work_insn_idx << " " |
| 803 | << dex_file_->PrettyMethod(dex_method_idx_); |
| Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 804 | } else { |
| 805 | if (!condition) { |
| 806 | LOG(ERROR) << error_msg << work_insn_idx; |
| Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 807 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 808 | return true; |
| 809 | } |
| 810 | } |
| Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 811 | } else { |
| 812 | // In a non-debug build, just fail the class. |
| 813 | if (!condition) { |
| Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 814 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 815 | return true; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return false; |
| 820 | } |
| 821 | |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 822 | static bool IsLargeMethod(const CodeItemDataAccessor& accessor) { |
| 823 | if (!accessor.HasCodeItem()) { |
| Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 827 | uint16_t registers_size = accessor.RegistersSize(); |
| 828 | uint32_t insns_size = accessor.InsnsSizeInCodeUnits(); |
| Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 829 | |
| 830 | return registers_size * insns_size > 4*1024*1024; |
| 831 | } |
| 832 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 833 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 834 | void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 835 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 836 | 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] | 837 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 838 | // Quick check whether there are any monitor_enter instructions before verifying. |
| 839 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| 840 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 841 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 842 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 843 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 844 | // got what we wanted. |
| 845 | Verify(); |
| 846 | return; |
| 847 | } |
| Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 848 | } |
| Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 851 | template <bool kVerifierDebug> |
| 852 | bool MethodVerifier<kVerifierDebug>::Verify() { |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 853 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 854 | // the name. |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 855 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 856 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 857 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 858 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 859 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 860 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 861 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 862 | if (!constructor_by_name) { |
| 863 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 864 | << "method is marked as constructor, but not named accordingly"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 865 | return false; |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 866 | } |
| 867 | is_constructor_ = true; |
| 868 | } else if (constructor_by_name) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 869 | LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_) |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 870 | << " not marked as constructor."; |
| 871 | is_constructor_ = true; |
| 872 | } |
| 873 | // If it's a constructor, check whether IsStatic() matches the name. |
| 874 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 875 | if (kIsDebugBuild) { |
| 876 | if (IsConstructor()) { |
| 877 | if (IsStatic() ^ static_constructor_by_name) { |
| 878 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 879 | << "constructor name doesn't match static flag"; |
| 880 | return false; |
| 881 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 882 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 883 | } |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 884 | |
| 885 | // Methods may only have one of public/protected/private. |
| 886 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 887 | if (kIsDebugBuild) { |
| 888 | size_t access_mod_count = |
| 889 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 890 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 891 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 892 | if (access_mod_count > 1) { |
| 893 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 894 | return false; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | // 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] | 899 | if (!code_item_accessor_.HasCodeItem()) { |
| Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 900 | // Only native or abstract methods may not have code. |
| 901 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 902 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 903 | return false; |
| 904 | } |
| 905 | |
| Nicolas Geoffray | 5e82b18 | 2020-12-08 08:57:36 +0000 | [diff] [blame] | 906 | // Test FastNative and CriticalNative annotations. We do this in the |
| 907 | // verifier for convenience. |
| 908 | if ((method_access_flags_ & kAccNative) != 0) { |
| 909 | // Fetch the flags from the annotations: the class linker hasn't processed |
| 910 | // them yet. |
| 911 | uint32_t native_access_flags = annotations::GetNativeMethodAnnotationAccessFlags( |
| 912 | *dex_file_, class_def_, dex_method_idx_); |
| 913 | if ((native_access_flags & kAccFastNative) != 0) { |
| 914 | if ((method_access_flags_ & kAccSynchronized) != 0) { |
| 915 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "fast native methods cannot be synchronized"; |
| 916 | return false; |
| 917 | } |
| 918 | } |
| 919 | if ((native_access_flags & kAccCriticalNative) != 0) { |
| 920 | if ((method_access_flags_ & kAccSynchronized) != 0) { |
| 921 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods cannot be synchronized"; |
| 922 | return false; |
| 923 | } |
| 924 | if ((method_access_flags_ & kAccStatic) == 0) { |
| 925 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "critical native methods must be static"; |
| 926 | return false; |
| 927 | } |
| 928 | const char* shorty = dex_file_->GetMethodShorty(method_id); |
| 929 | for (size_t i = 0, len = strlen(shorty); i < len; ++i) { |
| 930 | if (Primitive::GetType(shorty[i]) == Primitive::kPrimNot) { |
| 931 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << |
| 932 | "critical native methods must not have references as arguments or return type"; |
| 933 | return false; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 939 | // 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] | 940 | // 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] | 941 | if (kIsDebugBuild) { |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 942 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 943 | // Abstract methods are not allowed to have the following flags. |
| 944 | static constexpr uint32_t kForbidden = |
| 945 | kAccPrivate | |
| 946 | kAccStatic | |
| 947 | kAccFinal | |
| 948 | kAccNative | |
| 949 | kAccStrict | |
| 950 | kAccSynchronized; |
| 951 | if ((method_access_flags_ & kForbidden) != 0) { |
| 952 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 953 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 954 | return false; |
| 955 | } |
| 956 | } |
| David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 957 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 958 | // Interface methods must be public and abstract (if default methods are disabled). |
| Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 959 | uint32_t kRequired = kAccPublic; |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 960 | if ((method_access_flags_ & kRequired) != kRequired) { |
| Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 961 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | // In addition to the above, interface methods must not be protected. |
| 965 | static constexpr uint32_t kForbidden = kAccProtected; |
| 966 | if ((method_access_flags_ & kForbidden) != 0) { |
| 967 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 968 | return false; |
| 969 | } |
| 970 | } |
| 971 | // We also don't allow constructors to be abstract or native. |
| 972 | if (IsConstructor()) { |
| 973 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 974 | return false; |
| 975 | } |
| 976 | } |
| 977 | return true; |
| 978 | } |
| 979 | |
| 980 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 981 | if (kIsDebugBuild) { |
| 982 | // When there's code, the method must not be native or abstract. |
| 983 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 984 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 985 | return false; |
| 986 | } |
| 987 | |
| David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 988 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 989 | // Interfaces may always have static initializers for their fields. If we are running with |
| 990 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 991 | // Otherwise that is the only type of method allowed. |
| Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 992 | if (!(IsConstructor() && IsStatic())) { |
| Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 993 | if (IsInstanceConstructor()) { |
| 994 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 995 | return false; |
| 996 | } else if (method_access_flags_ & kAccFinal) { |
| 997 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 998 | return false; |
| Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 999 | } else { |
| 1000 | uint32_t access_flag_options = kAccPublic; |
| Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 1001 | if (dex_file_->SupportsDefaultMethods()) { |
| Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 1002 | access_flag_options |= kAccPrivate; |
| 1003 | } |
| 1004 | if (!(method_access_flags_ & access_flag_options)) { |
| 1005 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1006 | << "interfaces may not have protected or package-private members"; |
| 1007 | return false; |
| 1008 | } |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 1009 | } |
| Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Instance constructors must not be synchronized. |
| 1014 | if (IsInstanceConstructor()) { |
| 1015 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 1016 | if ((method_access_flags_ & kForbidden) != 0) { |
| 1017 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 1018 | return false; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1023 | // Consistency-check of the register counts. |
| 1024 | // ins + locals = registers, so make sure that ins <= registers. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1025 | if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) { |
| 1026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" |
| 1027 | << code_item_accessor_.InsSize() |
| 1028 | << " regs=" << code_item_accessor_.RegistersSize(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1029 | return false; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1030 | } |
| Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 1031 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1032 | // Allocate and initialize an array to hold instruction data. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1033 | insn_flags_.reset(allocator_.AllocArray<InstructionFlags>( |
| 1034 | code_item_accessor_.InsnsSizeInCodeUnits())); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1035 | DCHECK(insn_flags_ != nullptr); |
| 1036 | std::uninitialized_fill_n(insn_flags_.get(), |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1037 | code_item_accessor_.InsnsSizeInCodeUnits(), |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1038 | InstructionFlags()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | // Run through the instructions and see if the width checks out. |
| 1040 | bool result = ComputeWidthsAndCountOps(); |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 1041 | bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1042 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 1043 | result = result && ScanTryCatchBlocks(); |
| 1044 | // Perform static instruction verification. |
| Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1045 | result = result && (allow_runtime_only_instructions |
| 1046 | ? VerifyInstructions<true>() |
| 1047 | : VerifyInstructions<false>()); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1048 | // Perform code-flow analysis and return. |
| Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 1049 | result = result && VerifyCodeFlow(); |
| Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 1050 | |
| Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 1051 | return result; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1054 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1055 | bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() { |
| Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1056 | // We can't assume the instruction is well formed, handle the case where calculating the size |
| 1057 | // goes past the end of the code item. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1058 | SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end()); |
| 1059 | for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) { |
| Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1060 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 1061 | SafeDexInstructionIterator next = it; |
| 1062 | ++next; |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1063 | if (next.IsErrorState()) { |
| Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1066 | GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1069 | if (it != code_item_accessor_.end()) { |
| 1070 | const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1071 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1072 | << it.DexPc() << " vs. " << insns_size << ")"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1073 | return false; |
| 1074 | } |
| Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 1075 | DCHECK(GetInstructionFlags(0).IsOpcode()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1076 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1077 | return true; |
| 1078 | } |
| 1079 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1080 | template <bool kVerifierDebug> |
| 1081 | bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1082 | const uint32_t tries_size = code_item_accessor_.TriesSize(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1083 | if (tries_size == 0) { |
| 1084 | return true; |
| 1085 | } |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1086 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1087 | for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1088 | const uint32_t start = try_item.start_addr_; |
| 1089 | const uint32_t end = start + try_item.insn_count_; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1090 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1091 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1092 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1093 | return false; |
| 1094 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1095 | if (!GetInstructionFlags(start).IsOpcode()) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1096 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1097 | << "'try' block starts inside an instruction (" << start << ")"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1098 | return false; |
| 1099 | } |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1100 | DexInstructionIterator end_it(code_item_accessor_.Insns(), end); |
| 1101 | for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1102 | GetModifiableInstructionFlags(it.DexPc()).SetInTry(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1105 | // Iterate over each of the handlers to verify target addresses. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1106 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 1107 | const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1108 | ClassLinker* linker = GetClassLinker(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1109 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1110 | CatchHandlerIterator iterator(handlers_ptr); |
| 1111 | for (; iterator.HasNext(); iterator.Next()) { |
| Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 1112 | uint32_t dex_pc = iterator.GetHandlerAddress(); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1113 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1114 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1115 | << "exception handler starts at bad address (" << dex_pc << ")"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1116 | return false; |
| 1117 | } |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1118 | if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) { |
| Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 1119 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1120 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 1121 | return false; |
| 1122 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1123 | GetModifiableInstructionFlags(dex_pc).SetBranchTarget(); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1124 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1125 | // unresolved exception types will be ignored by exception delivery |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1126 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
| Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1127 | ObjPtr<mirror::Class> exception_type = |
| 1128 | linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1129 | if (exception_type == nullptr) { |
| 1130 | DCHECK(self_->IsExceptionPending()); |
| 1131 | self_->ClearException(); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1134 | } |
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1135 | handlers_ptr = iterator.EndDataPointer(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1136 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1137 | return true; |
| 1138 | } |
| 1139 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1140 | template <bool kVerifierDebug> |
| Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1141 | template <bool kAllowRuntimeOnlyInstructions> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1142 | bool MethodVerifier<kVerifierDebug>::VerifyInstructions() { |
| Andreas Gampe | 93adcb5 | 2019-06-06 20:16:07 -0700 | [diff] [blame] | 1143 | // Flag the start of the method as a branch target. |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1144 | GetModifiableInstructionFlags(0).SetBranchTarget(); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1145 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1146 | const uint32_t dex_pc = inst.DexPc(); |
| 1147 | if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1148 | DCHECK_NE(failures_.size(), 0U); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1149 | return false; |
| 1150 | } |
| Andreas Gampe | 93adcb5 | 2019-06-06 20:16:07 -0700 | [diff] [blame] | 1151 | // Flag some interesting instructions. |
| 1152 | if (inst->IsReturn()) { |
| 1153 | GetModifiableInstructionFlags(dex_pc).SetReturn(); |
| 1154 | } else if (inst->Opcode() == Instruction::CHECK_CAST) { |
| 1155 | // The dex-to-dex compiler wants type information to elide check-casts. |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1156 | GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1157 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1158 | } |
| 1159 | return true; |
| 1160 | } |
| 1161 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1162 | template <bool kVerifierDebug> |
| Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1163 | template <bool kAllowRuntimeOnlyInstructions> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1164 | bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst, |
| 1165 | uint32_t code_offset) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1166 | bool result = true; |
| 1167 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1168 | case Instruction::kVerifyRegA: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1169 | result = result && CheckRegisterIndex(inst->VRegA()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1170 | break; |
| 1171 | case Instruction::kVerifyRegAWide: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1172 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1173 | break; |
| 1174 | } |
| 1175 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1176 | case Instruction::kVerifyRegB: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1177 | result = result && CheckRegisterIndex(inst->VRegB()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | case Instruction::kVerifyRegBField: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1180 | result = result && CheckFieldIndex(inst->VRegB()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | case Instruction::kVerifyRegBMethod: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1183 | result = result && CheckMethodIndex(inst->VRegB()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | break; |
| 1185 | case Instruction::kVerifyRegBNewInstance: |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1186 | result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB())); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | case Instruction::kVerifyRegBString: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1189 | result = result && CheckStringIndex(inst->VRegB()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1190 | break; |
| 1191 | case Instruction::kVerifyRegBType: |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1192 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB())); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1193 | break; |
| 1194 | case Instruction::kVerifyRegBWide: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1195 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1196 | break; |
| Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1197 | case Instruction::kVerifyRegBCallSite: |
| 1198 | result = result && CheckCallSiteIndex(inst->VRegB()); |
| 1199 | break; |
| 1200 | case Instruction::kVerifyRegBMethodHandle: |
| 1201 | result = result && CheckMethodHandleIndex(inst->VRegB()); |
| 1202 | break; |
| 1203 | case Instruction::kVerifyRegBPrototype: |
| 1204 | result = result && CheckPrototypeIndex(inst->VRegB()); |
| 1205 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1206 | } |
| 1207 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1208 | case Instruction::kVerifyRegC: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1209 | result = result && CheckRegisterIndex(inst->VRegC()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1210 | break; |
| 1211 | case Instruction::kVerifyRegCField: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1212 | result = result && CheckFieldIndex(inst->VRegC()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1213 | break; |
| 1214 | case Instruction::kVerifyRegCNewArray: |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1215 | result = result && CheckNewArray(dex::TypeIndex(inst->VRegC())); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1216 | break; |
| 1217 | case Instruction::kVerifyRegCType: |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1218 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC())); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1219 | break; |
| 1220 | case Instruction::kVerifyRegCWide: |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1221 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1222 | break; |
| 1223 | } |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1224 | switch (inst->GetVerifyTypeArgumentH()) { |
| 1225 | case Instruction::kVerifyRegHPrototype: |
| 1226 | result = result && CheckPrototypeIndex(inst->VRegH()); |
| 1227 | break; |
| 1228 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1229 | switch (inst->GetVerifyExtraFlags()) { |
| 1230 | case Instruction::kVerifyArrayData: |
| 1231 | result = result && CheckArrayData(code_offset); |
| 1232 | break; |
| 1233 | case Instruction::kVerifyBranchTarget: |
| 1234 | result = result && CheckBranchTarget(code_offset); |
| 1235 | break; |
| 1236 | case Instruction::kVerifySwitchTargets: |
| 1237 | result = result && CheckSwitchTargets(code_offset); |
| 1238 | break; |
| Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1239 | case Instruction::kVerifyVarArgNonZero: |
| 1240 | // Fall-through. |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1241 | case Instruction::kVerifyVarArg: { |
| Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1242 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1243 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1244 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1245 | v_a > Instruction::kMaxVarArgRegs) { |
| 1246 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
| Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1247 | "non-range invoke"; |
| 1248 | return false; |
| 1249 | } |
| Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1250 | |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1251 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1252 | inst->GetVarArgs(args); |
| Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1253 | result = result && CheckVarArgRegs(v_a, args); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1254 | break; |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1255 | } |
| Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1256 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1257 | // Fall-through. |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1258 | case Instruction::kVerifyVarArgRange: |
| Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1259 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1260 | inst->VRegA() <= 0) { |
| 1261 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1262 | "range invoke"; |
| 1263 | return false; |
| 1264 | } |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1265 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1266 | break; |
| 1267 | case Instruction::kVerifyError: |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1268 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1269 | result = false; |
| 1270 | break; |
| 1271 | } |
| Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1272 | if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) { |
| Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1273 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1274 | result = false; |
| 1275 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1276 | return result; |
| 1277 | } |
| 1278 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1279 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1280 | inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) { |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1281 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1282 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1283 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | // 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] | 1287 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1288 | if (UNLIKELY(descriptor[0] != 'L')) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1289 | 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] | 1290 | return false; |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1291 | } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) { |
| Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1292 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1293 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1294 | Fail(VERIFY_ERROR_INSTANTIATION); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1295 | } |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1299 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1300 | bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) { |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1301 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1302 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1303 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | int bracket_count = 0; |
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1307 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1308 | const char* cp = descriptor; |
| 1309 | while (*cp++ == '[') { |
| 1310 | bracket_count++; |
| 1311 | } |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1312 | if (UNLIKELY(bracket_count == 0)) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1313 | /* The given class must be an array type. */ |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1314 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1315 | << "can't new-array class '" << descriptor << "' (not an array)"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1316 | return false; |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1317 | } else if (UNLIKELY(bracket_count > 255)) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1318 | /* It is illegal to create an array of more than 255 dimensions. */ |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1319 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1320 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1326 | template <bool kVerifierDebug> |
| 1327 | bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1328 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
| 1329 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1330 | const uint16_t* array_data; |
| 1331 | int32_t array_data_offset; |
| 1332 | |
| 1333 | DCHECK_LT(cur_offset, insn_count); |
| 1334 | /* make sure the start of the array data table is in range */ |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1335 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1336 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
| 1337 | cur_offset + array_data_offset + 2 >= insn_count)) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1338 | 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] | 1339 | << ", data offset " << array_data_offset |
| 1340 | << ", count " << insn_count; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1341 | return false; |
| 1342 | } |
| 1343 | /* offset to array data table is a relative branch-style offset */ |
| 1344 | array_data = insns + array_data_offset; |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1345 | // 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] | 1346 | if (UNLIKELY(!IsAligned<4>(array_data))) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1347 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1348 | << ", data offset " << array_data_offset; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1349 | return false; |
| 1350 | } |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1351 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1352 | // 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] | 1353 | if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) { |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1354 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1355 | << ", data offset " << array_data_offset |
| 1356 | << " not correctly visited, probably bad padding."; |
| 1357 | return false; |
| 1358 | } |
| 1359 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1360 | uint32_t value_width = array_data[1]; |
| Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1361 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1362 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1363 | /* make sure the end of the switch is in range */ |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1364 | if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1365 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1366 | << ", data offset " << array_data_offset << ", end " |
| 1367 | << cur_offset + array_data_offset + table_size |
| 1368 | << ", count " << insn_count; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1369 | return false; |
| 1370 | } |
| 1371 | return true; |
| 1372 | } |
| 1373 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1374 | template <bool kVerifierDebug> |
| 1375 | bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1376 | int32_t offset; |
| 1377 | bool isConditional, selfOkay; |
| 1378 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1379 | return false; |
| 1380 | } |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1381 | if (UNLIKELY(!selfOkay && offset == 0)) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1382 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1383 | << reinterpret_cast<void*>(cur_offset); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1384 | return false; |
| 1385 | } |
| Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1386 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1387 | // 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] | 1388 | 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] | 1389 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1390 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1391 | return false; |
| 1392 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1393 | int32_t abs_offset = cur_offset + offset; |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1394 | if (UNLIKELY(abs_offset < 0 || |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1395 | (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() || |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1396 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1397 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
| Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1398 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1399 | << reinterpret_cast<void*>(cur_offset); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1402 | GetModifiableInstructionFlags(abs_offset).SetBranchTarget(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1403 | return true; |
| 1404 | } |
| 1405 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1406 | template <bool kVerifierDebug> |
| 1407 | bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset, |
| 1408 | int32_t* pOffset, |
| 1409 | bool* pConditional, |
| 1410 | bool* selfOkay) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1411 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1412 | *pConditional = false; |
| 1413 | *selfOkay = false; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1414 | switch (*insns & 0xff) { |
| 1415 | case Instruction::GOTO: |
| 1416 | *pOffset = ((int16_t) *insns) >> 8; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1417 | break; |
| 1418 | case Instruction::GOTO_32: |
| 1419 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1420 | *selfOkay = true; |
| 1421 | break; |
| 1422 | case Instruction::GOTO_16: |
| 1423 | *pOffset = (int16_t) insns[1]; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1424 | break; |
| 1425 | case Instruction::IF_EQ: |
| 1426 | case Instruction::IF_NE: |
| 1427 | case Instruction::IF_LT: |
| 1428 | case Instruction::IF_GE: |
| 1429 | case Instruction::IF_GT: |
| 1430 | case Instruction::IF_LE: |
| 1431 | case Instruction::IF_EQZ: |
| 1432 | case Instruction::IF_NEZ: |
| 1433 | case Instruction::IF_LTZ: |
| 1434 | case Instruction::IF_GEZ: |
| 1435 | case Instruction::IF_GTZ: |
| 1436 | case Instruction::IF_LEZ: |
| 1437 | *pOffset = (int16_t) insns[1]; |
| 1438 | *pConditional = true; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1439 | break; |
| 1440 | default: |
| 1441 | return false; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1442 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1443 | return true; |
| 1444 | } |
| 1445 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1446 | template <bool kVerifierDebug> |
| 1447 | bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1448 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1449 | DCHECK_LT(cur_offset, insn_count); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1450 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1451 | /* make sure the start of the switch is in range */ |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1452 | 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] | 1453 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1454 | cur_offset + switch_offset + 2 > insn_count)) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1455 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1456 | << ", switch offset " << switch_offset |
| 1457 | << ", count " << insn_count; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1458 | return false; |
| 1459 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1460 | /* offset to switch table is a relative branch-style offset */ |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1461 | const uint16_t* switch_insns = insns + switch_offset; |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1462 | // 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] | 1463 | if (UNLIKELY(!IsAligned<4>(switch_insns))) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1464 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1465 | << ", switch offset " << switch_offset; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1468 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1469 | // 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] | 1470 | if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) { |
| Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1471 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1472 | << ", switch offset " << switch_offset |
| 1473 | << " not correctly visited, probably bad padding."; |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1477 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1478 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1479 | uint32_t switch_count = switch_insns[1]; |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1480 | int32_t targets_offset; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1481 | uint16_t expected_signature; |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1482 | if (is_packed_switch) { |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1483 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1484 | targets_offset = 4; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1485 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1486 | } else { |
| 1487 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1488 | targets_offset = 2 + 2 * switch_count; |
| 1489 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1490 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1491 | uint32_t table_size = targets_offset + switch_count * 2; |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1492 | if (UNLIKELY(switch_insns[0] != expected_signature)) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1493 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1494 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1495 | switch_insns[0], expected_signature); |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1498 | /* make sure the end of the switch is in range */ |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1499 | if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1500 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1501 | << ", switch offset " << switch_offset |
| 1502 | << ", end " << (cur_offset + switch_offset + table_size) |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1503 | << ", count " << insn_count; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1504 | return false; |
| 1505 | } |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1506 | |
| 1507 | constexpr int32_t keys_offset = 2; |
| 1508 | if (switch_count > 1) { |
| 1509 | if (is_packed_switch) { |
| 1510 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1511 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1512 | int32_t max_first_key = |
| 1513 | 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] | 1514 | if (UNLIKELY(first_key > max_first_key)) { |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1515 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1516 | << ", switch_count=" << switch_count; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1519 | } else { |
| 1520 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1521 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1522 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1523 | int32_t key = |
| 1524 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1525 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1526 | if (UNLIKELY(key <= last_key)) { |
| David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1527 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1528 | << ", this=" << key; |
| 1529 | return false; |
| 1530 | } |
| 1531 | last_key = key; |
| 1532 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1533 | } |
| 1534 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1535 | /* verify each switch target */ |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1536 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1537 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1538 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1539 | int32_t abs_offset = cur_offset + offset; |
| Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1540 | if (UNLIKELY(abs_offset < 0 || |
| 1541 | abs_offset >= static_cast<int32_t>(insn_count) || |
| 1542 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1543 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1544 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1545 | << reinterpret_cast<void*>(cur_offset) |
| 1546 | << "[" << targ << "]"; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1547 | return false; |
| 1548 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1549 | GetModifiableInstructionFlags(abs_offset).SetBranchTarget(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1550 | } |
| 1551 | return true; |
| 1552 | } |
| 1553 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1554 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1555 | bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1556 | const uint16_t registers_size = code_item_accessor_.RegistersSize(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1557 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1558 | /* Create and initialize table holding register status */ |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 1559 | reg_table_.Init(insn_flags_.get(), |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1560 | code_item_accessor_.InsnsSizeInCodeUnits(), |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1561 | registers_size, |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 1562 | allocator_, |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 1563 | GetRegTypeCache(), |
| 1564 | interesting_dex_pc_); |
| Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1565 | |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 1566 | work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache())); |
| 1567 | saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache())); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1568 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1569 | /* Initialize register types of method arguments. */ |
| 1570 | if (!SetTypesFromSignature()) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1571 | DCHECK_NE(failures_.size(), 0U); |
| 1572 | std::string prepend("Bad signature in "); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1573 | prepend += dex_file_->PrettyMethod(dex_method_idx_); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1574 | PrependToLastFailMessage(prepend); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
| Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1577 | // We may have a runtime failure here, clear. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 1578 | flags_.have_pending_runtime_throw_failure_ = false; |
| Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1579 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1580 | /* Perform code flow verification. */ |
| Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1581 | bool res = LIKELY(monitor_enter_dex_pcs_ == nullptr) |
| 1582 | ? CodeFlowVerifyMethod</*kMonitorDexPCs=*/ false>() |
| 1583 | : CodeFlowVerifyMethod</*kMonitorDexPCs=*/ true>(); |
| 1584 | if (UNLIKELY(!res)) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1585 | DCHECK_NE(failures_.size(), 0U); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1586 | return false; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1588 | return true; |
| 1589 | } |
| 1590 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1591 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1592 | void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1593 | if (!code_item_accessor_.HasCodeItem()) { |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1594 | vios->Stream() << "Native method\n"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1595 | return; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1596 | } |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1597 | { |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1598 | vios->Stream() << "Register Types:\n"; |
| 1599 | ScopedIndentation indent1(vios); |
| 1600 | reg_types_.Dump(vios->Stream()); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1601 | } |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1602 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1603 | ScopedIndentation indent1(vios); |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1604 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1605 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1606 | const size_t dex_pc = inst.DexPc(); |
| Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1607 | |
| 1608 | // Might be asked to dump before the table is initialized. |
| 1609 | if (reg_table_.IsInitialized()) { |
| 1610 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1611 | if (reg_line != nullptr) { |
| 1612 | vios->Stream() << reg_line->Dump(this) << "\n"; |
| 1613 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1614 | } |
| Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1615 | |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1616 | vios->Stream() |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1617 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1618 | const bool kDumpHexOfInstruction = false; |
| 1619 | if (kDumpHexOfInstruction) { |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1620 | vios->Stream() << inst->DumpHex(5) << " "; |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1621 | } |
| Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1622 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1623 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1626 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1627 | switch (descriptor) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1628 | case 'I': |
| 1629 | case 'C': |
| 1630 | case 'S': |
| 1631 | case 'B': |
| 1632 | case 'Z': |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1633 | case 'F': |
| 1634 | case 'D': |
| 1635 | case 'J': |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1636 | return true; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1637 | default: |
| 1638 | return false; |
| 1639 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1642 | template <bool kVerifierDebug> |
| 1643 | bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1644 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1645 | |
| 1646 | // Should have been verified earlier. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1647 | DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize()); |
| Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1648 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1649 | uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize(); |
| 1650 | size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */ |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1651 | |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1652 | // Include the "this" pointer. |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1653 | size_t cur_arg = 0; |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1654 | if (!IsStatic()) { |
| Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1655 | if (expected_args == 0) { |
| 1656 | // Expect at least a receiver. |
| 1657 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1658 | return false; |
| 1659 | } |
| 1660 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1661 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1662 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1663 | // called. |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1664 | const RegType& declaring_class = GetDeclaringClass(); |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1665 | if (IsConstructor()) { |
| 1666 | if (declaring_class.IsJavaLangObject()) { |
| 1667 | // "this" is implicitly initialized. |
| 1668 | reg_line->SetThisInitialized(); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1669 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1670 | } else { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1671 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1672 | this, |
| 1673 | arg_start + cur_arg, |
| 1674 | reg_types_.UninitializedThisArgument(declaring_class)); |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1675 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1676 | } else { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1677 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1678 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1679 | cur_arg++; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1682 | const dex::ProtoId& proto_id = |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1683 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1684 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1685 | |
| 1686 | for (; iterator.HasNext(); iterator.Next()) { |
| 1687 | const char* descriptor = iterator.GetDescriptor(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1688 | if (descriptor == nullptr) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1689 | LOG(FATAL) << "Null descriptor"; |
| 1690 | } |
| 1691 | if (cur_arg >= expected_args) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1692 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1693 | << " args, found more (" << descriptor << ")"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1694 | return false; |
| 1695 | } |
| 1696 | switch (descriptor[0]) { |
| 1697 | case 'L': |
| 1698 | case '[': |
| 1699 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1700 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1701 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1702 | // can return without doing anything or call virtual methods). |
| 1703 | { |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 1704 | // Note: don't check access. No error would be thrown for declaring or passing an |
| 1705 | // inaccessible class. Only actual accesses to fields or methods will. |
| 1706 | const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx()); |
| Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1707 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1708 | DCHECK(HasFailures()); |
| 1709 | return false; |
| 1710 | } |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1711 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1712 | } |
| 1713 | break; |
| 1714 | case 'Z': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1715 | 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] | 1716 | break; |
| 1717 | case 'C': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1718 | 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] | 1719 | break; |
| 1720 | case 'B': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1721 | 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] | 1722 | break; |
| 1723 | case 'I': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1724 | 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] | 1725 | break; |
| 1726 | case 'S': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1727 | 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] | 1728 | break; |
| 1729 | case 'F': |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1730 | 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] | 1731 | break; |
| 1732 | case 'J': |
| 1733 | case 'D': { |
| Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1734 | if (cur_arg + 1 >= expected_args) { |
| 1735 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1736 | << " args, found more (" << descriptor << ")"; |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1740 | const RegType* lo_half; |
| 1741 | const RegType* hi_half; |
| 1742 | if (descriptor[0] == 'J') { |
| 1743 | lo_half = ®_types_.LongLo(); |
| 1744 | hi_half = ®_types_.LongHi(); |
| 1745 | } else { |
| 1746 | lo_half = ®_types_.DoubleLo(); |
| 1747 | hi_half = ®_types_.DoubleHi(); |
| 1748 | } |
| 1749 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1750 | cur_arg++; |
| 1751 | break; |
| 1752 | } |
| 1753 | default: |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1754 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1755 | << descriptor << "'"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1756 | return false; |
| 1757 | } |
| 1758 | cur_arg++; |
| 1759 | } |
| 1760 | if (cur_arg != expected_args) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1761 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1762 | << " arguments, found " << cur_arg; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1763 | return false; |
| 1764 | } |
| 1765 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1766 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1767 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1768 | bool result; |
| 1769 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1770 | result = descriptor[1] == '\0'; |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1771 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1772 | size_t i = 0; |
| 1773 | do { |
| 1774 | i++; |
| 1775 | } while (descriptor[i] == '['); // process leading [ |
| 1776 | if (descriptor[i] == 'L') { // object array |
| 1777 | do { |
| 1778 | i++; // find closing ; |
| 1779 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1780 | result = descriptor[i] == ';'; |
| 1781 | } else { // primitive array |
| 1782 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1783 | } |
| 1784 | } else if (descriptor[0] == 'L') { |
| 1785 | // could be more thorough here, but shouldn't be required |
| 1786 | size_t i = 0; |
| 1787 | do { |
| 1788 | i++; |
| 1789 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1790 | result = descriptor[i] == ';'; |
| 1791 | } else { |
| 1792 | result = false; |
| 1793 | } |
| 1794 | if (!result) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1795 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1796 | << descriptor << "'"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1797 | } |
| 1798 | return result; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
| Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1801 | COLD_ATTR |
| 1802 | void HandleMonitorDexPcsWorkLine( |
| 1803 | std::vector<::art::verifier::MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs, |
| 1804 | RegisterLine* work_line) { |
| 1805 | monitor_enter_dex_pcs->clear(); // The new work line is more accurate than the previous one. |
| 1806 | |
| 1807 | std::map<uint32_t, ::art::verifier::MethodVerifier::DexLockInfo> depth_to_lock_info; |
| 1808 | auto collector = [&](uint32_t dex_reg, uint32_t depth) { |
| 1809 | auto insert_pair = depth_to_lock_info.emplace( |
| 1810 | depth, ::art::verifier::MethodVerifier::DexLockInfo(depth)); |
| 1811 | auto it = insert_pair.first; |
| 1812 | auto set_insert_pair = it->second.dex_registers.insert(dex_reg); |
| 1813 | DCHECK(set_insert_pair.second); |
| 1814 | }; |
| 1815 | work_line->IterateRegToLockDepths(collector); |
| 1816 | for (auto& pair : depth_to_lock_info) { |
| 1817 | monitor_enter_dex_pcs->push_back(pair.second); |
| 1818 | // Map depth to dex PC. |
| Andreas Gampe | 4ec4d48 | 2019-08-01 10:00:23 -0700 | [diff] [blame] | 1819 | 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] | 1820 | } |
| 1821 | } |
| 1822 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1823 | template <bool kVerifierDebug> |
| Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1824 | template <bool kMonitorDexPCs> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1825 | bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1826 | const uint16_t* insns = code_item_accessor_.Insns(); |
| 1827 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1828 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1829 | /* Begin by marking the first instruction as "changed". */ |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1830 | GetModifiableInstructionFlags(0).SetChanged(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1831 | uint32_t start_guess = 0; |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1832 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1833 | /* Continue until no instructions are marked "changed". */ |
| 1834 | while (true) { |
| Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1835 | if (allow_thread_suspension_) { |
| 1836 | self_->AllowThreadSuspension(); |
| 1837 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1838 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1839 | uint32_t insn_idx = start_guess; |
| 1840 | for (; insn_idx < insns_size; insn_idx++) { |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1841 | if (GetInstructionFlags(insn_idx).IsChanged()) |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1842 | break; |
| 1843 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1844 | if (insn_idx == insns_size) { |
| 1845 | if (start_guess != 0) { |
| 1846 | /* try again, starting from the top */ |
| 1847 | start_guess = 0; |
| 1848 | continue; |
| 1849 | } else { |
| 1850 | /* all flags are clear */ |
| 1851 | break; |
| 1852 | } |
| 1853 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1854 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1855 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1856 | // "changed" flags, we need to load the set of registers from the table. |
| 1857 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1858 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1859 | // target. |
| 1860 | work_insn_idx_ = insn_idx; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1861 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1862 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1863 | } else if (kIsDebugBuild) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1864 | /* |
| David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1865 | * Consistency check: retrieve the stored register line (assuming |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1866 | * a full table) and make sure it actually matches. |
| 1867 | */ |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1868 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1869 | if (register_line != nullptr) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1870 | if (work_line_->CompareLine(register_line) != 0) { |
| Andreas Gampe | 4a57d5f | 2019-04-03 12:31:18 -0700 | [diff] [blame] | 1871 | Dump(LOG_STREAM(FATAL_WITHOUT_ABORT)); |
| 1872 | LOG(FATAL_WITHOUT_ABORT) << info_messages_.str(); |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1873 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
| Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1874 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1875 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1876 | << " expected=" << register_line->Dump(this); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1877 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1878 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1879 | } |
| Andreas Gampe | b90cad5 | 2019-07-31 16:15:41 -0700 | [diff] [blame] | 1880 | |
| 1881 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1882 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1883 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1884 | // for a thread to be suspended). |
| 1885 | if (kMonitorDexPCs && UNLIKELY(work_insn_idx_ == interesting_dex_pc_)) { |
| 1886 | HandleMonitorDexPcsWorkLine(monitor_enter_dex_pcs_, work_line_.get()); |
| 1887 | } |
| 1888 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1889 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1890 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1891 | prepend += " failed to verify: "; |
| 1892 | PrependToLastFailMessage(prepend); |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1893 | return false; |
| 1894 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1895 | /* Clear "changed" and mark as visited. */ |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1896 | GetModifiableInstructionFlags(insn_idx).SetVisited(); |
| 1897 | GetModifiableInstructionFlags(insn_idx).ClearChanged(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1898 | } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1899 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1900 | if (kVerifierDebug) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1901 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1902 | * Scan for dead code. There's nothing "evil" about dead code |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1903 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1904 | * down the line, possibly in the verifier. |
| 1905 | * |
| 1906 | * If we've substituted "always throw" instructions into the stream, |
| 1907 | * we are almost certainly going to have some dead code. |
| 1908 | */ |
| 1909 | int dead_start = -1; |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1910 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1911 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1912 | const uint32_t insn_idx = inst.DexPc(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1913 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1914 | * Switch-statement data doesn't get "visited" by scanner. It |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1915 | * may or may not be preceded by a padding NOP (for alignment). |
| 1916 | */ |
| 1917 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1918 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1919 | insns[insn_idx] == Instruction::kArrayDataSignature || |
| Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1920 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1921 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1922 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1923 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1924 | GetModifiableInstructionFlags(insn_idx).SetVisited(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1927 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1928 | if (dead_start < 0) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1929 | dead_start = insn_idx; |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1930 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1931 | } else if (dead_start >= 0) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1932 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1933 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1934 | dead_start = -1; |
| 1935 | } |
| 1936 | } |
| 1937 | if (dead_start >= 0) { |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1938 | LogVerifyInfo() |
| 1939 | << "dead code " << reinterpret_cast<void*>(dead_start) |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1940 | << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1); |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1941 | } |
| Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1942 | // 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] | 1943 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
| Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1944 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1945 | // LOG(INFO) << info_messages_.str(); |
| 1946 | // } |
| jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1947 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1948 | return true; |
| 1949 | } |
| 1950 | |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 1951 | // Setup a register line for the given return instruction. |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1952 | template <bool kVerifierDebug> |
| 1953 | static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier, |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 1954 | const Instruction* ret_inst, |
| 1955 | RegisterLine* line) { |
| 1956 | Instruction::Code opcode = ret_inst->Opcode(); |
| 1957 | |
| 1958 | switch (opcode) { |
| 1959 | case Instruction::RETURN_VOID: |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 1960 | if (verifier->IsInstanceConstructor()) { |
| 1961 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 1962 | line->CheckConstructorReturn(verifier); |
| 1963 | } |
| 1964 | line->MarkAllRegistersAsConflicts(verifier); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case Instruction::RETURN: |
| 1968 | case Instruction::RETURN_OBJECT: |
| 1969 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 1970 | break; |
| 1971 | |
| 1972 | case Instruction::RETURN_WIDE: |
| 1973 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 1974 | break; |
| 1975 | |
| 1976 | default: |
| 1977 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 1978 | UNREACHABLE(); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 1982 | template <bool kVerifierDebug> |
| 1983 | bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1984 | /* |
| 1985 | * Once we finish decoding the instruction, we need to figure out where |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1986 | * we can go from here. There are three possible ways to transfer |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1987 | * control to another statement: |
| 1988 | * |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1989 | * (1) Continue to the next instruction. Applies to all but |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1990 | * unconditional branches, method returns, and exception throws. |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1991 | * (2) Branch to one or more possible locations. Applies to branches |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1992 | * and switch statements. |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1993 | * (3) Exception handlers. Applies to any instruction that can |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1994 | * throw an exception that is handled by an encompassing "try" |
| 1995 | * block. |
| 1996 | * |
| 1997 | * We can also return, in which case there is no successor instruction |
| 1998 | * from this point. |
| 1999 | * |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2000 | * The behavior can be determined from the opcode flags. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2001 | */ |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2002 | const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2003 | const Instruction* inst = Instruction::At(insns); |
| Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2004 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2005 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2006 | int32_t branch_target = 0; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2007 | bool just_set_result = false; |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 2008 | if (kVerifierDebug) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2009 | // Generate processing back trace to debug verifier |
| Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 2010 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl |
| 2011 | << work_line_->Dump(this); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2012 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2013 | |
| 2014 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2015 | * Make a copy of the previous register state. If the instruction |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2016 | * can throw an exception, we will copy/merge this into the "catch" |
| 2017 | * address rather than work_line, because we don't want the result |
| 2018 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2019 | * a type) to be visible to the exception handler. |
| 2020 | */ |
| Andreas Gampe | c658278 | 2019-07-10 15:06:18 -0700 | [diff] [blame] | 2021 | if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) && |
| 2022 | CurrentInsnFlags()->IsInTry()) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2023 | saved_line_->CopyFromLine(work_line_.get()); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2024 | } else if (kIsDebugBuild) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2025 | saved_line_->FillWithGarbage(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2026 | } |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 2027 | // Per-instruction flag, should not be set here. |
| 2028 | DCHECK(!flags_.have_pending_runtime_throw_failure_); |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2029 | bool exc_handler_unreachable = false; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2030 | |
| Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2031 | |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2032 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2033 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2034 | // branch target. |
| Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2035 | RegisterLineArenaUniquePtr branch_line; |
| 2036 | RegisterLineArenaUniquePtr fallthrough_line; |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2037 | |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2038 | switch (inst->Opcode()) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2039 | case Instruction::NOP: |
| 2040 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2041 | * A "pure" NOP has no effect on anything. Data tables start with |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2042 | * a signature that looks like a NOP; if we see one of these in |
| 2043 | * the course of executing code then we have a problem. |
| 2044 | */ |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2045 | if (inst->VRegA_10x() != 0) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2046 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2047 | } |
| 2048 | break; |
| 2049 | |
| 2050 | case Instruction::MOVE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2051 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2052 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2053 | case Instruction::MOVE_FROM16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2054 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2055 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2056 | case Instruction::MOVE_16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2057 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2058 | break; |
| 2059 | case Instruction::MOVE_WIDE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2060 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2061 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2062 | case Instruction::MOVE_WIDE_FROM16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2063 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2064 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2065 | case Instruction::MOVE_WIDE_16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2066 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | break; |
| 2068 | case Instruction::MOVE_OBJECT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2069 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2070 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2071 | case Instruction::MOVE_OBJECT_FROM16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2072 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2073 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2074 | case Instruction::MOVE_OBJECT_16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2075 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2076 | break; |
| 2077 | |
| 2078 | /* |
| 2079 | * The move-result instructions copy data out of a "pseudo-register" |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2080 | * with the results from the last method invocation. In practice we |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2081 | * might want to hold the result in an actual CPU register, so the |
| 2082 | * Dalvik spec requires that these only appear immediately after an |
| 2083 | * invoke or filled-new-array. |
| 2084 | * |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2085 | * These calls invalidate the "result" register. (This is now |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2086 | * redundant with the reset done below, but it can make the debug info |
| 2087 | * easier to read in some cases.) |
| 2088 | */ |
| 2089 | case Instruction::MOVE_RESULT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2090 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2091 | break; |
| 2092 | case Instruction::MOVE_RESULT_WIDE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2093 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | case Instruction::MOVE_RESULT_OBJECT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2096 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2097 | break; |
| 2098 | |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2099 | case Instruction::MOVE_EXCEPTION: |
| 2100 | if (!HandleMoveException(inst)) { |
| 2101 | exc_handler_unreachable = true; |
| Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2102 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2103 | break; |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 2104 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2105 | case Instruction::RETURN_VOID: |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2106 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2107 | if (!GetMethodReturnType().IsConflict()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2108 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2109 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2110 | } |
| 2111 | break; |
| 2112 | case Instruction::RETURN: |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2113 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2114 | /* check the method signature */ |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2115 | const RegType& return_type = GetMethodReturnType(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2116 | if (!return_type.IsCategory1Types()) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2117 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2118 | << return_type; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2119 | } else { |
| 2120 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2121 | // 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] | 2122 | const uint32_t vregA = inst->VRegA_11x(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2123 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2124 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2125 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2126 | return_type.IsShort() || return_type.IsChar()) && |
| 2127 | src_type.IsInteger())); |
| 2128 | /* check the register contents */ |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2129 | bool success = |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2130 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2131 | if (!success) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2132 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2133 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | break; |
| 2137 | case Instruction::RETURN_WIDE: |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2138 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2139 | /* check the method signature */ |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2140 | const RegType& return_type = GetMethodReturnType(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2141 | if (!return_type.IsCategory2Types()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2142 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2143 | } else { |
| 2144 | /* check the register contents */ |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2145 | const uint32_t vregA = inst->VRegA_11x(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2146 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2147 | if (!success) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2148 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2149 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | break; |
| 2153 | case Instruction::RETURN_OBJECT: |
| Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2154 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2155 | const RegType& return_type = GetMethodReturnType(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2156 | if (!return_type.IsReferenceTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2157 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2158 | } else { |
| 2159 | /* return_type is the *expected* return type, not register value */ |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2160 | DCHECK(!return_type.IsZeroOrNull()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2161 | DCHECK(!return_type.IsUninitializedReference()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2162 | const uint32_t vregA = inst->VRegA_11x(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2163 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
| Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2164 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2165 | // reference in vAA is an instance of the "return_type." |
| 2166 | if (reg_type.IsUndefined()) { |
| 2167 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2168 | } else if (reg_type.IsConflict()) { |
| 2169 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2170 | } else if (reg_type.IsUninitializedTypes()) { |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2171 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2172 | << reg_type << "'"; |
| Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2173 | } else if (!reg_type.IsReferenceTypes()) { |
| 2174 | // We really do expect a reference here. |
| 2175 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2176 | << reg_type; |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2177 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2178 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 2179 | Fail(api_level_ > 29u |
| 2180 | ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_UNRESOLVED_TYPE_CHECK) |
| Andreas Gampe | bc919af | 2019-07-10 14:09:51 -0700 | [diff] [blame] | 2181 | << " can't resolve returned type '" << return_type << "' or '" << reg_type << "'"; |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2182 | } else { |
| Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2183 | bool soft_error = false; |
| 2184 | // Check whether arrays are involved. They will show a valid class status, even |
| 2185 | // if their components are erroneous. |
| 2186 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2187 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
| Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2188 | if (soft_error) { |
| 2189 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2190 | << reg_type << " vs " << return_type; |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | if (!soft_error) { |
| 2195 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2196 | << "', but expected from declaration '" << return_type << "'"; |
| 2197 | } |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2198 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | break; |
| 2203 | |
| Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2204 | /* could be boolean, int, float, or a null reference */ |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2205 | case Instruction::CONST_4: { |
| 2206 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2207 | work_line_->SetRegisterType<LockOp::kClear>( |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 2208 | this, inst->VRegA_11n(), DetermineCat1Constant(val)); |
| Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2209 | break; |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2210 | } |
| 2211 | case Instruction::CONST_16: { |
| 2212 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2213 | work_line_->SetRegisterType<LockOp::kClear>( |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 2214 | this, inst->VRegA_21s(), DetermineCat1Constant(val)); |
| Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2215 | break; |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2216 | } |
| Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2217 | case Instruction::CONST: { |
| 2218 | int32_t val = inst->VRegB_31i(); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2219 | work_line_->SetRegisterType<LockOp::kClear>( |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 2220 | this, inst->VRegA_31i(), DetermineCat1Constant(val)); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2221 | break; |
| Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2222 | } |
| 2223 | case Instruction::CONST_HIGH16: { |
| 2224 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2225 | work_line_->SetRegisterType<LockOp::kClear>( |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 2226 | this, inst->VRegA_21h(), DetermineCat1Constant(val)); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2227 | break; |
| Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2228 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2229 | /* could be long or double; resolved upon use */ |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2230 | case Instruction::CONST_WIDE_16: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2231 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2232 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2233 | 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] | 2234 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2235 | break; |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2236 | } |
| 2237 | case Instruction::CONST_WIDE_32: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2238 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2239 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2240 | 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] | 2241 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2242 | break; |
| 2243 | } |
| 2244 | case Instruction::CONST_WIDE: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2245 | int64_t val = inst->VRegB_51l(); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2246 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2247 | 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] | 2248 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | case Instruction::CONST_WIDE_HIGH16: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2252 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2253 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2254 | 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] | 2255 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2256 | break; |
| 2257 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2258 | case Instruction::CONST_STRING: |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2259 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2260 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2261 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2262 | case Instruction::CONST_STRING_JUMBO: |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2263 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2264 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2265 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2266 | case Instruction::CONST_CLASS: { |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2267 | // Get type from instruction if unresolved then we need an access check |
| 2268 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2269 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2270 | // 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] | 2271 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2272 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2273 | : reg_types_.JavaLangClass()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2274 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2275 | } |
| Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2276 | case Instruction::CONST_METHOD_HANDLE: |
| 2277 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2278 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle()); |
| Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2279 | break; |
| 2280 | case Instruction::CONST_METHOD_TYPE: |
| 2281 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2282 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType()); |
| Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2283 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2284 | case Instruction::MONITOR_ENTER: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2285 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2286 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2287 | // untracked lock aliasing. |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2288 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2289 | uint32_t prev_idx = work_insn_idx_ - 1; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2290 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2291 | prev_idx--; |
| 2292 | } |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2293 | const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx); |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2294 | switch (prev_inst.Opcode()) { |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2295 | case Instruction::MOVE_OBJECT: |
| 2296 | case Instruction::MOVE_OBJECT_16: |
| 2297 | case Instruction::MOVE_OBJECT_FROM16: |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2298 | if (prev_inst.VRegB() == inst->VRegA_11x()) { |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2299 | // Redo the copy. This won't change the register types, but update the lock status |
| 2300 | // for the aliased register. |
| 2301 | work_line_->CopyRegister1(this, |
| Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2302 | prev_inst.VRegA(), |
| 2303 | prev_inst.VRegB(), |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2304 | kTypeCategoryRef); |
| 2305 | } |
| 2306 | break; |
| 2307 | |
| Alexey Grebenkin | ce75049 | 2018-05-31 23:42:20 +0300 | [diff] [blame] | 2308 | // Catch a case of register aliasing when two registers are linked to the same |
| 2309 | // java.lang.Class object via two consequent const-class instructions immediately |
| 2310 | // preceding monitor-enter called on one of those registers. |
| 2311 | case Instruction::CONST_CLASS: { |
| 2312 | // Get the second previous instruction. |
| 2313 | if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) { |
| 2314 | break; |
| 2315 | } |
| 2316 | prev_idx--; |
| 2317 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
| 2318 | prev_idx--; |
| 2319 | } |
| 2320 | const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx); |
| 2321 | |
| 2322 | // Match the pattern "const-class; const-class; monitor-enter;" |
| 2323 | if (prev2_inst.Opcode() != Instruction::CONST_CLASS) { |
| 2324 | break; |
| 2325 | } |
| 2326 | |
| 2327 | // Ensure both const-classes are called for the same type_idx. |
| 2328 | if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) { |
| 2329 | break; |
| 2330 | } |
| 2331 | |
| 2332 | // Update the lock status for the aliased register. |
| 2333 | if (prev_inst.VRegA() == inst->VRegA_11x()) { |
| 2334 | work_line_->CopyRegister1(this, |
| 2335 | prev2_inst.VRegA(), |
| 2336 | inst->VRegA_11x(), |
| 2337 | kTypeCategoryRef); |
| 2338 | } else if (prev2_inst.VRegA() == inst->VRegA_11x()) { |
| 2339 | work_line_->CopyRegister1(this, |
| 2340 | prev_inst.VRegA(), |
| 2341 | inst->VRegA_11x(), |
| 2342 | kTypeCategoryRef); |
| 2343 | } |
| 2344 | break; |
| 2345 | } |
| 2346 | |
| Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2347 | default: // Other instruction types ignored. |
| 2348 | break; |
| 2349 | } |
| 2350 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2351 | break; |
| 2352 | case Instruction::MONITOR_EXIT: |
| 2353 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2354 | * monitor-exit instructions are odd. They can throw exceptions, |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2355 | * 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] | 2356 | * pointing to the following instruction. (This behavior goes back |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2357 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2358 | * feature that Dalvik doesn't support.) |
| 2359 | * |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2360 | * In practice we don't need to worry about this. The only |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2361 | * exceptions that can be thrown from monitor-exit are for a |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2362 | * null reference and -exit without a matching -enter. If the |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2363 | * structured locking checks are working, the former would have |
| 2364 | * failed on the -enter instruction, and the latter is impossible. |
| 2365 | * |
| 2366 | * This is fortunate, because issue 3221411 prevents us from |
| 2367 | * chasing the "can throw" path when monitor verification is |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2368 | * enabled. If we can fully verify the locking we can ignore |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2369 | * some catch blocks (which will show up as "dead" code when |
| 2370 | * we skip them here); if we can't, then the code path could be |
| 2371 | * "live" so we still need to check it. |
| 2372 | */ |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2373 | opcode_flags &= ~Instruction::kThrow; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2374 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2375 | break; |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2376 | case Instruction::CHECK_CAST: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2377 | case Instruction::INSTANCE_OF: { |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2378 | /* |
| 2379 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2380 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2381 | * |
| 2382 | * 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] | 2383 | * dec_insn.vA when branching to a handler. |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2384 | */ |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2385 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2386 | 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] | 2387 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2388 | if (res_type.IsConflict()) { |
| Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2389 | // If this is a primitive type, fail HARD. |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 2390 | ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType( |
| Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2391 | type_idx, dex_cache_.Get(), class_loader_.Get()); |
| Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2392 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2393 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2394 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2395 | << GetDeclaringClass(); |
| 2396 | break; |
| 2397 | } |
| 2398 | |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2399 | DCHECK_NE(failures_.size(), 0U); |
| 2400 | if (!is_checkcast) { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2401 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2402 | inst->VRegA_22c(), |
| 2403 | reg_types_.Boolean()); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2404 | } |
| 2405 | break; // bad class |
| Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2406 | } |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2407 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2408 | 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] | 2409 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2410 | if (!res_type.IsNonZeroReferenceTypes()) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2411 | if (is_checkcast) { |
| 2412 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2413 | } else { |
| 2414 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2415 | } |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2416 | } else if (!orig_type.IsReferenceTypes()) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2417 | if (is_checkcast) { |
| 2418 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2419 | } else { |
| 2420 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2421 | } |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2422 | } else if (orig_type.IsUninitializedTypes()) { |
| 2423 | if (is_checkcast) { |
| 2424 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2425 | << orig_type_reg; |
| 2426 | } else { |
| 2427 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2428 | << orig_type_reg; |
| 2429 | } |
| jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2430 | } else { |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2431 | if (is_checkcast) { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2432 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2433 | } else { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2434 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2435 | inst->VRegA_22c(), |
| 2436 | reg_types_.Boolean()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2437 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2438 | } |
| jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2439 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2440 | } |
| 2441 | case Instruction::ARRAY_LENGTH: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2442 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2443 | if (res_type.IsReferenceTypes()) { |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2444 | if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) { |
| 2445 | // ie not an array or null |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2446 | 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] | 2447 | } else { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2448 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2449 | inst->VRegA_12x(), |
| 2450 | reg_types_.Integer()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2451 | } |
| Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2452 | } else { |
| 2453 | 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] | 2454 | } |
| 2455 | break; |
| 2456 | } |
| 2457 | case Instruction::NEW_INSTANCE: { |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2458 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2459 | if (res_type.IsConflict()) { |
| 2460 | DCHECK_NE(failures_.size(), 0U); |
| 2461 | break; // bad class |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2462 | } |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2463 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2464 | // can't create an instance of an interface or abstract class */ |
| 2465 | if (!res_type.IsInstantiableTypes()) { |
| 2466 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2467 | << "new-instance on primitive, interface or abstract class" << res_type; |
| Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2468 | // Soft failure so carry on to set register type. |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2469 | } |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2470 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2471 | // Any registers holding previous allocations from this address that have not yet been |
| 2472 | // initialized must be marked invalid. |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2473 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
| Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2474 | // add the new uninitialized reference to the register state |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2475 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2476 | break; |
| 2477 | } |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2478 | case Instruction::NEW_ARRAY: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2479 | VerifyNewArray(inst, false, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2480 | break; |
| 2481 | case Instruction::FILLED_NEW_ARRAY: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2482 | VerifyNewArray(inst, true, false); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2483 | just_set_result = true; // Filled new array sets result register |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2484 | break; |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2485 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2486 | VerifyNewArray(inst, true, true); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2487 | just_set_result = true; // Filled new array range sets result register |
| 2488 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2489 | case Instruction::CMPL_FLOAT: |
| 2490 | case Instruction::CMPG_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2491 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2492 | break; |
| 2493 | } |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2494 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2497 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2498 | break; |
| 2499 | case Instruction::CMPL_DOUBLE: |
| 2500 | case Instruction::CMPG_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2501 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2502 | reg_types_.DoubleHi())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2503 | break; |
| 2504 | } |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2505 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2506 | reg_types_.DoubleHi())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2507 | break; |
| 2508 | } |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2509 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2510 | break; |
| 2511 | case Instruction::CMP_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2512 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2513 | reg_types_.LongHi())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2514 | break; |
| 2515 | } |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2516 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2517 | reg_types_.LongHi())) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2518 | break; |
| 2519 | } |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2520 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2521 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2522 | case Instruction::THROW: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2523 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2524 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2525 | if (res_type.IsUninitializedTypes()) { |
| 2526 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
| Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2527 | } else if (!res_type.IsReferenceTypes()) { |
| 2528 | 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] | 2529 | } else { |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 2530 | Fail(res_type.IsUnresolvedTypes() |
| 2531 | ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK : VERIFY_ERROR_BAD_CLASS_SOFT) |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2532 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2533 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2534 | } |
| 2535 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2536 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2537 | case Instruction::GOTO: |
| 2538 | case Instruction::GOTO_16: |
| 2539 | case Instruction::GOTO_32: |
| 2540 | /* no effect on or use of registers */ |
| 2541 | break; |
| 2542 | |
| 2543 | case Instruction::PACKED_SWITCH: |
| 2544 | case Instruction::SPARSE_SWITCH: |
| 2545 | /* verify that vAA is an integer, or can be converted to one */ |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2546 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2547 | break; |
| 2548 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2549 | case Instruction::FILL_ARRAY_DATA: { |
| 2550 | /* Similar to the verification done for APUT */ |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2551 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2552 | /* array_type can be null if the reg type is Zero */ |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2553 | if (!array_type.IsZeroOrNull()) { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2554 | if (!array_type.IsArrayTypes()) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2555 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2556 | << array_type; |
| Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2557 | } else if (array_type.IsUnresolvedTypes()) { |
| 2558 | // If it's an unresolved array type, it must be non-primitive. |
| 2559 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2560 | << array_type; |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2561 | } else { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2562 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 2563 | class_loader_.Get()); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2564 | DCHECK(!component_type.IsConflict()); |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2565 | if (component_type.IsNonZeroReferenceTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2566 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2567 | << component_type; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2568 | } else { |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2569 | // Now verify if the element width in the table matches the element width declared in |
| 2570 | // the array |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2571 | const uint16_t* array_data = |
| 2572 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2573 | if (array_data[0] != Instruction::kArrayDataSignature) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2574 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2575 | } else { |
| 2576 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2577 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2578 | // in the table and expected from the array class. |
| 2579 | if (array_data[1] != elem_width) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2580 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2581 | << " vs " << elem_width << ")"; |
| jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2582 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2583 | } |
| 2584 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2588 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2589 | case Instruction::IF_EQ: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2590 | case Instruction::IF_NE: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2591 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2592 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2593 | bool mismatch = false; |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2594 | if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2595 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2596 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2597 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2598 | } else { // both integral? |
| 2599 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2600 | } |
| 2601 | if (mismatch) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2602 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2603 | << reg_type2 << ") must both be references or integral"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2604 | } |
| 2605 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2606 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2607 | case Instruction::IF_LT: |
| 2608 | case Instruction::IF_GE: |
| 2609 | case Instruction::IF_GT: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2610 | case Instruction::IF_LE: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2611 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2612 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2613 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2614 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2615 | << reg_type2 << ") must be integral"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2616 | } |
| 2617 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2618 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2619 | case Instruction::IF_EQZ: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2620 | case Instruction::IF_NEZ: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2621 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2622 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
| Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2623 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2624 | << " unexpected as arg to if-eqz/if-nez"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2625 | } |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2626 | |
| 2627 | // Find previous instruction - its existence is a precondition to peephole optimization. |
| Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2628 | if (UNLIKELY(0 == work_insn_idx_)) { |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2629 | break; |
| 2630 | } |
| Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2631 | uint32_t instance_of_idx = work_insn_idx_ - 1; |
| 2632 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
| 2633 | instance_of_idx--; |
| 2634 | } |
| 2635 | // Dex index 0 must be an opcode. |
| 2636 | DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode()); |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2637 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2638 | const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx); |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2639 | |
| 2640 | /* Check for peep-hole pattern of: |
| 2641 | * ...; |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2642 | * instance-of vX, vY, T; |
| 2643 | * ifXXX vX, label ; |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2644 | * ...; |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2645 | * label: |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2646 | * ...; |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2647 | * and sharpen the type of vY to be type T. |
| 2648 | * Note, this pattern can't be if: |
| 2649 | * - if there are other branches to this branch, |
| 2650 | * - when vX == vY. |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2651 | */ |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2652 | if (!CurrentInsnFlags()->IsBranchTarget() && |
| Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2653 | (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) && |
| 2654 | (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) && |
| 2655 | (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) { |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2656 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2657 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2658 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2659 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2660 | // and we don't want to disallow field and other operations on the object. If the value |
| 2661 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2662 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2663 | // type is assignable to the original then allow optimization. This check is performed to |
| 2664 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2665 | // interface from a class that would lose information relevant to field checks. |
| Andreas Gampe | d78122b | 2019-07-10 14:06:53 -0700 | [diff] [blame] | 2666 | // |
| 2667 | // Note: do not do an access check. This may mark this with a runtime throw that actually |
| 2668 | // 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] | 2669 | 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] | 2670 | const RegType& cast_type = ResolveClass<CheckAccess::kNo>( |
| Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2671 | dex::TypeIndex(instance_of_inst.VRegC_22c())); |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2672 | |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2673 | if (!orig_type.Equals(cast_type) && |
| 2674 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
| Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2675 | 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] | 2676 | !cast_type.GetClass()->IsInterface() && |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2677 | (orig_type.IsZeroOrNull() || |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2678 | orig_type.IsStrictlyAssignableFrom( |
| 2679 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2680 | RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(), |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 2681 | allocator_, |
| 2682 | GetRegTypeCache()); |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2683 | if (inst->Opcode() == Instruction::IF_EQZ) { |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2684 | fallthrough_line.reset(update_line); |
| Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2685 | } else { |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2686 | branch_line.reset(update_line); |
| 2687 | } |
| 2688 | update_line->CopyFromLine(work_line_.get()); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2689 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2690 | instance_of_inst.VRegB_22c(), |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2691 | cast_type); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2692 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2693 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2694 | // register encoding space of instance-of, and propagate type information to the source |
| 2695 | // of the move-object. |
| Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2696 | // Note: this is only valid if the move source was not clobbered. |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2697 | uint32_t move_idx = instance_of_idx - 1; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2698 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2699 | move_idx--; |
| 2700 | } |
| Andreas Gampe | 147a911 | 2019-07-31 16:17:10 -0700 | [diff] [blame] | 2701 | DCHECK(GetInstructionFlags(move_idx).IsOpcode()); |
| Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2702 | auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type]( |
| 2703 | uint16_t move_src, |
| 2704 | uint16_t move_trg) |
| 2705 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2706 | if (move_trg == instance_of_inst.VRegB_22c() && |
| 2707 | move_src != instance_of_inst.VRegA_22c()) { |
| 2708 | update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type); |
| 2709 | } |
| 2710 | }; |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2711 | const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx); |
| Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2712 | switch (move_inst.Opcode()) { |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2713 | case Instruction::MOVE_OBJECT: |
| Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2714 | maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x()); |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2715 | break; |
| 2716 | case Instruction::MOVE_OBJECT_FROM16: |
| Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2717 | maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x()); |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2718 | break; |
| 2719 | case Instruction::MOVE_OBJECT_16: |
| Andreas Gampe | 43e4325 | 2019-01-08 12:06:57 -0800 | [diff] [blame] | 2720 | maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x()); |
| Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2721 | break; |
| 2722 | default: |
| 2723 | break; |
| 2724 | } |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2729 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2730 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2731 | case Instruction::IF_LTZ: |
| 2732 | case Instruction::IF_GEZ: |
| 2733 | case Instruction::IF_GTZ: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2734 | case Instruction::IF_LEZ: { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2735 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2736 | if (!reg_type.IsIntegralTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2737 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2738 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2739 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2740 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2741 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2742 | case Instruction::AGET_BOOLEAN: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2743 | VerifyAGet(inst, reg_types_.Boolean(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2744 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2745 | case Instruction::AGET_BYTE: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2746 | VerifyAGet(inst, reg_types_.Byte(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2747 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2748 | case Instruction::AGET_CHAR: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2749 | VerifyAGet(inst, reg_types_.Char(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2750 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2751 | case Instruction::AGET_SHORT: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2752 | VerifyAGet(inst, reg_types_.Short(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2753 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2754 | case Instruction::AGET: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2755 | VerifyAGet(inst, reg_types_.Integer(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2756 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2757 | case Instruction::AGET_WIDE: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2758 | VerifyAGet(inst, reg_types_.LongLo(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2759 | break; |
| 2760 | case Instruction::AGET_OBJECT: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2761 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2762 | break; |
| 2763 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2764 | case Instruction::APUT_BOOLEAN: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2765 | VerifyAPut(inst, reg_types_.Boolean(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2766 | break; |
| 2767 | case Instruction::APUT_BYTE: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2768 | VerifyAPut(inst, reg_types_.Byte(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2769 | break; |
| 2770 | case Instruction::APUT_CHAR: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2771 | VerifyAPut(inst, reg_types_.Char(), true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2772 | break; |
| 2773 | case Instruction::APUT_SHORT: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2774 | VerifyAPut(inst, reg_types_.Short(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2775 | break; |
| 2776 | case Instruction::APUT: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2777 | VerifyAPut(inst, reg_types_.Integer(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2778 | break; |
| 2779 | case Instruction::APUT_WIDE: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2780 | VerifyAPut(inst, reg_types_.LongLo(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2781 | break; |
| 2782 | case Instruction::APUT_OBJECT: |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2783 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2784 | break; |
| 2785 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2786 | case Instruction::IGET_BOOLEAN: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2787 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2788 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2789 | case Instruction::IGET_BYTE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2790 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2791 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2792 | case Instruction::IGET_CHAR: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2793 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2794 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2795 | case Instruction::IGET_SHORT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2796 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2797 | break; |
| 2798 | case Instruction::IGET: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2799 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2800 | break; |
| 2801 | case Instruction::IGET_WIDE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2802 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2803 | break; |
| 2804 | case Instruction::IGET_OBJECT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2805 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2806 | false); |
| 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 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2809 | case Instruction::IPUT_BOOLEAN: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2810 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2811 | break; |
| 2812 | case Instruction::IPUT_BYTE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2813 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2814 | break; |
| 2815 | case Instruction::IPUT_CHAR: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2816 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case Instruction::IPUT_SHORT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2819 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2820 | break; |
| 2821 | case Instruction::IPUT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2822 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | case Instruction::IPUT_WIDE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2825 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2826 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2827 | case Instruction::IPUT_OBJECT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2828 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2829 | false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2830 | break; |
| 2831 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2832 | case Instruction::SGET_BOOLEAN: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2833 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2834 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2835 | case Instruction::SGET_BYTE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2836 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2837 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2838 | case Instruction::SGET_CHAR: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2839 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
| 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::SGET_SHORT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2842 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2843 | break; |
| 2844 | case Instruction::SGET: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2845 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2846 | break; |
| 2847 | case Instruction::SGET_WIDE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2848 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2849 | break; |
| 2850 | case Instruction::SGET_OBJECT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2851 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2852 | true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | |
| 2855 | case Instruction::SPUT_BOOLEAN: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2856 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2857 | break; |
| 2858 | case Instruction::SPUT_BYTE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2859 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2860 | break; |
| 2861 | case Instruction::SPUT_CHAR: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2862 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2863 | break; |
| 2864 | case Instruction::SPUT_SHORT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2865 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2866 | break; |
| 2867 | case Instruction::SPUT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2868 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2869 | break; |
| 2870 | case Instruction::SPUT_WIDE: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2871 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2872 | break; |
| 2873 | case Instruction::SPUT_OBJECT: |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2874 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2875 | true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2876 | break; |
| 2877 | |
| 2878 | case Instruction::INVOKE_VIRTUAL: |
| 2879 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2880 | case Instruction::INVOKE_SUPER: |
| David Srbecky | c5cd589 | 2021-03-23 08:09:15 +0000 | [diff] [blame] | 2881 | case Instruction::INVOKE_SUPER_RANGE: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2882 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| David Srbecky | c5cd589 | 2021-03-23 08:09:15 +0000 | [diff] [blame] | 2883 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2884 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2885 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2886 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2887 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2888 | const RegType* return_type = nullptr; |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2889 | if (called_method != nullptr) { |
| Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2890 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2891 | ? called_method->ResolveReturnType() |
| 2892 | : called_method->LookupResolvedReturnType(); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2893 | if (return_type_class != nullptr) { |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2894 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
| Vladimir Marko | 179b7c6 | 2019-03-22 13:38:57 +0000 | [diff] [blame] | 2895 | return_type_class, |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2896 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2897 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2898 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2899 | self_->ClearException(); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | if (return_type == nullptr) { |
| Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2903 | uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2904 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2905 | dex::TypeIndex return_type_idx = |
| 2906 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2907 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2908 | return_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2909 | } |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2910 | if (!return_type->IsLowHalf()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2911 | work_line_->SetResultRegisterType(this, *return_type); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2912 | } else { |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2913 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2914 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2915 | just_set_result = true; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2916 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2917 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2918 | case Instruction::INVOKE_DIRECT: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2919 | case Instruction::INVOKE_DIRECT_RANGE: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2920 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2921 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
| Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2922 | const char* return_type_descriptor; |
| 2923 | bool is_constructor; |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2924 | const RegType* return_type = nullptr; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2925 | if (called_method == nullptr) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2926 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2927 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2928 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2929 | dex::TypeIndex return_type_idx = |
| 2930 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2931 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2932 | } else { |
| 2933 | is_constructor = called_method->IsConstructor(); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2934 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
| Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2935 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2936 | ? called_method->ResolveReturnType() |
| 2937 | : called_method->LookupResolvedReturnType(); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2938 | if (return_type_class != nullptr) { |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2939 | return_type = &FromClass(return_type_descriptor, |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2940 | return_type_class, |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2941 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2942 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2943 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2944 | self_->ClearException(); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2945 | } |
| Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2946 | } |
| 2947 | if (is_constructor) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2948 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2949 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2950 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2951 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2952 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2953 | * this method (which implies that we're in a constructor ourselves). |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2954 | */ |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 2955 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2956 | if (this_type.IsConflict()) // failure. |
| 2957 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2958 | |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2959 | /* no null refs allowed (?) */ |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2960 | if (this_type.IsZeroOrNull()) { |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2961 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2962 | break; |
| jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2963 | } |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2964 | |
| 2965 | /* must be in same class or in superclass */ |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2966 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
| Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2967 | // TODO: re-enable constructor type verification |
| 2968 | // if (this_super_klass.IsConflict()) { |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2969 | // Unknown super class, fail so we re-check at runtime. |
| Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2970 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2971 | // break; |
| 2972 | // } |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2973 | |
| 2974 | /* arg must be an uninitialized reference */ |
| 2975 | if (!this_type.IsUninitializedTypes()) { |
| 2976 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2977 | << this_type; |
| 2978 | break; |
| 2979 | } |
| 2980 | |
| 2981 | /* |
| 2982 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2983 | * registers that have the same object instance in them, not just the "this" register. |
| 2984 | */ |
| Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 2985 | work_line_->MarkRefsAsInitialized(this, this_type); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2986 | } |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2987 | if (return_type == nullptr) { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 2988 | return_type = ®_types_.FromDescriptor(class_loader_.Get(), |
| 2989 | return_type_descriptor, |
| 2990 | false); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2991 | } |
| 2992 | if (!return_type->IsLowHalf()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2993 | work_line_->SetResultRegisterType(this, *return_type); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2994 | } else { |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2995 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2996 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2997 | just_set_result = true; |
| 2998 | break; |
| 2999 | } |
| 3000 | case Instruction::INVOKE_STATIC: |
| 3001 | case Instruction::INVOKE_STATIC_RANGE: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3002 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3003 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3004 | const char* descriptor; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3005 | if (called_method == nullptr) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3006 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3007 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3008 | dex::TypeIndex return_type_idx = |
| 3009 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3010 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3011 | } else { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3012 | descriptor = called_method->GetReturnTypeDescriptor(); |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3013 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3014 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 3015 | descriptor, |
| 3016 | false); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3017 | if (!return_type.IsLowHalf()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3018 | work_line_->SetResultRegisterType(this, return_type); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3019 | } else { |
| 3020 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3021 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3022 | just_set_result = true; |
| 3023 | } |
| 3024 | break; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3025 | case Instruction::INVOKE_INTERFACE: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3026 | case Instruction::INVOKE_INTERFACE_RANGE: { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3027 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3028 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3029 | if (abs_method != nullptr) { |
| Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3030 | ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass(); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3031 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3032 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3033 | << abs_method->PrettyMethod() << "'"; |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3034 | break; |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3035 | } |
| Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3036 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3037 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3038 | * interface or Object (see comments in RegType::JoinClass). |
| 3039 | */ |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3040 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3041 | if (this_type.IsZeroOrNull()) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3042 | /* null pointer always passes (and always fails at runtime) */ |
| 3043 | } else { |
| 3044 | if (this_type.IsUninitializedTypes()) { |
| 3045 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3046 | << this_type; |
| 3047 | break; |
| 3048 | } |
| 3049 | // In the past we have tried to assert that "called_interface" is assignable |
| 3050 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3051 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3052 | // implemented by "this_type". For example, two classes may implement the same |
| 3053 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3054 | // join will set "this_type" to the parent class and a test that this implements |
| 3055 | // the interface will incorrectly fail. |
| 3056 | } |
| 3057 | /* |
| 3058 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3059 | * the type information is in the abstract method, so we're good. |
| 3060 | */ |
| 3061 | const char* descriptor; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3062 | if (abs_method == nullptr) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3063 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3064 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3065 | dex::TypeIndex return_type_idx = |
| 3066 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3067 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3068 | } else { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3069 | descriptor = abs_method->GetReturnTypeDescriptor(); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3070 | } |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3071 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 3072 | descriptor, |
| 3073 | false); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3074 | if (!return_type.IsLowHalf()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3075 | work_line_->SetResultRegisterType(this, return_type); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3076 | } else { |
| 3077 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3078 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3079 | just_set_result = true; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3080 | break; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3081 | } |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3082 | case Instruction::INVOKE_POLYMORPHIC: |
| 3083 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3084 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3085 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3086 | if (called_method == nullptr) { |
| 3087 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3088 | if (failure_messages_.size() > 0) { |
| 3089 | std::string message = failure_messages_.back()->str(); |
| 3090 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3091 | } else { |
| 3092 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3093 | } |
| 3094 | break; |
| 3095 | } |
| 3096 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3097 | !CheckSignaturePolymorphicReceiver(inst)) { |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 3098 | DCHECK(HasFailures()); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3099 | break; |
| 3100 | } |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 3101 | const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 3102 | const dex::ProtoIndex proto_idx(vRegH); |
| Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3103 | const char* return_descriptor = |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3104 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3105 | const RegType& return_type = |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3106 | reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3107 | if (!return_type.IsLowHalf()) { |
| 3108 | work_line_->SetResultRegisterType(this, return_type); |
| 3109 | } else { |
| 3110 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3111 | } |
| Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3112 | just_set_result = true; |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3113 | break; |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3114 | } |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3115 | case Instruction::INVOKE_CUSTOM: |
| 3116 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3117 | // Verify registers based on method_type in the call site. |
| 3118 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3119 | |
| 3120 | // Step 1. Check the call site that produces the method handle for invocation |
| 3121 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3122 | if (!CheckCallSite(call_site_idx)) { |
| 3123 | DCHECK(HasFailures()); |
| 3124 | break; |
| 3125 | } |
| 3126 | |
| 3127 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3128 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3129 | // the first three arguments and CheckCallSite has checked the method handle type. |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 3130 | const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3131 | const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3132 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3133 | // Treat method as static as it has yet to be determined. |
| 3134 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3135 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3136 | |
| 3137 | // Step 3. Propagate return type information |
| 3138 | const RegType& return_type = |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3139 | reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3140 | if (!return_type.IsLowHalf()) { |
| 3141 | work_line_->SetResultRegisterType(this, return_type); |
| 3142 | } else { |
| 3143 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3144 | } |
| 3145 | just_set_result = true; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3146 | break; |
| 3147 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3148 | case Instruction::NEG_INT: |
| 3149 | case Instruction::NOT_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3150 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3151 | break; |
| 3152 | case Instruction::NEG_LONG: |
| 3153 | case Instruction::NOT_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3154 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3155 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3156 | break; |
| 3157 | case Instruction::NEG_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3158 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3159 | break; |
| 3160 | case Instruction::NEG_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3161 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3162 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3163 | break; |
| 3164 | case Instruction::INT_TO_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3165 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3166 | reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3167 | break; |
| 3168 | case Instruction::INT_TO_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3169 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3170 | break; |
| 3171 | case Instruction::INT_TO_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3172 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3173 | reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3174 | break; |
| 3175 | case Instruction::LONG_TO_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3176 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3177 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3178 | break; |
| 3179 | case Instruction::LONG_TO_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3180 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3181 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3182 | break; |
| 3183 | case Instruction::LONG_TO_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3184 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3185 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3186 | break; |
| 3187 | case Instruction::FLOAT_TO_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3188 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3189 | break; |
| 3190 | case Instruction::FLOAT_TO_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3191 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3192 | reg_types_.Float()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3193 | break; |
| 3194 | case Instruction::FLOAT_TO_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3195 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3196 | reg_types_.Float()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3197 | break; |
| 3198 | case Instruction::DOUBLE_TO_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3199 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3200 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3201 | break; |
| 3202 | case Instruction::DOUBLE_TO_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3203 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3204 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3205 | break; |
| 3206 | case Instruction::DOUBLE_TO_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3207 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3208 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3209 | break; |
| 3210 | case Instruction::INT_TO_BYTE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3211 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3212 | break; |
| 3213 | case Instruction::INT_TO_CHAR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3214 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3215 | break; |
| 3216 | case Instruction::INT_TO_SHORT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3217 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3218 | break; |
| 3219 | |
| 3220 | case Instruction::ADD_INT: |
| 3221 | case Instruction::SUB_INT: |
| 3222 | case Instruction::MUL_INT: |
| 3223 | case Instruction::REM_INT: |
| 3224 | case Instruction::DIV_INT: |
| 3225 | case Instruction::SHL_INT: |
| 3226 | case Instruction::SHR_INT: |
| 3227 | case Instruction::USHR_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3228 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3229 | reg_types_.Integer(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3230 | break; |
| 3231 | case Instruction::AND_INT: |
| 3232 | case Instruction::OR_INT: |
| 3233 | case Instruction::XOR_INT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3234 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3235 | reg_types_.Integer(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3236 | break; |
| 3237 | case Instruction::ADD_LONG: |
| 3238 | case Instruction::SUB_LONG: |
| 3239 | case Instruction::MUL_LONG: |
| 3240 | case Instruction::DIV_LONG: |
| 3241 | case Instruction::REM_LONG: |
| 3242 | case Instruction::AND_LONG: |
| 3243 | case Instruction::OR_LONG: |
| 3244 | case Instruction::XOR_LONG: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3245 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3246 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3247 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3248 | break; |
| 3249 | case Instruction::SHL_LONG: |
| 3250 | case Instruction::SHR_LONG: |
| 3251 | case Instruction::USHR_LONG: |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3252 | /* shift distance is Int, making these different from other binary operations */ |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3253 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3254 | reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3255 | break; |
| 3256 | case Instruction::ADD_FLOAT: |
| 3257 | case Instruction::SUB_FLOAT: |
| 3258 | case Instruction::MUL_FLOAT: |
| 3259 | case Instruction::DIV_FLOAT: |
| 3260 | case Instruction::REM_FLOAT: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3261 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3262 | reg_types_.Float(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3263 | break; |
| 3264 | case Instruction::ADD_DOUBLE: |
| 3265 | case Instruction::SUB_DOUBLE: |
| 3266 | case Instruction::MUL_DOUBLE: |
| 3267 | case Instruction::DIV_DOUBLE: |
| 3268 | case Instruction::REM_DOUBLE: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3269 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3270 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3271 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3272 | break; |
| 3273 | case Instruction::ADD_INT_2ADDR: |
| 3274 | case Instruction::SUB_INT_2ADDR: |
| 3275 | case Instruction::MUL_INT_2ADDR: |
| 3276 | case Instruction::REM_INT_2ADDR: |
| 3277 | case Instruction::SHL_INT_2ADDR: |
| 3278 | case Instruction::SHR_INT_2ADDR: |
| 3279 | case Instruction::USHR_INT_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3280 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3281 | reg_types_.Integer(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3282 | break; |
| 3283 | case Instruction::AND_INT_2ADDR: |
| 3284 | case Instruction::OR_INT_2ADDR: |
| 3285 | case Instruction::XOR_INT_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3286 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3287 | reg_types_.Integer(), true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3288 | break; |
| 3289 | case Instruction::DIV_INT_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3290 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3291 | reg_types_.Integer(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3292 | break; |
| 3293 | case Instruction::ADD_LONG_2ADDR: |
| 3294 | case Instruction::SUB_LONG_2ADDR: |
| 3295 | case Instruction::MUL_LONG_2ADDR: |
| 3296 | case Instruction::DIV_LONG_2ADDR: |
| 3297 | case Instruction::REM_LONG_2ADDR: |
| 3298 | case Instruction::AND_LONG_2ADDR: |
| 3299 | case Instruction::OR_LONG_2ADDR: |
| 3300 | case Instruction::XOR_LONG_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3301 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3302 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3303 | reg_types_.LongLo(), reg_types_.LongHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3304 | break; |
| 3305 | case Instruction::SHL_LONG_2ADDR: |
| 3306 | case Instruction::SHR_LONG_2ADDR: |
| 3307 | case Instruction::USHR_LONG_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3308 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3309 | reg_types_.Integer()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3310 | break; |
| 3311 | case Instruction::ADD_FLOAT_2ADDR: |
| 3312 | case Instruction::SUB_FLOAT_2ADDR: |
| 3313 | case Instruction::MUL_FLOAT_2ADDR: |
| 3314 | case Instruction::DIV_FLOAT_2ADDR: |
| 3315 | case Instruction::REM_FLOAT_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3316 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3317 | reg_types_.Float(), false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3318 | break; |
| 3319 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3320 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3321 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3322 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3323 | case Instruction::REM_DOUBLE_2ADDR: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3324 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3325 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3326 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3327 | break; |
| 3328 | case Instruction::ADD_INT_LIT16: |
| Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3329 | case Instruction::RSUB_INT_LIT16: |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3330 | case Instruction::MUL_INT_LIT16: |
| 3331 | case Instruction::DIV_INT_LIT16: |
| 3332 | case Instruction::REM_INT_LIT16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3333 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3334 | true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3335 | break; |
| 3336 | case Instruction::AND_INT_LIT16: |
| 3337 | case Instruction::OR_INT_LIT16: |
| 3338 | case Instruction::XOR_INT_LIT16: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3339 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3340 | true); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3341 | break; |
| 3342 | case Instruction::ADD_INT_LIT8: |
| 3343 | case Instruction::RSUB_INT_LIT8: |
| 3344 | case Instruction::MUL_INT_LIT8: |
| 3345 | case Instruction::DIV_INT_LIT8: |
| 3346 | case Instruction::REM_INT_LIT8: |
| 3347 | case Instruction::SHL_INT_LIT8: |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3348 | case Instruction::SHR_INT_LIT8: |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3349 | case Instruction::USHR_INT_LIT8: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3350 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3351 | false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3352 | break; |
| 3353 | case Instruction::AND_INT_LIT8: |
| 3354 | case Instruction::OR_INT_LIT8: |
| 3355 | case Instruction::XOR_INT_LIT8: |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3356 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3357 | false); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3358 | break; |
| 3359 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3360 | /* These should never appear during verification. */ |
| Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3361 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
| David Srbecky | c5cd589 | 2021-03-23 08:09:15 +0000 | [diff] [blame] | 3362 | case Instruction::UNUSED_E3 ... Instruction::UNUSED_F9: |
| David Srbecky | 61c6242 | 2021-04-27 16:22:48 +0100 | [diff] [blame] | 3363 | case Instruction::UNUSED_73: |
| Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3364 | case Instruction::UNUSED_79: |
| 3365 | case Instruction::UNUSED_7A: |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3366 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3367 | break; |
| 3368 | |
| 3369 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3370 | * DO NOT add a "default" clause here. Without it the compiler will |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3371 | * complain if an instruction is missing (which is desirable). |
| 3372 | */ |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3373 | } // end - switch (dec_insn.opcode) |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3374 | |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3375 | if (flags_.have_pending_hard_failure_) { |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 3376 | if (IsAotMode()) { |
| Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3377 | /* When AOT compiling, check that the last failure is a hard failure */ |
| Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3378 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3379 | LOG(ERROR) << "Pending failures:"; |
| 3380 | for (auto& error : failures_) { |
| 3381 | LOG(ERROR) << error; |
| 3382 | } |
| 3383 | for (auto& error_msg : failure_messages_) { |
| 3384 | LOG(ERROR) << error_msg->str(); |
| 3385 | } |
| 3386 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3387 | } |
| Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3388 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3389 | /* immediate failure, reject class */ |
| 3390 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3391 | return false; |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3392 | } else if (flags_.have_pending_runtime_throw_failure_) { |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3393 | LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw"; |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3394 | /* checking interpreter will throw, mark following code as unreachable */ |
| jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3395 | opcode_flags = Instruction::kThrow; |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3396 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3397 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3398 | // very end. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3399 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3400 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3401 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3402 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3403 | * not expensive and it makes our debugging output cleaner.) |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3404 | */ |
| 3405 | if (!just_set_result) { |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 3406 | work_line_->SetResultTypeToUnknown(GetRegTypeCache()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3409 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3410 | * Handle "branch". Tag the branch target. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3411 | * |
| 3412 | * NOTE: instructions like Instruction::EQZ provide information about the |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3413 | * 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] | 3414 | * somebody could get a reference field, check it for zero, and if the |
| 3415 | * branch is taken immediately store that register in a boolean field |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3416 | * 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] | 3417 | * that, and will reject the code. |
| 3418 | * |
| 3419 | * TODO: avoid re-fetching the branch target |
| 3420 | */ |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3421 | if ((opcode_flags & Instruction::kBranch) != 0) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3422 | bool isConditional, selfOkay; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3423 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3424 | /* should never happen after static verification */ |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3425 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3426 | return false; |
| 3427 | } |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3428 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3429 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), |
| 3430 | work_insn_idx_ + branch_target)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3431 | return false; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3432 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3433 | /* update branch target, set "changed" if appropriate */ |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3434 | if (nullptr != branch_line) { |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3435 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3436 | return false; |
| 3437 | } |
| 3438 | } else { |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3439 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3440 | return false; |
| 3441 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3442 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3446 | * Handle "switch". Tag all possible branch targets. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3447 | * |
| 3448 | * We've already verified that the table is structurally sound, so we |
| 3449 | * just need to walk through and tag the targets. |
| 3450 | */ |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3451 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3452 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3453 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3454 | int switch_count = switch_insns[1]; |
| 3455 | int offset_to_targets, targ; |
| 3456 | |
| 3457 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3458 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3459 | offset_to_targets = 4; |
| 3460 | } else { |
| 3461 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
| Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3462 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3463 | offset_to_targets = 2 + 2 * switch_count; |
| 3464 | } |
| 3465 | |
| 3466 | /* verify each switch target */ |
| 3467 | for (targ = 0; targ < switch_count; targ++) { |
| 3468 | int offset; |
| 3469 | uint32_t abs_offset; |
| 3470 | |
| 3471 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3472 | offset = switch_insns[offset_to_targets + targ * 2] | |
| Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3473 | (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] | 3474 | abs_offset = work_insn_idx_ + offset; |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3475 | DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits()); |
| 3476 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3477 | return false; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3478 | } |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3479 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3480 | return false; |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3481 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3486 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3487 | * "try" block when they throw, control transfers out of the method.) |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3488 | */ |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3489 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3490 | bool has_catch_all_handler = false; |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3491 | const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3492 | CHECK(try_item != nullptr); |
| Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 3493 | CatchHandlerIterator iterator(code_item_accessor_, *try_item); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3494 | |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3495 | // 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] | 3496 | ClassLinker* linker = GetClassLinker(); |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3497 | |
| Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3498 | for (; iterator.HasNext(); iterator.Next()) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3499 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3500 | if (!handler_type_idx.IsValid()) { |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3501 | has_catch_all_handler = true; |
| 3502 | } else { |
| 3503 | // It is also a catch-all if it is java.lang.Throwable. |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3504 | ObjPtr<mirror::Class> klass = |
| Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3505 | linker->ResolveType(handler_type_idx, dex_cache_, class_loader_); |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3506 | if (klass != nullptr) { |
| Vladimir Marko | adbceb7 | 2018-05-29 14:34:14 +0100 | [diff] [blame] | 3507 | if (klass == GetClassRoot<mirror::Throwable>()) { |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3508 | has_catch_all_handler = true; |
| 3509 | } |
| 3510 | } else { |
| 3511 | // Clear exception. |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3512 | DCHECK(self_->IsExceptionPending()); |
| 3513 | self_->ClearException(); |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3514 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3515 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3516 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3517 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3518 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3519 | * modifies any registers. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3520 | */ |
| Andreas Gampe | bfcca58 | 2019-04-19 12:01:55 -0700 | [diff] [blame] | 3521 | if (kVerifierDebug) { |
| 3522 | LogVerifyInfo() << "Updating exception handler 0x" |
| 3523 | << std::hex << iterator.GetHandlerAddress(); |
| 3524 | } |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3525 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3526 | return false; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3527 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3531 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3532 | * instruction. This does apply to monitor-exit because of async exception handling. |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3533 | */ |
| Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3534 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3535 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3536 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3537 | * 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] | 3538 | * it will do so before grabbing the lock). |
| 3539 | */ |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3540 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3541 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3542 | << "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] | 3543 | return false; |
| 3544 | } |
| 3545 | } |
| 3546 | } |
| 3547 | |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3548 | /* Handle "continue". Tag the next consecutive instruction. |
| 3549 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3550 | * because it changes work_line_ when performing peephole optimization |
| 3551 | * and this change should not be used in those cases. |
| 3552 | */ |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3553 | if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3554 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3555 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3556 | if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) { |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3557 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3558 | return false; |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3559 | } |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3560 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3561 | // next instruction isn't one. |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3562 | if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) { |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3563 | return false; |
| 3564 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3565 | if (nullptr != fallthrough_line) { |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3566 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3567 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3568 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3569 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
| Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3570 | // 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] | 3571 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3572 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
| Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3573 | } |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3574 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3575 | if (next_line != nullptr) { |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3576 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3577 | // needed. If the merge changes the state of the registers then the work line will be |
| 3578 | // updated. |
| 3579 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3580 | return false; |
| 3581 | } |
| 3582 | } else { |
| 3583 | /* |
| 3584 | * We're not recording register data for the next instruction, so we don't know what the |
| 3585 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3586 | */ |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3587 | GetModifiableInstructionFlags(next_insn_idx).SetChanged(); |
| Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3588 | } |
| 3589 | } |
| Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3590 | |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3591 | /* 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] | 3592 | if ((opcode_flags & Instruction::kReturn) != 0) { |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3593 | work_line_->VerifyMonitorStackEmpty(this); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | /* |
| jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3597 | * Update start_guess. Advance to the next instruction of that's |
| 3598 | * possible, otherwise use the branch target if one was found. If |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3599 | * neither of those exists we're in a return or throw; leave start_guess |
| 3600 | * alone and let the caller sort it out. |
| 3601 | */ |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3602 | if ((opcode_flags & Instruction::kContinue) != 0) { |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3603 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3604 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
| Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3605 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3606 | /* we're still okay if branch_target is zero */ |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3607 | *start_guess = work_insn_idx_ + branch_target; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3608 | } |
| 3609 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3610 | DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits()); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3611 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3612 | |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3613 | if (flags_.have_pending_runtime_throw_failure_) { |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 3614 | Fail(VERIFY_ERROR_RUNTIME_THROW, /* pending_exc= */ false); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3615 | // Reset the pending_runtime_throw flag now. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3616 | flags_.have_pending_runtime_throw_failure_ = false; |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3619 | return true; |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3620 | } // NOLINT(readability/fn_size) |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3621 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3622 | template <bool kVerifierDebug> |
| 3623 | template <CheckAccess C> |
| 3624 | const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) { |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 3625 | ClassLinker* linker = GetClassLinker(); |
| Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3626 | ObjPtr<mirror::Class> klass = can_load_classes_ |
| Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3627 | ? linker->ResolveType(class_idx, dex_cache_, class_loader_) |
| 3628 | : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()); |
| Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3629 | if (can_load_classes_ && klass == nullptr) { |
| 3630 | DCHECK(self_->IsExceptionPending()); |
| 3631 | self_->ClearException(); |
| 3632 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3633 | const RegType* result = nullptr; |
| Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3634 | if (klass != nullptr) { |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3635 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3636 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3637 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3638 | UninstantiableError(descriptor); |
| 3639 | precise = false; |
| 3640 | } |
| Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3641 | result = reg_types_.FindClass(klass, precise); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3642 | if (result == nullptr) { |
| 3643 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3644 | result = reg_types_.InsertClass(descriptor, klass, precise); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3645 | } |
| 3646 | } else { |
| 3647 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3648 | result = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3649 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3650 | DCHECK(result != nullptr); |
| 3651 | if (result->IsConflict()) { |
| 3652 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3653 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3654 | << "' in " << GetDeclaringClass(); |
| 3655 | return *result; |
| 3656 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3657 | |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3658 | // If requested, check if access is allowed. Unresolved types are included in this check, as the |
| 3659 | // interpreter only tests whether access is allowed when a class is not pre-verified and runs in |
| 3660 | // the access-checks interpreter. If result is primitive, skip the access check. |
| 3661 | // |
| 3662 | // 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] | 3663 | if (C != CheckAccess::kNo && |
| Andreas Gampe | dc39d32 | 2018-09-04 09:26:03 -0700 | [diff] [blame] | 3664 | result->IsNonZeroReferenceTypes() && |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3665 | ((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) |
| 3666 | || !result->IsUnresolvedTypes())) { |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3667 | const RegType& referrer = GetDeclaringClass(); |
| David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 3668 | if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) && |
| 3669 | !referrer.CanAccess(*result)) { |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3670 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '" |
| Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3671 | << referrer << "' -> '" << *result << "'"; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3672 | } |
| Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3673 | } |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3674 | return *result; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3675 | } |
| 3676 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3677 | template <bool kVerifierDebug> |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3678 | bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) { |
| 3679 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 3680 | // where one entrypoint to the catch block is not actually an exception path. |
| 3681 | if (work_insn_idx_ == 0) { |
| 3682 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 3683 | return true; |
| 3684 | } |
| 3685 | /* |
| 3686 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 3687 | * that as part of extracting the exception type from the catch block list. |
| 3688 | */ |
| 3689 | auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) -> |
| 3690 | std::pair<bool, const RegType*> { |
| 3691 | const RegType* common_super = nullptr; |
| 3692 | if (code_item_accessor_.TriesSize() != 0) { |
| 3693 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 3694 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3695 | const RegType* unresolved = nullptr; |
| 3696 | for (uint32_t i = 0; i < handlers_size; i++) { |
| 3697 | CatchHandlerIterator iterator(handlers_ptr); |
| 3698 | for (; iterator.HasNext(); iterator.Next()) { |
| 3699 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3700 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
| 3701 | common_super = ®_types_.JavaLangThrowable(false); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3702 | } else { |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3703 | // Do access checks only on resolved exception classes. |
| 3704 | const RegType& exception = |
| 3705 | ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex()); |
| 3706 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
| 3707 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
| 3708 | if (exception.IsUnresolvedTypes()) { |
| 3709 | if (unresolved == nullptr) { |
| 3710 | unresolved = &exception; |
| 3711 | } else { |
| 3712 | unresolved = &unresolved->SafeMerge(exception, ®_types_, this); |
| 3713 | } |
| 3714 | } else { |
| 3715 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " |
| 3716 | << exception; |
| 3717 | return std::make_pair(true, ®_types_.Conflict()); |
| 3718 | } |
| 3719 | } else if (common_super == nullptr) { |
| 3720 | common_super = &exception; |
| 3721 | } else if (common_super->Equals(exception)) { |
| 3722 | // odd case, but nothing to do |
| 3723 | } else { |
| 3724 | common_super = &common_super->Merge(exception, ®_types_, this); |
| 3725 | if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3726 | *common_super, this), |
| 3727 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3728 | work_insn_idx_)) { |
| 3729 | break; |
| 3730 | } |
| Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3731 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3732 | } |
| 3733 | } |
| 3734 | } |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3735 | handlers_ptr = iterator.EndDataPointer(); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3736 | } |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3737 | if (unresolved != nullptr) { |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3738 | // Soft-fail, but do not handle this with a synthetic throw. |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 3739 | Fail(VERIFY_ERROR_UNRESOLVED_TYPE_CHECK, /*pending_exc=*/ false) |
| 3740 | << "Unresolved catch handler"; |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 3741 | bool should_continue = true; |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3742 | if (common_super != nullptr) { |
| 3743 | unresolved = &unresolved->Merge(*common_super, ®_types_, this); |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 3744 | } else { |
| 3745 | should_continue = !PotentiallyMarkRuntimeThrow(); |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3746 | } |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 3747 | return std::make_pair(should_continue, unresolved); |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3748 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3749 | } |
| Andreas Gampe | 6087bc2 | 2019-06-03 15:52:08 -0700 | [diff] [blame] | 3750 | if (common_super == nullptr) { |
| 3751 | /* no catch blocks, or no catches with classes we can find */ |
| 3752 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
| 3753 | return std::make_pair(true, ®_types_.Conflict()); |
| 3754 | } |
| 3755 | return std::make_pair(true, common_super); |
| 3756 | }; |
| 3757 | auto result = caught_exc_type_fn(); |
| 3758 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second); |
| 3759 | return result.first; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3762 | template <bool kVerifierDebug> |
| 3763 | ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess( |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3764 | uint32_t dex_method_idx, MethodType method_type) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3765 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3766 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3767 | if (klass_type.IsConflict()) { |
| 3768 | std::string append(" in attempt to access method "); |
| 3769 | append += dex_file_->GetMethodName(method_id); |
| 3770 | AppendToLastFailMessage(append); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3771 | return nullptr; |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3772 | } |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3773 | if (klass_type.IsUnresolvedTypes()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3774 | return nullptr; // Can't resolve Class so no more to do here |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3775 | } |
| Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3776 | ObjPtr<mirror::Class> klass = klass_type.GetClass(); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3777 | const RegType& referrer = GetDeclaringClass(); |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 3778 | ClassLinker* class_linker = GetClassLinker(); |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3779 | |
| David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 3780 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3781 | if (res_method == nullptr) { |
| Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 3782 | res_method = class_linker->FindResolvedMethod( |
| 3783 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3784 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3785 | |
| Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3786 | bool must_fail = false; |
| 3787 | // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method |
| 3788 | // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's |
| 3789 | // hard to see the differences. |
| 3790 | // If we don't have res_method here we must fail. Just use this bool to make sure of that with a |
| 3791 | // DCHECK. |
| Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3792 | if (res_method == nullptr) { |
| Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3793 | must_fail = true; |
| Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3794 | // Try to find the method also with the other type for better error reporting below |
| 3795 | // 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] | 3796 | res_method = class_linker->FindIncompatibleMethod( |
| 3797 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
| Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3798 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3799 | |
| 3800 | if (res_method == nullptr) { |
| 3801 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3802 | << klass->PrettyDescriptor() << "." |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3803 | << dex_file_->GetMethodName(method_id) << " " |
| 3804 | << dex_file_->GetMethodSignature(method_id); |
| 3805 | return nullptr; |
| 3806 | } |
| 3807 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3808 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3809 | // enforce them here. |
| 3810 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3811 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3812 | << res_method->PrettyMethod(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3813 | return nullptr; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3814 | } |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3815 | // Disallow any calls to class initializers. |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3816 | if (res_method->IsClassInitializer()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3817 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3818 | << res_method->PrettyMethod(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3819 | return nullptr; |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3820 | } |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3821 | |
| 3822 | // Check that interface methods are static or match interface classes. |
| 3823 | // We only allow statics if we don't have default methods enabled. |
| 3824 | // |
| 3825 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3826 | // while this check implies an IncompatibleClassChangeError. |
| 3827 | if (klass->IsInterface()) { |
| Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3828 | // 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] | 3829 | // default methods are supported for the dex file), or invoke-static. |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3830 | if (method_type != METHOD_INTERFACE && |
| Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3831 | method_type != METHOD_STATIC && |
| Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3832 | (!dex_file_->SupportsDefaultMethods() || |
| Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3833 | method_type != METHOD_DIRECT) && |
| Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3834 | method_type != METHOD_SUPER) { |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3835 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3836 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3837 | << " is in an interface class " << klass->PrettyClass(); |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3838 | return nullptr; |
| 3839 | } |
| 3840 | } else { |
| 3841 | if (method_type == METHOD_INTERFACE) { |
| 3842 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3843 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3844 | << " is in a non-interface class " << klass->PrettyClass(); |
| Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3845 | return nullptr; |
| 3846 | } |
| 3847 | } |
| 3848 | |
| Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3849 | // Check specifically for non-public object methods being provided for interface dispatch. This |
| 3850 | // can occur if we failed to find a method with FindInterfaceMethod but later find one with |
| 3851 | // FindClassMethod for error message use. |
| 3852 | if (method_type == METHOD_INTERFACE && |
| 3853 | res_method->GetDeclaringClass()->IsObjectClass() && |
| 3854 | !res_method->IsPublic()) { |
| 3855 | Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "." |
| 3856 | << dex_file_->GetMethodName(method_id) << " " |
| 3857 | << dex_file_->GetMethodSignature(method_id) << " resolved to " |
| 3858 | << "non-public object method " << res_method->PrettyMethod() << " " |
| 3859 | << "but non-public Object methods are excluded from interface " |
| 3860 | << "method resolution."; |
| 3861 | return nullptr; |
| 3862 | } |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3863 | // Check if access is allowed. |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3864 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3865 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 3866 | << res_method->PrettyMethod() |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3867 | << " from " << referrer << ")"; |
| jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3868 | return res_method; |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3869 | } |
| jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3870 | // 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] | 3871 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3872 | 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] | 3873 | << res_method->PrettyMethod(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3874 | return nullptr; |
| jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3875 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3876 | // 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] | 3877 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 3878 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 3879 | // arguments. |
| Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3880 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3881 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3882 | ((method_type == METHOD_SUPER || |
| 3883 | method_type == METHOD_VIRTUAL || |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3884 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 3885 | ((method_type == METHOD_POLYMORPHIC) && |
| 3886 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
| Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3887 | 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] | 3888 | "type of " << res_method->PrettyMethod(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3889 | return nullptr; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3890 | } |
| Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3891 | // Make sure we weren't expecting to fail. |
| 3892 | DCHECK(!must_fail) << "invoke type (" << method_type << ")" |
| 3893 | << klass->PrettyDescriptor() << "." |
| 3894 | << dex_file_->GetMethodName(method_id) << " " |
| 3895 | << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to " |
| 3896 | << res_method->PrettyMethod() << " without error. Initially this method was " |
| 3897 | << "not found so we were expecting to fail for some reason."; |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3898 | return res_method; |
| 3899 | } |
| 3900 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3901 | template <bool kVerifierDebug> |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3902 | template <class T> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 3903 | ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator( |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3904 | 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] | 3905 | DCHECK_EQ(!is_range, inst->HasVarArgs()); |
| 3906 | |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3907 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3908 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3909 | // definition (which doesn't have register count values). |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3910 | const size_t expected_args = inst->VRegA(); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3911 | /* caught by static verifier */ |
| 3912 | DCHECK(is_range || expected_args <= 5); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3913 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3914 | if (expected_args > code_item_accessor_.OutsSize()) { |
| Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 3915 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3916 | << ") exceeds outsSize (" |
| 3917 | << code_item_accessor_.OutsSize() << ")"; |
| Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 3918 | return nullptr; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3919 | } |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3920 | |
| 3921 | /* |
| 3922 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3923 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3924 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3925 | */ |
| 3926 | if (method_type != METHOD_STATIC) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3927 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3928 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3929 | CHECK(flags_.have_pending_hard_failure_); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3930 | return nullptr; |
| 3931 | } |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3932 | bool is_init = false; |
| 3933 | if (actual_arg_type.IsUninitializedTypes()) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3934 | if (res_method) { |
| 3935 | if (!res_method->IsConstructor()) { |
| 3936 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3937 | return nullptr; |
| 3938 | } |
| 3939 | } else { |
| 3940 | // Check whether the name of the called method is "<init>" |
| Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 3941 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
| Jeff Hao | 0d08727 | 2014-08-04 14:47:17 -0700 | [diff] [blame] | 3942 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3943 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3944 | return nullptr; |
| 3945 | } |
| 3946 | } |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3947 | is_init = true; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3948 | } |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3949 | const RegType& adjusted_type = is_init |
| 3950 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 3951 | : actual_arg_type; |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3952 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) { |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3953 | const RegType* res_method_class; |
| Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 3954 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 3955 | // class. It would be wrong to use this for the type check (interface type checks are |
| 3956 | // postponed to runtime). |
| 3957 | if (res_method != nullptr && !res_method->IsMiranda()) { |
| Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3958 | ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass(); |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3959 | std::string temp; |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3960 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 3961 | klass->CannotBeAssignedFromOtherTypes()); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3962 | } else { |
| Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 3963 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3964 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3965 | res_method_class = ®_types_.FromDescriptor( |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 3966 | class_loader_.Get(), |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3967 | dex_file_->StringByTypeIdx(class_idx), |
| 3968 | false); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3969 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3970 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3971 | Fail(adjusted_type.IsUnresolvedTypes() |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 3972 | ? VERIFY_ERROR_UNRESOLVED_TYPE_CHECK |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3973 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 3974 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 3975 | << *res_method_class << "'"; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3976 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3977 | // the compiler. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 3978 | if (flags_.have_pending_hard_failure_) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3979 | return nullptr; |
| 3980 | } |
| 3981 | } |
| 3982 | } |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3983 | } |
| 3984 | |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3985 | uint32_t arg[5]; |
| 3986 | if (!is_range) { |
| 3987 | inst->GetVarArgs(arg); |
| 3988 | } |
| 3989 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3990 | for ( ; it->HasNext(); it->Next()) { |
| 3991 | if (sig_registers >= expected_args) { |
| 3992 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3993 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3994 | return nullptr; |
| 3995 | } |
| 3996 | |
| 3997 | const char* param_descriptor = it->GetDescriptor(); |
| 3998 | |
| 3999 | if (param_descriptor == nullptr) { |
| 4000 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 4001 | "component"; |
| 4002 | return nullptr; |
| 4003 | } |
| 4004 | |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4005 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(), |
| 4006 | param_descriptor, |
| 4007 | false); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4008 | 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] | 4009 | arg[sig_registers]; |
| 4010 | if (reg_type.IsIntegralTypes()) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4011 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4012 | if (!src_type.IsIntegralTypes()) { |
| 4013 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4014 | << " but expected " << reg_type; |
| Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4015 | return nullptr; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4016 | } |
| Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4017 | } else { |
| 4018 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4019 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4020 | // the compiler. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4021 | if (flags_.have_pending_hard_failure_) { |
| Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4022 | return nullptr; |
| 4023 | } |
| 4024 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4025 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4026 | // instructions not specifying register pairs by the first component, but require them |
| 4027 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4028 | // none, this will fail below. |
| 4029 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4030 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4031 | if (second_reg != get_reg + 1) { |
| 4032 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4033 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4034 | << second_reg << "."; |
| 4035 | return nullptr; |
| 4036 | } |
| 4037 | } |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4041 | } |
| 4042 | if (expected_args != sig_registers) { |
| 4043 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4044 | " argument registers, method signature has " << sig_registers; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4045 | return nullptr; |
| 4046 | } |
| 4047 | return res_method; |
| 4048 | } |
| 4049 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4050 | template <bool kVerifierDebug> |
| 4051 | void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4052 | MethodType method_type, |
| 4053 | bool is_range) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4054 | // As the method may not have been resolved, make this static check against what we expect. |
| 4055 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4056 | // 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] | 4057 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4058 | DexFileParameterIterator it(*dex_file_, |
| 4059 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4060 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4063 | template <bool kVerifierDebug> |
| 4064 | bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) { |
| Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4065 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4066 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4067 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4068 | return false; |
| 4069 | } |
| 4070 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4071 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| Alex Light | 1e52a07 | 2019-06-25 09:12:04 -0700 | [diff] [blame] | 4072 | // 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] | 4073 | // main values (method handle, name, method_type). |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4074 | static const size_t kRequiredArguments = 3; |
| 4075 | if (it.Size() < kRequiredArguments) { |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4076 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4077 | << " has too few arguments: " |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4078 | << it.Size() << " < " << kRequiredArguments; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4079 | return false; |
| 4080 | } |
| 4081 | |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4082 | std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] = |
| 4083 | { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() }, |
| 4084 | { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() }, |
| 4085 | { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() } |
| 4086 | }; |
| 4087 | uint32_t index[kRequiredArguments]; |
| 4088 | |
| 4089 | // Check arguments have expected types and are within permitted ranges. |
| 4090 | for (size_t i = 0; i < kRequiredArguments; ++i) { |
| 4091 | if (it.GetValueType() != type_and_max[i].first) { |
| 4092 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4093 | << " argument " << i << " has wrong type " |
| 4094 | << it.GetValueType() << "!=" << type_and_max[i].first; |
| 4095 | return false; |
| 4096 | } |
| 4097 | index[i] = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4098 | if (index[i] >= type_and_max[i].second) { |
| 4099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4100 | << " argument " << i << " bad index " |
| 4101 | << index[i] << " >= " << type_and_max[i].second; |
| 4102 | return false; |
| 4103 | } |
| 4104 | it.Next(); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 4105 | } |
| Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4106 | |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4107 | // Check method handle kind is valid. |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4108 | const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4109 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4110 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 4111 | << " argument 0 method handle type is not InvokeStatic: " |
| 4112 | << mh.method_handle_type_; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4113 | return false; |
| 4114 | } |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4115 | return true; |
| 4116 | } |
| 4117 | |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4118 | class MethodParamListDescriptorIterator { |
| 4119 | public: |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4120 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4121 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4122 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4123 | } |
| 4124 | |
| 4125 | bool HasNext() { |
| 4126 | return pos_ < params_size_; |
| 4127 | } |
| 4128 | |
| 4129 | void Next() { |
| 4130 | ++pos_; |
| 4131 | } |
| 4132 | |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4133 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4134 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4135 | } |
| 4136 | |
| 4137 | private: |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4138 | ArtMethod* res_method_; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4139 | size_t pos_; |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4140 | const dex::TypeList* params_; |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4141 | const size_t params_size_; |
| 4142 | }; |
| 4143 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4144 | template <bool kVerifierDebug> |
| 4145 | ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs( |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4146 | const Instruction* inst, MethodType method_type, bool is_range) { |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4147 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4148 | // we're making. |
| Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4149 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
| Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4150 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4151 | if (res_method == nullptr) { // error or class is unresolved |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4152 | // Check what we can statically. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4153 | if (!flags_.have_pending_hard_failure_) { |
| Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4154 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4155 | } |
| 4156 | return nullptr; |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4157 | } |
| 4158 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4159 | // 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] | 4160 | // 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] | 4161 | if (method_type == METHOD_SUPER) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4162 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4163 | const RegType& reference_type = reg_types_.FromDescriptor( |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4164 | class_loader_.Get(), |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4165 | dex_file_->StringByTypeIdx(class_idx), |
| 4166 | false); |
| 4167 | if (reference_type.IsUnresolvedTypes()) { |
| Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4168 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4169 | return nullptr; |
| 4170 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4171 | if (reference_type.GetClass()->IsInterface()) { |
| Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4172 | if (!GetDeclaringClass().HasClass()) { |
| 4173 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4174 | << "interface invoke-super"; |
| 4175 | return nullptr; |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4176 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
| Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4177 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4178 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4179 | << " in method " |
| 4180 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4181 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4182 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
| Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4183 | return nullptr; |
| Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4184 | } |
| 4185 | } else { |
| 4186 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4187 | if (super.IsUnresolvedTypes()) { |
| 4188 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4189 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4190 | << " to super " << res_method->PrettyMethod(); |
| Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4191 | return nullptr; |
| 4192 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4193 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
| Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4194 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
| Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4195 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4196 | << dex_file_->PrettyMethod(dex_method_idx_) |
| Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4197 | << " to super " << super |
| 4198 | << "." << res_method->GetName() |
| 4199 | << res_method->GetSignature(); |
| 4200 | return nullptr; |
| 4201 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4202 | } |
| 4203 | } |
| Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4204 | |
| Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4205 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4206 | // 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] | 4207 | dex::ProtoIndex proto_idx(inst->VRegH()); |
| 4208 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx)); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4209 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4210 | } else { |
| 4211 | // Process the target method's signature. |
| 4212 | MethodParamListDescriptorIterator it(res_method); |
| 4213 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4214 | } |
| 4215 | } |
| 4216 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4217 | template <bool kVerifierDebug> |
| 4218 | bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
| Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 4219 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4220 | const char* method_name = method->GetName(); |
| 4221 | |
| 4222 | const char* expected_return_descriptor; |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4223 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots(); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4224 | if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) { |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4225 | expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4226 | } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) { |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4227 | expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name); |
| 4228 | } else { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4229 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4230 | << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor(); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4231 | return false; |
| 4232 | } |
| 4233 | |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4234 | if (expected_return_descriptor == nullptr) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4235 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4236 | << "Signature polymorphic method name invalid: " << method_name; |
| 4237 | return false; |
| 4238 | } |
| 4239 | |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4240 | const dex::TypeList* types = method->GetParameterTypeList(); |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4241 | if (types->Size() != 1) { |
| 4242 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4243 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4244 | return false; |
| 4245 | } |
| 4246 | |
| 4247 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4248 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4249 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4250 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4251 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
| 4255 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4256 | if (strcmp(return_descriptor, expected_return_descriptor) != 0) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4257 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4258 | << "Signature polymorphic method has unexpected return type: " << return_descriptor |
| 4259 | << " != " << expected_return_descriptor; |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4260 | return false; |
| 4261 | } |
| 4262 | |
| 4263 | return true; |
| 4264 | } |
| 4265 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4266 | template <bool kVerifierDebug> |
| 4267 | bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4268 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4269 | if (this_type.IsZeroOrNull()) { |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4270 | /* null pointer always passes (and always fails at run time) */ |
| 4271 | return true; |
| 4272 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4273 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4274 | << "invoke-polymorphic receiver is not a reference: " |
| 4275 | << this_type; |
| 4276 | return false; |
| 4277 | } else if (this_type.IsUninitializedReference()) { |
| 4278 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4279 | << "invoke-polymorphic receiver is uninitialized: " |
| 4280 | << this_type; |
| 4281 | return false; |
| 4282 | } else if (!this_type.HasClass()) { |
| 4283 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4284 | << "invoke-polymorphic receiver has no class: " |
| 4285 | << this_type; |
| 4286 | return false; |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4287 | } else { |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4288 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots(); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4289 | if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) && |
| 4290 | !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) { |
| 4291 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4292 | << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: " |
| 4293 | << this_type; |
| 4294 | return false; |
| 4295 | } |
| Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4296 | } |
| 4297 | return true; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4298 | } |
| 4299 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4300 | template <bool kVerifierDebug> |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4301 | void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst, |
| 4302 | bool is_filled, |
| 4303 | bool is_range) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4304 | dex::TypeIndex type_idx; |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4305 | if (!is_filled) { |
| 4306 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4307 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4308 | } else if (!is_range) { |
| 4309 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4310 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4311 | } else { |
| 4312 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4313 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4314 | } |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4315 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4316 | if (res_type.IsConflict()) { // bad class |
| 4317 | DCHECK_NE(failures_.size(), 0U); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4318 | } else { |
| 4319 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4320 | if (!res_type.IsArrayTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4321 | 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] | 4322 | } else if (!is_filled) { |
| 4323 | /* make sure "size" register is valid type */ |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4324 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4325 | /* set register type to array class */ |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4326 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4327 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4328 | } else { |
| Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4329 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4330 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4331 | // 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] | 4332 | const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get()); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4333 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4334 | uint32_t arg[5]; |
| 4335 | if (!is_range) { |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4336 | inst->GetVarArgs(arg); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4337 | } |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4338 | for (size_t ui = 0; ui < arg_count; ui++) { |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4339 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4340 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4341 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4342 | return; |
| 4343 | } |
| 4344 | } |
| 4345 | // filled-array result goes into "result" register |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4346 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4347 | work_line_->SetResultRegisterType(this, precise_type); |
| Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4352 | template <bool kVerifierDebug> |
| 4353 | void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst, |
| 4354 | const RegType& insn_type, |
| 4355 | bool is_primitive) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4356 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4357 | if (!index_type.IsArrayIndexTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4358 | 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] | 4359 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4360 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4361 | if (array_type.IsZeroOrNull()) { |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4362 | // 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] | 4363 | // instruction type. |
| 4364 | if (!is_primitive) { |
| 4365 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null()); |
| 4366 | } else if (insn_type.IsInteger()) { |
| 4367 | // Pick a non-zero constant (to distinguish with null) that can fit in any primitive. |
| 4368 | // We cannot use 'insn_type' as it could be a float array or an int array. |
| 4369 | work_line_->SetRegisterType<LockOp::kClear>( |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 4370 | this, inst->VRegA_23x(), DetermineCat1Constant(1)); |
| Andreas Gampe | 52f205a | 2017-12-01 12:16:07 -0800 | [diff] [blame] | 4371 | } else if (insn_type.IsCategory1Types()) { |
| 4372 | // Category 1 |
| 4373 | // The 'insn_type' is exactly the type we need. |
| 4374 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4375 | } else { |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4376 | // Category 2 |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4377 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4378 | reg_types_.FromCat2ConstLo(0, false), |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4379 | reg_types_.FromCat2ConstHi(0, false)); |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4380 | } |
| jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4381 | } else if (!array_type.IsArrayTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4382 | 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] | 4383 | } else if (array_type.IsUnresolvedMergedReference()) { |
| Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4384 | // Unresolved array types must be reference array types. |
| 4385 | if (is_primitive) { |
| 4386 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4387 | << " source for category 1 aget"; |
| 4388 | } else { |
| 4389 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4390 | << " because of missing class"; |
| Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4391 | // Approximate with java.lang.Object[]. |
| 4392 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4393 | inst->VRegA_23x(), |
| 4394 | reg_types_.JavaLangObject(false)); |
| Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4395 | } |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4396 | } else { |
| 4397 | /* verify the class */ |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4398 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get()); |
| jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4399 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4400 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4401 | << " source for aget-object"; |
| 4402 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4403 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4404 | << " source for category 1 aget"; |
| 4405 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4406 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4407 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4408 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4409 | << " incompatible with aget of type " << insn_type; |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4410 | } else { |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4411 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4412 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4413 | // doubles. |
| 4414 | if (!component_type.IsLowHalf()) { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4415 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4416 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4417 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
| Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4418 | component_type.HighHalf(®_types_)); |
| 4419 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4420 | } |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4425 | template <bool kVerifierDebug> |
| 4426 | void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type, |
| 4427 | const RegType& insn_type, |
| 4428 | const uint32_t vregA) { |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4429 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4430 | bool instruction_compatible; |
| 4431 | bool value_compatible; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4432 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4433 | if (target_type.IsIntegralTypes()) { |
| Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4434 | instruction_compatible = target_type.Equals(insn_type); |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4435 | value_compatible = value_type.IsIntegralTypes(); |
| 4436 | } else if (target_type.IsFloat()) { |
| 4437 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4438 | value_compatible = value_type.IsFloatTypes(); |
| 4439 | } else if (target_type.IsLong()) { |
| 4440 | instruction_compatible = insn_type.IsLong(); |
| Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4441 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4442 | // as target_type depends on the resolved type of the field. |
| 4443 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4444 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
| Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4445 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4446 | } else { |
| 4447 | value_compatible = false; |
| 4448 | } |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4449 | } else if (target_type.IsDouble()) { |
| 4450 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
| Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4451 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4452 | // as target_type depends on the resolved type of the field. |
| 4453 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4454 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
| Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4455 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4456 | } else { |
| 4457 | value_compatible = false; |
| 4458 | } |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4459 | } else { |
| 4460 | instruction_compatible = false; // reference with primitive store |
| 4461 | value_compatible = false; // unused |
| 4462 | } |
| 4463 | if (!instruction_compatible) { |
| 4464 | // This is a global failure rather than a class change failure as the instructions and |
| 4465 | // the descriptors for the type should have been consistent within the same file at |
| 4466 | // compile time. |
| 4467 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4468 | << "' but expected type '" << target_type << "'"; |
| 4469 | return; |
| 4470 | } |
| 4471 | if (!value_compatible) { |
| 4472 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4473 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4474 | return; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4478 | template <bool kVerifierDebug> |
| 4479 | void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst, |
| 4480 | const RegType& insn_type, |
| 4481 | bool is_primitive) { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4482 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4483 | if (!index_type.IsArrayIndexTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4484 | 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] | 4485 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4486 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4487 | if (array_type.IsZeroOrNull()) { |
| Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4488 | // Null array type; this code path will fail at runtime. |
| 4489 | // Still check that the given value matches the instruction's type. |
| Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4490 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4491 | // and fits multiple register types. |
| 4492 | const RegType* modified_reg_type = &insn_type; |
| 4493 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4494 | (modified_reg_type == ®_types_.LongLo())) { |
| 4495 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4496 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4497 | if (modified_reg_type == ®_types_.Integer()) { |
| 4498 | if (&value_type == ®_types_.Float()) { |
| 4499 | modified_reg_type = &value_type; |
| 4500 | } |
| 4501 | } else { |
| 4502 | if (&value_type == ®_types_.DoubleLo()) { |
| 4503 | modified_reg_type = &value_type; |
| 4504 | } |
| 4505 | } |
| 4506 | } |
| 4507 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
| jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4508 | } else if (!array_type.IsArrayTypes()) { |
| jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4509 | 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] | 4510 | } else if (array_type.IsUnresolvedMergedReference()) { |
| Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4511 | // Unresolved array types must be reference array types. |
| 4512 | if (is_primitive) { |
| 4513 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4514 | << "' but unresolved type '" << array_type << "'"; |
| 4515 | } else { |
| 4516 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4517 | << " because of missing class"; |
| 4518 | } |
| Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4519 | } else { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4520 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get()); |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4521 | const uint32_t vregA = inst->VRegA_23x(); |
| Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4522 | if (is_primitive) { |
| Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4523 | VerifyPrimitivePut(component_type, insn_type, vregA); |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4524 | } else { |
| Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4525 | if (!component_type.IsReferenceTypes()) { |
| 4526 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4527 | << " source for aput-object"; |
| 4528 | } else { |
| 4529 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4530 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4531 | // incompatible classes will be caught at runtime as an array store exception |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4532 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
| Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4533 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | } |
| 4537 | } |
| 4538 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4539 | template <bool kVerifierDebug> |
| 4540 | ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4541 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4542 | // Check access to class |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4543 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
| Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4544 | if (klass_type.IsConflict()) { // bad class |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4545 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4546 | field_idx, dex_file_->GetFieldName(field_id), |
| 4547 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4548 | return nullptr; |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4549 | } |
| Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4550 | if (klass_type.IsUnresolvedTypes()) { |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4551 | // Accessibility checks depend on resolved fields. |
| David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4552 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4553 | !failures_.empty() || |
| 4554 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4555 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4556 | 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] | 4557 | } |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4558 | ClassLinker* class_linker = GetClassLinker(); |
| Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4559 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4560 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4561 | if (field == nullptr) { |
| Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4562 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
| Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4563 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4564 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4565 | DCHECK(self_->IsExceptionPending()); |
| 4566 | self_->ClearException(); |
| 4567 | return nullptr; |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4568 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4569 | field->GetAccessFlags())) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4570 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4571 | << " from " << GetDeclaringClass(); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4572 | return nullptr; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4573 | } else if (!field->IsStatic()) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4574 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4575 | return nullptr; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4576 | } |
| Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4577 | return field; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4580 | template <bool kVerifierDebug> |
| 4581 | ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) { |
| Andreas Gampe | b34981b | 2019-05-06 13:00:40 -0700 | [diff] [blame] | 4582 | if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) { |
| 4583 | // Trying to read a field from something that isn't a reference. |
| 4584 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4585 | << "non-reference type " << obj_type; |
| 4586 | return nullptr; |
| 4587 | } |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4588 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4589 | // Check access to class. |
| Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4590 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4591 | if (klass_type.IsConflict()) { |
| 4592 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4593 | field_idx, dex_file_->GetFieldName(field_id), |
| 4594 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4595 | return nullptr; |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4596 | } |
| jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4597 | if (klass_type.IsUnresolvedTypes()) { |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4598 | // Accessibility checks depend on resolved fields. |
| David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4599 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4600 | !failures_.empty() || |
| 4601 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
| Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4602 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4603 | return nullptr; // Can't resolve Class so no more to do here |
| Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4604 | } |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4605 | ClassLinker* class_linker = GetClassLinker(); |
| Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4606 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4607 | |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4608 | if (field == nullptr) { |
| Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4609 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
| Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4610 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4611 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4612 | DCHECK(self_->IsExceptionPending()); |
| 4613 | self_->ClearException(); |
| 4614 | return nullptr; |
| Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4615 | } else if (obj_type.IsZeroOrNull()) { |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4616 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4617 | // Fall through into a few last soft failure checks below. |
| Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4618 | } else { |
| David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4619 | std::string temp; |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4620 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4621 | const RegType& field_klass = |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4622 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4623 | if (obj_type.IsUninitializedTypes()) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4624 | // Field accesses through uninitialized references are only allowable for constructors where |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4625 | // the field is declared in this class. |
| 4626 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4627 | // appear in constructors. |
| 4628 | if (!obj_type.IsUninitializedThisReference() || |
| 4629 | !IsConstructor() || |
| 4630 | !field_klass.Equals(GetDeclaringClass())) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4631 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4632 | << " of a not fully initialized object within the context" |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4633 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4634 | return nullptr; |
| 4635 | } |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4636 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4637 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4638 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4639 | // 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] | 4640 | VerifyError type; |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 4641 | bool is_aot = IsAotMode(); |
| Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4642 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4643 | // Compiler & unresolved types involved, retry at runtime. |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 4644 | type = VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK; |
| Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4645 | } else { |
| Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4646 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4647 | // and still missing classes. This is a hard failure. |
| Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4648 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4649 | } |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4650 | Fail(type) << "cannot access instance field " << field->PrettyField() |
| Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4651 | << " from object of type " << obj_type; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4652 | return nullptr; |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4653 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4654 | } |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4655 | |
| 4656 | // Few last soft failure checks. |
| 4657 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4658 | field->GetAccessFlags())) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4659 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4660 | << " from " << GetDeclaringClass(); |
| 4661 | return nullptr; |
| 4662 | } else if (field->IsStatic()) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4663 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4664 | << " to not be static"; |
| 4665 | return nullptr; |
| 4666 | } |
| 4667 | |
| 4668 | return field; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4671 | template <bool kVerifierDebug> |
| 4672 | template <FieldAccessType kAccType> |
| 4673 | void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst, |
| 4674 | const RegType& insn_type, |
| 4675 | bool is_primitive, |
| 4676 | bool is_static) { |
| Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4677 | uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static); |
| Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4678 | ArtField* field; |
| Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4679 | if (is_static) { |
| Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4680 | field = GetStaticField(field_idx); |
| Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4681 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4682 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
| David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4683 | |
| 4684 | // One is not allowed to access fields on uninitialized references, except to write to |
| 4685 | // fields in the constructor (before calling another constructor). |
| 4686 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 4687 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 4688 | // checks at the same time that we're verifying a constructor). |
| 4689 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 4690 | object_type.IsUninitializedThisReference(); |
| 4691 | const RegType& adjusted_type = should_adjust |
| 4692 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 4693 | : object_type; |
| 4694 | field = GetInstanceField(adjusted_type, field_idx); |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4695 | if (UNLIKELY(flags_.have_pending_hard_failure_)) { |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4696 | return; |
| 4697 | } |
| Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4698 | if (should_adjust) { |
| 4699 | if (field == nullptr) { |
| 4700 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 4701 | << "to the superclass being initialized in " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4702 | << dex_file_->PrettyMethod(dex_method_idx_); |
| Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4703 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4704 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4705 | << field->PrettyField() << " of a not fully initialized " |
| Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4706 | << "object within the context of " |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4707 | << dex_file_->PrettyMethod(dex_method_idx_); |
| Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4708 | return; |
| 4709 | } |
| 4710 | } |
| Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4711 | } |
| Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4712 | const RegType* field_type = nullptr; |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4713 | if (field != nullptr) { |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4714 | if (kAccType == FieldAccessType::kAccPut) { |
| 4715 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4716 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4717 | << " from other class " << GetDeclaringClass(); |
| Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 4718 | // Keep hunting for possible hard fails. |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4722 | ObjPtr<mirror::Class> field_type_class = |
| Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 4723 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4724 | if (field_type_class != nullptr) { |
| Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4725 | field_type = &FromClass(field->GetTypeDescriptor(), |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4726 | field_type_class, |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4727 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4728 | } else { |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4729 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4730 | self_->ClearException(); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4731 | } |
| David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4732 | } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) { |
| Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4733 | // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to |
| 4734 | // redo verification at runtime as the field may be final, unless the field id shows it's in |
| 4735 | // the same class. |
| 4736 | // |
| 4737 | // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an |
| 4738 | // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class |
| 4739 | // to the access-checks interpreter. |
| 4740 | // |
| 4741 | // Note: see b/34966607. This and above may be changed in the future. |
| 4742 | if (kAccType == FieldAccessType::kAccPut) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4743 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4744 | const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4745 | const RegType* field_class_type = ®_types_.FromDescriptor(class_loader_.Get(), |
| Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4746 | field_class_descriptor, |
| 4747 | false); |
| 4748 | if (!field_class_type->Equals(GetDeclaringClass())) { |
| 4749 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of " |
| 4750 | << field_class_descriptor |
| 4751 | << "." |
| 4752 | << dex_file_->GetFieldName(field_id) |
| 4753 | << " from other class " |
| 4754 | << GetDeclaringClass(); |
| 4755 | } |
| 4756 | } |
| Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4757 | } |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4758 | if (field_type == nullptr) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4759 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4760 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4761 | field_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4762 | } |
| Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4763 | DCHECK(field_type != nullptr); |
| Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4764 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4765 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4766 | "Unexpected third access type"); |
| 4767 | if (kAccType == FieldAccessType::kAccPut) { |
| 4768 | // sput or iput. |
| 4769 | if (is_primitive) { |
| 4770 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4771 | } else { |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4772 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
| Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4773 | // If the field type is not a reference, this is a global failure rather than |
| 4774 | // a class change failure as the instructions and the descriptors for the type |
| 4775 | // should have been consistent within the same file at compile time. |
| 4776 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4777 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4778 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
| Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4779 | << " to be compatible with type '" << insn_type |
| 4780 | << "' but found type '" << *field_type |
| 4781 | << "' in put-object"; |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4782 | return; |
| 4783 | } |
| 4784 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4785 | } |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4786 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4787 | // sget or iget. |
| 4788 | if (is_primitive) { |
| 4789 | if (field_type->Equals(insn_type) || |
| 4790 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4791 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4792 | // expected that read is of the correct primitive type or that int reads are reading |
| 4793 | // floats or long reads are reading doubles |
| 4794 | } else { |
| 4795 | // This is a global failure rather than a class change failure as the instructions and |
| 4796 | // the descriptors for the type should have been consistent within the same file at |
| 4797 | // compile time |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4798 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4799 | << " to be of type '" << insn_type |
| 4800 | << "' but found type '" << *field_type << "' in get"; |
| 4801 | return; |
| 4802 | } |
| Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4803 | } else { |
| David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4804 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
| Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4805 | // If the field type is not a reference, this is a global failure rather than |
| 4806 | // a class change failure as the instructions and the descriptors for the type |
| 4807 | // should have been consistent within the same file at compile time. |
| 4808 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4809 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4810 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
| Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4811 | << " to be compatible with type '" << insn_type |
| 4812 | << "' but found type '" << *field_type |
| 4813 | << "' in get-object"; |
| Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4814 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4815 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
| Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4816 | } |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4817 | return; |
| 4818 | } |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4819 | } |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4820 | if (!field_type->IsLowHalf()) { |
| Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4821 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4822 | } else { |
| 4823 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4824 | } |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4825 | } else { |
| Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4826 | LOG(FATAL) << "Unexpected case."; |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4827 | } |
| 4828 | } |
| 4829 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4830 | template <bool kVerifierDebug> |
| 4831 | bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn, |
| 4832 | RegisterLine* merge_line, |
| 4833 | bool update_merge_line) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4834 | bool changed = true; |
| 4835 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4836 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4837 | /* |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4838 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4839 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4840 | * 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] | 4841 | */ |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4842 | target_line->CopyFromLine(merge_line); |
| Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4843 | if (GetInstructionFlags(next_insn).IsReturn()) { |
| Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4844 | // Verify that the monitor stack is empty on return. |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4845 | merge_line->VerifyMonitorStackEmpty(this); |
| 4846 | |
| Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4847 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4848 | // 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] | 4849 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn); |
| Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4850 | AdjustReturnLine(this, ret_inst, target_line); |
| Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4851 | // Directly bail if a hard failure was found. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4852 | if (flags_.have_pending_hard_failure_) { |
| Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 4853 | return false; |
| 4854 | } |
| Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4855 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4856 | } else { |
| Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 4857 | RegisterLineArenaUniquePtr copy; |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4858 | if (kVerifierDebug) { |
| Andreas Gampe | d09c059 | 2019-04-19 15:44:05 -0700 | [diff] [blame] | 4859 | copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache())); |
| Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4860 | copy->CopyFromLine(target_line); |
| 4861 | } |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4862 | changed = target_line->MergeRegisters(this, merge_line); |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 4863 | if (flags_.have_pending_hard_failure_) { |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4864 | return false; |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4865 | } |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4866 | if (kVerifierDebug && changed) { |
| Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4867 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
| Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4868 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
| Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4869 | << copy->Dump(this) << " MERGE\n" |
| 4870 | << merge_line->Dump(this) << " ==\n" |
| Andreas Gampe | 2ad6cce | 2019-04-11 16:17:39 -0700 | [diff] [blame] | 4871 | << target_line->Dump(this); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4872 | } |
| Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4873 | if (update_merge_line && changed) { |
| 4874 | merge_line->CopyFromLine(target_line); |
| 4875 | } |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4876 | } |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4877 | if (changed) { |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 4878 | GetModifiableInstructionFlags(next_insn).SetChanged(); |
| jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4879 | } |
| 4880 | return true; |
| 4881 | } |
| 4882 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4883 | template <bool kVerifierDebug> |
| 4884 | const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() { |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4885 | if (return_type_ == nullptr) { |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 4886 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4887 | const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 4888 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
| 4889 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
| 4890 | return_type_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false); |
| Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4891 | } |
| 4892 | return *return_type_; |
| Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 4895 | template <bool kVerifierDebug> |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 4896 | const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value) { |
| 4897 | // Imprecise constant type. |
| 4898 | if (value < -32768) { |
| 4899 | return reg_types_.IntConstant(); |
| 4900 | } else if (value < -128) { |
| 4901 | return reg_types_.ShortConstant(); |
| 4902 | } else if (value < 0) { |
| 4903 | return reg_types_.ByteConstant(); |
| 4904 | } else if (value == 0) { |
| 4905 | return reg_types_.Zero(); |
| 4906 | } else if (value == 1) { |
| 4907 | return reg_types_.One(); |
| 4908 | } else if (value < 128) { |
| 4909 | return reg_types_.PosByteConstant(); |
| 4910 | } else if (value < 32768) { |
| 4911 | return reg_types_.PosShortConstant(); |
| 4912 | } else if (value < 65536) { |
| 4913 | return reg_types_.CharConstant(); |
| Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 4914 | } else { |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 4915 | return reg_types_.IntConstant(); |
| Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 4916 | } |
| 4917 | } |
| 4918 | |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4919 | template <bool kVerifierDebug> |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4920 | bool MethodVerifier<kVerifierDebug>::PotentiallyMarkRuntimeThrow() { |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4921 | if (IsAotMode() || IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kT)) { |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4922 | return false; |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4923 | } |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4924 | // Compatibility mode: we treat the following code unreachable and the verifier |
| 4925 | // will not analyze it. |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4926 | // The verifier may fail before we touch any instruction, for the signature of a method. So |
| 4927 | // add a check. |
| 4928 | if (work_insn_idx_ < dex::kDexNoIndex) { |
| 4929 | const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_); |
| 4930 | Instruction::Code opcode = inst.Opcode(); |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4931 | if (opcode == Instruction::MOVE_EXCEPTION) { |
| 4932 | // This is an unreachable handler. The instruction doesn't throw, but we |
| 4933 | // mark the method as having a pending runtime throw failure so that |
| 4934 | // the compiler does not try to compile it. |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 4935 | Fail(VERIFY_ERROR_RUNTIME_THROW, /* pending_exc= */ false); |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4936 | return true; |
| 4937 | } |
| 4938 | // How to handle runtime failures for instructions that are not flagged kThrow. |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4939 | if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 && |
| 4940 | !impl::IsCompatThrow(opcode) && |
| 4941 | GetInstructionFlags(work_insn_idx_).IsInTry()) { |
| 4942 | if (Runtime::Current()->IsVerifierMissingKThrowFatal()) { |
| 4943 | LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode; |
| 4944 | UNREACHABLE(); |
| 4945 | } |
| 4946 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise |
| 4947 | // we'll try to merge garbage. |
| 4948 | // Note: this assumes that Fail is called before we do any work_line modifications. |
| 4949 | saved_line_->CopyFromLine(work_line_.get()); |
| 4950 | } |
| 4951 | } |
| Nicolas Geoffray | cd133d8 | 2021-06-25 16:10:39 +0100 | [diff] [blame] | 4952 | flags_.have_pending_runtime_throw_failure_ = true; |
| 4953 | return true; |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4956 | } // namespace |
| 4957 | } // namespace impl |
| 4958 | |
| 4959 | MethodVerifier::MethodVerifier(Thread* self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4960 | ClassLinker* class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 4961 | ArenaPool* arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4962 | VerifierDeps* verifier_deps, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4963 | const DexFile* dex_file, |
| Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 4964 | const dex::ClassDef& class_def, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4965 | const dex::CodeItem* code_item, |
| 4966 | uint32_t dex_method_idx, |
| 4967 | bool can_load_classes, |
| 4968 | bool allow_thread_suspension, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 4969 | bool allow_soft_failures, |
| 4970 | bool aot_mode) |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4971 | : self_(self), |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 4972 | arena_stack_(arena_pool), |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4973 | allocator_(&arena_stack_), |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4974 | reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension), |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4975 | reg_table_(allocator_), |
| 4976 | work_insn_idx_(dex::kDexNoIndex), |
| 4977 | dex_method_idx_(dex_method_idx), |
| 4978 | dex_file_(dex_file), |
| Nicolas Geoffray | 1960c42 | 2020-11-04 08:45:32 +0000 | [diff] [blame] | 4979 | class_def_(class_def), |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4980 | code_item_accessor_(*dex_file, code_item), |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 4981 | // TODO: make it designated initialization when we compile as C++20. |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 4982 | flags_({false, false, aot_mode}), |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4983 | encountered_failure_types_(0), |
| 4984 | can_load_classes_(can_load_classes), |
| 4985 | allow_soft_failures_(allow_soft_failures), |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4986 | class_linker_(class_linker), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4987 | verifier_deps_(verifier_deps), |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 4988 | link_(nullptr) { |
| 4989 | self->PushVerifier(this); |
| 4990 | } |
| 4991 | |
| 4992 | MethodVerifier::~MethodVerifier() { |
| 4993 | Thread::Current()->PopVerifier(this); |
| 4994 | STLDeleteElements(&failure_messages_); |
| 4995 | } |
| 4996 | |
| 4997 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 4998 | ClassLinker* class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 4999 | ArenaPool* arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5000 | VerifierDeps* verifier_deps, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5001 | uint32_t method_idx, |
| 5002 | const DexFile* dex_file, |
| 5003 | Handle<mirror::DexCache> dex_cache, |
| 5004 | Handle<mirror::ClassLoader> class_loader, |
| 5005 | const dex::ClassDef& class_def, |
| 5006 | const dex::CodeItem* code_item, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5007 | uint32_t method_access_flags, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5008 | bool allow_soft_failures, |
| 5009 | HardFailLogMode log_level, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5010 | uint32_t api_level, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5011 | bool aot_mode, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5012 | std::string* hard_failure_msg) { |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5013 | if (VLOG_IS_ON(verifier_debug)) { |
| 5014 | return VerifyMethod<true>(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5015 | class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5016 | arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5017 | verifier_deps, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5018 | method_idx, |
| 5019 | dex_file, |
| 5020 | dex_cache, |
| 5021 | class_loader, |
| 5022 | class_def, |
| 5023 | code_item, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5024 | method_access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5025 | allow_soft_failures, |
| 5026 | log_level, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5027 | api_level, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5028 | aot_mode, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5029 | hard_failure_msg); |
| 5030 | } else { |
| 5031 | return VerifyMethod<false>(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5032 | class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5033 | arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5034 | verifier_deps, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5035 | method_idx, |
| 5036 | dex_file, |
| 5037 | dex_cache, |
| 5038 | class_loader, |
| 5039 | class_def, |
| 5040 | code_item, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5041 | method_access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5042 | allow_soft_failures, |
| 5043 | log_level, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5044 | api_level, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5045 | aot_mode, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5046 | hard_failure_msg); |
| 5047 | } |
| 5048 | } |
| 5049 | |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5050 | // Return whether the runtime knows how to execute a method without needing to |
| Nicolas Geoffray | bd57059 | 2020-12-23 16:37:44 +0000 | [diff] [blame] | 5051 | // re-verify it at runtime (and therefore save on first use of the class). |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5052 | // The AOT/JIT compiled code is not affected. |
| 5053 | static inline bool CanRuntimeHandleVerificationFailure(uint32_t encountered_failure_types) { |
| 5054 | constexpr uint32_t unresolved_mask = |
| Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 5055 | verifier::VerifyError::VERIFY_ERROR_UNRESOLVED_TYPE_CHECK | |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 5056 | verifier::VerifyError::VERIFY_ERROR_NO_CLASS | |
| Nicolas Geoffray | b677aff | 2020-12-23 17:07:10 +0000 | [diff] [blame] | 5057 | verifier::VerifyError::VERIFY_ERROR_CLASS_CHANGE | |
| Nicolas Geoffray | 54ed015 | 2020-12-23 17:29:32 +0000 | [diff] [blame] | 5058 | verifier::VerifyError::VERIFY_ERROR_INSTANTIATION | |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5059 | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS | |
| 5060 | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD | |
| Nicolas Geoffray | bd57059 | 2020-12-23 16:37:44 +0000 | [diff] [blame] | 5061 | verifier::VerifyError::VERIFY_ERROR_NO_METHOD | |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 5062 | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD | |
| 5063 | verifier::VerifyError::VERIFY_ERROR_RUNTIME_THROW; |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5064 | return (encountered_failure_types & (~unresolved_mask)) == 0; |
| 5065 | } |
| 5066 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5067 | template <bool kVerifierDebug> |
| 5068 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5069 | ClassLinker* class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5070 | ArenaPool* arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5071 | VerifierDeps* verifier_deps, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5072 | uint32_t method_idx, |
| 5073 | const DexFile* dex_file, |
| 5074 | Handle<mirror::DexCache> dex_cache, |
| 5075 | Handle<mirror::ClassLoader> class_loader, |
| 5076 | const dex::ClassDef& class_def, |
| 5077 | const dex::CodeItem* code_item, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5078 | uint32_t method_access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5079 | bool allow_soft_failures, |
| 5080 | HardFailLogMode log_level, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5081 | uint32_t api_level, |
| Andreas Gampe | fef91cc | 2019-07-25 14:13:23 -0700 | [diff] [blame] | 5082 | bool aot_mode, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5083 | std::string* hard_failure_msg) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5084 | MethodVerifier::FailureData result; |
| 5085 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
| 5086 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5087 | impl::MethodVerifier<kVerifierDebug> verifier(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5088 | class_linker, |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5089 | arena_pool, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5090 | verifier_deps, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5091 | dex_file, |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5092 | code_item, |
| 5093 | method_idx, |
| Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 5094 | /* can_load_classes= */ true, |
| 5095 | /* allow_thread_suspension= */ true, |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5096 | allow_soft_failures, |
| 5097 | aot_mode, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5098 | dex_cache, |
| 5099 | class_loader, |
| 5100 | class_def, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5101 | method_access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5102 | /* verify to dump */ false, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5103 | api_level); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5104 | if (verifier.Verify()) { |
| 5105 | // Verification completed, however failures may be pending that didn't cause the verification |
| 5106 | // to hard fail. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5107 | CHECK(!verifier.flags_.have_pending_hard_failure_); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5108 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5109 | if (verifier.failures_.size() != 0) { |
| 5110 | if (VLOG_IS_ON(verifier)) { |
| 5111 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
| 5112 | << dex_file->PrettyMethod(method_idx) << "\n"); |
| 5113 | } |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5114 | if (kVerifierDebug) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5115 | LOG(INFO) << verifier.info_messages_.str(); |
| 5116 | verifier.Dump(LOG_STREAM(INFO)); |
| 5117 | } |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5118 | if (CanRuntimeHandleVerificationFailure(verifier.encountered_failure_types_)) { |
| Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 5119 | if (verifier.encountered_failure_types_ & VERIFY_ERROR_UNRESOLVED_TYPE_CHECK) { |
| 5120 | result.kind = FailureKind::kTypeChecksFailure; |
| 5121 | } else { |
| 5122 | result.kind = FailureKind::kAccessChecksFailure; |
| 5123 | } |
| Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5124 | } else { |
| 5125 | result.kind = FailureKind::kSoftFailure; |
| 5126 | } |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5127 | } |
| 5128 | } else { |
| 5129 | // Bad method data. |
| 5130 | CHECK_NE(verifier.failures_.size(), 0U); |
| Nicolas Geoffray | f8b5288 | 2021-06-25 17:00:30 +0100 | [diff] [blame] | 5131 | CHECK(verifier.flags_.have_pending_hard_failure_); |
| 5132 | if (VLOG_IS_ON(verifier)) { |
| 5133 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
| 5134 | } |
| 5135 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 5136 | LogSeverity severity; |
| 5137 | switch (log_level) { |
| 5138 | case HardFailLogMode::kLogVerbose: |
| 5139 | severity = LogSeverity::VERBOSE; |
| 5140 | break; |
| 5141 | case HardFailLogMode::kLogWarning: |
| 5142 | severity = LogSeverity::WARNING; |
| 5143 | break; |
| 5144 | case HardFailLogMode::kLogInternalFatal: |
| 5145 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
| 5146 | break; |
| 5147 | default: |
| 5148 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 5149 | UNREACHABLE(); |
| 5150 | } |
| 5151 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
| 5152 | << dex_file->PrettyMethod(method_idx) |
| 5153 | << "\n"); |
| 5154 | } |
| 5155 | if (hard_failure_msg != nullptr) { |
| 5156 | CHECK(!verifier.failure_messages_.empty()); |
| 5157 | *hard_failure_msg = |
| 5158 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 5159 | } |
| 5160 | result.kind = FailureKind::kHardFailure; |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5161 | |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5162 | if (kVerifierDebug || VLOG_IS_ON(verifier)) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5163 | LOG(ERROR) << verifier.info_messages_.str(); |
| 5164 | verifier.Dump(LOG_STREAM(ERROR)); |
| 5165 | } |
| 5166 | // Under verifier-debug, dump the complete log into the error message. |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5167 | if (kVerifierDebug && hard_failure_msg != nullptr) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5168 | hard_failure_msg->append("\n"); |
| 5169 | hard_failure_msg->append(verifier.info_messages_.str()); |
| 5170 | hard_failure_msg->append("\n"); |
| 5171 | std::ostringstream oss; |
| 5172 | verifier.Dump(oss); |
| 5173 | hard_failure_msg->append(oss.str()); |
| 5174 | } |
| 5175 | } |
| 5176 | if (kTimeVerifyMethod) { |
| 5177 | uint64_t duration_ns = NanoTime() - start_ns; |
| 5178 | if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) { |
| Andreas Gampe | efdd1b0 | 2019-05-07 12:30:10 -0700 | [diff] [blame] | 5179 | double bytecodes_per_second = |
| 5180 | verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5181 | LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx) |
| 5182 | << " took " << PrettyDuration(duration_ns) |
| Andreas Gampe | efdd1b0 | 2019-05-07 12:30:10 -0700 | [diff] [blame] | 5183 | << (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "") |
| Andreas Gampe | d91f841 | 2019-06-13 10:11:14 -0700 | [diff] [blame] | 5184 | << " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)" |
| 5185 | << " (" << verifier.allocator_.ApproximatePeakBytes() |
| 5186 | << "B approximate peak alloc)"; |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5187 | } |
| 5188 | } |
| 5189 | result.types = verifier.encountered_failure_types_; |
| 5190 | return result; |
| 5191 | } |
| 5192 | |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5193 | MethodVerifier* MethodVerifier::CalculateVerificationInfo( |
| 5194 | Thread* self, |
| 5195 | ArtMethod* method, |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 5196 | uint32_t dex_pc) { |
| 5197 | StackHandleScope<2> hs(self); |
| 5198 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 5199 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method->GetClassLoader())); |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5200 | std::unique_ptr<impl::MethodVerifier<false>> verifier( |
| 5201 | new impl::MethodVerifier<false>(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5202 | Runtime::Current()->GetClassLinker(), |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5203 | Runtime::Current()->GetArenaPool(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5204 | /* verifier_deps= */ nullptr, |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5205 | method->GetDexFile(), |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5206 | method->GetCodeItem(), |
| 5207 | method->GetDexMethodIndex(), |
| 5208 | /* can_load_classes= */ false, |
| 5209 | /* allow_thread_suspension= */ false, |
| 5210 | /* allow_soft_failures= */ true, |
| 5211 | Runtime::Current()->IsAotCompiler(), |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5212 | dex_cache, |
| 5213 | class_loader, |
| 5214 | *method->GetDeclaringClass()->GetClassDef(), |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5215 | method->GetAccessFlags(), |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5216 | /* verify_to_dump= */ false, |
| Alex Light | af52cbe | 2019-05-22 15:05:09 -0700 | [diff] [blame] | 5217 | // Just use the verifier at the current skd-version. |
| 5218 | // This might affect what soft-verifier errors are reported. |
| 5219 | // Callers can then filter out relevant errors if needed. |
| 5220 | Runtime::Current()->GetTargetSdkVersion())); |
| Nicolas Geoffray | 2cb2527 | 2021-07-15 14:24:29 +0100 | [diff] [blame] | 5221 | verifier->interesting_dex_pc_ = dex_pc; |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5222 | verifier->Verify(); |
| 5223 | if (VLOG_IS_ON(verifier)) { |
| 5224 | verifier->DumpFailures(VLOG_STREAM(verifier)); |
| 5225 | VLOG(verifier) << verifier->info_messages_.str(); |
| 5226 | verifier->Dump(VLOG_STREAM(verifier)); |
| 5227 | } |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5228 | if (verifier->flags_.have_pending_hard_failure_) { |
| Alex Light | e2ddce3 | 2019-05-22 17:08:35 +0000 | [diff] [blame] | 5229 | return nullptr; |
| 5230 | } else { |
| 5231 | return verifier.release(); |
| 5232 | } |
| 5233 | } |
| 5234 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5235 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 5236 | VariableIndentationOutputStream* vios, |
| 5237 | uint32_t dex_method_idx, |
| 5238 | const DexFile* dex_file, |
| 5239 | Handle<mirror::DexCache> dex_cache, |
| 5240 | Handle<mirror::ClassLoader> class_loader, |
| 5241 | const dex::ClassDef& class_def, |
| 5242 | const dex::CodeItem* code_item, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5243 | uint32_t method_access_flags, |
| 5244 | uint32_t api_level) { |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5245 | impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>( |
| 5246 | self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5247 | Runtime::Current()->GetClassLinker(), |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5248 | Runtime::Current()->GetArenaPool(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5249 | /* verifier_deps= */ nullptr, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5250 | dex_file, |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5251 | code_item, |
| 5252 | dex_method_idx, |
| 5253 | /* can_load_classes= */ true, |
| 5254 | /* allow_thread_suspension= */ true, |
| 5255 | /* allow_soft_failures= */ true, |
| 5256 | Runtime::Current()->IsAotCompiler(), |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5257 | dex_cache, |
| 5258 | class_loader, |
| 5259 | class_def, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5260 | method_access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5261 | /* verify_to_dump= */ true, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5262 | api_level); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5263 | verifier->Verify(); |
| 5264 | verifier->DumpFailures(vios->Stream()); |
| 5265 | vios->Stream() << verifier->info_messages_.str(); |
| 5266 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 5267 | // and querying any info is dangerous/can abort. |
| Andreas Gampe | 43884b2 | 2019-06-27 14:05:52 -0700 | [diff] [blame] | 5268 | if (verifier->flags_.have_pending_hard_failure_) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5269 | delete verifier; |
| 5270 | return nullptr; |
| 5271 | } else { |
| 5272 | verifier->Dump(vios); |
| 5273 | return verifier; |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | void MethodVerifier::FindLocksAtDexPc( |
| 5278 | ArtMethod* m, |
| 5279 | uint32_t dex_pc, |
| 5280 | std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs, |
| 5281 | uint32_t api_level) { |
| 5282 | StackHandleScope<2> hs(Thread::Current()); |
| 5283 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 5284 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5285 | impl::MethodVerifier<false> verifier(hs.Self(), |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5286 | Runtime::Current()->GetClassLinker(), |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5287 | Runtime::Current()->GetArenaPool(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5288 | /* verifier_deps= */ nullptr, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5289 | m->GetDexFile(), |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5290 | m->GetCodeItem(), |
| 5291 | m->GetDexMethodIndex(), |
| 5292 | /* can_load_classes= */ false, |
| 5293 | /* allow_thread_suspension= */ false, |
| 5294 | /* allow_soft_failures= */ true, |
| 5295 | Runtime::Current()->IsAotCompiler(), |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5296 | dex_cache, |
| 5297 | class_loader, |
| 5298 | m->GetClassDef(), |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5299 | m->GetAccessFlags(), |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5300 | /* verify_to_dump= */ false, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5301 | api_level); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5302 | verifier.interesting_dex_pc_ = dex_pc; |
| 5303 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
| 5304 | verifier.FindLocksAtDexPc(); |
| 5305 | } |
| 5306 | |
| 5307 | MethodVerifier* MethodVerifier::CreateVerifier(Thread* self, |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5308 | VerifierDeps* verifier_deps, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5309 | const DexFile* dex_file, |
| 5310 | Handle<mirror::DexCache> dex_cache, |
| 5311 | Handle<mirror::ClassLoader> class_loader, |
| 5312 | const dex::ClassDef& class_def, |
| 5313 | const dex::CodeItem* code_item, |
| 5314 | uint32_t method_idx, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5315 | uint32_t access_flags, |
| 5316 | bool can_load_classes, |
| 5317 | bool allow_soft_failures, |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5318 | bool verify_to_dump, |
| 5319 | bool allow_thread_suspension, |
| 5320 | uint32_t api_level) { |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5321 | return new impl::MethodVerifier<false>(self, |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5322 | Runtime::Current()->GetClassLinker(), |
| Andreas Gampe | f1468b5 | 2019-07-26 09:22:39 -0700 | [diff] [blame] | 5323 | Runtime::Current()->GetArenaPool(), |
| Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5324 | verifier_deps, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5325 | dex_file, |
| Andreas Gampe | 0134a28 | 2019-07-29 11:34:55 -0700 | [diff] [blame] | 5326 | code_item, |
| 5327 | method_idx, |
| 5328 | can_load_classes, |
| 5329 | allow_thread_suspension, |
| 5330 | allow_soft_failures, |
| 5331 | Runtime::Current()->IsAotCompiler(), |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5332 | dex_cache, |
| 5333 | class_loader, |
| 5334 | class_def, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5335 | access_flags, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5336 | verify_to_dump, |
| Andreas Gampe | 99db7bb | 2019-04-19 23:05:47 -0700 | [diff] [blame] | 5337 | api_level); |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5338 | } |
| 5339 | |
| Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 5340 | void MethodVerifier::Init(ClassLinker* class_linker) { |
| 5341 | art::verifier::RegTypeCache::Init(class_linker); |
| Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5342 | } |
| 5343 | |
| Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5344 | void MethodVerifier::Shutdown() { |
| Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5345 | verifier::RegTypeCache::ShutDown(); |
| Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5346 | } |
| jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5347 | |
| Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5348 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5349 | RegTypeCache::VisitStaticRoots(visitor); |
| Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
| Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5352 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5353 | reg_types_.VisitRoots(visitor, root_info); |
| Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5354 | } |
| 5355 | |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5356 | std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) { |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5357 | // Mark the error type as encountered. |
| 5358 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
| 5359 | |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5360 | if (pending_exc) { |
| 5361 | switch (error) { |
| 5362 | case VERIFY_ERROR_NO_CLASS: |
| Nicolas Geoffray | 04ea42c | 2021-01-22 10:04:56 +0000 | [diff] [blame] | 5363 | case VERIFY_ERROR_UNRESOLVED_TYPE_CHECK: |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5364 | case VERIFY_ERROR_NO_METHOD: |
| 5365 | case VERIFY_ERROR_ACCESS_CLASS: |
| 5366 | case VERIFY_ERROR_ACCESS_FIELD: |
| 5367 | case VERIFY_ERROR_ACCESS_METHOD: |
| 5368 | case VERIFY_ERROR_INSTANTIATION: |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 5369 | case VERIFY_ERROR_CLASS_CHANGE: { |
| 5370 | PotentiallyMarkRuntimeThrow(); |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5371 | break; |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 5372 | } |
| Andreas Gampe | 7a82acc | 2019-07-01 13:54:21 -0700 | [diff] [blame] | 5373 | |
| Nicolas Geoffray | 35a3720 | 2021-01-29 17:53:32 +0000 | [diff] [blame] | 5374 | case VERIFY_ERROR_LOCKING: |
| Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 5375 | PotentiallyMarkRuntimeThrow(); |
| Nicolas Geoffray | 35a3720 | 2021-01-29 17:53:32 +0000 | [diff] [blame] | 5376 | // This will be reported to the runtime as a soft failure. |
| 5377 | break; |
| 5378 | |
| Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 5379 | // Indication that verification should be retried at runtime. |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5380 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
| 5381 | if (!allow_soft_failures_) { |
| 5382 | flags_.have_pending_hard_failure_ = true; |
| 5383 | } |
| 5384 | break; |
| 5385 | |
| Nicolas Geoffray | 35a3720 | 2021-01-29 17:53:32 +0000 | [diff] [blame] | 5386 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 5387 | // marked as rejected to prevent it from being compiled. |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5388 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
| Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5389 | flags_.have_pending_hard_failure_ = true; |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5390 | break; |
| Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5391 | } |
| Nicolas Geoffray | 2ec3823 | 2021-07-02 16:36:29 +0100 | [diff] [blame] | 5392 | |
| 5393 | case VERIFY_ERROR_RUNTIME_THROW: { |
| 5394 | LOG(FATAL) << "UNREACHABLE"; |
| 5395 | } |
| Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5396 | } |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5397 | } else if (kIsDebugBuild) { |
| 5398 | CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT); |
| 5399 | CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD); |
| Andreas Gampe | 4a30f89 | 2019-07-10 04:57:58 +0000 | [diff] [blame] | 5400 | } |
| Andreas Gampe | 4146e06 | 2019-07-10 13:18:04 -0700 | [diff] [blame] | 5401 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5402 | failures_.push_back(error); |
| 5403 | std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(), |
| 5404 | work_insn_idx_)); |
| 5405 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
| 5406 | failure_messages_.push_back(failure_message); |
| 5407 | return *failure_message; |
| Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
| Andreas Gampe | fc25ae9 | 2019-04-19 22:22:57 -0700 | [diff] [blame] | 5410 | ScopedNewLine MethodVerifier::LogVerifyInfo() { |
| 5411 | ScopedNewLine ret{info_messages_}; |
| 5412 | ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_) |
| 5413 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 5414 | return ret; |
| 5415 | } |
| 5416 | |
| 5417 | static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) { |
| 5418 | static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure |
| 5419 | && FailureKind::kSoftFailure < FailureKind::kHardFailure, |
| 5420 | "Unexpected FailureKind order"); |
| 5421 | return std::max(fk1, fk2); |
| 5422 | } |
| 5423 | |
| 5424 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 5425 | kind = FailureKindMax(kind, fd.kind); |
| 5426 | types |= fd.types; |
| Andreas Gampe | 51de69e | 2019-04-19 15:14:14 -0700 | [diff] [blame] | 5427 | } |
| 5428 | |
| Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5429 | } // namespace verifier |
| Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5430 | } // namespace art |