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 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 25 | #include "base/aborting.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 26 | #include "base/enums.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 27 | #include "base/leb128.h" |
David Sehr | 9c4a015 | 2018-04-05 12:23:54 -0700 | [diff] [blame] | 28 | #include "base/indenter.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 29 | #include "base/logging.h" // For VLOG. |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 30 | #include "base/mutex-inl.h" |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 31 | #include "base/sdk_version.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 32 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 33 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 34 | #include "base/time_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 35 | #include "base/utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 36 | #include "class_linker.h" |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 37 | #include "class_root.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 38 | #include "compiler_callbacks.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 39 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 40 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 41 | #include "dex/dex_file-inl.h" |
| 42 | #include "dex/dex_file_exception_helpers.h" |
| 43 | #include "dex/dex_instruction-inl.h" |
| 44 | #include "dex/dex_instruction_utils.h" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 45 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 47 | #include "handle_scope-inl.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 48 | #include "intern_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 50 | #include "mirror/class.h" |
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" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 57 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 58 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 59 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 60 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 61 | #include "stack.h" |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 62 | #include "vdex_file.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 63 | #include "verifier_compiler_binding.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 64 | #include "verifier_deps.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 65 | |
| 66 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 67 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 68 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 69 | using android::base::StringPrintf; |
| 70 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 71 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 72 | |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 73 | // On VLOG(verifier), should we dump the whole state when we run into a hard failure? |
| 74 | static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true; |
| 75 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 76 | // We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make |
| 77 | // sure we only print this once. |
| 78 | static bool gPrintedDxMonitorText = false; |
| 79 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 80 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator) |
| 81 | : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {} |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 82 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 83 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 84 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 85 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 86 | DCHECK_GT(insns_size, 0U); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 87 | register_lines_.resize(insns_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 88 | for (uint32_t i = 0; i < insns_size; i++) { |
| 89 | bool interesting = false; |
| 90 | switch (mode) { |
| 91 | case kTrackRegsAll: |
| 92 | interesting = flags[i].IsOpcode(); |
| 93 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 94 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 95 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 96 | break; |
| 97 | case kTrackRegsBranches: |
| 98 | interesting = flags[i].IsBranchTarget(); |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | if (interesting) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 104 | register_lines_[i].reset(RegisterLine::Create(registers_size, verifier)); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 109 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 110 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 111 | // Note: returns true on failure. |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 112 | inline bool MethodVerifier::FailOrAbort(bool condition, |
| 113 | const char* error_msg, |
| 114 | uint32_t work_insn_idx) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 115 | if (kIsDebugBuild) { |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 116 | // In a debug build, abort if the error condition is wrong. Only warn if |
| 117 | // we are already aborting (as this verification is likely run to print |
| 118 | // lock information). |
| 119 | if (LIKELY(gAborting == 0)) { |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 120 | DCHECK(condition) << error_msg << work_insn_idx << " " |
| 121 | << dex_file_->PrettyMethod(dex_method_idx_); |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 122 | } else { |
| 123 | if (!condition) { |
| 124 | LOG(ERROR) << error_msg << work_insn_idx; |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 125 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 129 | } else { |
| 130 | // In a non-debug build, just fail the class. |
| 131 | if (!condition) { |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 132 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 140 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 141 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 142 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 143 | reg_line->CheckConstructorReturn(verifier); |
| 144 | } |
| 145 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 146 | } |
| 147 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 148 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 149 | ObjPtr<mirror::Class> klass, |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 150 | CompilerCallbacks* callbacks, |
| 151 | bool allow_soft_failures, |
| 152 | HardFailLogMode log_level, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 153 | uint32_t api_level, |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 154 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 155 | if (klass->IsVerified()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 156 | return FailureKind::kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 157 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 158 | bool early_failure = false; |
| 159 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 160 | const DexFile& dex_file = klass->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 161 | const dex::ClassDef* class_def = klass->GetClassDef(); |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 162 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 163 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 164 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 165 | early_failure = true; |
| 166 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 167 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 168 | early_failure = true; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 169 | failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 170 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 171 | early_failure = true; |
| 172 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 173 | } |
| 174 | if (early_failure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 175 | *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 176 | if (callbacks != nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 177 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 178 | callbacks->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 179 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 180 | return FailureKind::kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 181 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 182 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 183 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 184 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 185 | return VerifyClass(self, |
| 186 | &dex_file, |
| 187 | dex_cache, |
| 188 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 189 | *class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 190 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 191 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 192 | log_level, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 193 | api_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 194 | error); |
| 195 | } |
| 196 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 197 | static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) { |
| 198 | static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure |
| 199 | && FailureKind::kSoftFailure < FailureKind::kHardFailure, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 200 | "Unexpected FailureKind order"); |
| 201 | return std::max(fk1, fk2); |
| 202 | } |
| 203 | |
| 204 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 205 | kind = FailureKindMax(kind, fd.kind); |
| 206 | types |= fd.types; |
| 207 | } |
| 208 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 209 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 210 | const DexFile* dex_file, |
| 211 | Handle<mirror::DexCache> dex_cache, |
| 212 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 213 | const dex::ClassDef& class_def, |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 214 | CompilerCallbacks* callbacks, |
| 215 | bool allow_soft_failures, |
| 216 | HardFailLogMode log_level, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 217 | uint32_t api_level, |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 218 | std::string* error) { |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 219 | // A class must not be abstract and final. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 220 | if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 221 | *error = "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 222 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 223 | *error += ": class is abstract and final."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 224 | return FailureKind::kHardFailure; |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 227 | ClassAccessor accessor(*dex_file, class_def); |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 228 | SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(accessor.GetDescriptor()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 229 | |
| 230 | int64_t previous_method_idx[2] = { -1, -1 }; |
| 231 | MethodVerifier::FailureData failure_data; |
| 232 | ClassLinker* const linker = Runtime::Current()->GetClassLinker(); |
| 233 | |
| 234 | for (const ClassAccessor::Method& method : accessor.GetMethods()) { |
| 235 | int64_t* previous_idx = &previous_method_idx[method.IsStaticOrDirect() ? 0u : 1u]; |
| 236 | self->AllowThreadSuspension(); |
| 237 | const uint32_t method_idx = method.GetIndex(); |
| 238 | if (method_idx == *previous_idx) { |
| 239 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 240 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 241 | continue; |
| 242 | } |
| 243 | *previous_idx = method_idx; |
| 244 | const InvokeType type = method.GetInvokeType(class_def.access_flags_); |
| 245 | ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 246 | method_idx, dex_cache, class_loader, /* referrer= */ nullptr, type); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 247 | if (resolved_method == nullptr) { |
| 248 | DCHECK(self->IsExceptionPending()); |
| 249 | // We couldn't resolve the method, but continue regardless. |
| 250 | self->ClearException(); |
| 251 | } else { |
| 252 | DCHECK(resolved_method->GetDeclaringClassUnchecked() != nullptr) << type; |
| 253 | } |
| 254 | std::string hard_failure_msg; |
| 255 | MethodVerifier::FailureData result = VerifyMethod(self, |
| 256 | method_idx, |
| 257 | dex_file, |
| 258 | dex_cache, |
| 259 | class_loader, |
| 260 | class_def, |
| 261 | method.GetCodeItem(), |
| 262 | resolved_method, |
| 263 | method.GetAccessFlags(), |
| 264 | callbacks, |
| 265 | allow_soft_failures, |
| 266 | log_level, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 267 | /*need_precise_constants=*/ false, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 268 | api_level, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 269 | &hard_failure_msg); |
| 270 | if (result.kind == FailureKind::kHardFailure) { |
| 271 | if (failure_data.kind == FailureKind::kHardFailure) { |
| 272 | // If we logged an error before, we need a newline. |
| 273 | *error += "\n"; |
| 274 | } else { |
| 275 | // If we didn't log a hard failure before, print the header of the message. |
| 276 | *error += "Verifier rejected class "; |
| 277 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
| 278 | *error += ":"; |
| 279 | } |
| 280 | *error += " "; |
| 281 | *error += hard_failure_msg; |
| 282 | } |
| 283 | failure_data.Merge(result); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 284 | } |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 285 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 286 | if (failure_data.kind == FailureKind::kNoFailure) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 287 | return FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 288 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 289 | if ((failure_data.types & VERIFY_ERROR_LOCKING) != 0) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 290 | // Print a warning about expected slow-down. Use a string temporary to print one contiguous |
| 291 | // warning. |
| 292 | std::string tmp = |
| 293 | StringPrintf("Class %s failed lock verification and will run slower.", |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 294 | PrettyDescriptor(accessor.GetDescriptor()).c_str()); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 295 | if (!gPrintedDxMonitorText) { |
| 296 | tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n" |
| 297 | "and incorrect proguard optimizations."; |
| 298 | gPrintedDxMonitorText = true; |
| 299 | } |
| 300 | LOG(WARNING) << tmp; |
| 301 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 302 | return failure_data.kind; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 303 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 306 | static bool IsLargeMethod(const CodeItemDataAccessor& accessor) { |
| 307 | if (!accessor.HasCodeItem()) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 311 | uint16_t registers_size = accessor.RegistersSize(); |
| 312 | uint32_t insns_size = accessor.InsnsSizeInCodeUnits(); |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 313 | |
| 314 | return registers_size * insns_size > 4*1024*1024; |
| 315 | } |
| 316 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 317 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 318 | uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 319 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 320 | Handle<mirror::DexCache> dex_cache, |
| 321 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 322 | const dex::ClassDef& class_def, |
| 323 | const dex::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 324 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 325 | uint32_t method_access_flags, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 326 | CompilerCallbacks* callbacks, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 327 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 328 | HardFailLogMode log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 329 | bool need_precise_constants, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 330 | uint32_t api_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 331 | std::string* hard_failure_msg) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 332 | MethodVerifier::FailureData result; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 333 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 334 | |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 335 | MethodVerifier verifier(self, |
| 336 | dex_file, |
| 337 | dex_cache, |
| 338 | class_loader, |
| 339 | class_def, |
| 340 | code_item, |
| 341 | method_idx, |
| 342 | method, |
| 343 | method_access_flags, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 344 | /* can_load_classes= */ true, |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 345 | allow_soft_failures, |
| 346 | need_precise_constants, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 347 | /* verify to dump */ false, |
| 348 | /* allow_thread_suspension= */ true, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 349 | api_level); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 350 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 351 | // Verification completed, however failures may be pending that didn't cause the verification |
| 352 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 353 | CHECK(!verifier.have_pending_hard_failure_); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 354 | |
| 355 | if (code_item != nullptr && callbacks != nullptr) { |
| 356 | // Let the interested party know that the method was verified. |
| 357 | callbacks->MethodVerified(&verifier); |
| 358 | } |
| 359 | |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 360 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 361 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 362 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 363 | << dex_file->PrettyMethod(method_idx) << "\n"); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 364 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 365 | if (VLOG_IS_ON(verifier_debug)) { |
| 366 | std::cout << "\n" << verifier.info_messages_.str(); |
| 367 | verifier.Dump(std::cout); |
| 368 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 369 | result.kind = FailureKind::kSoftFailure; |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 370 | if (method != nullptr && |
| 371 | !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 372 | method->SetDontCompile(); |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 373 | } |
| 374 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 375 | if (method != nullptr) { |
| 376 | if (verifier.HasInstructionThatWillThrow()) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 377 | method->SetDontCompile(); |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 378 | if (Runtime::Current()->IsAotCompiler() && |
| 379 | (callbacks != nullptr) && !callbacks->IsBootImage()) { |
| 380 | // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger |
| 381 | // re-verification at runtime. |
| 382 | // The dead code after the throw is not verified and might be invalid. This may cause |
| 383 | // the JIT compiler to crash since it assumes that all the code is valid. |
| 384 | // |
| 385 | // There's a strong assumption that the entire boot image is verified and all its dex |
| 386 | // code is valid (even the dead and unverified one). As such this is done only for apps. |
| 387 | // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are |
| 388 | // fully verified). |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 389 | result.kind = FailureKind::kSoftFailure; |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 390 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 391 | } |
| 392 | if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 393 | method->SetMustCountLocks(); |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 394 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 395 | } |
| 396 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 397 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 398 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 399 | |
| 400 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 401 | // Failed due to being forced into interpreter. This is ok because |
| 402 | // we just want to skip verification. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 403 | result.kind = FailureKind::kSoftFailure; |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 404 | } else { |
| 405 | CHECK(verifier.have_pending_hard_failure_); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 406 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 407 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 408 | } |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 409 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 410 | LogSeverity severity; |
| 411 | switch (log_level) { |
| 412 | case HardFailLogMode::kLogVerbose: |
| 413 | severity = LogSeverity::VERBOSE; |
| 414 | break; |
| 415 | case HardFailLogMode::kLogWarning: |
| 416 | severity = LogSeverity::WARNING; |
| 417 | break; |
| 418 | case HardFailLogMode::kLogInternalFatal: |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 419 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 420 | break; |
| 421 | default: |
| 422 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 423 | UNREACHABLE(); |
| 424 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 425 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 426 | << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 427 | << "\n"); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 428 | } |
| 429 | if (hard_failure_msg != nullptr) { |
| 430 | CHECK(!verifier.failure_messages_.empty()); |
| 431 | *hard_failure_msg = |
| 432 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 433 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 434 | result.kind = FailureKind::kHardFailure; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 435 | |
| 436 | if (callbacks != nullptr) { |
| 437 | // Let the interested party know that we failed the class. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 438 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 439 | callbacks->ClassRejected(ref); |
| 440 | } |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 441 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 442 | if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 443 | std::cout << "\n" << verifier.info_messages_.str(); |
| 444 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 445 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 446 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 447 | if (kTimeVerifyMethod) { |
| 448 | uint64_t duration_ns = NanoTime() - start_ns; |
Andreas Gampe | 0b0ffc1 | 2018-08-01 14:41:27 -0700 | [diff] [blame] | 449 | if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 450 | LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 451 | << " took " << PrettyDuration(duration_ns) |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 452 | << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 453 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 454 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 455 | result.types = verifier.encountered_failure_types_; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 456 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 459 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 460 | VariableIndentationOutputStream* vios, |
| 461 | uint32_t dex_method_idx, |
| 462 | const DexFile* dex_file, |
| 463 | Handle<mirror::DexCache> dex_cache, |
| 464 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 465 | const dex::ClassDef& class_def, |
| 466 | const dex::CodeItem* code_item, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 467 | ArtMethod* method, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 468 | uint32_t method_access_flags, |
| 469 | uint32_t api_level) { |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 470 | MethodVerifier* verifier = new MethodVerifier(self, |
| 471 | dex_file, |
| 472 | dex_cache, |
| 473 | class_loader, |
| 474 | class_def, |
| 475 | code_item, |
| 476 | dex_method_idx, |
| 477 | method, |
| 478 | method_access_flags, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 479 | /* can_load_classes= */ true, |
| 480 | /* allow_soft_failures= */ true, |
| 481 | /* need_precise_constants= */ true, |
| 482 | /* verify_to_dump= */ true, |
| 483 | /* allow_thread_suspension= */ true, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 484 | api_level); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 485 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 486 | verifier->DumpFailures(vios->Stream()); |
| 487 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 488 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 489 | // and querying any info is dangerous/can abort. |
| 490 | if (verifier->have_pending_hard_failure_) { |
| 491 | delete verifier; |
| 492 | return nullptr; |
| 493 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 494 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 495 | return verifier; |
| 496 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 499 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 500 | const DexFile* dex_file, |
| 501 | Handle<mirror::DexCache> dex_cache, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 502 | Handle<mirror::ClassLoader> class_loader, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 503 | const dex::ClassDef& class_def, |
| 504 | const dex::CodeItem* code_item, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 505 | uint32_t dex_method_idx, |
| 506 | ArtMethod* method, |
| 507 | uint32_t method_access_flags, |
| 508 | bool can_load_classes, |
| 509 | bool allow_soft_failures, |
| 510 | bool need_precise_constants, |
| 511 | bool verify_to_dump, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 512 | bool allow_thread_suspension, |
| 513 | uint32_t api_level) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 514 | : self_(self), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 515 | arena_stack_(Runtime::Current()->GetArenaPool()), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 516 | allocator_(&arena_stack_), |
Alex Light | d9aff13 | 2017-10-31 22:30:05 +0000 | [diff] [blame] | 517 | reg_types_(can_load_classes, allocator_, allow_thread_suspension), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 518 | reg_table_(allocator_), |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 519 | work_insn_idx_(dex::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 520 | dex_method_idx_(dex_method_idx), |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 521 | method_being_verified_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 522 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 523 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 524 | dex_file_(dex_file), |
| 525 | dex_cache_(dex_cache), |
| 526 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 527 | class_def_(class_def), |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 528 | code_item_accessor_(*dex_file, code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 529 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 530 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 531 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 532 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 533 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 534 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 535 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 536 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 537 | monitor_enter_count_(0), |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 538 | encountered_failure_types_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 539 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 540 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 541 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 542 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 543 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 544 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 545 | allow_thread_suspension_(allow_thread_suspension), |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 546 | is_constructor_(false), |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 547 | link_(nullptr), |
| 548 | api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 549 | self->PushVerifier(this); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 552 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 553 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 554 | STLDeleteElements(&failure_messages_); |
| 555 | } |
| 556 | |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 557 | void MethodVerifier::FindLocksAtDexPc( |
| 558 | ArtMethod* m, |
| 559 | uint32_t dex_pc, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 560 | std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs, |
| 561 | uint32_t api_level) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 562 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 563 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 564 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 565 | MethodVerifier verifier(hs.Self(), |
| 566 | m->GetDexFile(), |
| 567 | dex_cache, |
| 568 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 569 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 570 | m->GetCodeItem(), |
| 571 | m->GetDexMethodIndex(), |
| 572 | m, |
| 573 | m->GetAccessFlags(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 574 | /* can_load_classes= */ false, |
| 575 | /* allow_soft_failures= */ true, |
| 576 | /* need_precise_constants= */ false, |
| 577 | /* verify_to_dump= */ false, |
| 578 | /* allow_thread_suspension= */ false, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 579 | api_level); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 580 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 581 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 582 | verifier.FindLocksAtDexPc(); |
| 583 | } |
| 584 | |
| 585 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 586 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 587 | 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] | 588 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 589 | // Quick check whether there are any monitor_enter instructions before verifying. |
| 590 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| 591 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 592 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 593 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 594 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 595 | // got what we wanted. |
| 596 | Verify(); |
| 597 | return; |
| 598 | } |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 599 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 602 | bool MethodVerifier::Verify() { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 603 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 604 | // the name. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 605 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 606 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 607 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 608 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 609 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 610 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 611 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 612 | if (!constructor_by_name) { |
| 613 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 614 | << "method is marked as constructor, but not named accordingly"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 615 | return false; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 616 | } |
| 617 | is_constructor_ = true; |
| 618 | } else if (constructor_by_name) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 619 | LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_) |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 620 | << " not marked as constructor."; |
| 621 | is_constructor_ = true; |
| 622 | } |
| 623 | // If it's a constructor, check whether IsStatic() matches the name. |
| 624 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 625 | if (kIsDebugBuild) { |
| 626 | if (IsConstructor()) { |
| 627 | if (IsStatic() ^ static_constructor_by_name) { |
| 628 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 629 | << "constructor name doesn't match static flag"; |
| 630 | return false; |
| 631 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 632 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 633 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 634 | |
| 635 | // Methods may only have one of public/protected/private. |
| 636 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 637 | if (kIsDebugBuild) { |
| 638 | size_t access_mod_count = |
| 639 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 640 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 641 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 642 | if (access_mod_count > 1) { |
| 643 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | // 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] | 649 | if (!code_item_accessor_.HasCodeItem()) { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 650 | // Only native or abstract methods may not have code. |
| 651 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 652 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 653 | return false; |
| 654 | } |
| 655 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 656 | // 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] | 657 | // 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] | 658 | if (kIsDebugBuild) { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 659 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 660 | // Abstract methods are not allowed to have the following flags. |
| 661 | static constexpr uint32_t kForbidden = |
| 662 | kAccPrivate | |
| 663 | kAccStatic | |
| 664 | kAccFinal | |
| 665 | kAccNative | |
| 666 | kAccStrict | |
| 667 | kAccSynchronized; |
| 668 | if ((method_access_flags_ & kForbidden) != 0) { |
| 669 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 670 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 671 | return false; |
| 672 | } |
| 673 | } |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 674 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 675 | // Interface methods must be public and abstract (if default methods are disabled). |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 676 | uint32_t kRequired = kAccPublic; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 677 | if ((method_access_flags_ & kRequired) != kRequired) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 678 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | // In addition to the above, interface methods must not be protected. |
| 682 | static constexpr uint32_t kForbidden = kAccProtected; |
| 683 | if ((method_access_flags_ & kForbidden) != 0) { |
| 684 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 685 | return false; |
| 686 | } |
| 687 | } |
| 688 | // We also don't allow constructors to be abstract or native. |
| 689 | if (IsConstructor()) { |
| 690 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 691 | return false; |
| 692 | } |
| 693 | } |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 698 | if (kIsDebugBuild) { |
| 699 | // When there's code, the method must not be native or abstract. |
| 700 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 701 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 702 | return false; |
| 703 | } |
| 704 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 705 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 706 | // Interfaces may always have static initializers for their fields. If we are running with |
| 707 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 708 | // Otherwise that is the only type of method allowed. |
Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 709 | if (!(IsConstructor() && IsStatic())) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 710 | if (IsInstanceConstructor()) { |
| 711 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 712 | return false; |
| 713 | } else if (method_access_flags_ & kAccFinal) { |
| 714 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 715 | return false; |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 716 | } else { |
| 717 | uint32_t access_flag_options = kAccPublic; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 718 | if (dex_file_->SupportsDefaultMethods()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 719 | access_flag_options |= kAccPrivate; |
| 720 | } |
| 721 | if (!(method_access_flags_ & access_flag_options)) { |
| 722 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 723 | << "interfaces may not have protected or package-private members"; |
| 724 | return false; |
| 725 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 726 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| 730 | // Instance constructors must not be synchronized. |
| 731 | if (IsInstanceConstructor()) { |
| 732 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 733 | if ((method_access_flags_ & kForbidden) != 0) { |
| 734 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 735 | return false; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 740 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 741 | if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) { |
| 742 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" |
| 743 | << code_item_accessor_.InsSize() |
| 744 | << " regs=" << code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 745 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 746 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 747 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 748 | // Allocate and initialize an array to hold instruction data. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 749 | insn_flags_.reset(allocator_.AllocArray<InstructionFlags>( |
| 750 | code_item_accessor_.InsnsSizeInCodeUnits())); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 751 | DCHECK(insn_flags_ != nullptr); |
| 752 | std::uninitialized_fill_n(insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 753 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 754 | InstructionFlags()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 755 | // Run through the instructions and see if the width checks out. |
| 756 | bool result = ComputeWidthsAndCountOps(); |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 757 | bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 758 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 759 | result = result && ScanTryCatchBlocks(); |
| 760 | // Perform static instruction verification. |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 761 | result = result && (allow_runtime_only_instructions |
| 762 | ? VerifyInstructions<true>() |
| 763 | : VerifyInstructions<false>()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 764 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 765 | result = result && VerifyCodeFlow(); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 766 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 767 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 770 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 771 | // Mark the error type as encountered. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 772 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 773 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 774 | switch (error) { |
| 775 | case VERIFY_ERROR_NO_CLASS: |
| 776 | case VERIFY_ERROR_NO_FIELD: |
| 777 | case VERIFY_ERROR_NO_METHOD: |
| 778 | case VERIFY_ERROR_ACCESS_CLASS: |
| 779 | case VERIFY_ERROR_ACCESS_FIELD: |
| 780 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 781 | case VERIFY_ERROR_INSTANTIATION: |
| 782 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 783 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 784 | case VERIFY_ERROR_LOCKING: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 785 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 786 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 787 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 788 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 789 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 790 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 791 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 792 | // to an interpreter. |
| 793 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 794 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 795 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 796 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 797 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 798 | |
| 799 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 800 | // try to merge garbage. |
| 801 | // Note: this assumes that Fail is called before we do any work_line modifications. |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 802 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 803 | // add a check. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 804 | if (work_insn_idx_ < dex::kDexNoIndex) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 805 | const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 806 | int opcode_flags = Instruction::FlagsOf(inst.Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 807 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 808 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 809 | saved_line_->CopyFromLine(work_line_.get()); |
| 810 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 811 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 812 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 813 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 814 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 815 | // Indication that verification should be retried at runtime. |
| 816 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 817 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 818 | have_pending_hard_failure_ = true; |
| 819 | } |
| 820 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 821 | |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 822 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 823 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 824 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 825 | have_pending_hard_failure_ = true; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 826 | if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) { |
| 827 | ScopedObjectAccess soa(Thread::Current()); |
| 828 | std::ostringstream oss; |
| 829 | Dump(oss); |
| 830 | LOG(ERROR) << oss.str(); |
| 831 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 832 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 835 | failures_.push_back(error); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 836 | std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 837 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 838 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 839 | failure_messages_.push_back(failure_message); |
| 840 | return *failure_message; |
| 841 | } |
| 842 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 843 | std::ostream& MethodVerifier::LogVerifyInfo() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 844 | return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_) |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 845 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 846 | } |
| 847 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 848 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 849 | size_t failure_num = failure_messages_.size(); |
| 850 | DCHECK_NE(failure_num, 0U); |
| 851 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 852 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 853 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 854 | delete last_fail_message; |
| 855 | } |
| 856 | |
Vladimir Marko | 5c657fe | 2016-11-03 15:12:29 +0000 | [diff] [blame] | 857 | void MethodVerifier::AppendToLastFailMessage(const std::string& append) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 858 | size_t failure_num = failure_messages_.size(); |
| 859 | DCHECK_NE(failure_num, 0U); |
| 860 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 861 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 864 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 865 | size_t new_instance_count = 0; |
| 866 | size_t monitor_enter_count = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 867 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 868 | // We can't assume the instruction is well formed, handle the case where calculating the size |
| 869 | // goes past the end of the code item. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 870 | SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end()); |
| 871 | for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 872 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 873 | SafeDexInstructionIterator next = it; |
| 874 | ++next; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 875 | if (next.IsErrorState()) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | Instruction::Code opcode = it->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 879 | switch (opcode) { |
| 880 | case Instruction::APUT_OBJECT: |
| 881 | case Instruction::CHECK_CAST: |
| 882 | has_check_casts_ = true; |
| 883 | break; |
| 884 | case Instruction::INVOKE_VIRTUAL: |
| 885 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 886 | case Instruction::INVOKE_INTERFACE: |
| 887 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 888 | has_virtual_or_interface_invokes_ = true; |
| 889 | break; |
| 890 | case Instruction::MONITOR_ENTER: |
| 891 | monitor_enter_count++; |
| 892 | break; |
| 893 | case Instruction::NEW_INSTANCE: |
| 894 | new_instance_count++; |
| 895 | break; |
| 896 | default: |
| 897 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 898 | } |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 899 | GetInstructionFlags(it.DexPc()).SetIsOpcode(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 902 | if (it != code_item_accessor_.end()) { |
| 903 | const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 904 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 905 | << it.DexPc() << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 906 | return false; |
| 907 | } |
| 908 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 909 | new_instance_count_ = new_instance_count; |
| 910 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 911 | return true; |
| 912 | } |
| 913 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 914 | bool MethodVerifier::ScanTryCatchBlocks() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 915 | const uint32_t tries_size = code_item_accessor_.TriesSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 916 | if (tries_size == 0) { |
| 917 | return true; |
| 918 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 919 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 920 | for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 921 | const uint32_t start = try_item.start_addr_; |
| 922 | const uint32_t end = start + try_item.insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 923 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 924 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 925 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 926 | return false; |
| 927 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 928 | if (!GetInstructionFlags(start).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 929 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 930 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 931 | return false; |
| 932 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 933 | DexInstructionIterator end_it(code_item_accessor_.Insns(), end); |
| 934 | for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) { |
| 935 | GetInstructionFlags(it.DexPc()).SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 938 | // Iterate over each of the handlers to verify target addresses. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 939 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 940 | const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 941 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 942 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 943 | CatchHandlerIterator iterator(handlers_ptr); |
| 944 | for (; iterator.HasNext(); iterator.Next()) { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 945 | uint32_t dex_pc = iterator.GetHandlerAddress(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 946 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 947 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 948 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 949 | return false; |
| 950 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 951 | if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) { |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 952 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 953 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 954 | return false; |
| 955 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 956 | GetInstructionFlags(dex_pc).SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 957 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 958 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 959 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 960 | ObjPtr<mirror::Class> exception_type = |
| 961 | linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 962 | if (exception_type == nullptr) { |
| 963 | DCHECK(self_->IsExceptionPending()); |
| 964 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 967 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 968 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 969 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 970 | return true; |
| 971 | } |
| 972 | |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 973 | template <bool kAllowRuntimeOnlyInstructions> |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 974 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 975 | /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 976 | GetInstructionFlags(0).SetBranchTarget(); |
| 977 | GetInstructionFlags(0).SetCompileTimeInfoPoint(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 978 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 979 | const uint32_t dex_pc = inst.DexPc(); |
| 980 | if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 981 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 982 | return false; |
| 983 | } |
| 984 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 985 | // All invoke points are marked as "Throw" points already. |
| 986 | // We are relying on this to also count all the invokes as interesting. |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 987 | if (inst->IsBranch()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 988 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 989 | // The compiler also needs safepoints for fall-through to loop heads. |
| 990 | // Such a loop head must be a target of a branch. |
| 991 | int32_t offset = 0; |
| 992 | bool cond, self_ok; |
| 993 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 994 | DCHECK(target_ok); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 995 | GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 996 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 997 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 998 | } else if (inst->IsReturn()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 999 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1000 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1001 | } |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1005 | template <bool kAllowRuntimeOnlyInstructions> |
| 1006 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Andreas Gampe | e05cc66 | 2017-05-15 10:17:30 -0700 | [diff] [blame] | 1007 | if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1008 | // Experimental instructions don't yet have verifier support implementation. |
| 1009 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 1010 | // with a virtual register that was created by an experimental instruction, |
| 1011 | // the data flow analysis will fail. |
| 1012 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 1013 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 1014 | have_pending_experimental_failure_ = true; |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1018 | bool result = true; |
| 1019 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1020 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1021 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1024 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1025 | break; |
| 1026 | } |
| 1027 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1028 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1029 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1032 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1033 | break; |
| 1034 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1035 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1036 | break; |
| 1037 | case Instruction::kVerifyRegBNewInstance: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1038 | result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | break; |
| 1040 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1041 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1042 | break; |
| 1043 | case Instruction::kVerifyRegBType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1044 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1045 | break; |
| 1046 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1047 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1048 | break; |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1049 | case Instruction::kVerifyRegBCallSite: |
| 1050 | result = result && CheckCallSiteIndex(inst->VRegB()); |
| 1051 | break; |
| 1052 | case Instruction::kVerifyRegBMethodHandle: |
| 1053 | result = result && CheckMethodHandleIndex(inst->VRegB()); |
| 1054 | break; |
| 1055 | case Instruction::kVerifyRegBPrototype: |
| 1056 | result = result && CheckPrototypeIndex(inst->VRegB()); |
| 1057 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1058 | } |
| 1059 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1060 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1061 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1062 | break; |
| 1063 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1064 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1065 | break; |
| 1066 | case Instruction::kVerifyRegCNewArray: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1067 | result = result && CheckNewArray(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1068 | break; |
| 1069 | case Instruction::kVerifyRegCType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1070 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1071 | break; |
| 1072 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1073 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1074 | break; |
| 1075 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1076 | switch (inst->GetVerifyTypeArgumentH()) { |
| 1077 | case Instruction::kVerifyRegHPrototype: |
| 1078 | result = result && CheckPrototypeIndex(inst->VRegH()); |
| 1079 | break; |
| 1080 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1081 | switch (inst->GetVerifyExtraFlags()) { |
| 1082 | case Instruction::kVerifyArrayData: |
| 1083 | result = result && CheckArrayData(code_offset); |
| 1084 | break; |
| 1085 | case Instruction::kVerifyBranchTarget: |
| 1086 | result = result && CheckBranchTarget(code_offset); |
| 1087 | break; |
| 1088 | case Instruction::kVerifySwitchTargets: |
| 1089 | result = result && CheckSwitchTargets(code_offset); |
| 1090 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1091 | case Instruction::kVerifyVarArgNonZero: |
| 1092 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1093 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1094 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1095 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1096 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1097 | v_a > Instruction::kMaxVarArgRegs) { |
| 1098 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1099 | "non-range invoke"; |
| 1100 | return false; |
| 1101 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1102 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1103 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1104 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1105 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1106 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1107 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1108 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1109 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1110 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1111 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1112 | inst->VRegA() <= 0) { |
| 1113 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1114 | "range invoke"; |
| 1115 | return false; |
| 1116 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1117 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1118 | break; |
| 1119 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1120 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1121 | result = false; |
| 1122 | break; |
| 1123 | } |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1124 | if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1125 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1126 | result = false; |
| 1127 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1128 | return result; |
| 1129 | } |
| 1130 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1131 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1132 | if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1133 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1134 | << code_item_accessor_.RegistersSize() << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1135 | return false; |
| 1136 | } |
| 1137 | return true; |
| 1138 | } |
| 1139 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1140 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1141 | if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1142 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1143 | << "+1 >= " << code_item_accessor_.RegistersSize() << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | return true; |
| 1147 | } |
| 1148 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1149 | inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) { |
| 1150 | uint32_t limit = dex_file_->NumCallSiteIds(); |
| 1151 | if (UNLIKELY(idx >= limit)) { |
| 1152 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max " |
| 1153 | << limit << ")"; |
| 1154 | return false; |
| 1155 | } |
| 1156 | return true; |
| 1157 | } |
| 1158 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1159 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1160 | if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1161 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 1162 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
| 1165 | return true; |
| 1166 | } |
| 1167 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1168 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1169 | if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1170 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 1171 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | return true; |
| 1175 | } |
| 1176 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1177 | inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) { |
| 1178 | uint32_t limit = dex_file_->NumMethodHandles(); |
| 1179 | if (UNLIKELY(idx >= limit)) { |
| 1180 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max " |
| 1181 | << limit << ")"; |
| 1182 | return false; |
| 1183 | } |
| 1184 | return true; |
| 1185 | } |
| 1186 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1187 | inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1188 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1189 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1190 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
| 1193 | // 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] | 1194 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1195 | if (UNLIKELY(descriptor[0] != 'L')) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1196 | 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] | 1197 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1198 | } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) { |
Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1199 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1200 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1201 | Fail(VERIFY_ERROR_INSTANTIATION); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1206 | inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1207 | if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1208 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max " |
| 1209 | << dex_file_->GetHeader().proto_ids_size_ << ")"; |
| 1210 | return false; |
| 1211 | } |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1215 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1216 | if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1217 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 1218 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1219 | return false; |
| 1220 | } |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1224 | inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1225 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1226 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1227 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1228 | return false; |
| 1229 | } |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1233 | bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1234 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1235 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1236 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1240 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1241 | const char* cp = descriptor; |
| 1242 | while (*cp++ == '[') { |
| 1243 | bracket_count++; |
| 1244 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1245 | if (UNLIKELY(bracket_count == 0)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1246 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1247 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1248 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1249 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1250 | } else if (UNLIKELY(bracket_count > 255)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1251 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1252 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1253 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1254 | return false; |
| 1255 | } |
| 1256 | return true; |
| 1257 | } |
| 1258 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1259 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1260 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
| 1261 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1262 | const uint16_t* array_data; |
| 1263 | int32_t array_data_offset; |
| 1264 | |
| 1265 | DCHECK_LT(cur_offset, insn_count); |
| 1266 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1267 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1268 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
| 1269 | cur_offset + array_data_offset + 2 >= insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1270 | 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] | 1271 | << ", data offset " << array_data_offset |
| 1272 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1273 | return false; |
| 1274 | } |
| 1275 | /* offset to array data table is a relative branch-style offset */ |
| 1276 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1277 | // 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] | 1278 | if (UNLIKELY(!IsAligned<4>(array_data))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1279 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1280 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1281 | return false; |
| 1282 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1283 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1284 | // 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] | 1285 | if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1286 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1287 | << ", data offset " << array_data_offset |
| 1288 | << " not correctly visited, probably bad padding."; |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1292 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1293 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1294 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1295 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1296 | if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1297 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1298 | << ", data offset " << array_data_offset << ", end " |
| 1299 | << cur_offset + array_data_offset + table_size |
| 1300 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1306 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1307 | int32_t offset; |
| 1308 | bool isConditional, selfOkay; |
| 1309 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1310 | return false; |
| 1311 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1312 | if (UNLIKELY(!selfOkay && offset == 0)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1313 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1314 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1317 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1318 | // 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] | 1319 | 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] | 1320 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1321 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1322 | return false; |
| 1323 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1324 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1325 | if (UNLIKELY(abs_offset < 0 || |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1326 | (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() || |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1327 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1328 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1329 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1330 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1331 | return false; |
| 1332 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1333 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1334 | return true; |
| 1335 | } |
| 1336 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1337 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1338 | bool* selfOkay) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1339 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1340 | *pConditional = false; |
| 1341 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1342 | switch (*insns & 0xff) { |
| 1343 | case Instruction::GOTO: |
| 1344 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1345 | break; |
| 1346 | case Instruction::GOTO_32: |
| 1347 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1348 | *selfOkay = true; |
| 1349 | break; |
| 1350 | case Instruction::GOTO_16: |
| 1351 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1352 | break; |
| 1353 | case Instruction::IF_EQ: |
| 1354 | case Instruction::IF_NE: |
| 1355 | case Instruction::IF_LT: |
| 1356 | case Instruction::IF_GE: |
| 1357 | case Instruction::IF_GT: |
| 1358 | case Instruction::IF_LE: |
| 1359 | case Instruction::IF_EQZ: |
| 1360 | case Instruction::IF_NEZ: |
| 1361 | case Instruction::IF_LTZ: |
| 1362 | case Instruction::IF_GEZ: |
| 1363 | case Instruction::IF_GTZ: |
| 1364 | case Instruction::IF_LEZ: |
| 1365 | *pOffset = (int16_t) insns[1]; |
| 1366 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1367 | break; |
| 1368 | default: |
| 1369 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1370 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1374 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1375 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1376 | DCHECK_LT(cur_offset, insn_count); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1377 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1378 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1379 | 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] | 1380 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1381 | cur_offset + switch_offset + 2 > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1382 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1383 | << ", switch offset " << switch_offset |
| 1384 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1385 | return false; |
| 1386 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1387 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1388 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1389 | // 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] | 1390 | if (UNLIKELY(!IsAligned<4>(switch_insns))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1391 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1392 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1393 | return false; |
| 1394 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1395 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1396 | // 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] | 1397 | if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1398 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1399 | << ", switch offset " << switch_offset |
| 1400 | << " not correctly visited, probably bad padding."; |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1404 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1405 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1406 | uint32_t switch_count = switch_insns[1]; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1407 | int32_t targets_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1408 | uint16_t expected_signature; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1409 | if (is_packed_switch) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1410 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1411 | targets_offset = 4; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1412 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1413 | } else { |
| 1414 | /* 0=sig, 1=count, 2..count*2 = keys */ |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1415 | targets_offset = 2 + 2 * switch_count; |
| 1416 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1417 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1418 | uint32_t table_size = targets_offset + switch_count * 2; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1419 | if (UNLIKELY(switch_insns[0] != expected_signature)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1420 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1421 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1422 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1423 | return false; |
| 1424 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1425 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1426 | if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1427 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1428 | << ", switch offset " << switch_offset |
| 1429 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1430 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1431 | return false; |
| 1432 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1433 | |
| 1434 | constexpr int32_t keys_offset = 2; |
| 1435 | if (switch_count > 1) { |
| 1436 | if (is_packed_switch) { |
| 1437 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1438 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1439 | int32_t max_first_key = |
| 1440 | 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] | 1441 | if (UNLIKELY(first_key > max_first_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1442 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1443 | << ", switch_count=" << switch_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1446 | } else { |
| 1447 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1448 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1449 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1450 | int32_t key = |
| 1451 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1452 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1453 | if (UNLIKELY(key <= last_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1454 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1455 | << ", this=" << key; |
| 1456 | return false; |
| 1457 | } |
| 1458 | last_key = key; |
| 1459 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1462 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1463 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1464 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1465 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1466 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1467 | if (UNLIKELY(abs_offset < 0 || |
| 1468 | abs_offset >= static_cast<int32_t>(insn_count) || |
| 1469 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1470 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1471 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1472 | << reinterpret_cast<void*>(cur_offset) |
| 1473 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1474 | return false; |
| 1475 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1476 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1477 | } |
| 1478 | return true; |
| 1479 | } |
| 1480 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1481 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1482 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1483 | for (uint32_t idx = 0; idx < vA; idx++) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1484 | if (UNLIKELY(arg[idx] >= registers_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1485 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1486 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1487 | return false; |
| 1488 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1494 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1495 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1496 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1497 | // integer overflow when adding them here. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1498 | if (UNLIKELY(vA + vC > registers_size)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1499 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1500 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1501 | return false; |
| 1502 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1503 | return true; |
| 1504 | } |
| 1505 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1506 | bool MethodVerifier::VerifyCodeFlow() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1507 | const uint16_t registers_size = code_item_accessor_.RegistersSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1508 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1509 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1510 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1511 | insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1512 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1513 | registers_size, |
| 1514 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1515 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1516 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1517 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1518 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1519 | /* Initialize register types of method arguments. */ |
| 1520 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1521 | DCHECK_NE(failures_.size(), 0U); |
| 1522 | std::string prepend("Bad signature in "); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1523 | prepend += dex_file_->PrettyMethod(dex_method_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1524 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1525 | return false; |
| 1526 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1527 | // We may have a runtime failure here, clear. |
| 1528 | have_pending_runtime_throw_failure_ = false; |
| 1529 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1530 | /* Perform code flow verification. */ |
| 1531 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1532 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1533 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1534 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1535 | return true; |
| 1536 | } |
| 1537 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1538 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1539 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1540 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1541 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1542 | } |
| 1543 | return os; |
| 1544 | } |
| 1545 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1546 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1547 | VariableIndentationOutputStream vios(&os); |
| 1548 | Dump(&vios); |
| 1549 | } |
| 1550 | |
| 1551 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1552 | if (!code_item_accessor_.HasCodeItem()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1553 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1554 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1555 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1556 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1557 | vios->Stream() << "Register Types:\n"; |
| 1558 | ScopedIndentation indent1(vios); |
| 1559 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1560 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1561 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1562 | ScopedIndentation indent1(vios); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1563 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1564 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1565 | const size_t dex_pc = inst.DexPc(); |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1566 | |
| 1567 | // Might be asked to dump before the table is initialized. |
| 1568 | if (reg_table_.IsInitialized()) { |
| 1569 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1570 | if (reg_line != nullptr) { |
| 1571 | vios->Stream() << reg_line->Dump(this) << "\n"; |
| 1572 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1573 | } |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1574 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1575 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1576 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1577 | const bool kDumpHexOfInstruction = false; |
| 1578 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1579 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1580 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1581 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1582 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1585 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1586 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | case 'I': |
| 1588 | case 'C': |
| 1589 | case 'S': |
| 1590 | case 'B': |
| 1591 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1592 | case 'F': |
| 1593 | case 'D': |
| 1594 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1595 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1596 | default: |
| 1597 | return false; |
| 1598 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1601 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1602 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1603 | |
| 1604 | // Should have been verified earlier. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1605 | DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize()); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1606 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1607 | uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize(); |
| 1608 | size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1609 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1610 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1611 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1612 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1613 | if (expected_args == 0) { |
| 1614 | // Expect at least a receiver. |
| 1615 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1616 | return false; |
| 1617 | } |
| 1618 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1619 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1620 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1621 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1622 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1623 | if (IsConstructor()) { |
| 1624 | if (declaring_class.IsJavaLangObject()) { |
| 1625 | // "this" is implicitly initialized. |
| 1626 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1627 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1628 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1629 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1630 | this, |
| 1631 | arg_start + cur_arg, |
| 1632 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1633 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1634 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1635 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1636 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1637 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 1640 | const dex::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1641 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1642 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1643 | |
| 1644 | for (; iterator.HasNext(); iterator.Next()) { |
| 1645 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1646 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1647 | LOG(FATAL) << "Null descriptor"; |
| 1648 | } |
| 1649 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1650 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1651 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | switch (descriptor[0]) { |
| 1655 | case 'L': |
| 1656 | case '[': |
| 1657 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1658 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1659 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1660 | // can return without doing anything or call virtual methods). |
| 1661 | { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 1662 | // Note: don't check access. No error would be thrown for declaring or passing an |
| 1663 | // inaccessible class. Only actual accesses to fields or methods will. |
| 1664 | const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1665 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1666 | DCHECK(HasFailures()); |
| 1667 | return false; |
| 1668 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1669 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1670 | } |
| 1671 | break; |
| 1672 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1673 | 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] | 1674 | break; |
| 1675 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1676 | 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] | 1677 | break; |
| 1678 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1679 | 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] | 1680 | break; |
| 1681 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1682 | 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] | 1683 | break; |
| 1684 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1685 | 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] | 1686 | break; |
| 1687 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1688 | 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] | 1689 | break; |
| 1690 | case 'J': |
| 1691 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1692 | if (cur_arg + 1 >= expected_args) { |
| 1693 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1694 | << " args, found more (" << descriptor << ")"; |
| 1695 | return false; |
| 1696 | } |
| 1697 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1698 | const RegType* lo_half; |
| 1699 | const RegType* hi_half; |
| 1700 | if (descriptor[0] == 'J') { |
| 1701 | lo_half = ®_types_.LongLo(); |
| 1702 | hi_half = ®_types_.LongHi(); |
| 1703 | } else { |
| 1704 | lo_half = ®_types_.DoubleLo(); |
| 1705 | hi_half = ®_types_.DoubleHi(); |
| 1706 | } |
| 1707 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1708 | cur_arg++; |
| 1709 | break; |
| 1710 | } |
| 1711 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1712 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1713 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1714 | return false; |
| 1715 | } |
| 1716 | cur_arg++; |
| 1717 | } |
| 1718 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1719 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1720 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1721 | return false; |
| 1722 | } |
| 1723 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1724 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1725 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1726 | bool result; |
| 1727 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1728 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1729 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1730 | size_t i = 0; |
| 1731 | do { |
| 1732 | i++; |
| 1733 | } while (descriptor[i] == '['); // process leading [ |
| 1734 | if (descriptor[i] == 'L') { // object array |
| 1735 | do { |
| 1736 | i++; // find closing ; |
| 1737 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1738 | result = descriptor[i] == ';'; |
| 1739 | } else { // primitive array |
| 1740 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1741 | } |
| 1742 | } else if (descriptor[0] == 'L') { |
| 1743 | // could be more thorough here, but shouldn't be required |
| 1744 | size_t i = 0; |
| 1745 | do { |
| 1746 | i++; |
| 1747 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1748 | result = descriptor[i] == ';'; |
| 1749 | } else { |
| 1750 | result = false; |
| 1751 | } |
| 1752 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1753 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1754 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1755 | } |
| 1756 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1759 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1760 | const uint16_t* insns = code_item_accessor_.Insns(); |
| 1761 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1762 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1763 | /* Begin by marking the first instruction as "changed". */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1764 | GetInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1765 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1766 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1767 | /* Continue until no instructions are marked "changed". */ |
| 1768 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1769 | if (allow_thread_suspension_) { |
| 1770 | self_->AllowThreadSuspension(); |
| 1771 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1772 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1773 | uint32_t insn_idx = start_guess; |
| 1774 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1775 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1776 | break; |
| 1777 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1778 | if (insn_idx == insns_size) { |
| 1779 | if (start_guess != 0) { |
| 1780 | /* try again, starting from the top */ |
| 1781 | start_guess = 0; |
| 1782 | continue; |
| 1783 | } else { |
| 1784 | /* all flags are clear */ |
| 1785 | break; |
| 1786 | } |
| 1787 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1788 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1789 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1790 | // "changed" flags, we need to load the set of registers from the table. |
| 1791 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1792 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1793 | // target. |
| 1794 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1795 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1796 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1797 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1798 | /* |
| 1799 | * Sanity check: retrieve the stored register line (assuming |
| 1800 | * a full table) and make sure it actually matches. |
| 1801 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1802 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1803 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1804 | if (work_line_->CompareLine(register_line) != 0) { |
| 1805 | Dump(std::cout); |
| 1806 | std::cout << info_messages_.str(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1807 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1808 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1809 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1810 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1811 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1812 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1813 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1814 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1815 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1816 | prepend += " failed to verify: "; |
| 1817 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1818 | return false; |
| 1819 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1820 | /* Clear "changed" and mark as visited. */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1821 | GetInstructionFlags(insn_idx).SetVisited(); |
| 1822 | GetInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1823 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1824 | |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 1825 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1826 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1827 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1828 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1829 | * down the line, possibly in the verifier. |
| 1830 | * |
| 1831 | * If we've substituted "always throw" instructions into the stream, |
| 1832 | * we are almost certainly going to have some dead code. |
| 1833 | */ |
| 1834 | int dead_start = -1; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1835 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1836 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1837 | const uint32_t insn_idx = inst.DexPc(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1838 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1839 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1840 | * may or may not be preceded by a padding NOP (for alignment). |
| 1841 | */ |
| 1842 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1843 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1844 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1845 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1846 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1847 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1848 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1849 | GetInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1852 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1853 | if (dead_start < 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1854 | dead_start = insn_idx; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1855 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1856 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1857 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1858 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1859 | dead_start = -1; |
| 1860 | } |
| 1861 | } |
| 1862 | if (dead_start >= 0) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1863 | LogVerifyInfo() |
| 1864 | << "dead code " << reinterpret_cast<void*>(dead_start) |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1865 | << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1866 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1867 | // 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] | 1868 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1869 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1870 | // LOG(INFO) << info_messages_.str(); |
| 1871 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1872 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1873 | return true; |
| 1874 | } |
| 1875 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1876 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1877 | // is no such field. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1878 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 1879 | const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1880 | DCHECK(class_def != nullptr); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 1881 | ClassAccessor accessor(dex_file, *class_def); |
| 1882 | for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) { |
| 1883 | if (field.IsFinal()) { |
| 1884 | return field.GetIndex(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1885 | } |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1886 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 1887 | return dex::kDexNoIndex; |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 1890 | // Setup a register line for the given return instruction. |
| 1891 | static void AdjustReturnLine(MethodVerifier* verifier, |
| 1892 | const Instruction* ret_inst, |
| 1893 | RegisterLine* line) { |
| 1894 | Instruction::Code opcode = ret_inst->Opcode(); |
| 1895 | |
| 1896 | switch (opcode) { |
| 1897 | case Instruction::RETURN_VOID: |
| 1898 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 1899 | SafelyMarkAllRegistersAsConflicts(verifier, line); |
| 1900 | break; |
| 1901 | |
| 1902 | case Instruction::RETURN: |
| 1903 | case Instruction::RETURN_OBJECT: |
| 1904 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 1905 | break; |
| 1906 | |
| 1907 | case Instruction::RETURN_WIDE: |
| 1908 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 1909 | break; |
| 1910 | |
| 1911 | default: |
| 1912 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 1913 | UNREACHABLE(); |
| 1914 | } |
| 1915 | } |
| 1916 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1917 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1918 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1919 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1920 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1921 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1922 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1923 | monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one. |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1924 | |
| 1925 | std::map<uint32_t, DexLockInfo> depth_to_lock_info; |
| 1926 | auto collector = [&](uint32_t dex_reg, uint32_t depth) { |
| 1927 | auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth)); |
| 1928 | auto it = insert_pair.first; |
| 1929 | auto set_insert_pair = it->second.dex_registers.insert(dex_reg); |
| 1930 | DCHECK(set_insert_pair.second); |
| 1931 | }; |
| 1932 | work_line_->IterateRegToLockDepths(collector); |
| 1933 | for (auto& pair : depth_to_lock_info) { |
| 1934 | monitor_enter_dex_pcs_->push_back(pair.second); |
| 1935 | // Map depth to dex PC. |
| 1936 | (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc = |
| 1937 | work_line_->GetMonitorEnterDexPc(pair.second.dex_pc); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1941 | /* |
| 1942 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1943 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1944 | * control to another statement: |
| 1945 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1946 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1947 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1948 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1949 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1950 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1951 | * throw an exception that is handled by an encompassing "try" |
| 1952 | * block. |
| 1953 | * |
| 1954 | * We can also return, in which case there is no successor instruction |
| 1955 | * from this point. |
| 1956 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1957 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1958 | */ |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1959 | const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1960 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1961 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1962 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1963 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1964 | bool just_set_result = false; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 1965 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1966 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1967 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1968 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1969 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1972 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1973 | * can throw an exception, we will copy/merge this into the "catch" |
| 1974 | * address rather than work_line, because we don't want the result |
| 1975 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1976 | * a type) to be visible to the exception handler. |
| 1977 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1978 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1979 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1980 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1981 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1982 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 1983 | DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1984 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1985 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1986 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1987 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1988 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 1989 | RegisterLineArenaUniquePtr branch_line; |
| 1990 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1991 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1992 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1993 | case Instruction::NOP: |
| 1994 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1995 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1996 | * a signature that looks like a NOP; if we see one of these in |
| 1997 | * the course of executing code then we have a problem. |
| 1998 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1999 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2000 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2001 | } |
| 2002 | break; |
| 2003 | |
| 2004 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2005 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2006 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2007 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2008 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2009 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2010 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2011 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2012 | break; |
| 2013 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2014 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2015 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2016 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2017 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2018 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2019 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2020 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2021 | break; |
| 2022 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2023 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2024 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2025 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2026 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2027 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2029 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2030 | break; |
| 2031 | |
| 2032 | /* |
| 2033 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2034 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2035 | * might want to hold the result in an actual CPU register, so the |
| 2036 | * Dalvik spec requires that these only appear immediately after an |
| 2037 | * invoke or filled-new-array. |
| 2038 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2039 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2040 | * redundant with the reset done below, but it can make the debug info |
| 2041 | * easier to read in some cases.) |
| 2042 | */ |
| 2043 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2044 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | break; |
| 2046 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2047 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2048 | break; |
| 2049 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2050 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2051 | break; |
| 2052 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2053 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2054 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 2055 | // where one entrypoint to the catch block is not actually an exception path. |
| 2056 | if (work_insn_idx_ == 0) { |
| 2057 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 2058 | break; |
| 2059 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2060 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2061 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2062 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2063 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2064 | const RegType& res_type = GetCaughtExceptionType(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2065 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2066 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2067 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2068 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2069 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2070 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2071 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2072 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2073 | } |
| 2074 | break; |
| 2075 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2076 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2077 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2078 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2079 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2080 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2081 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2082 | } else { |
| 2083 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2084 | // 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] | 2085 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2086 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2087 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2088 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2089 | return_type.IsShort() || return_type.IsChar()) && |
| 2090 | src_type.IsInteger())); |
| 2091 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2092 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2093 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2094 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2095 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2096 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | break; |
| 2100 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2101 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2102 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2103 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2104 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2105 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2106 | } else { |
| 2107 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2108 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2109 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2110 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2111 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2112 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | break; |
| 2116 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2117 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2118 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2119 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2120 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2121 | } else { |
| 2122 | /* return_type is the *expected* return type, not register value */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2123 | DCHECK(!return_type.IsZeroOrNull()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2124 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2125 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2126 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2127 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2128 | // reference in vAA is an instance of the "return_type." |
| 2129 | if (reg_type.IsUndefined()) { |
| 2130 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2131 | } else if (reg_type.IsConflict()) { |
| 2132 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2133 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2134 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2135 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2136 | } else if (!reg_type.IsReferenceTypes()) { |
| 2137 | // We really do expect a reference here. |
| 2138 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2139 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2140 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2141 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 2142 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 2143 | << "' or '" << reg_type << "'"; |
| 2144 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2145 | bool soft_error = false; |
| 2146 | // Check whether arrays are involved. They will show a valid class status, even |
| 2147 | // if their components are erroneous. |
| 2148 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2149 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2150 | if (soft_error) { |
| 2151 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2152 | << reg_type << " vs " << return_type; |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | if (!soft_error) { |
| 2157 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2158 | << "', but expected from declaration '" << return_type << "'"; |
| 2159 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2160 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | break; |
| 2165 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2166 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2167 | case Instruction::CONST_4: { |
| 2168 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2169 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2170 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2171 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2172 | } |
| 2173 | case Instruction::CONST_16: { |
| 2174 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2175 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2176 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2177 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2178 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2179 | case Instruction::CONST: { |
| 2180 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2181 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2182 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2183 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2184 | } |
| 2185 | case Instruction::CONST_HIGH16: { |
| 2186 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2187 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2188 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2189 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2190 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2191 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2192 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2193 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2194 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2195 | 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] | 2196 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2197 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2198 | } |
| 2199 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2200 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2201 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2202 | 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] | 2203 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2204 | break; |
| 2205 | } |
| 2206 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2207 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2208 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2209 | 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] | 2210 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2211 | break; |
| 2212 | } |
| 2213 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2214 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2215 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2216 | 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] | 2217 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2218 | break; |
| 2219 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2220 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2221 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2222 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2223 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2224 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2225 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2226 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2227 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2228 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2229 | // Get type from instruction if unresolved then we need an access check |
| 2230 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2231 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2232 | // 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] | 2233 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2234 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2235 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2236 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2237 | } |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2238 | case Instruction::CONST_METHOD_HANDLE: |
| 2239 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2240 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle()); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2241 | break; |
| 2242 | case Instruction::CONST_METHOD_TYPE: |
| 2243 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2244 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType()); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2245 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2246 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2247 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2248 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2249 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2250 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2251 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2252 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2253 | prev_idx--; |
| 2254 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2255 | const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2256 | switch (prev_inst.Opcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2257 | case Instruction::MOVE_OBJECT: |
| 2258 | case Instruction::MOVE_OBJECT_16: |
| 2259 | case Instruction::MOVE_OBJECT_FROM16: |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2260 | if (prev_inst.VRegB() == inst->VRegA_11x()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2261 | // Redo the copy. This won't change the register types, but update the lock status |
| 2262 | // for the aliased register. |
| 2263 | work_line_->CopyRegister1(this, |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2264 | prev_inst.VRegA(), |
| 2265 | prev_inst.VRegB(), |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2266 | kTypeCategoryRef); |
| 2267 | } |
| 2268 | break; |
| 2269 | |
Alexey Grebenkin | ce75049 | 2018-05-31 23:42:20 +0300 | [diff] [blame] | 2270 | // Catch a case of register aliasing when two registers are linked to the same |
| 2271 | // java.lang.Class object via two consequent const-class instructions immediately |
| 2272 | // preceding monitor-enter called on one of those registers. |
| 2273 | case Instruction::CONST_CLASS: { |
| 2274 | // Get the second previous instruction. |
| 2275 | if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) { |
| 2276 | break; |
| 2277 | } |
| 2278 | prev_idx--; |
| 2279 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
| 2280 | prev_idx--; |
| 2281 | } |
| 2282 | const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx); |
| 2283 | |
| 2284 | // Match the pattern "const-class; const-class; monitor-enter;" |
| 2285 | if (prev2_inst.Opcode() != Instruction::CONST_CLASS) { |
| 2286 | break; |
| 2287 | } |
| 2288 | |
| 2289 | // Ensure both const-classes are called for the same type_idx. |
| 2290 | if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) { |
| 2291 | break; |
| 2292 | } |
| 2293 | |
| 2294 | // Update the lock status for the aliased register. |
| 2295 | if (prev_inst.VRegA() == inst->VRegA_11x()) { |
| 2296 | work_line_->CopyRegister1(this, |
| 2297 | prev2_inst.VRegA(), |
| 2298 | inst->VRegA_11x(), |
| 2299 | kTypeCategoryRef); |
| 2300 | } else if (prev2_inst.VRegA() == inst->VRegA_11x()) { |
| 2301 | work_line_->CopyRegister1(this, |
| 2302 | prev_inst.VRegA(), |
| 2303 | inst->VRegA_11x(), |
| 2304 | kTypeCategoryRef); |
| 2305 | } |
| 2306 | break; |
| 2307 | } |
| 2308 | |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2309 | default: // Other instruction types ignored. |
| 2310 | break; |
| 2311 | } |
| 2312 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2313 | break; |
| 2314 | case Instruction::MONITOR_EXIT: |
| 2315 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2316 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2317 | * 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] | 2318 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2319 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2320 | * feature that Dalvik doesn't support.) |
| 2321 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2322 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2324 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2325 | * structured locking checks are working, the former would have |
| 2326 | * failed on the -enter instruction, and the latter is impossible. |
| 2327 | * |
| 2328 | * This is fortunate, because issue 3221411 prevents us from |
| 2329 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2330 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2331 | * some catch blocks (which will show up as "dead" code when |
| 2332 | * we skip them here); if we can't, then the code path could be |
| 2333 | * "live" so we still need to check it. |
| 2334 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2335 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2336 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2337 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2338 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2339 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2340 | /* |
| 2341 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2342 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2343 | * |
| 2344 | * 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] | 2345 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2346 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2347 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2348 | 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] | 2349 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2350 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2351 | // If this is a primitive type, fail HARD. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2352 | ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType( |
| 2353 | type_idx, dex_cache_.Get(), class_loader_.Get()); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2354 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2355 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2356 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2357 | << GetDeclaringClass(); |
| 2358 | break; |
| 2359 | } |
| 2360 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2361 | DCHECK_NE(failures_.size(), 0U); |
| 2362 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2363 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2364 | inst->VRegA_22c(), |
| 2365 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2366 | } |
| 2367 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2368 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2369 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2370 | 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] | 2371 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2372 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2373 | if (is_checkcast) { |
| 2374 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2375 | } else { |
| 2376 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2377 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2378 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2379 | if (is_checkcast) { |
| 2380 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2381 | } else { |
| 2382 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2383 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2384 | } else if (orig_type.IsUninitializedTypes()) { |
| 2385 | if (is_checkcast) { |
| 2386 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2387 | << orig_type_reg; |
| 2388 | } else { |
| 2389 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2390 | << orig_type_reg; |
| 2391 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2392 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2393 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2394 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2395 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2396 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2397 | inst->VRegA_22c(), |
| 2398 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2399 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2400 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2401 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2402 | } |
| 2403 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2404 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2405 | if (res_type.IsReferenceTypes()) { |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2406 | if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) { |
| 2407 | // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2408 | 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] | 2409 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2410 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2411 | inst->VRegA_12x(), |
| 2412 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2413 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2414 | } else { |
| 2415 | 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] | 2416 | } |
| 2417 | break; |
| 2418 | } |
| 2419 | case Instruction::NEW_INSTANCE: { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2420 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2421 | if (res_type.IsConflict()) { |
| 2422 | DCHECK_NE(failures_.size(), 0U); |
| 2423 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2424 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2425 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2426 | // can't create an instance of an interface or abstract class */ |
| 2427 | if (!res_type.IsInstantiableTypes()) { |
| 2428 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2429 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2430 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2431 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2432 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2433 | // Any registers holding previous allocations from this address that have not yet been |
| 2434 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2435 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2436 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2437 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2438 | break; |
| 2439 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2440 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2441 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2442 | break; |
| 2443 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2444 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2445 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2446 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2447 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2448 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2449 | just_set_result = true; // Filled new array range sets result register |
| 2450 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2451 | case Instruction::CMPL_FLOAT: |
| 2452 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2453 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2454 | break; |
| 2455 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2456 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2457 | break; |
| 2458 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2459 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2460 | break; |
| 2461 | case Instruction::CMPL_DOUBLE: |
| 2462 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2463 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2464 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2465 | break; |
| 2466 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2467 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2468 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2469 | break; |
| 2470 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2471 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2472 | break; |
| 2473 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2474 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2475 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2476 | break; |
| 2477 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2478 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2479 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2480 | break; |
| 2481 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2482 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2483 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2484 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2485 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2486 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2487 | if (res_type.IsUninitializedTypes()) { |
| 2488 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2489 | } else if (!res_type.IsReferenceTypes()) { |
| 2490 | 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] | 2491 | } else { |
| 2492 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2493 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2494 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2495 | } |
| 2496 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2497 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2498 | case Instruction::GOTO: |
| 2499 | case Instruction::GOTO_16: |
| 2500 | case Instruction::GOTO_32: |
| 2501 | /* no effect on or use of registers */ |
| 2502 | break; |
| 2503 | |
| 2504 | case Instruction::PACKED_SWITCH: |
| 2505 | case Instruction::SPARSE_SWITCH: |
| 2506 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2507 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2508 | break; |
| 2509 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2510 | case Instruction::FILL_ARRAY_DATA: { |
| 2511 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2512 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2513 | /* array_type can be null if the reg type is Zero */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2514 | if (!array_type.IsZeroOrNull()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2515 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2516 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2517 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2518 | } else if (array_type.IsUnresolvedTypes()) { |
| 2519 | // If it's an unresolved array type, it must be non-primitive. |
| 2520 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2521 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2522 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2523 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2524 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2525 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2526 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2527 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2528 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2529 | // Now verify if the element width in the table matches the element width declared in |
| 2530 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2531 | const uint16_t* array_data = |
| 2532 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2533 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2534 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2535 | } else { |
| 2536 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2537 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2538 | // in the table and expected from the array class. |
| 2539 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2540 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2541 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2542 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2543 | } |
| 2544 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2545 | } |
| 2546 | } |
| 2547 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2548 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2549 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2550 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2551 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2552 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2553 | bool mismatch = false; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2554 | if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2555 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2556 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2557 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2558 | } else { // both integral? |
| 2559 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2560 | } |
| 2561 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2562 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2563 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2564 | } |
| 2565 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2566 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2567 | case Instruction::IF_LT: |
| 2568 | case Instruction::IF_GE: |
| 2569 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2570 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2571 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2572 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2573 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2574 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2575 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2576 | } |
| 2577 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2578 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2579 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2580 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2581 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2582 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2583 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2584 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2585 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2586 | |
| 2587 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2588 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2589 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2590 | instance_of_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2591 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2592 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2593 | } |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 2594 | if (FailOrAbort(GetInstructionFlags(instance_of_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2595 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2596 | work_insn_idx_)) { |
| 2597 | break; |
| 2598 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2599 | } else { |
| 2600 | break; |
| 2601 | } |
| 2602 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2603 | const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2604 | |
| 2605 | /* Check for peep-hole pattern of: |
| 2606 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2607 | * instance-of vX, vY, T; |
| 2608 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2609 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2610 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2611 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2612 | * and sharpen the type of vY to be type T. |
| 2613 | * Note, this pattern can't be if: |
| 2614 | * - if there are other branches to this branch, |
| 2615 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2616 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2617 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2618 | (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) && |
| 2619 | (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) && |
| 2620 | (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2621 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2622 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2623 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2624 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2625 | // and we don't want to disallow field and other operations on the object. If the value |
| 2626 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2627 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2628 | // type is assignable to the original then allow optimization. This check is performed to |
| 2629 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2630 | // interface from a class that would lose information relevant to field checks. |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2631 | const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c()); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2632 | const RegType& cast_type = ResolveClass<CheckAccess::kYes>( |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2633 | dex::TypeIndex(instance_of_inst.VRegC_22c())); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2634 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2635 | if (!orig_type.Equals(cast_type) && |
| 2636 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2637 | 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] | 2638 | !cast_type.GetClass()->IsInterface() && |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2639 | (orig_type.IsZeroOrNull() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2640 | orig_type.IsStrictlyAssignableFrom( |
| 2641 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2642 | RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(), |
| 2643 | this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2644 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2645 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2646 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2647 | branch_line.reset(update_line); |
| 2648 | } |
| 2649 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2650 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2651 | instance_of_inst.VRegB_22c(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2652 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2653 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2654 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2655 | // register encoding space of instance-of, and propagate type information to the source |
| 2656 | // of the move-object. |
| 2657 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2658 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2659 | move_idx--; |
| 2660 | } |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 2661 | if (FailOrAbort(GetInstructionFlags(move_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2662 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2663 | work_insn_idx_)) { |
| 2664 | break; |
| 2665 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2666 | const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2667 | switch (move_inst.Opcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2668 | case Instruction::MOVE_OBJECT: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2669 | if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2670 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2671 | move_inst.VRegB_12x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2672 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2673 | } |
| 2674 | break; |
| 2675 | case Instruction::MOVE_OBJECT_FROM16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2676 | if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2677 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2678 | move_inst.VRegB_22x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2679 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2680 | } |
| 2681 | break; |
| 2682 | case Instruction::MOVE_OBJECT_16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2683 | if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2684 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2685 | move_inst.VRegB_32x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2686 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2687 | } |
| 2688 | break; |
| 2689 | default: |
| 2690 | break; |
| 2691 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2696 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2697 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2698 | case Instruction::IF_LTZ: |
| 2699 | case Instruction::IF_GEZ: |
| 2700 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2701 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2702 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2703 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2704 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2705 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2706 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2707 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2708 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2709 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2710 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2711 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2712 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2713 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2714 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2715 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2716 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2717 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2718 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2719 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2720 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2721 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2722 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2723 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2725 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2726 | break; |
| 2727 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2728 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2729 | break; |
| 2730 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2731 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2732 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2733 | break; |
| 2734 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2735 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2736 | break; |
| 2737 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2738 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2739 | break; |
| 2740 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2741 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2742 | break; |
| 2743 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2744 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2745 | break; |
| 2746 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2747 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2748 | break; |
| 2749 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2750 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2751 | break; |
| 2752 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2753 | case Instruction::IGET_BOOLEAN: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2754 | case Instruction::IGET_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2755 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
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::IGET_BYTE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2758 | case Instruction::IGET_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2759 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2760 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2761 | case Instruction::IGET_CHAR: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2762 | case Instruction::IGET_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2763 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2764 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2765 | case Instruction::IGET_SHORT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2766 | case Instruction::IGET_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2767 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2768 | break; |
| 2769 | case Instruction::IGET: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2770 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2771 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2772 | break; |
| 2773 | case Instruction::IGET_WIDE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2774 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2775 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2776 | break; |
| 2777 | case Instruction::IGET_OBJECT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2778 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2779 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2780 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2781 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2782 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2783 | case Instruction::IPUT_BOOLEAN: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2784 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2785 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2786 | break; |
| 2787 | case Instruction::IPUT_BYTE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2788 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2789 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2790 | break; |
| 2791 | case Instruction::IPUT_CHAR: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2792 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2793 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2794 | break; |
| 2795 | case Instruction::IPUT_SHORT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2796 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2797 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2798 | break; |
| 2799 | case Instruction::IPUT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2800 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2801 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2802 | break; |
| 2803 | case Instruction::IPUT_WIDE: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2804 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2805 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2806 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2807 | case Instruction::IPUT_OBJECT: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2808 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2809 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2810 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2811 | break; |
| 2812 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2813 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2814 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2815 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2816 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2817 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2818 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2819 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2820 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2821 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2822 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2823 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2826 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2829 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2830 | break; |
| 2831 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2832 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2833 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2834 | break; |
| 2835 | |
| 2836 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2837 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2838 | break; |
| 2839 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2840 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2841 | break; |
| 2842 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2843 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2844 | break; |
| 2845 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2846 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2847 | break; |
| 2848 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2849 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2850 | break; |
| 2851 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2852 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2855 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2856 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2857 | break; |
| 2858 | |
| 2859 | case Instruction::INVOKE_VIRTUAL: |
| 2860 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2861 | case Instruction::INVOKE_SUPER: |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2862 | case Instruction::INVOKE_SUPER_RANGE: |
| 2863 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2864 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2865 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2866 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE || |
| 2867 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2868 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2869 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2870 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2871 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2872 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2873 | if (called_method != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2874 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2875 | ? called_method->ResolveReturnType() |
| 2876 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2877 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2878 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2879 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2880 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2881 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2882 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2883 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | if (return_type == nullptr) { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 2887 | uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 2888 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2889 | dex::TypeIndex return_type_idx = |
| 2890 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2891 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2892 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2893 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2894 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2895 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2896 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2897 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2898 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2899 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2900 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2901 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2902 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2903 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2904 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2905 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2906 | const char* return_type_descriptor; |
| 2907 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2908 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2909 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2910 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 2911 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2912 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2913 | dex::TypeIndex return_type_idx = |
| 2914 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2915 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2916 | } else { |
| 2917 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2918 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2919 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2920 | ? called_method->ResolveReturnType() |
| 2921 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2922 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2923 | return_type = &FromClass(return_type_descriptor, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2924 | return_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2925 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2926 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2927 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2928 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2929 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2930 | } |
| 2931 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2932 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2933 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2934 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2935 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2936 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2937 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2938 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 2939 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2940 | if (this_type.IsConflict()) // failure. |
| 2941 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2942 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2943 | /* no null refs allowed (?) */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2944 | if (this_type.IsZeroOrNull()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2945 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2946 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2947 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2948 | |
| 2949 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2950 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2951 | // TODO: re-enable constructor type verification |
| 2952 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2953 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2954 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2955 | // break; |
| 2956 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2957 | |
| 2958 | /* arg must be an uninitialized reference */ |
| 2959 | if (!this_type.IsUninitializedTypes()) { |
| 2960 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2961 | << this_type; |
| 2962 | break; |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2967 | * registers that have the same object instance in them, not just the "this" register. |
| 2968 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 2969 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2970 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2971 | if (return_type == nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2972 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2973 | } |
| 2974 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2975 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2976 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2977 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2978 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2979 | just_set_result = true; |
| 2980 | break; |
| 2981 | } |
| 2982 | case Instruction::INVOKE_STATIC: |
| 2983 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2984 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2985 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2986 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2987 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2988 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 2989 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2990 | dex::TypeIndex return_type_idx = |
| 2991 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2992 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2993 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2994 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2995 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2996 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2997 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2998 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2999 | } else { |
| 3000 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3001 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3002 | just_set_result = true; |
| 3003 | } |
| 3004 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3005 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3006 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3007 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3008 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3009 | if (abs_method != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3010 | ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3011 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3012 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3013 | << abs_method->PrettyMethod() << "'"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3014 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3015 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3016 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3017 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3018 | * interface or Object (see comments in RegType::JoinClass). |
| 3019 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3020 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3021 | if (this_type.IsZeroOrNull()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3022 | /* null pointer always passes (and always fails at runtime) */ |
| 3023 | } else { |
| 3024 | if (this_type.IsUninitializedTypes()) { |
| 3025 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3026 | << this_type; |
| 3027 | break; |
| 3028 | } |
| 3029 | // In the past we have tried to assert that "called_interface" is assignable |
| 3030 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3031 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3032 | // implemented by "this_type". For example, two classes may implement the same |
| 3033 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3034 | // join will set "this_type" to the parent class and a test that this implements |
| 3035 | // the interface will incorrectly fail. |
| 3036 | } |
| 3037 | /* |
| 3038 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3039 | * the type information is in the abstract method, so we're good. |
| 3040 | */ |
| 3041 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3042 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3043 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 3044 | const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3045 | dex::TypeIndex return_type_idx = |
| 3046 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3047 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3048 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3049 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3050 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3051 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3052 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3053 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3054 | } else { |
| 3055 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3056 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3057 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3058 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3059 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3060 | case Instruction::INVOKE_POLYMORPHIC: |
| 3061 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3062 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3063 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3064 | if (called_method == nullptr) { |
| 3065 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3066 | if (failure_messages_.size() > 0) { |
| 3067 | std::string message = failure_messages_.back()->str(); |
| 3068 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3069 | } else { |
| 3070 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3071 | } |
| 3072 | break; |
| 3073 | } |
| 3074 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3075 | !CheckSignaturePolymorphicReceiver(inst)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 3076 | DCHECK(HasFailures()); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3077 | break; |
| 3078 | } |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 3079 | const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 3080 | const dex::ProtoIndex proto_idx(vRegH); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3081 | const char* return_descriptor = |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3082 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3083 | const RegType& return_type = |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3084 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3085 | if (!return_type.IsLowHalf()) { |
| 3086 | work_line_->SetResultRegisterType(this, return_type); |
| 3087 | } else { |
| 3088 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3089 | } |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3090 | just_set_result = true; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3091 | break; |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3092 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3093 | case Instruction::INVOKE_CUSTOM: |
| 3094 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3095 | // Verify registers based on method_type in the call site. |
| 3096 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3097 | |
| 3098 | // Step 1. Check the call site that produces the method handle for invocation |
| 3099 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3100 | if (!CheckCallSite(call_site_idx)) { |
| 3101 | DCHECK(HasFailures()); |
| 3102 | break; |
| 3103 | } |
| 3104 | |
| 3105 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3106 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3107 | // the first three arguments and CheckCallSite has checked the method handle type. |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 3108 | const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 3109 | const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3110 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3111 | // Treat method as static as it has yet to be determined. |
| 3112 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3113 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3114 | |
| 3115 | // Step 3. Propagate return type information |
| 3116 | const RegType& return_type = |
| 3117 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
| 3118 | if (!return_type.IsLowHalf()) { |
| 3119 | work_line_->SetResultRegisterType(this, return_type); |
| 3120 | } else { |
| 3121 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3122 | } |
| 3123 | just_set_result = true; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3124 | break; |
| 3125 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3126 | case Instruction::NEG_INT: |
| 3127 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3128 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3129 | break; |
| 3130 | case Instruction::NEG_LONG: |
| 3131 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3132 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3133 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3134 | break; |
| 3135 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3136 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3137 | break; |
| 3138 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3139 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3140 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3141 | break; |
| 3142 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3143 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3144 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3145 | break; |
| 3146 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3147 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3148 | break; |
| 3149 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3150 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3151 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3152 | break; |
| 3153 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3154 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
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::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3158 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3159 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3160 | break; |
| 3161 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3162 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3163 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3164 | break; |
| 3165 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3166 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3167 | break; |
| 3168 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3169 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3170 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3171 | break; |
| 3172 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3173 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3174 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3175 | break; |
| 3176 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3177 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3178 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3179 | break; |
| 3180 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3181 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3182 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3183 | break; |
| 3184 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3185 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3186 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3187 | break; |
| 3188 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3189 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3190 | break; |
| 3191 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3192 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3193 | break; |
| 3194 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3195 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3196 | break; |
| 3197 | |
| 3198 | case Instruction::ADD_INT: |
| 3199 | case Instruction::SUB_INT: |
| 3200 | case Instruction::MUL_INT: |
| 3201 | case Instruction::REM_INT: |
| 3202 | case Instruction::DIV_INT: |
| 3203 | case Instruction::SHL_INT: |
| 3204 | case Instruction::SHR_INT: |
| 3205 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3206 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3207 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3208 | break; |
| 3209 | case Instruction::AND_INT: |
| 3210 | case Instruction::OR_INT: |
| 3211 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3212 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3213 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3214 | break; |
| 3215 | case Instruction::ADD_LONG: |
| 3216 | case Instruction::SUB_LONG: |
| 3217 | case Instruction::MUL_LONG: |
| 3218 | case Instruction::DIV_LONG: |
| 3219 | case Instruction::REM_LONG: |
| 3220 | case Instruction::AND_LONG: |
| 3221 | case Instruction::OR_LONG: |
| 3222 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3223 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3224 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3225 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3226 | break; |
| 3227 | case Instruction::SHL_LONG: |
| 3228 | case Instruction::SHR_LONG: |
| 3229 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3230 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3231 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3232 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3233 | break; |
| 3234 | case Instruction::ADD_FLOAT: |
| 3235 | case Instruction::SUB_FLOAT: |
| 3236 | case Instruction::MUL_FLOAT: |
| 3237 | case Instruction::DIV_FLOAT: |
| 3238 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3239 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3240 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3241 | break; |
| 3242 | case Instruction::ADD_DOUBLE: |
| 3243 | case Instruction::SUB_DOUBLE: |
| 3244 | case Instruction::MUL_DOUBLE: |
| 3245 | case Instruction::DIV_DOUBLE: |
| 3246 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3247 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3248 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3249 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3250 | break; |
| 3251 | case Instruction::ADD_INT_2ADDR: |
| 3252 | case Instruction::SUB_INT_2ADDR: |
| 3253 | case Instruction::MUL_INT_2ADDR: |
| 3254 | case Instruction::REM_INT_2ADDR: |
| 3255 | case Instruction::SHL_INT_2ADDR: |
| 3256 | case Instruction::SHR_INT_2ADDR: |
| 3257 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3258 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3259 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3260 | break; |
| 3261 | case Instruction::AND_INT_2ADDR: |
| 3262 | case Instruction::OR_INT_2ADDR: |
| 3263 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3264 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3265 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3266 | break; |
| 3267 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3268 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3269 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3270 | break; |
| 3271 | case Instruction::ADD_LONG_2ADDR: |
| 3272 | case Instruction::SUB_LONG_2ADDR: |
| 3273 | case Instruction::MUL_LONG_2ADDR: |
| 3274 | case Instruction::DIV_LONG_2ADDR: |
| 3275 | case Instruction::REM_LONG_2ADDR: |
| 3276 | case Instruction::AND_LONG_2ADDR: |
| 3277 | case Instruction::OR_LONG_2ADDR: |
| 3278 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3279 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3280 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3281 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3282 | break; |
| 3283 | case Instruction::SHL_LONG_2ADDR: |
| 3284 | case Instruction::SHR_LONG_2ADDR: |
| 3285 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3286 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3287 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3288 | break; |
| 3289 | case Instruction::ADD_FLOAT_2ADDR: |
| 3290 | case Instruction::SUB_FLOAT_2ADDR: |
| 3291 | case Instruction::MUL_FLOAT_2ADDR: |
| 3292 | case Instruction::DIV_FLOAT_2ADDR: |
| 3293 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3294 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3295 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3296 | break; |
| 3297 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3298 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3299 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3300 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3301 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3302 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3303 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3304 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3305 | break; |
| 3306 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3307 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3308 | case Instruction::MUL_INT_LIT16: |
| 3309 | case Instruction::DIV_INT_LIT16: |
| 3310 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3311 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3312 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3313 | break; |
| 3314 | case Instruction::AND_INT_LIT16: |
| 3315 | case Instruction::OR_INT_LIT16: |
| 3316 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3317 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3318 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3319 | break; |
| 3320 | case Instruction::ADD_INT_LIT8: |
| 3321 | case Instruction::RSUB_INT_LIT8: |
| 3322 | case Instruction::MUL_INT_LIT8: |
| 3323 | case Instruction::DIV_INT_LIT8: |
| 3324 | case Instruction::REM_INT_LIT8: |
| 3325 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3326 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3327 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3328 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3329 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3330 | break; |
| 3331 | case Instruction::AND_INT_LIT8: |
| 3332 | case Instruction::OR_INT_LIT8: |
| 3333 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3334 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3335 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3336 | break; |
| 3337 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3338 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3339 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3340 | if (IsConstructor() && !IsStatic()) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3341 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3342 | if (declaring_class.IsUnresolvedReference()) { |
| 3343 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3344 | // manually over the underlying dex file. |
| 3345 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3346 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3347 | if (first_index != dex::kDexNoIndex) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3348 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3349 | << first_index; |
| 3350 | } |
| 3351 | break; |
| 3352 | } |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3353 | ObjPtr<mirror::Class> klass = declaring_class.GetClass(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3354 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3355 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3356 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3357 | << klass->GetInstanceField(i)->PrettyField(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3358 | break; |
| 3359 | } |
| 3360 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3361 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3362 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3363 | // quickened opcodes (otherwise this could be a fall-through). |
| 3364 | if (!IsConstructor()) { |
| 3365 | if (!GetMethodReturnType().IsConflict()) { |
| 3366 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3367 | } |
| 3368 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3369 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3370 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3371 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3372 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3373 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3374 | case Instruction::UNUSED_79: |
| 3375 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3376 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3377 | break; |
| 3378 | |
| 3379 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3380 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3381 | * complain if an instruction is missing (which is desirable). |
| 3382 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3383 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3384 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3385 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3386 | if (Runtime::Current()->IsAotCompiler()) { |
| 3387 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3388 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3389 | LOG(ERROR) << "Pending failures:"; |
| 3390 | for (auto& error : failures_) { |
| 3391 | LOG(ERROR) << error; |
| 3392 | } |
| 3393 | for (auto& error_msg : failure_messages_) { |
| 3394 | LOG(ERROR) << error_msg->str(); |
| 3395 | } |
| 3396 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3397 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3398 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3399 | /* immediate failure, reject class */ |
| 3400 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3401 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3402 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3403 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3404 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3405 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3406 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3407 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3408 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3409 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3410 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3411 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3412 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3413 | */ |
| 3414 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3415 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3418 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3419 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3420 | * |
| 3421 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3422 | * 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] | 3423 | * somebody could get a reference field, check it for zero, and if the |
| 3424 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3425 | * 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] | 3426 | * that, and will reject the code. |
| 3427 | * |
| 3428 | * TODO: avoid re-fetching the branch target |
| 3429 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3430 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3431 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3432 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3433 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3434 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3435 | return false; |
| 3436 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3437 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3438 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), |
| 3439 | work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3440 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3441 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3442 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3443 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3444 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3445 | return false; |
| 3446 | } |
| 3447 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3448 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3449 | return false; |
| 3450 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3451 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3455 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3456 | * |
| 3457 | * We've already verified that the table is structurally sound, so we |
| 3458 | * just need to walk through and tag the targets. |
| 3459 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3460 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3461 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3462 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3463 | int switch_count = switch_insns[1]; |
| 3464 | int offset_to_targets, targ; |
| 3465 | |
| 3466 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3467 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3468 | offset_to_targets = 4; |
| 3469 | } else { |
| 3470 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3471 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3472 | offset_to_targets = 2 + 2 * switch_count; |
| 3473 | } |
| 3474 | |
| 3475 | /* verify each switch target */ |
| 3476 | for (targ = 0; targ < switch_count; targ++) { |
| 3477 | int offset; |
| 3478 | uint32_t abs_offset; |
| 3479 | |
| 3480 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3481 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3482 | (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] | 3483 | abs_offset = work_insn_idx_ + offset; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3484 | DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits()); |
| 3485 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3486 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3487 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3488 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3489 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3490 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3495 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3496 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3497 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3498 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3499 | bool has_catch_all_handler = false; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 3500 | const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3501 | CHECK(try_item != nullptr); |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 3502 | CatchHandlerIterator iterator(code_item_accessor_, *try_item); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3503 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3504 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3505 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3506 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3507 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3508 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3509 | if (!handler_type_idx.IsValid()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3510 | has_catch_all_handler = true; |
| 3511 | } else { |
| 3512 | // It is also a catch-all if it is java.lang.Throwable. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3513 | ObjPtr<mirror::Class> klass = |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3514 | linker->ResolveType(handler_type_idx, dex_cache_, class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3515 | if (klass != nullptr) { |
Vladimir Marko | adbceb7 | 2018-05-29 14:34:14 +0100 | [diff] [blame] | 3516 | if (klass == GetClassRoot<mirror::Throwable>()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3517 | has_catch_all_handler = true; |
| 3518 | } |
| 3519 | } else { |
| 3520 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3521 | DCHECK(self_->IsExceptionPending()); |
| 3522 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3523 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3524 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3525 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3526 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3527 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3528 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3529 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3530 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3531 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3532 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3536 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3537 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3538 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3539 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3540 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3541 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3542 | * 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] | 3543 | * it will do so before grabbing the lock). |
| 3544 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3545 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3546 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3547 | << "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] | 3548 | return false; |
| 3549 | } |
| 3550 | } |
| 3551 | } |
| 3552 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3553 | /* Handle "continue". Tag the next consecutive instruction. |
| 3554 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3555 | * because it changes work_line_ when performing peephole optimization |
| 3556 | * and this change should not be used in those cases. |
| 3557 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3558 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3559 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3560 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3561 | if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3562 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3563 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3564 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3565 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3566 | // next instruction isn't one. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3567 | if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3568 | return false; |
| 3569 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3570 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3571 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3572 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3573 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3574 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3575 | // 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] | 3576 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3577 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3578 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3579 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3580 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3581 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3582 | // needed. If the merge changes the state of the registers then the work line will be |
| 3583 | // updated. |
| 3584 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3585 | return false; |
| 3586 | } |
| 3587 | } else { |
| 3588 | /* |
| 3589 | * We're not recording register data for the next instruction, so we don't know what the |
| 3590 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3591 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3592 | GetInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3593 | } |
| 3594 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3595 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3596 | /* 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] | 3597 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3598 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3602 | * Update start_guess. Advance to the next instruction of that's |
| 3603 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3604 | * neither of those exists we're in a return or throw; leave start_guess |
| 3605 | * alone and let the caller sort it out. |
| 3606 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3607 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3608 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3609 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3610 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3611 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3612 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3613 | } |
| 3614 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3615 | DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits()); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3616 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3617 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3618 | if (have_pending_runtime_throw_failure_) { |
| 3619 | have_any_pending_runtime_throw_failure_ = true; |
| 3620 | // Reset the pending_runtime_throw flag now. |
| 3621 | have_pending_runtime_throw_failure_ = false; |
| 3622 | } |
| 3623 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3624 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3625 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3626 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3627 | void MethodVerifier::UninstantiableError(const char* descriptor) { |
| 3628 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 3629 | << "non-instantiable klass " << descriptor; |
| 3630 | } |
| 3631 | |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3632 | inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3633 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 3634 | } |
| 3635 | |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3636 | template <MethodVerifier::CheckAccess C> |
| 3637 | const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3638 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3639 | ObjPtr<mirror::Class> klass = can_load_classes_ |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3640 | ? linker->ResolveType(class_idx, dex_cache_, class_loader_) |
| 3641 | : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3642 | if (can_load_classes_ && klass == nullptr) { |
| 3643 | DCHECK(self_->IsExceptionPending()); |
| 3644 | self_->ClearException(); |
| 3645 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3646 | const RegType* result = nullptr; |
Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3647 | if (klass != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3648 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3649 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3650 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3651 | UninstantiableError(descriptor); |
| 3652 | precise = false; |
| 3653 | } |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3654 | result = reg_types_.FindClass(klass, precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3655 | if (result == nullptr) { |
| 3656 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 3657 | result = reg_types_.InsertClass(descriptor, klass, precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3658 | } |
| 3659 | } else { |
| 3660 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3661 | result = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3662 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3663 | DCHECK(result != nullptr); |
| 3664 | if (result->IsConflict()) { |
| 3665 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3666 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3667 | << "' in " << GetDeclaringClass(); |
| 3668 | return *result; |
| 3669 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3670 | |
| 3671 | // Record result of class resolution attempt. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3672 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3673 | |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3674 | // If requested, check if access is allowed. Unresolved types are included in this check, as the |
| 3675 | // interpreter only tests whether access is allowed when a class is not pre-verified and runs in |
| 3676 | // the access-checks interpreter. If result is primitive, skip the access check. |
| 3677 | // |
| 3678 | // Note: we do this for unresolved classes to trigger re-verification at runtime. |
Andreas Gampe | dc39d32 | 2018-09-04 09:26:03 -0700 | [diff] [blame] | 3679 | if (C == CheckAccess::kYes && |
| 3680 | result->IsNonZeroReferenceTypes() && |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 3681 | (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !result->IsUnresolvedTypes())) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3682 | const RegType& referrer = GetDeclaringClass(); |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 3683 | if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) && |
| 3684 | !referrer.CanAccess(*result)) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3685 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3686 | << referrer << "' -> '" << *result << "'"; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3687 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3688 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3689 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
Andreas Gampe | dd30907 | 2017-08-31 07:47:37 -0700 | [diff] [blame] | 3692 | // Instantiate ResolveClass variants. This is required as the -inl file has a function with a call |
| 3693 | // to ResolveClass, and compilers may decide to inline, requiring a symbol. |
Andreas Gampe | ae0b1f4 | 2017-08-30 17:36:26 -0700 | [diff] [blame] | 3694 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>( |
| 3695 | dex::TypeIndex class_idx); |
| 3696 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>( |
| 3697 | dex::TypeIndex class_idx); |
| 3698 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3699 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3700 | const RegType* common_super = nullptr; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3701 | if (code_item_accessor_.TriesSize() != 0) { |
| 3702 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3703 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3704 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3705 | CatchHandlerIterator iterator(handlers_ptr); |
| 3706 | for (; iterator.HasNext(); iterator.Next()) { |
| 3707 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3708 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3709 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3710 | } else { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3711 | const RegType& exception = |
| 3712 | ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3713 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3714 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3715 | if (exception.IsUnresolvedTypes()) { |
| 3716 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3717 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3718 | return exception; |
| 3719 | } else { |
| 3720 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3721 | return reg_types_.Conflict(); |
| 3722 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3723 | } else if (common_super == nullptr) { |
| 3724 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3725 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3726 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3727 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3728 | common_super = &common_super->Merge(exception, ®_types_, this); |
Mathieu Chartier | c2a2bda | 2018-03-12 11:21:52 -0700 | [diff] [blame] | 3729 | if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3730 | *common_super, this), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3731 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3732 | work_insn_idx_)) { |
| 3733 | break; |
| 3734 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3739 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3740 | } |
| 3741 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3742 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3743 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3744 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3745 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3746 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3747 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3750 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3751 | uint32_t dex_method_idx, MethodType method_type) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 3752 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3753 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3754 | if (klass_type.IsConflict()) { |
| 3755 | std::string append(" in attempt to access method "); |
| 3756 | append += dex_file_->GetMethodName(method_id); |
| 3757 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3758 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3759 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3760 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3761 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3762 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3763 | ObjPtr<mirror::Class> klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3764 | const RegType& referrer = GetDeclaringClass(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 3765 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3766 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3767 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3768 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3769 | if (res_method == nullptr) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 3770 | res_method = class_linker->FindResolvedMethod( |
| 3771 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3772 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3773 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3774 | // Record result of method resolution attempt. The klass resolution has recorded whether |
| 3775 | // the class is an interface or not and therefore the type of the lookup performed above. |
| 3776 | // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type. |
| 3777 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method); |
| 3778 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3779 | bool must_fail = false; |
| 3780 | // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method |
| 3781 | // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's |
| 3782 | // hard to see the differences. |
| 3783 | // If we don't have res_method here we must fail. Just use this bool to make sure of that with a |
| 3784 | // DCHECK. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3785 | if (res_method == nullptr) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3786 | must_fail = true; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3787 | // Try to find the method also with the other type for better error reporting below |
| 3788 | // 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] | 3789 | res_method = class_linker->FindIncompatibleMethod( |
| 3790 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3791 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3792 | |
| 3793 | if (res_method == nullptr) { |
| 3794 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3795 | << klass->PrettyDescriptor() << "." |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3796 | << dex_file_->GetMethodName(method_id) << " " |
| 3797 | << dex_file_->GetMethodSignature(method_id); |
| 3798 | return nullptr; |
| 3799 | } |
| 3800 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3801 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3802 | // enforce them here. |
| 3803 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3804 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3805 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3806 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3807 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3808 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3809 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3810 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3811 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3812 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3813 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3814 | |
| 3815 | // Check that interface methods are static or match interface classes. |
| 3816 | // We only allow statics if we don't have default methods enabled. |
| 3817 | // |
| 3818 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3819 | // while this check implies an IncompatibleClassChangeError. |
| 3820 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3821 | // 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] | 3822 | // default methods are supported for the dex file), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3823 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3824 | method_type != METHOD_STATIC && |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3825 | (!dex_file_->SupportsDefaultMethods() || |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3826 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3827 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3828 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3829 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3830 | << " is in an interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3831 | return nullptr; |
| 3832 | } |
| 3833 | } else { |
| 3834 | if (method_type == METHOD_INTERFACE) { |
| 3835 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3836 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3837 | << " is in a non-interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3838 | return nullptr; |
| 3839 | } |
| 3840 | } |
| 3841 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3842 | // Check specifically for non-public object methods being provided for interface dispatch. This |
| 3843 | // can occur if we failed to find a method with FindInterfaceMethod but later find one with |
| 3844 | // FindClassMethod for error message use. |
| 3845 | if (method_type == METHOD_INTERFACE && |
| 3846 | res_method->GetDeclaringClass()->IsObjectClass() && |
| 3847 | !res_method->IsPublic()) { |
| 3848 | Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "." |
| 3849 | << dex_file_->GetMethodName(method_id) << " " |
| 3850 | << dex_file_->GetMethodSignature(method_id) << " resolved to " |
| 3851 | << "non-public object method " << res_method->PrettyMethod() << " " |
| 3852 | << "but non-public Object methods are excluded from interface " |
| 3853 | << "method resolution."; |
| 3854 | return nullptr; |
| 3855 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3856 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3857 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3858 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 3859 | << res_method->PrettyMethod() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3860 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3861 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3862 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3863 | // 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] | 3864 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3865 | 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] | 3866 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3867 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3868 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3869 | // 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] | 3870 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 3871 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 3872 | // arguments. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3873 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3874 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3875 | ((method_type == METHOD_SUPER || |
| 3876 | method_type == METHOD_VIRTUAL || |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3877 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 3878 | ((method_type == METHOD_POLYMORPHIC) && |
| 3879 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3880 | 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] | 3881 | "type of " << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3882 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3883 | } |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3884 | // Make sure we weren't expecting to fail. |
| 3885 | DCHECK(!must_fail) << "invoke type (" << method_type << ")" |
| 3886 | << klass->PrettyDescriptor() << "." |
| 3887 | << dex_file_->GetMethodName(method_id) << " " |
| 3888 | << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to " |
| 3889 | << res_method->PrettyMethod() << " without error. Initially this method was " |
| 3890 | << "not found so we were expecting to fail for some reason."; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3891 | return res_method; |
| 3892 | } |
| 3893 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3894 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3895 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 3896 | 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] | 3897 | DCHECK_EQ(!is_range, inst->HasVarArgs()); |
| 3898 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3899 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3900 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3901 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3902 | const size_t expected_args = inst->VRegA(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3903 | /* caught by static verifier */ |
| 3904 | DCHECK(is_range || expected_args <= 5); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3905 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3906 | if (expected_args > code_item_accessor_.OutsSize()) { |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 3907 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3908 | << ") exceeds outsSize (" |
| 3909 | << code_item_accessor_.OutsSize() << ")"; |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 3910 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3911 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3912 | |
| 3913 | /* |
| 3914 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3915 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3916 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3917 | */ |
| 3918 | if (method_type != METHOD_STATIC) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3919 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3920 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3921 | CHECK(have_pending_hard_failure_); |
| 3922 | return nullptr; |
| 3923 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3924 | bool is_init = false; |
| 3925 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3926 | if (res_method) { |
| 3927 | if (!res_method->IsConstructor()) { |
| 3928 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3929 | return nullptr; |
| 3930 | } |
| 3931 | } else { |
| 3932 | // Check whether the name of the called method is "<init>" |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 3933 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Jeff Hao | 0d08727 | 2014-08-04 14:47:17 -0700 | [diff] [blame] | 3934 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3935 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3936 | return nullptr; |
| 3937 | } |
| 3938 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3939 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3940 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3941 | const RegType& adjusted_type = is_init |
| 3942 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 3943 | : actual_arg_type; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3944 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3945 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 3946 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 3947 | // class. It would be wrong to use this for the type check (interface type checks are |
| 3948 | // postponed to runtime). |
| 3949 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3950 | ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3951 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3952 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 3953 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3954 | } else { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 3955 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3956 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3957 | res_method_class = ®_types_.FromDescriptor( |
| 3958 | GetClassLoader(), |
| 3959 | dex_file_->StringByTypeIdx(class_idx), |
| 3960 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3961 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3962 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3963 | Fail(adjusted_type.IsUnresolvedTypes() |
| 3964 | ? VERIFY_ERROR_NO_CLASS |
| 3965 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 3966 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 3967 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3968 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3969 | // the compiler. |
| 3970 | if (have_pending_hard_failure_) { |
| 3971 | return nullptr; |
| 3972 | } |
| 3973 | } |
| 3974 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3975 | } |
| 3976 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3977 | uint32_t arg[5]; |
| 3978 | if (!is_range) { |
| 3979 | inst->GetVarArgs(arg); |
| 3980 | } |
| 3981 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3982 | for ( ; it->HasNext(); it->Next()) { |
| 3983 | if (sig_registers >= expected_args) { |
| 3984 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3985 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3986 | return nullptr; |
| 3987 | } |
| 3988 | |
| 3989 | const char* param_descriptor = it->GetDescriptor(); |
| 3990 | |
| 3991 | if (param_descriptor == nullptr) { |
| 3992 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 3993 | "component"; |
| 3994 | return nullptr; |
| 3995 | } |
| 3996 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3997 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3998 | 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] | 3999 | arg[sig_registers]; |
| 4000 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4001 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4002 | if (!src_type.IsIntegralTypes()) { |
| 4003 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4004 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4005 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4006 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4007 | } else { |
| 4008 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4009 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4010 | // the compiler. |
| 4011 | if (have_pending_hard_failure_) { |
| 4012 | return nullptr; |
| 4013 | } |
| 4014 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4015 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4016 | // instructions not specifying register pairs by the first component, but require them |
| 4017 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4018 | // none, this will fail below. |
| 4019 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4020 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4021 | if (second_reg != get_reg + 1) { |
| 4022 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4023 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4024 | << second_reg << "."; |
| 4025 | return nullptr; |
| 4026 | } |
| 4027 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4031 | } |
| 4032 | if (expected_args != sig_registers) { |
| 4033 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4034 | " argument registers, method signature has " << sig_registers; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4035 | return nullptr; |
| 4036 | } |
| 4037 | return res_method; |
| 4038 | } |
| 4039 | |
| 4040 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4041 | MethodType method_type, |
| 4042 | bool is_range) { |
| 4043 | // As the method may not have been resolved, make this static check against what we expect. |
| 4044 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4045 | // 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] | 4046 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4047 | DexFileParameterIterator it(*dex_file_, |
| 4048 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4049 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4050 | } |
| 4051 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4052 | bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) { |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4053 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4054 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4055 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4056 | return false; |
| 4057 | } |
| 4058 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4059 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 4060 | // Check essential arguments are provided. The dex file verifier has verified indicies of the |
| 4061 | // main values (method handle, name, method_type). |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4062 | static const size_t kRequiredArguments = 3; |
| 4063 | if (it.Size() < kRequiredArguments) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4064 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4065 | << " has too few arguments: " |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4066 | << it.Size() << " < " << kRequiredArguments; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4067 | return false; |
| 4068 | } |
| 4069 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4070 | std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] = |
| 4071 | { { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() }, |
| 4072 | { EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() }, |
| 4073 | { EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() } |
| 4074 | }; |
| 4075 | uint32_t index[kRequiredArguments]; |
| 4076 | |
| 4077 | // Check arguments have expected types and are within permitted ranges. |
| 4078 | for (size_t i = 0; i < kRequiredArguments; ++i) { |
| 4079 | if (it.GetValueType() != type_and_max[i].first) { |
| 4080 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4081 | << " argument " << i << " has wrong type " |
| 4082 | << it.GetValueType() << "!=" << type_and_max[i].first; |
| 4083 | return false; |
| 4084 | } |
| 4085 | index[i] = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4086 | if (index[i] >= type_and_max[i].second) { |
| 4087 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx |
| 4088 | << " argument " << i << " bad index " |
| 4089 | << index[i] << " >= " << type_and_max[i].second; |
| 4090 | return false; |
| 4091 | } |
| 4092 | it.Next(); |
Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 4093 | } |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4094 | |
Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 4095 | // Check method handle kind is valid. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4096 | const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4097 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4098 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 4099 | << " argument 0 method handle type is not InvokeStatic: " |
| 4100 | << mh.method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4101 | return false; |
| 4102 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4103 | return true; |
| 4104 | } |
| 4105 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4106 | class MethodParamListDescriptorIterator { |
| 4107 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4108 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4109 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4110 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4111 | } |
| 4112 | |
| 4113 | bool HasNext() { |
| 4114 | return pos_ < params_size_; |
| 4115 | } |
| 4116 | |
| 4117 | void Next() { |
| 4118 | ++pos_; |
| 4119 | } |
| 4120 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4121 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4122 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4123 | } |
| 4124 | |
| 4125 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4126 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4127 | size_t pos_; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4128 | const dex::TypeList* params_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4129 | const size_t params_size_; |
| 4130 | }; |
| 4131 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4132 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4133 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4134 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4135 | // we're making. |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4136 | const uint32_t method_idx = GetMethodIdxOfInvoke(inst); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4137 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4138 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4139 | // Check what we can statically. |
| 4140 | if (!have_pending_hard_failure_) { |
| 4141 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4142 | } |
| 4143 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4144 | } |
| 4145 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4146 | // 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] | 4147 | // 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] | 4148 | if (method_type == METHOD_SUPER) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4149 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4150 | const RegType& reference_type = reg_types_.FromDescriptor( |
| 4151 | GetClassLoader(), |
| 4152 | dex_file_->StringByTypeIdx(class_idx), |
| 4153 | false); |
| 4154 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4155 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4156 | return nullptr; |
| 4157 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4158 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4159 | // TODO Can we verify anything else. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 4160 | if (class_idx == class_def_.class_idx_) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4161 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface"; |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4162 | return nullptr; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4163 | } |
| 4164 | // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS |
| 4165 | // does. |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4166 | if (!GetDeclaringClass().HasClass()) { |
| 4167 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4168 | << "interface invoke-super"; |
| 4169 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4170 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4171 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4172 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4173 | << " in method " |
| 4174 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4175 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4176 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4177 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4178 | } |
| 4179 | } else { |
| 4180 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4181 | if (super.IsUnresolvedTypes()) { |
| 4182 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4183 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4184 | << " to super " << res_method->PrettyMethod(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4185 | return nullptr; |
| 4186 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4187 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4188 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4189 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4190 | << dex_file_->PrettyMethod(dex_method_idx_) |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4191 | << " to super " << super |
| 4192 | << "." << res_method->GetName() |
| 4193 | << res_method->GetSignature(); |
| 4194 | return nullptr; |
| 4195 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4196 | } |
| 4197 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4198 | |
Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4199 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4200 | // 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] | 4201 | dex::ProtoIndex proto_idx(inst->VRegH()); |
| 4202 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4203 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4204 | } else { |
| 4205 | // Process the target method's signature. |
| 4206 | MethodParamListDescriptorIterator it(res_method); |
| 4207 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 4212 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4213 | const char* method_name = method->GetName(); |
| 4214 | |
| 4215 | const char* expected_return_descriptor; |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4216 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = |
| 4217 | Runtime::Current()->GetClassLinker()->GetClassRoots(); |
| 4218 | if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4219 | expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4220 | } else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4221 | expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name); |
| 4222 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4223 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4224 | << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4225 | return false; |
| 4226 | } |
| 4227 | |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4228 | if (expected_return_descriptor == nullptr) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4229 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4230 | << "Signature polymorphic method name invalid: " << method_name; |
| 4231 | return false; |
| 4232 | } |
| 4233 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4234 | const dex::TypeList* types = method->GetParameterTypeList(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4235 | if (types->Size() != 1) { |
| 4236 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4237 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4238 | return false; |
| 4239 | } |
| 4240 | |
| 4241 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4242 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4243 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4244 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4245 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4246 | return false; |
| 4247 | } |
| 4248 | |
| 4249 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4250 | if (strcmp(return_descriptor, expected_return_descriptor) != 0) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4251 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4252 | << "Signature polymorphic method has unexpected return type: " << return_descriptor |
| 4253 | << " != " << expected_return_descriptor; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4254 | return false; |
| 4255 | } |
| 4256 | |
| 4257 | return true; |
| 4258 | } |
| 4259 | |
| 4260 | bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
| 4261 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4262 | if (this_type.IsZeroOrNull()) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4263 | /* null pointer always passes (and always fails at run time) */ |
| 4264 | return true; |
| 4265 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4266 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4267 | << "invoke-polymorphic receiver is not a reference: " |
| 4268 | << this_type; |
| 4269 | return false; |
| 4270 | } else if (this_type.IsUninitializedReference()) { |
| 4271 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4272 | << "invoke-polymorphic receiver is uninitialized: " |
| 4273 | << this_type; |
| 4274 | return false; |
| 4275 | } else if (!this_type.HasClass()) { |
| 4276 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4277 | << "invoke-polymorphic receiver has no class: " |
| 4278 | << this_type; |
| 4279 | return false; |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 4280 | } else { |
| 4281 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = |
| 4282 | Runtime::Current()->GetClassLinker()->GetClassRoots(); |
| 4283 | if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) && |
| 4284 | !this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) { |
| 4285 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4286 | << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: " |
| 4287 | << this_type; |
| 4288 | return false; |
| 4289 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4290 | } |
| 4291 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4294 | uint16_t MethodVerifier::GetMethodIdxOfInvoke(const Instruction* inst) { |
| 4295 | switch (inst->Opcode()) { |
| 4296 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: |
| 4297 | case Instruction::INVOKE_VIRTUAL_QUICK: { |
| 4298 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 4299 | << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_; |
| 4300 | DCHECK(method_being_verified_ != nullptr); |
| 4301 | uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_); |
| 4302 | CHECK_NE(method_idx, DexFile::kDexNoIndex16); |
| 4303 | return method_idx; |
| 4304 | } |
| 4305 | default: { |
| 4306 | return inst->VRegB(); |
| 4307 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4308 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4309 | } |
| 4310 | |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4311 | uint16_t MethodVerifier::GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static) { |
| 4312 | if (is_static) { |
| 4313 | return inst->VRegB_21c(); |
| 4314 | } else if (inst->IsQuickened()) { |
| 4315 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
| 4316 | DCHECK(method_being_verified_ != nullptr); |
| 4317 | uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_); |
| 4318 | CHECK_NE(field_idx, DexFile::kDexNoIndex16); |
| 4319 | return field_idx; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4320 | } else { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4321 | return inst->VRegC_22c(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 4325 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4326 | dex::TypeIndex type_idx; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4327 | if (!is_filled) { |
| 4328 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4329 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4330 | } else if (!is_range) { |
| 4331 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4332 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4333 | } else { |
| 4334 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4335 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4336 | } |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4337 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4338 | if (res_type.IsConflict()) { // bad class |
| 4339 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4340 | } else { |
| 4341 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4342 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4343 | 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] | 4344 | } else if (!is_filled) { |
| 4345 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4346 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4347 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4348 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4349 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4350 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4351 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4352 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4353 | // the list and fail. It's legal, if silly, for arg_count to be zero. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4354 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4355 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4356 | uint32_t arg[5]; |
| 4357 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4358 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4359 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4360 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4361 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4362 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4363 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4364 | return; |
| 4365 | } |
| 4366 | } |
| 4367 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4368 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4369 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4374 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4375 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4376 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4377 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4378 | 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] | 4379 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4380 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4381 | if (array_type.IsZeroOrNull()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4382 | // 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] | 4383 | // instruction type. |
| 4384 | if (!is_primitive) { |
| 4385 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null()); |
| 4386 | } else if (insn_type.IsInteger()) { |
| 4387 | // Pick a non-zero constant (to distinguish with null) that can fit in any primitive. |
| 4388 | // We cannot use 'insn_type' as it could be a float array or an int array. |
| 4389 | work_line_->SetRegisterType<LockOp::kClear>( |
| 4390 | this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_)); |
| 4391 | } else if (insn_type.IsCategory1Types()) { |
| 4392 | // Category 1 |
| 4393 | // The 'insn_type' is exactly the type we need. |
| 4394 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4395 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4396 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4397 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4398 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4399 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4400 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4401 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4402 | 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] | 4403 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4404 | // Unresolved array types must be reference array types. |
| 4405 | if (is_primitive) { |
| 4406 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4407 | << " source for category 1 aget"; |
| 4408 | } else { |
| 4409 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4410 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4411 | // Approximate with java.lang.Object[]. |
| 4412 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4413 | inst->VRegA_23x(), |
| 4414 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4415 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4416 | } else { |
| 4417 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4418 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4419 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4420 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4421 | << " source for aget-object"; |
| 4422 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4423 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4424 | << " source for category 1 aget"; |
| 4425 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4426 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4427 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4428 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4429 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4430 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4431 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4432 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4433 | // doubles. |
| 4434 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4435 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4436 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4437 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4438 | component_type.HighHalf(®_types_)); |
| 4439 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4445 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4446 | const uint32_t vregA) { |
| 4447 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4448 | bool instruction_compatible; |
| 4449 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4450 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4451 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4452 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4453 | value_compatible = value_type.IsIntegralTypes(); |
| 4454 | } else if (target_type.IsFloat()) { |
| 4455 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4456 | value_compatible = value_type.IsFloatTypes(); |
| 4457 | } else if (target_type.IsLong()) { |
| 4458 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4459 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4460 | // as target_type depends on the resolved type of the field. |
| 4461 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4462 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4463 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4464 | } else { |
| 4465 | value_compatible = false; |
| 4466 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4467 | } else if (target_type.IsDouble()) { |
| 4468 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4469 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4470 | // as target_type depends on the resolved type of the field. |
| 4471 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4472 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4473 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4474 | } else { |
| 4475 | value_compatible = false; |
| 4476 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4477 | } else { |
| 4478 | instruction_compatible = false; // reference with primitive store |
| 4479 | value_compatible = false; // unused |
| 4480 | } |
| 4481 | if (!instruction_compatible) { |
| 4482 | // This is a global failure rather than a class change failure as the instructions and |
| 4483 | // the descriptors for the type should have been consistent within the same file at |
| 4484 | // compile time. |
| 4485 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4486 | << "' but expected type '" << target_type << "'"; |
| 4487 | return; |
| 4488 | } |
| 4489 | if (!value_compatible) { |
| 4490 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4491 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4492 | return; |
| 4493 | } |
| 4494 | } |
| 4495 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4496 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4497 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4498 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4499 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4500 | 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] | 4501 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4502 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4503 | if (array_type.IsZeroOrNull()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4504 | // Null array type; this code path will fail at runtime. |
| 4505 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4506 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4507 | // and fits multiple register types. |
| 4508 | const RegType* modified_reg_type = &insn_type; |
| 4509 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4510 | (modified_reg_type == ®_types_.LongLo())) { |
| 4511 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4512 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4513 | if (modified_reg_type == ®_types_.Integer()) { |
| 4514 | if (&value_type == ®_types_.Float()) { |
| 4515 | modified_reg_type = &value_type; |
| 4516 | } |
| 4517 | } else { |
| 4518 | if (&value_type == ®_types_.DoubleLo()) { |
| 4519 | modified_reg_type = &value_type; |
| 4520 | } |
| 4521 | } |
| 4522 | } |
| 4523 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4524 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4525 | 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] | 4526 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4527 | // Unresolved array types must be reference array types. |
| 4528 | if (is_primitive) { |
| 4529 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4530 | << "' but unresolved type '" << array_type << "'"; |
| 4531 | } else { |
| 4532 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4533 | << " because of missing class"; |
| 4534 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4535 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4536 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4537 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4538 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4539 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4540 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4541 | if (!component_type.IsReferenceTypes()) { |
| 4542 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4543 | << " source for aput-object"; |
| 4544 | } else { |
| 4545 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4546 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4547 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4548 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4549 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4550 | } |
| 4551 | } |
| 4552 | } |
| 4553 | } |
| 4554 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4555 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4556 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4557 | // Check access to class |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4558 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4559 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4560 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4561 | field_idx, dex_file_->GetFieldName(field_id), |
| 4562 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4563 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4564 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4565 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4566 | // Accessibility checks depend on resolved fields. |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4567 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4568 | !failures_.empty() || |
| 4569 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4570 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4571 | 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] | 4572 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4573 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4574 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4575 | |
| 4576 | // Record result of the field resolution attempt. |
| 4577 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4578 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4579 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4580 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4581 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4582 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4583 | DCHECK(self_->IsExceptionPending()); |
| 4584 | self_->ClearException(); |
| 4585 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4586 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4587 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4588 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4589 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4590 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4591 | } else if (!field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4592 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4593 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4594 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4595 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4598 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4599 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4600 | // Check access to class. |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4601 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4602 | if (klass_type.IsConflict()) { |
| 4603 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4604 | field_idx, dex_file_->GetFieldName(field_id), |
| 4605 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4606 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4607 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4608 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4609 | // Accessibility checks depend on resolved fields. |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4610 | DCHECK(klass_type.Equals(GetDeclaringClass()) || |
| 4611 | !failures_.empty() || |
| 4612 | IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP)); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4613 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4614 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4615 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4616 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4617 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4618 | |
| 4619 | // Record result of the field resolution attempt. |
| 4620 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4621 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4622 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4623 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4624 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4625 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4626 | DCHECK(self_->IsExceptionPending()); |
| 4627 | self_->ClearException(); |
| 4628 | return nullptr; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4629 | } else if (obj_type.IsZeroOrNull()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4630 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4631 | // Fall through into a few last soft failure checks below. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4632 | } else if (!obj_type.IsReferenceTypes()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4633 | // Trying to read a field from something that isn't a reference. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4634 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4635 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4636 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4637 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4638 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4639 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4640 | const RegType& field_klass = |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4641 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4642 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4643 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4644 | // the field is declared in this class. |
| 4645 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4646 | // appear in constructors. |
| 4647 | if (!obj_type.IsUninitializedThisReference() || |
| 4648 | !IsConstructor() || |
| 4649 | !field_klass.Equals(GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4650 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4651 | << " of a not fully initialized object within the context" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4652 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4653 | return nullptr; |
| 4654 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4655 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4656 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4657 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4658 | // 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] | 4659 | VerifyError type; |
| 4660 | bool is_aot = Runtime::Current()->IsAotCompiler(); |
| 4661 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4662 | // Compiler & unresolved types involved, retry at runtime. |
| 4663 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4664 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4665 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4666 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4667 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4668 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4669 | Fail(type) << "cannot access instance field " << field->PrettyField() |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4670 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4671 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4672 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4673 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4674 | |
| 4675 | // Few last soft failure checks. |
| 4676 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4677 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4678 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4679 | << " from " << GetDeclaringClass(); |
| 4680 | return nullptr; |
| 4681 | } else if (field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4682 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4683 | << " to not be static"; |
| 4684 | return nullptr; |
| 4685 | } |
| 4686 | |
| 4687 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4688 | } |
| 4689 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4690 | template <MethodVerifier::FieldAccessType kAccType> |
| 4691 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4692 | bool is_primitive, bool is_static) { |
Nicolas Geoffray | 69b1cf1 | 2018-03-21 10:44:58 +0000 | [diff] [blame] | 4693 | uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4694 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4695 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4696 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4697 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4698 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4699 | |
| 4700 | // One is not allowed to access fields on uninitialized references, except to write to |
| 4701 | // fields in the constructor (before calling another constructor). |
| 4702 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 4703 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 4704 | // checks at the same time that we're verifying a constructor). |
| 4705 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 4706 | object_type.IsUninitializedThisReference(); |
| 4707 | const RegType& adjusted_type = should_adjust |
| 4708 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 4709 | : object_type; |
| 4710 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4711 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 4712 | return; |
| 4713 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4714 | if (should_adjust) { |
| 4715 | if (field == nullptr) { |
| 4716 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 4717 | << "to the superclass being initialized in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4718 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4719 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4720 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4721 | << field->PrettyField() << " of a not fully initialized " |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4722 | << "object within the context of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4723 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4724 | return; |
| 4725 | } |
| 4726 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4727 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4728 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4729 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4730 | if (kAccType == FieldAccessType::kAccPut) { |
| 4731 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4732 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4733 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 4734 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4738 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 4739 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4740 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4741 | field_type = &FromClass(field->GetTypeDescriptor(), |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4742 | field_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4743 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4744 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4745 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4746 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4747 | } |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 4748 | } else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) { |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4749 | // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to |
| 4750 | // redo verification at runtime as the field may be final, unless the field id shows it's in |
| 4751 | // the same class. |
| 4752 | // |
| 4753 | // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an |
| 4754 | // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class |
| 4755 | // to the access-checks interpreter. |
| 4756 | // |
| 4757 | // Note: see b/34966607. This and above may be changed in the future. |
| 4758 | if (kAccType == FieldAccessType::kAccPut) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4759 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 4760 | const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 4761 | const RegType* field_class_type = ®_types_.FromDescriptor(GetClassLoader(), |
| 4762 | field_class_descriptor, |
| 4763 | false); |
| 4764 | if (!field_class_type->Equals(GetDeclaringClass())) { |
| 4765 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of " |
| 4766 | << field_class_descriptor |
| 4767 | << "." |
| 4768 | << dex_file_->GetFieldName(field_id) |
| 4769 | << " from other class " |
| 4770 | << GetDeclaringClass(); |
| 4771 | } |
| 4772 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4773 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4774 | if (field_type == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4775 | const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4776 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4777 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4778 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4779 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4780 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4781 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4782 | "Unexpected third access type"); |
| 4783 | if (kAccType == FieldAccessType::kAccPut) { |
| 4784 | // sput or iput. |
| 4785 | if (is_primitive) { |
| 4786 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4787 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4788 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4789 | // If the field type is not a reference, this is a global failure rather than |
| 4790 | // a class change failure as the instructions and the descriptors for the type |
| 4791 | // should have been consistent within the same file at compile time. |
| 4792 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4793 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4794 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4795 | << " to be compatible with type '" << insn_type |
| 4796 | << "' but found type '" << *field_type |
| 4797 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4798 | return; |
| 4799 | } |
| 4800 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4801 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4802 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4803 | // sget or iget. |
| 4804 | if (is_primitive) { |
| 4805 | if (field_type->Equals(insn_type) || |
| 4806 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4807 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4808 | // expected that read is of the correct primitive type or that int reads are reading |
| 4809 | // floats or long reads are reading doubles |
| 4810 | } else { |
| 4811 | // This is a global failure rather than a class change failure as the instructions and |
| 4812 | // the descriptors for the type should have been consistent within the same file at |
| 4813 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4814 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4815 | << " to be of type '" << insn_type |
| 4816 | << "' but found type '" << *field_type << "' in get"; |
| 4817 | return; |
| 4818 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4819 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4820 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4821 | // If the field type is not a reference, this is a global failure rather than |
| 4822 | // a class change failure as the instructions and the descriptors for the type |
| 4823 | // should have been consistent within the same file at compile time. |
| 4824 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4825 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4826 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4827 | << " to be compatible with type '" << insn_type |
| 4828 | << "' but found type '" << *field_type |
| 4829 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4830 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4831 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4832 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4833 | return; |
| 4834 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4835 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4836 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4837 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4838 | } else { |
| 4839 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4840 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4841 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4842 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4843 | } |
| 4844 | } |
| 4845 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4846 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4847 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4848 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4849 | return false; |
| 4850 | } |
| 4851 | return true; |
| 4852 | } |
| 4853 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 4854 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 4855 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 4856 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 4857 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 4858 | return false; |
| 4859 | } |
| 4860 | return true; |
| 4861 | } |
| 4862 | |
| 4863 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 4864 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 4865 | } |
| 4866 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4867 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 4868 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4869 | bool changed = true; |
| 4870 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4871 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4872 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4873 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4874 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4875 | * 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] | 4876 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4877 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4878 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4879 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4880 | merge_line->VerifyMonitorStackEmpty(this); |
| 4881 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4882 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4883 | // 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] | 4884 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4885 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4886 | // Directly bail if a hard failure was found. |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 4887 | if (have_pending_hard_failure_) { |
| 4888 | return false; |
| 4889 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4890 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4891 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 4892 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 4893 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4894 | copy.reset(RegisterLine::Create(target_line->NumRegs(), this)); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4895 | copy->CopyFromLine(target_line); |
| 4896 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4897 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4898 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4899 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4900 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 4901 | if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4902 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4903 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4904 | << copy->Dump(this) << " MERGE\n" |
| 4905 | << merge_line->Dump(this) << " ==\n" |
| 4906 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4907 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4908 | if (update_merge_line && changed) { |
| 4909 | merge_line->CopyFromLine(target_line); |
| 4910 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4911 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4912 | if (changed) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4913 | GetInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4914 | } |
| 4915 | return true; |
| 4916 | } |
| 4917 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4918 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4919 | return &GetInstructionFlags(work_insn_idx_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4920 | } |
| 4921 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4922 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4923 | if (return_type_ == nullptr) { |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 4924 | if (method_being_verified_ != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 4925 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 4926 | ? method_being_verified_->ResolveReturnType() |
| 4927 | : method_being_verified_->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4928 | if (return_type_class != nullptr) { |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 4929 | return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(), |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4930 | return_type_class, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4931 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4932 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4933 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4934 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4935 | } |
| 4936 | } |
| 4937 | if (return_type_ == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4938 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4939 | const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4940 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4941 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4942 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4943 | } |
| 4944 | } |
| 4945 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4946 | } |
| 4947 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4948 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4949 | if (declaring_class_ == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame^] | 4950 | const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4951 | const char* descriptor |
| 4952 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Nicolas Geoffray | b041a40 | 2017-11-13 15:16:22 +0000 | [diff] [blame] | 4953 | if (method_being_verified_ != nullptr) { |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 4954 | ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4955 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4956 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4957 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4958 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4959 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4960 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4963 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4964 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 4965 | DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4966 | std::vector<int32_t> result; |
| 4967 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4968 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4969 | if (type.IsConstant()) { |
| 4970 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4971 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4972 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4973 | } else if (type.IsConstantLo()) { |
| 4974 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4975 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4976 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4977 | } else if (type.IsConstantHi()) { |
| 4978 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4979 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4980 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4981 | } else if (type.IsIntegralTypes()) { |
| 4982 | result.push_back(kIntVReg); |
| 4983 | result.push_back(0); |
| 4984 | } else if (type.IsFloat()) { |
| 4985 | result.push_back(kFloatVReg); |
| 4986 | result.push_back(0); |
| 4987 | } else if (type.IsLong()) { |
| 4988 | result.push_back(kLongLoVReg); |
| 4989 | result.push_back(0); |
| 4990 | result.push_back(kLongHiVReg); |
| 4991 | result.push_back(0); |
| 4992 | ++i; |
| 4993 | } else if (type.IsDouble()) { |
| 4994 | result.push_back(kDoubleLoVReg); |
| 4995 | result.push_back(0); |
| 4996 | result.push_back(kDoubleHiVReg); |
| 4997 | result.push_back(0); |
| 4998 | ++i; |
| 4999 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 5000 | result.push_back(kUndefined); |
| 5001 | result.push_back(0); |
| 5002 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5003 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5004 | result.push_back(kReferenceVReg); |
| 5005 | result.push_back(0); |
| 5006 | } |
| 5007 | } |
| 5008 | return result; |
| 5009 | } |
| 5010 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5011 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5012 | if (precise) { |
| 5013 | // Precise constant type. |
| 5014 | return reg_types_.FromCat1Const(value, true); |
| 5015 | } else { |
| 5016 | // Imprecise constant type. |
| 5017 | if (value < -32768) { |
| 5018 | return reg_types_.IntConstant(); |
| 5019 | } else if (value < -128) { |
| 5020 | return reg_types_.ShortConstant(); |
| 5021 | } else if (value < 0) { |
| 5022 | return reg_types_.ByteConstant(); |
| 5023 | } else if (value == 0) { |
| 5024 | return reg_types_.Zero(); |
| 5025 | } else if (value == 1) { |
| 5026 | return reg_types_.One(); |
| 5027 | } else if (value < 128) { |
| 5028 | return reg_types_.PosByteConstant(); |
| 5029 | } else if (value < 32768) { |
| 5030 | return reg_types_.PosShortConstant(); |
| 5031 | } else if (value < 65536) { |
| 5032 | return reg_types_.CharConstant(); |
| 5033 | } else { |
| 5034 | return reg_types_.IntConstant(); |
| 5035 | } |
| 5036 | } |
| 5037 | } |
| 5038 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5039 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5040 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5041 | } |
| 5042 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5043 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5044 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5045 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5046 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5047 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5048 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5049 | } |
| 5050 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5051 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5052 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5053 | } |
| 5054 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5055 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
Vladimir Marko | 2cffc5d | 2018-05-29 15:40:56 +0100 | [diff] [blame] | 5056 | ObjPtr<mirror::Class> klass, |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5057 | bool precise) { |
| 5058 | DCHECK(klass != nullptr); |
| 5059 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 5060 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 5061 | << "non-instantiable klass " << descriptor; |
| 5062 | precise = false; |
| 5063 | } |
| 5064 | return reg_types_.FromClass(descriptor, klass, precise); |
| 5065 | } |
| 5066 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5067 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5068 | } // namespace art |