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" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 27 | #include "base/logging.h" // For VLOG. |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 28 | #include "base/mutex-inl.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 30 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 31 | #include "base/time_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 32 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 33 | #include "compiler_callbacks.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file-inl.h" |
| 35 | #include "dex/dex_file_exception_helpers.h" |
| 36 | #include "dex/dex_instruction-inl.h" |
| 37 | #include "dex/dex_instruction_utils.h" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 38 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 40 | #include "handle_scope-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 41 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 42 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 43 | #include "leb128.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 45 | #include "mirror/class.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 46 | #include "mirror/dex_cache-inl.h" |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 47 | #include "mirror/method_handle_impl.h" |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 48 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | #include "mirror/object-inl.h" |
| 50 | #include "mirror/object_array-inl.h" |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 51 | #include "mirror/var_handle.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 52 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 53 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 54 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 56 | #include "stack.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 57 | #include "utils.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 58 | #include "verifier_compiler_binding.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 59 | #include "verifier_deps.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 62 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 63 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 64 | using android::base::StringPrintf; |
| 65 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 66 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 68 | // On VLOG(verifier), should we dump the whole state when we run into a hard failure? |
| 69 | static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true; |
| 70 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 71 | // We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make |
| 72 | // sure we only print this once. |
| 73 | static bool gPrintedDxMonitorText = false; |
| 74 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 75 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator) |
| 76 | : register_lines_(allocator.Adapter(kArenaAllocVerifier)) {} |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 77 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 78 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 79 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 80 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 81 | DCHECK_GT(insns_size, 0U); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 82 | register_lines_.resize(insns_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 83 | for (uint32_t i = 0; i < insns_size; i++) { |
| 84 | bool interesting = false; |
| 85 | switch (mode) { |
| 86 | case kTrackRegsAll: |
| 87 | interesting = flags[i].IsOpcode(); |
| 88 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 89 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 90 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 91 | break; |
| 92 | case kTrackRegsBranches: |
| 93 | interesting = flags[i].IsBranchTarget(); |
| 94 | break; |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | if (interesting) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 99 | register_lines_[i].reset(RegisterLine::Create(registers_size, verifier)); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 104 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 105 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 106 | // Note: returns true on failure. |
| 107 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 108 | const char* error_msg, uint32_t work_insn_idx) { |
| 109 | if (kIsDebugBuild) { |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 110 | // In a debug build, abort if the error condition is wrong. Only warn if |
| 111 | // we are already aborting (as this verification is likely run to print |
| 112 | // lock information). |
| 113 | if (LIKELY(gAborting == 0)) { |
| 114 | DCHECK(condition) << error_msg << work_insn_idx; |
| 115 | } else { |
| 116 | if (!condition) { |
| 117 | LOG(ERROR) << error_msg << work_insn_idx; |
| 118 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 119 | return true; |
| 120 | } |
| 121 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 122 | } else { |
| 123 | // In a non-debug build, just fail the class. |
| 124 | if (!condition) { |
| 125 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 133 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 134 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 135 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 136 | reg_line->CheckConstructorReturn(verifier); |
| 137 | } |
| 138 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 139 | } |
| 140 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 141 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 142 | mirror::Class* klass, |
| 143 | CompilerCallbacks* callbacks, |
| 144 | bool allow_soft_failures, |
| 145 | HardFailLogMode log_level, |
| 146 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 147 | if (klass->IsVerified()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 148 | return FailureKind::kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 149 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 150 | bool early_failure = false; |
| 151 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 152 | const DexFile& dex_file = klass->GetDexFile(); |
| 153 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 154 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 155 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 156 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 157 | early_failure = true; |
| 158 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 159 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 160 | early_failure = true; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 161 | failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 162 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 163 | early_failure = true; |
| 164 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 165 | } |
| 166 | if (early_failure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 167 | *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 168 | if (callbacks != nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 169 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 170 | callbacks->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 171 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 172 | return FailureKind::kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 173 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 174 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 175 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 176 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 177 | return VerifyClass(self, |
| 178 | &dex_file, |
| 179 | dex_cache, |
| 180 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 181 | *class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 182 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 183 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 184 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 185 | error); |
| 186 | } |
| 187 | |
| 188 | template <bool kDirect> |
| 189 | static bool HasNextMethod(ClassDataItemIterator* it) { |
| 190 | return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod(); |
| 191 | } |
| 192 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 193 | static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) { |
| 194 | static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure |
| 195 | && FailureKind::kSoftFailure < FailureKind::kHardFailure, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 196 | "Unexpected FailureKind order"); |
| 197 | return std::max(fk1, fk2); |
| 198 | } |
| 199 | |
| 200 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 201 | kind = FailureKindMax(kind, fd.kind); |
| 202 | types |= fd.types; |
| 203 | } |
| 204 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 205 | template <bool kDirect> |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 206 | MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self, |
| 207 | ClassLinker* linker, |
| 208 | const DexFile* dex_file, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 209 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 210 | ClassDataItemIterator* it, |
| 211 | Handle<mirror::DexCache> dex_cache, |
| 212 | Handle<mirror::ClassLoader> class_loader, |
| 213 | CompilerCallbacks* callbacks, |
| 214 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 215 | HardFailLogMode log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 216 | bool need_precise_constants, |
| 217 | std::string* error_string) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 218 | DCHECK(it != nullptr); |
| 219 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 220 | MethodVerifier::FailureData failure_data; |
| 221 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 222 | int64_t previous_method_idx = -1; |
| 223 | while (HasNextMethod<kDirect>(it)) { |
| 224 | self->AllowThreadSuspension(); |
| 225 | uint32_t method_idx = it->GetMemberIndex(); |
| 226 | if (method_idx == previous_method_idx) { |
| 227 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 228 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 229 | it->Next(); |
| 230 | continue; |
| 231 | } |
| 232 | previous_method_idx = method_idx; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 233 | InvokeType type = it->GetMethodInvokeType(class_def); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 234 | ArtMethod* method = linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 235 | method_idx, dex_cache, class_loader, /* referrer */ nullptr, type); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 236 | if (method == nullptr) { |
| 237 | DCHECK(self->IsExceptionPending()); |
| 238 | // We couldn't resolve the method, but continue regardless. |
| 239 | self->ClearException(); |
| 240 | } else { |
| 241 | DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type; |
| 242 | } |
| 243 | StackHandleScope<1> hs(self); |
| 244 | std::string hard_failure_msg; |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 245 | MethodVerifier::FailureData result = VerifyMethod(self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 246 | method_idx, |
| 247 | dex_file, |
| 248 | dex_cache, |
| 249 | class_loader, |
| 250 | class_def, |
| 251 | it->GetMethodCodeItem(), |
| 252 | method, |
| 253 | it->GetMethodAccessFlags(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 254 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 255 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 256 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 257 | need_precise_constants, |
| 258 | &hard_failure_msg); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 259 | if (result.kind == FailureKind::kHardFailure) { |
| 260 | if (failure_data.kind == FailureKind::kHardFailure) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 261 | // If we logged an error before, we need a newline. |
| 262 | *error_string += "\n"; |
| 263 | } else { |
| 264 | // If we didn't log a hard failure before, print the header of the message. |
| 265 | *error_string += "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 266 | *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 267 | *error_string += ":"; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 268 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 269 | *error_string += " "; |
| 270 | *error_string += hard_failure_msg; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 271 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 272 | failure_data.Merge(result); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 273 | it->Next(); |
| 274 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 275 | |
| 276 | return failure_data; |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 279 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 280 | const DexFile* dex_file, |
| 281 | Handle<mirror::DexCache> dex_cache, |
| 282 | Handle<mirror::ClassLoader> class_loader, |
| 283 | const DexFile::ClassDef& class_def, |
| 284 | CompilerCallbacks* callbacks, |
| 285 | bool allow_soft_failures, |
| 286 | HardFailLogMode log_level, |
| 287 | std::string* error) { |
Andreas Gampe | c654816 | 2017-12-08 12:15:22 -0800 | [diff] [blame] | 288 | SCOPED_TRACE << "VerifyClass " << PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 289 | |
| 290 | // A class must not be abstract and final. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 291 | if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 292 | *error = "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 293 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 294 | *error += ": class is abstract and final."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 295 | return FailureKind::kHardFailure; |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 296 | } |
| 297 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 298 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 299 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 300 | // empty class, probably a marker interface |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 301 | return FailureKind::kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 302 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 303 | ClassDataItemIterator it(*dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 304 | it.SkipAllFields(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 305 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 306 | // Direct methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 307 | MethodVerifier::FailureData data1 = VerifyMethods<true>(self, |
| 308 | linker, |
| 309 | dex_file, |
| 310 | class_def, |
| 311 | &it, |
| 312 | dex_cache, |
| 313 | class_loader, |
| 314 | callbacks, |
| 315 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 316 | log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 317 | false /* need precise constants */, |
| 318 | error); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 319 | // Virtual methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 320 | MethodVerifier::FailureData data2 = VerifyMethods<false>(self, |
| 321 | linker, |
| 322 | dex_file, |
| 323 | class_def, |
| 324 | &it, |
| 325 | dex_cache, |
| 326 | class_loader, |
| 327 | callbacks, |
| 328 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 329 | log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 330 | false /* need precise constants */, |
| 331 | error); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 332 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 333 | data1.Merge(data2); |
| 334 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 335 | if (data1.kind == FailureKind::kNoFailure) { |
| 336 | return FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 337 | } else { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 338 | if ((data1.types & VERIFY_ERROR_LOCKING) != 0) { |
| 339 | // Print a warning about expected slow-down. Use a string temporary to print one contiguous |
| 340 | // warning. |
| 341 | std::string tmp = |
| 342 | StringPrintf("Class %s failed lock verification and will run slower.", |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 343 | PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str()); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 344 | if (!gPrintedDxMonitorText) { |
| 345 | tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n" |
| 346 | "and incorrect proguard optimizations."; |
| 347 | gPrintedDxMonitorText = true; |
| 348 | } |
| 349 | LOG(WARNING) << tmp; |
| 350 | } |
| 351 | return data1.kind; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 352 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 355 | static bool IsLargeMethod(const CodeItemDataAccessor& accessor) { |
| 356 | if (!accessor.HasCodeItem()) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 360 | uint16_t registers_size = accessor.RegistersSize(); |
| 361 | uint32_t insns_size = accessor.InsnsSizeInCodeUnits(); |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 362 | |
| 363 | return registers_size * insns_size > 4*1024*1024; |
| 364 | } |
| 365 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 366 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 367 | uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 368 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 369 | Handle<mirror::DexCache> dex_cache, |
| 370 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 371 | const DexFile::ClassDef& class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 372 | const DexFile::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 373 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 374 | uint32_t method_access_flags, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 375 | CompilerCallbacks* callbacks, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 376 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 377 | HardFailLogMode log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 378 | bool need_precise_constants, |
| 379 | std::string* hard_failure_msg) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 380 | MethodVerifier::FailureData result; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 381 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 382 | |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 383 | MethodVerifier verifier(self, |
| 384 | dex_file, |
| 385 | dex_cache, |
| 386 | class_loader, |
| 387 | class_def, |
| 388 | code_item, |
| 389 | method_idx, |
| 390 | method, |
| 391 | method_access_flags, |
| 392 | true /* can_load_classes */, |
| 393 | allow_soft_failures, |
| 394 | need_precise_constants, |
| 395 | false /* verify to dump */, |
| 396 | true /* allow_thread_suspension */); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 397 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 398 | // Verification completed, however failures may be pending that didn't cause the verification |
| 399 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 400 | CHECK(!verifier.have_pending_hard_failure_); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 401 | |
| 402 | if (code_item != nullptr && callbacks != nullptr) { |
| 403 | // Let the interested party know that the method was verified. |
| 404 | callbacks->MethodVerified(&verifier); |
| 405 | } |
| 406 | |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 407 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 408 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 409 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 410 | << dex_file->PrettyMethod(method_idx) << "\n"); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 411 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 412 | if (VLOG_IS_ON(verifier_debug)) { |
| 413 | std::cout << "\n" << verifier.info_messages_.str(); |
| 414 | verifier.Dump(std::cout); |
| 415 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 416 | result.kind = FailureKind::kSoftFailure; |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 417 | if (method != nullptr && |
| 418 | !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 419 | method->SetDontCompile(); |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 422 | if (method != nullptr) { |
| 423 | if (verifier.HasInstructionThatWillThrow()) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 424 | method->SetDontCompile(); |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 425 | if (Runtime::Current()->IsAotCompiler() && |
| 426 | (callbacks != nullptr) && !callbacks->IsBootImage()) { |
| 427 | // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger |
| 428 | // re-verification at runtime. |
| 429 | // The dead code after the throw is not verified and might be invalid. This may cause |
| 430 | // the JIT compiler to crash since it assumes that all the code is valid. |
| 431 | // |
| 432 | // There's a strong assumption that the entire boot image is verified and all its dex |
| 433 | // code is valid (even the dead and unverified one). As such this is done only for apps. |
| 434 | // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are |
| 435 | // fully verified). |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 436 | result.kind = FailureKind::kSoftFailure; |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 437 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 438 | } |
| 439 | if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 440 | method->SetMustCountLocks(); |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 441 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 442 | } |
| 443 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 444 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 445 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 446 | |
| 447 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 448 | // Failed due to being forced into interpreter. This is ok because |
| 449 | // we just want to skip verification. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 450 | result.kind = FailureKind::kSoftFailure; |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 451 | } else { |
| 452 | CHECK(verifier.have_pending_hard_failure_); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 453 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 454 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 455 | } |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 456 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 457 | LogSeverity severity; |
| 458 | switch (log_level) { |
| 459 | case HardFailLogMode::kLogVerbose: |
| 460 | severity = LogSeverity::VERBOSE; |
| 461 | break; |
| 462 | case HardFailLogMode::kLogWarning: |
| 463 | severity = LogSeverity::WARNING; |
| 464 | break; |
| 465 | case HardFailLogMode::kLogInternalFatal: |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 466 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 467 | break; |
| 468 | default: |
| 469 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 470 | UNREACHABLE(); |
| 471 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 472 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 473 | << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 474 | << "\n"); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 475 | } |
| 476 | if (hard_failure_msg != nullptr) { |
| 477 | CHECK(!verifier.failure_messages_.empty()); |
| 478 | *hard_failure_msg = |
| 479 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 480 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 481 | result.kind = FailureKind::kHardFailure; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 482 | |
| 483 | if (callbacks != nullptr) { |
| 484 | // Let the interested party know that we failed the class. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 485 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 486 | callbacks->ClassRejected(ref); |
| 487 | } |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 488 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 489 | if (VLOG_IS_ON(verifier) || VLOG_IS_ON(verifier_debug)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 490 | std::cout << "\n" << verifier.info_messages_.str(); |
| 491 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 492 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 493 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 494 | if (kTimeVerifyMethod) { |
| 495 | uint64_t duration_ns = NanoTime() - start_ns; |
| 496 | if (duration_ns > MsToNs(100)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 497 | LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 498 | << " took " << PrettyDuration(duration_ns) |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 499 | << (IsLargeMethod(verifier.CodeItem()) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 500 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 501 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 502 | result.types = verifier.encountered_failure_types_; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 503 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 506 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 507 | VariableIndentationOutputStream* vios, |
| 508 | uint32_t dex_method_idx, |
| 509 | const DexFile* dex_file, |
| 510 | Handle<mirror::DexCache> dex_cache, |
| 511 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 512 | const DexFile::ClassDef& class_def, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 513 | const DexFile::CodeItem* code_item, |
| 514 | ArtMethod* method, |
| 515 | uint32_t method_access_flags) { |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 516 | MethodVerifier* verifier = new MethodVerifier(self, |
| 517 | dex_file, |
| 518 | dex_cache, |
| 519 | class_loader, |
| 520 | class_def, |
| 521 | code_item, |
| 522 | dex_method_idx, |
| 523 | method, |
| 524 | method_access_flags, |
| 525 | true /* can_load_classes */, |
| 526 | true /* allow_soft_failures */, |
| 527 | true /* need_precise_constants */, |
| 528 | true /* verify_to_dump */, |
| 529 | true /* allow_thread_suspension */); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 530 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 531 | verifier->DumpFailures(vios->Stream()); |
| 532 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 533 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 534 | // and querying any info is dangerous/can abort. |
| 535 | if (verifier->have_pending_hard_failure_) { |
| 536 | delete verifier; |
| 537 | return nullptr; |
| 538 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 539 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 540 | return verifier; |
| 541 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 544 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 545 | const DexFile* dex_file, |
| 546 | Handle<mirror::DexCache> dex_cache, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 547 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 548 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 549 | const DexFile::CodeItem* code_item, |
| 550 | uint32_t dex_method_idx, |
| 551 | ArtMethod* method, |
| 552 | uint32_t method_access_flags, |
| 553 | bool can_load_classes, |
| 554 | bool allow_soft_failures, |
| 555 | bool need_precise_constants, |
| 556 | bool verify_to_dump, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 557 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 558 | : self_(self), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 559 | arena_stack_(Runtime::Current()->GetArenaPool()), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 560 | allocator_(&arena_stack_), |
Alex Light | d9aff13 | 2017-10-31 22:30:05 +0000 | [diff] [blame] | 561 | reg_types_(can_load_classes, allocator_, allow_thread_suspension), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 562 | reg_table_(allocator_), |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 563 | work_insn_idx_(dex::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 564 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 565 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 566 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 567 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 568 | dex_file_(dex_file), |
| 569 | dex_cache_(dex_cache), |
| 570 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 571 | class_def_(class_def), |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 572 | code_item_accessor_(*dex_file, code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 573 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 574 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 575 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 576 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 577 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 578 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 579 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 580 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 581 | monitor_enter_count_(0), |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 582 | encountered_failure_types_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 583 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 584 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 585 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 586 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 587 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 588 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 589 | allow_thread_suspension_(allow_thread_suspension), |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 590 | is_constructor_(false), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 591 | link_(nullptr) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 592 | self->PushVerifier(this); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 595 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 596 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 597 | STLDeleteElements(&failure_messages_); |
| 598 | } |
| 599 | |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 600 | void MethodVerifier::FindLocksAtDexPc( |
| 601 | ArtMethod* m, |
| 602 | uint32_t dex_pc, |
| 603 | std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 604 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 605 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 606 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 607 | MethodVerifier verifier(hs.Self(), |
| 608 | m->GetDexFile(), |
| 609 | dex_cache, |
| 610 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 611 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 612 | m->GetCodeItem(), |
| 613 | m->GetDexMethodIndex(), |
| 614 | m, |
| 615 | m->GetAccessFlags(), |
| 616 | false /* can_load_classes */, |
| 617 | true /* allow_soft_failures */, |
| 618 | false /* need_precise_constants */, |
| 619 | false /* verify_to_dump */, |
| 620 | false /* allow_thread_suspension */); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 621 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 622 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 623 | verifier.FindLocksAtDexPc(); |
| 624 | } |
| 625 | |
| 626 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 627 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 628 | 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] | 629 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 630 | // Quick check whether there are any monitor_enter instructions before verifying. |
| 631 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
| 632 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 633 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 634 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 635 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 636 | // got what we wanted. |
| 637 | Verify(); |
| 638 | return; |
| 639 | } |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 640 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 643 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 644 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 645 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 646 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 647 | MethodVerifier verifier(hs.Self(), |
| 648 | m->GetDexFile(), |
| 649 | dex_cache, |
| 650 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 651 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 652 | m->GetCodeItem(), |
| 653 | m->GetDexMethodIndex(), |
| 654 | m, |
| 655 | m->GetAccessFlags(), |
| 656 | true /* can_load_classes */, |
| 657 | true /* allow_soft_failures */, |
| 658 | false /* need_precise_constants */, |
| 659 | false /* verify_to_dump */, |
| 660 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 661 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 664 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 665 | CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 666 | |
| 667 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 668 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 669 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 670 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 671 | bool success = Verify(); |
| 672 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 673 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 674 | } |
| 675 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 676 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 677 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 678 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 679 | const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 680 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 683 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 684 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 685 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 686 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 687 | MethodVerifier verifier(hs.Self(), |
| 688 | m->GetDexFile(), |
| 689 | dex_cache, |
| 690 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 691 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 692 | m->GetCodeItem(), |
| 693 | m->GetDexMethodIndex(), |
| 694 | m, |
| 695 | m->GetAccessFlags(), |
| 696 | true /* can_load_classes */, |
| 697 | true /* allow_soft_failures */, |
| 698 | false /* need_precise_constants */, |
| 699 | false /* verify_to_dump */, |
| 700 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 701 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 704 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 705 | CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 706 | |
| 707 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 708 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 709 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 710 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 711 | bool success = Verify(); |
| 712 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 713 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 714 | } |
| 715 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 716 | if (register_line == nullptr) { |
| 717 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 718 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 719 | const Instruction* inst = &code_item_accessor_.InstructionAt(dex_pc); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 720 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 721 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 724 | bool MethodVerifier::Verify() { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 725 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 726 | // the name. |
| 727 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 728 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 729 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 730 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 731 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 732 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 733 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 734 | if (!constructor_by_name) { |
| 735 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 736 | << "method is marked as constructor, but not named accordingly"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 737 | return false; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 738 | } |
| 739 | is_constructor_ = true; |
| 740 | } else if (constructor_by_name) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 741 | LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_) |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 742 | << " not marked as constructor."; |
| 743 | is_constructor_ = true; |
| 744 | } |
| 745 | // If it's a constructor, check whether IsStatic() matches the name. |
| 746 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 747 | if (kIsDebugBuild) { |
| 748 | if (IsConstructor()) { |
| 749 | if (IsStatic() ^ static_constructor_by_name) { |
| 750 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 751 | << "constructor name doesn't match static flag"; |
| 752 | return false; |
| 753 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 754 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 755 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 756 | |
| 757 | // Methods may only have one of public/protected/private. |
| 758 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 759 | if (kIsDebugBuild) { |
| 760 | size_t access_mod_count = |
| 761 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 762 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 763 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 764 | if (access_mod_count > 1) { |
| 765 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 766 | return false; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // 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] | 771 | if (!code_item_accessor_.HasCodeItem()) { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 772 | // Only native or abstract methods may not have code. |
| 773 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 774 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 775 | return false; |
| 776 | } |
| 777 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 778 | // 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] | 779 | // 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] | 780 | if (kIsDebugBuild) { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 781 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 782 | // Abstract methods are not allowed to have the following flags. |
| 783 | static constexpr uint32_t kForbidden = |
| 784 | kAccPrivate | |
| 785 | kAccStatic | |
| 786 | kAccFinal | |
| 787 | kAccNative | |
| 788 | kAccStrict | |
| 789 | kAccSynchronized; |
| 790 | if ((method_access_flags_ & kForbidden) != 0) { |
| 791 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 792 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 793 | return false; |
| 794 | } |
| 795 | } |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 796 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 797 | // Interface methods must be public and abstract (if default methods are disabled). |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 798 | uint32_t kRequired = kAccPublic; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 799 | if ((method_access_flags_ & kRequired) != kRequired) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 800 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | // In addition to the above, interface methods must not be protected. |
| 804 | static constexpr uint32_t kForbidden = kAccProtected; |
| 805 | if ((method_access_flags_ & kForbidden) != 0) { |
| 806 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | // We also don't allow constructors to be abstract or native. |
| 811 | if (IsConstructor()) { |
| 812 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 813 | return false; |
| 814 | } |
| 815 | } |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 820 | if (kIsDebugBuild) { |
| 821 | // When there's code, the method must not be native or abstract. |
| 822 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 823 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 824 | return false; |
| 825 | } |
| 826 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 827 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 828 | // Interfaces may always have static initializers for their fields. If we are running with |
| 829 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 830 | // Otherwise that is the only type of method allowed. |
Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 831 | if (!(IsConstructor() && IsStatic())) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 832 | if (IsInstanceConstructor()) { |
| 833 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 834 | return false; |
| 835 | } else if (method_access_flags_ & kAccFinal) { |
| 836 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 837 | return false; |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 838 | } else { |
| 839 | uint32_t access_flag_options = kAccPublic; |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 840 | if (dex_file_->SupportsDefaultMethods()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 841 | access_flag_options |= kAccPrivate; |
| 842 | } |
| 843 | if (!(method_access_flags_ & access_flag_options)) { |
| 844 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 845 | << "interfaces may not have protected or package-private members"; |
| 846 | return false; |
| 847 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 848 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | // Instance constructors must not be synchronized. |
| 853 | if (IsInstanceConstructor()) { |
| 854 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 855 | if ((method_access_flags_ & kForbidden) != 0) { |
| 856 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 857 | return false; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 862 | // 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] | 863 | if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) { |
| 864 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" |
| 865 | << code_item_accessor_.InsSize() |
| 866 | << " regs=" << code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 867 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 868 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 869 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 870 | // Allocate and initialize an array to hold instruction data. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 871 | insn_flags_.reset(allocator_.AllocArray<InstructionFlags>( |
| 872 | code_item_accessor_.InsnsSizeInCodeUnits())); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 873 | DCHECK(insn_flags_ != nullptr); |
| 874 | std::uninitialized_fill_n(insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 875 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 876 | InstructionFlags()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 877 | // Run through the instructions and see if the width checks out. |
| 878 | bool result = ComputeWidthsAndCountOps(); |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 879 | bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 880 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 881 | result = result && ScanTryCatchBlocks(); |
| 882 | // Perform static instruction verification. |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 883 | result = result && (allow_runtime_only_instructions |
| 884 | ? VerifyInstructions<true>() |
| 885 | : VerifyInstructions<false>()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 886 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 887 | result = result && VerifyCodeFlow(); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 888 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 889 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 892 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 893 | // Mark the error type as encountered. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 894 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 895 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 896 | switch (error) { |
| 897 | case VERIFY_ERROR_NO_CLASS: |
| 898 | case VERIFY_ERROR_NO_FIELD: |
| 899 | case VERIFY_ERROR_NO_METHOD: |
| 900 | case VERIFY_ERROR_ACCESS_CLASS: |
| 901 | case VERIFY_ERROR_ACCESS_FIELD: |
| 902 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 903 | case VERIFY_ERROR_INSTANTIATION: |
| 904 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 905 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 906 | case VERIFY_ERROR_LOCKING: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 907 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 908 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 909 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 910 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 911 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 912 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 913 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 914 | // to an interpreter. |
| 915 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 916 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 917 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 918 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 919 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 920 | |
| 921 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 922 | // try to merge garbage. |
| 923 | // 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] | 924 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 925 | // add a check. |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 926 | if (work_insn_idx_ < dex::kDexNoIndex) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 927 | const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 928 | int opcode_flags = Instruction::FlagsOf(inst.Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 929 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 930 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 931 | saved_line_->CopyFromLine(work_line_.get()); |
| 932 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 933 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 934 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 935 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 936 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 937 | // Indication that verification should be retried at runtime. |
| 938 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 939 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 940 | have_pending_hard_failure_ = true; |
| 941 | } |
| 942 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 943 | |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 944 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 945 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 946 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 947 | have_pending_hard_failure_ = true; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 948 | if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) { |
| 949 | ScopedObjectAccess soa(Thread::Current()); |
| 950 | std::ostringstream oss; |
| 951 | Dump(oss); |
| 952 | LOG(ERROR) << oss.str(); |
| 953 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 954 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 955 | } |
| 956 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 957 | failures_.push_back(error); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 958 | 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] | 959 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 960 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 961 | failure_messages_.push_back(failure_message); |
| 962 | return *failure_message; |
| 963 | } |
| 964 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 965 | std::ostream& MethodVerifier::LogVerifyInfo() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 966 | return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_) |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 967 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 968 | } |
| 969 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 970 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 971 | size_t failure_num = failure_messages_.size(); |
| 972 | DCHECK_NE(failure_num, 0U); |
| 973 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 974 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 975 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 976 | delete last_fail_message; |
| 977 | } |
| 978 | |
Vladimir Marko | 5c657fe | 2016-11-03 15:12:29 +0000 | [diff] [blame] | 979 | void MethodVerifier::AppendToLastFailMessage(const std::string& append) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 980 | size_t failure_num = failure_messages_.size(); |
| 981 | DCHECK_NE(failure_num, 0U); |
| 982 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 983 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 984 | } |
| 985 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 986 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 987 | size_t new_instance_count = 0; |
| 988 | size_t monitor_enter_count = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 989 | |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 990 | // We can't assume the instruction is well formed, handle the case where calculating the size |
| 991 | // goes past the end of the code item. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 992 | SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end()); |
| 993 | for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 994 | // In case the instruction goes past the end of the code item, make sure to not process it. |
| 995 | SafeDexInstructionIterator next = it; |
| 996 | ++next; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 997 | if (next.IsErrorState()) { |
Mathieu Chartier | af7c902 | 2017-10-27 09:42:46 -0700 | [diff] [blame] | 998 | break; |
| 999 | } |
| 1000 | Instruction::Code opcode = it->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1001 | switch (opcode) { |
| 1002 | case Instruction::APUT_OBJECT: |
| 1003 | case Instruction::CHECK_CAST: |
| 1004 | has_check_casts_ = true; |
| 1005 | break; |
| 1006 | case Instruction::INVOKE_VIRTUAL: |
| 1007 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 1008 | case Instruction::INVOKE_INTERFACE: |
| 1009 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 1010 | has_virtual_or_interface_invokes_ = true; |
| 1011 | break; |
| 1012 | case Instruction::MONITOR_ENTER: |
| 1013 | monitor_enter_count++; |
| 1014 | break; |
| 1015 | case Instruction::NEW_INSTANCE: |
| 1016 | new_instance_count++; |
| 1017 | break; |
| 1018 | default: |
| 1019 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1020 | } |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1021 | GetInstructionFlags(it.DexPc()).SetIsOpcode(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1024 | if (it != code_item_accessor_.end()) { |
| 1025 | const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1027 | << it.DexPc() << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
| 1030 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1031 | new_instance_count_ = new_instance_count; |
| 1032 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1033 | return true; |
| 1034 | } |
| 1035 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1036 | bool MethodVerifier::ScanTryCatchBlocks() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1037 | const uint32_t tries_size = code_item_accessor_.TriesSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1038 | if (tries_size == 0) { |
| 1039 | return true; |
| 1040 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1041 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
| 1042 | for (const DexFile::TryItem& try_item : code_item_accessor_.TryItems()) { |
| 1043 | const uint32_t start = try_item.start_addr_; |
| 1044 | const uint32_t end = start + try_item.insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1045 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1046 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1047 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1048 | return false; |
| 1049 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1050 | if (!GetInstructionFlags(start).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1051 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1052 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1053 | return false; |
| 1054 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1055 | DexInstructionIterator end_it(code_item_accessor_.Insns(), end); |
| 1056 | for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) { |
| 1057 | GetInstructionFlags(it.DexPc()).SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1060 | // Iterate over each of the handlers to verify target addresses. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1061 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
| 1062 | const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1063 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1064 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1065 | CatchHandlerIterator iterator(handlers_ptr); |
| 1066 | for (; iterator.HasNext(); iterator.Next()) { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 1067 | uint32_t dex_pc = iterator.GetHandlerAddress(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1068 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1069 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1070 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1071 | return false; |
| 1072 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1073 | if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) { |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 1074 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1075 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 1076 | return false; |
| 1077 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1078 | GetInstructionFlags(dex_pc).SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1079 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1080 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1081 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1082 | ObjPtr<mirror::Class> exception_type = |
| 1083 | linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1084 | if (exception_type == nullptr) { |
| 1085 | DCHECK(self_->IsExceptionPending()); |
| 1086 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1089 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1090 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1091 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
| 1094 | |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1095 | template <bool kAllowRuntimeOnlyInstructions> |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1096 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1097 | /* 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] | 1098 | GetInstructionFlags(0).SetBranchTarget(); |
| 1099 | GetInstructionFlags(0).SetCompileTimeInfoPoint(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1100 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1101 | const uint32_t dex_pc = inst.DexPc(); |
| 1102 | if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1103 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1104 | return false; |
| 1105 | } |
| 1106 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1107 | // All invoke points are marked as "Throw" points already. |
| 1108 | // 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] | 1109 | if (inst->IsBranch()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1110 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1111 | // The compiler also needs safepoints for fall-through to loop heads. |
| 1112 | // Such a loop head must be a target of a branch. |
| 1113 | int32_t offset = 0; |
| 1114 | bool cond, self_ok; |
| 1115 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 1116 | DCHECK(target_ok); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1117 | GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1118 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1119 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 1120 | } else if (inst->IsReturn()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1121 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1122 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1123 | } |
| 1124 | return true; |
| 1125 | } |
| 1126 | |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1127 | template <bool kAllowRuntimeOnlyInstructions> |
| 1128 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Andreas Gampe | e05cc66 | 2017-05-15 10:17:30 -0700 | [diff] [blame] | 1129 | if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1130 | // Experimental instructions don't yet have verifier support implementation. |
| 1131 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 1132 | // with a virtual register that was created by an experimental instruction, |
| 1133 | // the data flow analysis will fail. |
| 1134 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 1135 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 1136 | have_pending_experimental_failure_ = true; |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1140 | bool result = true; |
| 1141 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1142 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1143 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1144 | break; |
| 1145 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1146 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1147 | break; |
| 1148 | } |
| 1149 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1150 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1151 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1154 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1155 | break; |
| 1156 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1157 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1158 | break; |
| 1159 | case Instruction::kVerifyRegBNewInstance: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1160 | result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1161 | break; |
| 1162 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1163 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1164 | break; |
| 1165 | case Instruction::kVerifyRegBType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1166 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1169 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1170 | break; |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1171 | case Instruction::kVerifyRegBCallSite: |
| 1172 | result = result && CheckCallSiteIndex(inst->VRegB()); |
| 1173 | break; |
| 1174 | case Instruction::kVerifyRegBMethodHandle: |
| 1175 | result = result && CheckMethodHandleIndex(inst->VRegB()); |
| 1176 | break; |
| 1177 | case Instruction::kVerifyRegBPrototype: |
| 1178 | result = result && CheckPrototypeIndex(inst->VRegB()); |
| 1179 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1180 | } |
| 1181 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1182 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1183 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | break; |
| 1185 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1186 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | case Instruction::kVerifyRegCNewArray: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1189 | result = result && CheckNewArray(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1190 | break; |
| 1191 | case Instruction::kVerifyRegCType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1192 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1193 | break; |
| 1194 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1195 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1196 | break; |
| 1197 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1198 | switch (inst->GetVerifyTypeArgumentH()) { |
| 1199 | case Instruction::kVerifyRegHPrototype: |
| 1200 | result = result && CheckPrototypeIndex(inst->VRegH()); |
| 1201 | break; |
| 1202 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1203 | switch (inst->GetVerifyExtraFlags()) { |
| 1204 | case Instruction::kVerifyArrayData: |
| 1205 | result = result && CheckArrayData(code_offset); |
| 1206 | break; |
| 1207 | case Instruction::kVerifyBranchTarget: |
| 1208 | result = result && CheckBranchTarget(code_offset); |
| 1209 | break; |
| 1210 | case Instruction::kVerifySwitchTargets: |
| 1211 | result = result && CheckSwitchTargets(code_offset); |
| 1212 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1213 | case Instruction::kVerifyVarArgNonZero: |
| 1214 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1215 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1216 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1217 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1218 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1219 | v_a > Instruction::kMaxVarArgRegs) { |
| 1220 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1221 | "non-range invoke"; |
| 1222 | return false; |
| 1223 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1224 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1225 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1226 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1227 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1228 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1229 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1230 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1231 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1232 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1233 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1234 | inst->VRegA() <= 0) { |
| 1235 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1236 | "range invoke"; |
| 1237 | return false; |
| 1238 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1239 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1242 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1243 | result = false; |
| 1244 | break; |
| 1245 | } |
Andreas Gampe | bf1cb77 | 2017-05-15 15:39:00 -0700 | [diff] [blame] | 1246 | if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1247 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1248 | result = false; |
| 1249 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1250 | return result; |
| 1251 | } |
| 1252 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1253 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1254 | if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1255 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1256 | << code_item_accessor_.RegistersSize() << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1257 | return false; |
| 1258 | } |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1262 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1263 | if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1264 | 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] | 1265 | << "+1 >= " << code_item_accessor_.RegistersSize() << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1266 | return false; |
| 1267 | } |
| 1268 | return true; |
| 1269 | } |
| 1270 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1271 | inline bool MethodVerifier::CheckCallSiteIndex(uint32_t idx) { |
| 1272 | uint32_t limit = dex_file_->NumCallSiteIds(); |
| 1273 | if (UNLIKELY(idx >= limit)) { |
| 1274 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max " |
| 1275 | << limit << ")"; |
| 1276 | return false; |
| 1277 | } |
| 1278 | return true; |
| 1279 | } |
| 1280 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1281 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1282 | if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1283 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 1284 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1285 | return false; |
| 1286 | } |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1290 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1291 | if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1292 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 1293 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1294 | return false; |
| 1295 | } |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 1299 | inline bool MethodVerifier::CheckMethodHandleIndex(uint32_t idx) { |
| 1300 | uint32_t limit = dex_file_->NumMethodHandles(); |
| 1301 | if (UNLIKELY(idx >= limit)) { |
| 1302 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max " |
| 1303 | << limit << ")"; |
| 1304 | return false; |
| 1305 | } |
| 1306 | return true; |
| 1307 | } |
| 1308 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1309 | inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1310 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1311 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1312 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1313 | return false; |
| 1314 | } |
| 1315 | // 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] | 1316 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1317 | if (UNLIKELY(descriptor[0] != 'L')) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1318 | 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] | 1319 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1320 | } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) { |
Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1321 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1322 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1323 | Fail(VERIFY_ERROR_INSTANTIATION); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1324 | } |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1328 | inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1329 | if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1330 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max " |
| 1331 | << dex_file_->GetHeader().proto_ids_size_ << ")"; |
| 1332 | return false; |
| 1333 | } |
| 1334 | return true; |
| 1335 | } |
| 1336 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1337 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1338 | if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1339 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 1340 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1341 | return false; |
| 1342 | } |
| 1343 | return true; |
| 1344 | } |
| 1345 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1346 | inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1347 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1348 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1349 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1350 | return false; |
| 1351 | } |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1355 | bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1356 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1357 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1358 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1359 | return false; |
| 1360 | } |
| 1361 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1362 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1363 | const char* cp = descriptor; |
| 1364 | while (*cp++ == '[') { |
| 1365 | bracket_count++; |
| 1366 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1367 | if (UNLIKELY(bracket_count == 0)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1368 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1369 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1370 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1371 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1372 | } else if (UNLIKELY(bracket_count > 255)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1373 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1374 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1375 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1376 | return false; |
| 1377 | } |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1381 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1382 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
| 1383 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1384 | const uint16_t* array_data; |
| 1385 | int32_t array_data_offset; |
| 1386 | |
| 1387 | DCHECK_LT(cur_offset, insn_count); |
| 1388 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1389 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1390 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
| 1391 | cur_offset + array_data_offset + 2 >= insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1392 | 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] | 1393 | << ", data offset " << array_data_offset |
| 1394 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1395 | return false; |
| 1396 | } |
| 1397 | /* offset to array data table is a relative branch-style offset */ |
| 1398 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1399 | // 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] | 1400 | if (UNLIKELY(!IsAligned<4>(array_data))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1401 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1402 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1405 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1406 | // 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] | 1407 | if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1408 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1409 | << ", data offset " << array_data_offset |
| 1410 | << " not correctly visited, probably bad padding."; |
| 1411 | return false; |
| 1412 | } |
| 1413 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1414 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1415 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1416 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1417 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1418 | if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1419 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1420 | << ", data offset " << array_data_offset << ", end " |
| 1421 | << cur_offset + array_data_offset + table_size |
| 1422 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1423 | return false; |
| 1424 | } |
| 1425 | return true; |
| 1426 | } |
| 1427 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1428 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1429 | int32_t offset; |
| 1430 | bool isConditional, selfOkay; |
| 1431 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1432 | return false; |
| 1433 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1434 | if (UNLIKELY(!selfOkay && offset == 0)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1435 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1436 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1439 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1440 | // 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] | 1441 | 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] | 1442 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1443 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1446 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1447 | if (UNLIKELY(abs_offset < 0 || |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1448 | (uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() || |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1449 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1450 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1451 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1452 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1455 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1456 | return true; |
| 1457 | } |
| 1458 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1459 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1460 | bool* selfOkay) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1461 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1462 | *pConditional = false; |
| 1463 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1464 | switch (*insns & 0xff) { |
| 1465 | case Instruction::GOTO: |
| 1466 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1467 | break; |
| 1468 | case Instruction::GOTO_32: |
| 1469 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1470 | *selfOkay = true; |
| 1471 | break; |
| 1472 | case Instruction::GOTO_16: |
| 1473 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1474 | break; |
| 1475 | case Instruction::IF_EQ: |
| 1476 | case Instruction::IF_NE: |
| 1477 | case Instruction::IF_LT: |
| 1478 | case Instruction::IF_GE: |
| 1479 | case Instruction::IF_GT: |
| 1480 | case Instruction::IF_LE: |
| 1481 | case Instruction::IF_EQZ: |
| 1482 | case Instruction::IF_NEZ: |
| 1483 | case Instruction::IF_LTZ: |
| 1484 | case Instruction::IF_GEZ: |
| 1485 | case Instruction::IF_GTZ: |
| 1486 | case Instruction::IF_LEZ: |
| 1487 | *pOffset = (int16_t) insns[1]; |
| 1488 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1489 | break; |
| 1490 | default: |
| 1491 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1492 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1493 | return true; |
| 1494 | } |
| 1495 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1496 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1497 | const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits(); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1498 | DCHECK_LT(cur_offset, insn_count); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1499 | const uint16_t* insns = code_item_accessor_.Insns() + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1500 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1501 | 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] | 1502 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1503 | cur_offset + switch_offset + 2 > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1504 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1505 | << ", switch offset " << switch_offset |
| 1506 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1507 | return false; |
| 1508 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1509 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1510 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1511 | // 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] | 1512 | if (UNLIKELY(!IsAligned<4>(switch_insns))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1513 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1514 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1515 | return false; |
| 1516 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1517 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1518 | // 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] | 1519 | if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1520 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1521 | << ", switch offset " << switch_offset |
| 1522 | << " not correctly visited, probably bad padding."; |
| 1523 | return false; |
| 1524 | } |
| 1525 | |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1526 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1527 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1528 | uint32_t switch_count = switch_insns[1]; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1529 | int32_t targets_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1530 | uint16_t expected_signature; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1531 | if (is_packed_switch) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1532 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1533 | targets_offset = 4; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1534 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1535 | } else { |
| 1536 | /* 0=sig, 1=count, 2..count*2 = keys */ |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1537 | targets_offset = 2 + 2 * switch_count; |
| 1538 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1539 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1540 | uint32_t table_size = targets_offset + switch_count * 2; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1541 | if (UNLIKELY(switch_insns[0] != expected_signature)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1542 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1543 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1544 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1545 | return false; |
| 1546 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1547 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1548 | if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1549 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1550 | << ", switch offset " << switch_offset |
| 1551 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1552 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1553 | return false; |
| 1554 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1555 | |
| 1556 | constexpr int32_t keys_offset = 2; |
| 1557 | if (switch_count > 1) { |
| 1558 | if (is_packed_switch) { |
| 1559 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1560 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1561 | int32_t max_first_key = |
| 1562 | 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] | 1563 | if (UNLIKELY(first_key > max_first_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1564 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1565 | << ", switch_count=" << switch_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1566 | return false; |
| 1567 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1568 | } else { |
| 1569 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1570 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1571 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1572 | int32_t key = |
| 1573 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1574 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1575 | if (UNLIKELY(key <= last_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1576 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1577 | << ", this=" << key; |
| 1578 | return false; |
| 1579 | } |
| 1580 | last_key = key; |
| 1581 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1584 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1585 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1586 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1587 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1588 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1589 | if (UNLIKELY(abs_offset < 0 || |
| 1590 | abs_offset >= static_cast<int32_t>(insn_count) || |
| 1591 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1592 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1593 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1594 | << reinterpret_cast<void*>(cur_offset) |
| 1595 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1596 | return false; |
| 1597 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1598 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1599 | } |
| 1600 | return true; |
| 1601 | } |
| 1602 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1603 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1604 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1605 | for (uint32_t idx = 0; idx < vA; idx++) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1606 | if (UNLIKELY(arg[idx] >= registers_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1607 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1608 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1609 | return false; |
| 1610 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1616 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1617 | uint16_t registers_size = code_item_accessor_.RegistersSize(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1618 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1619 | // integer overflow when adding them here. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1620 | if (UNLIKELY(vA + vC > registers_size)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1621 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1622 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1623 | return false; |
| 1624 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1625 | return true; |
| 1626 | } |
| 1627 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1628 | bool MethodVerifier::VerifyCodeFlow() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1629 | const uint16_t registers_size = code_item_accessor_.RegistersSize(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1630 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1631 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1632 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1633 | insn_flags_.get(), |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1634 | code_item_accessor_.InsnsSizeInCodeUnits(), |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1635 | registers_size, |
| 1636 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1637 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1638 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1639 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1640 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1641 | /* Initialize register types of method arguments. */ |
| 1642 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1643 | DCHECK_NE(failures_.size(), 0U); |
| 1644 | std::string prepend("Bad signature in "); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1645 | prepend += dex_file_->PrettyMethod(dex_method_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1646 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1647 | return false; |
| 1648 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1649 | // We may have a runtime failure here, clear. |
| 1650 | have_pending_runtime_throw_failure_ = false; |
| 1651 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1652 | /* Perform code flow verification. */ |
| 1653 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1654 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1655 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1656 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
| 1659 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1660 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1661 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1662 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1663 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1664 | } |
| 1665 | return os; |
| 1666 | } |
| 1667 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1668 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1669 | VariableIndentationOutputStream vios(&os); |
| 1670 | Dump(&vios); |
| 1671 | } |
| 1672 | |
| 1673 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1674 | if (!code_item_accessor_.HasCodeItem()) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1675 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1676 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1677 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1678 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1679 | vios->Stream() << "Register Types:\n"; |
| 1680 | ScopedIndentation indent1(vios); |
| 1681 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1682 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1683 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1684 | ScopedIndentation indent1(vios); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1685 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1686 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1687 | const size_t dex_pc = inst.DexPc(); |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1688 | |
| 1689 | // Might be asked to dump before the table is initialized. |
| 1690 | if (reg_table_.IsInitialized()) { |
| 1691 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1692 | if (reg_line != nullptr) { |
| 1693 | vios->Stream() << reg_line->Dump(this) << "\n"; |
| 1694 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1695 | } |
Andreas Gampe | 077d9db | 2018-01-19 18:54:14 -0800 | [diff] [blame] | 1696 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1697 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1698 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1699 | const bool kDumpHexOfInstruction = false; |
| 1700 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1701 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1702 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1703 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1704 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1707 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1708 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1709 | case 'I': |
| 1710 | case 'C': |
| 1711 | case 'S': |
| 1712 | case 'B': |
| 1713 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1714 | case 'F': |
| 1715 | case 'D': |
| 1716 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1717 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1718 | default: |
| 1719 | return false; |
| 1720 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1723 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1724 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1725 | |
| 1726 | // Should have been verified earlier. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1727 | DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize()); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1728 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1729 | uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize(); |
| 1730 | size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1731 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1732 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1733 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1734 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1735 | if (expected_args == 0) { |
| 1736 | // Expect at least a receiver. |
| 1737 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1738 | return false; |
| 1739 | } |
| 1740 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1741 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1742 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1743 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1744 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1745 | if (IsConstructor()) { |
| 1746 | if (declaring_class.IsJavaLangObject()) { |
| 1747 | // "this" is implicitly initialized. |
| 1748 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1749 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1750 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1751 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1752 | this, |
| 1753 | arg_start + cur_arg, |
| 1754 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1755 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1756 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1757 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1758 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1759 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1762 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1763 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1764 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1765 | |
| 1766 | for (; iterator.HasNext(); iterator.Next()) { |
| 1767 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1768 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1769 | LOG(FATAL) << "Null descriptor"; |
| 1770 | } |
| 1771 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1772 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1773 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1774 | return false; |
| 1775 | } |
| 1776 | switch (descriptor[0]) { |
| 1777 | case 'L': |
| 1778 | case '[': |
| 1779 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1780 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1781 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1782 | // can return without doing anything or call virtual methods). |
| 1783 | { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 1784 | // Note: don't check access. No error would be thrown for declaring or passing an |
| 1785 | // inaccessible class. Only actual accesses to fields or methods will. |
| 1786 | const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1787 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1788 | DCHECK(HasFailures()); |
| 1789 | return false; |
| 1790 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1791 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1792 | } |
| 1793 | break; |
| 1794 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1795 | 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] | 1796 | break; |
| 1797 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1798 | 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] | 1799 | break; |
| 1800 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1801 | 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] | 1802 | break; |
| 1803 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1804 | 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] | 1805 | break; |
| 1806 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1807 | 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] | 1808 | break; |
| 1809 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1810 | 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] | 1811 | break; |
| 1812 | case 'J': |
| 1813 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1814 | if (cur_arg + 1 >= expected_args) { |
| 1815 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1816 | << " args, found more (" << descriptor << ")"; |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1820 | const RegType* lo_half; |
| 1821 | const RegType* hi_half; |
| 1822 | if (descriptor[0] == 'J') { |
| 1823 | lo_half = ®_types_.LongLo(); |
| 1824 | hi_half = ®_types_.LongHi(); |
| 1825 | } else { |
| 1826 | lo_half = ®_types_.DoubleLo(); |
| 1827 | hi_half = ®_types_.DoubleHi(); |
| 1828 | } |
| 1829 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1830 | cur_arg++; |
| 1831 | break; |
| 1832 | } |
| 1833 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1834 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1835 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1836 | return false; |
| 1837 | } |
| 1838 | cur_arg++; |
| 1839 | } |
| 1840 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1841 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1842 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1843 | return false; |
| 1844 | } |
| 1845 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1846 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1847 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1848 | bool result; |
| 1849 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1850 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1851 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1852 | size_t i = 0; |
| 1853 | do { |
| 1854 | i++; |
| 1855 | } while (descriptor[i] == '['); // process leading [ |
| 1856 | if (descriptor[i] == 'L') { // object array |
| 1857 | do { |
| 1858 | i++; // find closing ; |
| 1859 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1860 | result = descriptor[i] == ';'; |
| 1861 | } else { // primitive array |
| 1862 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1863 | } |
| 1864 | } else if (descriptor[0] == 'L') { |
| 1865 | // could be more thorough here, but shouldn't be required |
| 1866 | size_t i = 0; |
| 1867 | do { |
| 1868 | i++; |
| 1869 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1870 | result = descriptor[i] == ';'; |
| 1871 | } else { |
| 1872 | result = false; |
| 1873 | } |
| 1874 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1875 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1876 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1877 | } |
| 1878 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1881 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1882 | const uint16_t* insns = code_item_accessor_.Insns(); |
| 1883 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1884 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1885 | /* Begin by marking the first instruction as "changed". */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1886 | GetInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1887 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1888 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1889 | /* Continue until no instructions are marked "changed". */ |
| 1890 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1891 | if (allow_thread_suspension_) { |
| 1892 | self_->AllowThreadSuspension(); |
| 1893 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1894 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1895 | uint32_t insn_idx = start_guess; |
| 1896 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1897 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1898 | break; |
| 1899 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1900 | if (insn_idx == insns_size) { |
| 1901 | if (start_guess != 0) { |
| 1902 | /* try again, starting from the top */ |
| 1903 | start_guess = 0; |
| 1904 | continue; |
| 1905 | } else { |
| 1906 | /* all flags are clear */ |
| 1907 | break; |
| 1908 | } |
| 1909 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1910 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1911 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1912 | // "changed" flags, we need to load the set of registers from the table. |
| 1913 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1914 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1915 | // target. |
| 1916 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1917 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1918 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1919 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1920 | /* |
| 1921 | * Sanity check: retrieve the stored register line (assuming |
| 1922 | * a full table) and make sure it actually matches. |
| 1923 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1924 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1925 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1926 | if (work_line_->CompareLine(register_line) != 0) { |
| 1927 | Dump(std::cout); |
| 1928 | std::cout << info_messages_.str(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1929 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1930 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1931 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1932 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1933 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1934 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1935 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1936 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1937 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1938 | prepend += " failed to verify: "; |
| 1939 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1940 | return false; |
| 1941 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1942 | /* Clear "changed" and mark as visited. */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1943 | GetInstructionFlags(insn_idx).SetVisited(); |
| 1944 | GetInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1945 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1946 | |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 1947 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1948 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1949 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1950 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1951 | * down the line, possibly in the verifier. |
| 1952 | * |
| 1953 | * If we've substituted "always throw" instructions into the stream, |
| 1954 | * we are almost certainly going to have some dead code. |
| 1955 | */ |
| 1956 | int dead_start = -1; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1957 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1958 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1959 | const uint32_t insn_idx = inst.DexPc(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1960 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1961 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1962 | * may or may not be preceded by a padding NOP (for alignment). |
| 1963 | */ |
| 1964 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1965 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1966 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1967 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1968 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1969 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1970 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1971 | GetInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1974 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1975 | if (dead_start < 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1976 | dead_start = insn_idx; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1977 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1978 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1979 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1980 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1981 | dead_start = -1; |
| 1982 | } |
| 1983 | } |
| 1984 | if (dead_start >= 0) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1985 | LogVerifyInfo() |
| 1986 | << "dead code " << reinterpret_cast<void*>(dead_start) |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1987 | << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1988 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1989 | // 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] | 1990 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1991 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1992 | // LOG(INFO) << info_messages_.str(); |
| 1993 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1994 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1995 | return true; |
| 1996 | } |
| 1997 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1998 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1999 | // is no such field. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2000 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2001 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 2002 | DCHECK(class_def != nullptr); |
| 2003 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 2004 | DCHECK(class_data != nullptr); |
| 2005 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 2006 | it.SkipStaticFields(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2007 | while (it.HasNextInstanceField()) { |
| 2008 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 2009 | return it.GetMemberIndex(); |
| 2010 | } |
| 2011 | it.Next(); |
| 2012 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2013 | return dex::kDexNoIndex; |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2016 | // Setup a register line for the given return instruction. |
| 2017 | static void AdjustReturnLine(MethodVerifier* verifier, |
| 2018 | const Instruction* ret_inst, |
| 2019 | RegisterLine* line) { |
| 2020 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2021 | |
| 2022 | switch (opcode) { |
| 2023 | case Instruction::RETURN_VOID: |
| 2024 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2025 | SafelyMarkAllRegistersAsConflicts(verifier, line); |
| 2026 | break; |
| 2027 | |
| 2028 | case Instruction::RETURN: |
| 2029 | case Instruction::RETURN_OBJECT: |
| 2030 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 2031 | break; |
| 2032 | |
| 2033 | case Instruction::RETURN_WIDE: |
| 2034 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 2035 | break; |
| 2036 | |
| 2037 | default: |
| 2038 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 2039 | UNREACHABLE(); |
| 2040 | } |
| 2041 | } |
| 2042 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2043 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2044 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 2045 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 2046 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 2047 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2048 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2049 | 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] | 2050 | |
| 2051 | std::map<uint32_t, DexLockInfo> depth_to_lock_info; |
| 2052 | auto collector = [&](uint32_t dex_reg, uint32_t depth) { |
| 2053 | auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth)); |
| 2054 | auto it = insert_pair.first; |
| 2055 | auto set_insert_pair = it->second.dex_registers.insert(dex_reg); |
| 2056 | DCHECK(set_insert_pair.second); |
| 2057 | }; |
| 2058 | work_line_->IterateRegToLockDepths(collector); |
| 2059 | for (auto& pair : depth_to_lock_info) { |
| 2060 | monitor_enter_dex_pcs_->push_back(pair.second); |
| 2061 | // Map depth to dex PC. |
| 2062 | (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc = |
| 2063 | work_line_->GetMonitorEnterDexPc(pair.second.dex_pc); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | /* |
| 2068 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2069 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2070 | * control to another statement: |
| 2071 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2072 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2073 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2074 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2075 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2076 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2077 | * throw an exception that is handled by an encompassing "try" |
| 2078 | * block. |
| 2079 | * |
| 2080 | * We can also return, in which case there is no successor instruction |
| 2081 | * from this point. |
| 2082 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2083 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2084 | */ |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2085 | const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2086 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2087 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2088 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2089 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2090 | bool just_set_result = false; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 2091 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2092 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2093 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2094 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2095 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2096 | |
| 2097 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2098 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2099 | * can throw an exception, we will copy/merge this into the "catch" |
| 2100 | * address rather than work_line, because we don't want the result |
| 2101 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2102 | * a type) to be visible to the exception handler. |
| 2103 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2104 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2105 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2106 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2107 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2108 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 2109 | 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] | 2110 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2111 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2112 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2113 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2114 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2115 | RegisterLineArenaUniquePtr branch_line; |
| 2116 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2117 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2118 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2119 | case Instruction::NOP: |
| 2120 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2121 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2122 | * a signature that looks like a NOP; if we see one of these in |
| 2123 | * the course of executing code then we have a problem. |
| 2124 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2125 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2126 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2127 | } |
| 2128 | break; |
| 2129 | |
| 2130 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2131 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2132 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2133 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2134 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2135 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2136 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2137 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2138 | break; |
| 2139 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2140 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2141 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2142 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2143 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2144 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2145 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2146 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2147 | break; |
| 2148 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2149 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2150 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2151 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2152 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2153 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2154 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2155 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2156 | break; |
| 2157 | |
| 2158 | /* |
| 2159 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2160 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2161 | * might want to hold the result in an actual CPU register, so the |
| 2162 | * Dalvik spec requires that these only appear immediately after an |
| 2163 | * invoke or filled-new-array. |
| 2164 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2165 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2166 | * redundant with the reset done below, but it can make the debug info |
| 2167 | * easier to read in some cases.) |
| 2168 | */ |
| 2169 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2170 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2171 | break; |
| 2172 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2173 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2174 | break; |
| 2175 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2176 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2177 | break; |
| 2178 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2179 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2180 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 2181 | // where one entrypoint to the catch block is not actually an exception path. |
| 2182 | if (work_insn_idx_ == 0) { |
| 2183 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 2184 | break; |
| 2185 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2186 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2187 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2188 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2189 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2190 | const RegType& res_type = GetCaughtExceptionType(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2191 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2192 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2193 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2194 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2195 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2196 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2197 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2198 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2199 | } |
| 2200 | break; |
| 2201 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2202 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2203 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2204 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2205 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2206 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2207 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2208 | } else { |
| 2209 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2210 | // 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] | 2211 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2212 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2213 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2214 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2215 | return_type.IsShort() || return_type.IsChar()) && |
| 2216 | src_type.IsInteger())); |
| 2217 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2218 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2219 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2220 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2221 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2222 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | break; |
| 2226 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2227 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2228 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2229 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2230 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2231 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2232 | } else { |
| 2233 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2234 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2235 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2236 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2237 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2238 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2239 | } |
| 2240 | } |
| 2241 | break; |
| 2242 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2243 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2244 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2245 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2246 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2247 | } else { |
| 2248 | /* return_type is the *expected* return type, not register value */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2249 | DCHECK(!return_type.IsZeroOrNull()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2250 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2251 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2252 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2253 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2254 | // reference in vAA is an instance of the "return_type." |
| 2255 | if (reg_type.IsUndefined()) { |
| 2256 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2257 | } else if (reg_type.IsConflict()) { |
| 2258 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2259 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2260 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2261 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2262 | } else if (!reg_type.IsReferenceTypes()) { |
| 2263 | // We really do expect a reference here. |
| 2264 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2265 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2266 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2267 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 2268 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 2269 | << "' or '" << reg_type << "'"; |
| 2270 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2271 | bool soft_error = false; |
| 2272 | // Check whether arrays are involved. They will show a valid class status, even |
| 2273 | // if their components are erroneous. |
| 2274 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2275 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2276 | if (soft_error) { |
| 2277 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2278 | << reg_type << " vs " << return_type; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | if (!soft_error) { |
| 2283 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2284 | << "', but expected from declaration '" << return_type << "'"; |
| 2285 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2286 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | break; |
| 2291 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2292 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2293 | case Instruction::CONST_4: { |
| 2294 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2295 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2296 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2297 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2298 | } |
| 2299 | case Instruction::CONST_16: { |
| 2300 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2301 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2302 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2303 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2304 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2305 | case Instruction::CONST: { |
| 2306 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2307 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2308 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2309 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2310 | } |
| 2311 | case Instruction::CONST_HIGH16: { |
| 2312 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2313 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2314 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2315 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2316 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2317 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2318 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2319 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2320 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2321 | 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] | 2322 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2324 | } |
| 2325 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2326 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2327 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2328 | 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] | 2329 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2330 | break; |
| 2331 | } |
| 2332 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2333 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2334 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2335 | 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] | 2336 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2337 | break; |
| 2338 | } |
| 2339 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2340 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2341 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2342 | 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] | 2343 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2344 | break; |
| 2345 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2346 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2347 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2348 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2349 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2350 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2351 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2352 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2353 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2354 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2355 | // Get type from instruction if unresolved then we need an access check |
| 2356 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2357 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2358 | // 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] | 2359 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2360 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2361 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2362 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2363 | } |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2364 | case Instruction::CONST_METHOD_HANDLE: |
| 2365 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2366 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle()); |
| 2367 | // TODO: add compiler support for const-method-{handle,type} (b/66890674) |
| 2368 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 2369 | break; |
| 2370 | case Instruction::CONST_METHOD_TYPE: |
| 2371 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2372 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType()); |
| 2373 | // TODO: add compiler support for const-method-{handle,type} (b/66890674) |
| 2374 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 2375 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2376 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2377 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2378 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2379 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2380 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2381 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2382 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2383 | prev_idx--; |
| 2384 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2385 | const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2386 | switch (prev_inst.Opcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2387 | case Instruction::MOVE_OBJECT: |
| 2388 | case Instruction::MOVE_OBJECT_16: |
| 2389 | case Instruction::MOVE_OBJECT_FROM16: |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2390 | if (prev_inst.VRegB() == inst->VRegA_11x()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2391 | // Redo the copy. This won't change the register types, but update the lock status |
| 2392 | // for the aliased register. |
| 2393 | work_line_->CopyRegister1(this, |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2394 | prev_inst.VRegA(), |
| 2395 | prev_inst.VRegB(), |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2396 | kTypeCategoryRef); |
| 2397 | } |
| 2398 | break; |
| 2399 | |
| 2400 | default: // Other instruction types ignored. |
| 2401 | break; |
| 2402 | } |
| 2403 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2404 | break; |
| 2405 | case Instruction::MONITOR_EXIT: |
| 2406 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2407 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2408 | * 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] | 2409 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2410 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2411 | * feature that Dalvik doesn't support.) |
| 2412 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2413 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2414 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2415 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2416 | * structured locking checks are working, the former would have |
| 2417 | * failed on the -enter instruction, and the latter is impossible. |
| 2418 | * |
| 2419 | * This is fortunate, because issue 3221411 prevents us from |
| 2420 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2421 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2422 | * some catch blocks (which will show up as "dead" code when |
| 2423 | * we skip them here); if we can't, then the code path could be |
| 2424 | * "live" so we still need to check it. |
| 2425 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2426 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2427 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2428 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2429 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2430 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2431 | /* |
| 2432 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2433 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2434 | * |
| 2435 | * 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] | 2436 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2437 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2438 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2439 | 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] | 2440 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2441 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2442 | // If this is a primitive type, fail HARD. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2443 | ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType( |
| 2444 | type_idx, dex_cache_.Get(), class_loader_.Get()); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2445 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2446 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2447 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2448 | << GetDeclaringClass(); |
| 2449 | break; |
| 2450 | } |
| 2451 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2452 | DCHECK_NE(failures_.size(), 0U); |
| 2453 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2454 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2455 | inst->VRegA_22c(), |
| 2456 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2457 | } |
| 2458 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2459 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2460 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2461 | 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] | 2462 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2463 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2464 | if (is_checkcast) { |
| 2465 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2466 | } else { |
| 2467 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2468 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2469 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2470 | if (is_checkcast) { |
| 2471 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2472 | } else { |
| 2473 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2474 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2475 | } else if (orig_type.IsUninitializedTypes()) { |
| 2476 | if (is_checkcast) { |
| 2477 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2478 | << orig_type_reg; |
| 2479 | } else { |
| 2480 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2481 | << orig_type_reg; |
| 2482 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2483 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2484 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2485 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2486 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2487 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2488 | inst->VRegA_22c(), |
| 2489 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2490 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2491 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2492 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2493 | } |
| 2494 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2495 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2496 | if (res_type.IsReferenceTypes()) { |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2497 | if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) { |
| 2498 | // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2499 | 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] | 2500 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2501 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2502 | inst->VRegA_12x(), |
| 2503 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2504 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2505 | } else { |
| 2506 | 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] | 2507 | } |
| 2508 | break; |
| 2509 | } |
| 2510 | case Instruction::NEW_INSTANCE: { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2511 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2512 | if (res_type.IsConflict()) { |
| 2513 | DCHECK_NE(failures_.size(), 0U); |
| 2514 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2515 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2516 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2517 | // can't create an instance of an interface or abstract class */ |
| 2518 | if (!res_type.IsInstantiableTypes()) { |
| 2519 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2520 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2521 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2522 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2523 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2524 | // Any registers holding previous allocations from this address that have not yet been |
| 2525 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2526 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2527 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2528 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2529 | break; |
| 2530 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2531 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2532 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2533 | break; |
| 2534 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2535 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2536 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2537 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2538 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2539 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2540 | just_set_result = true; // Filled new array range sets result register |
| 2541 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2542 | case Instruction::CMPL_FLOAT: |
| 2543 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2544 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2545 | break; |
| 2546 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2547 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2548 | break; |
| 2549 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2550 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2551 | break; |
| 2552 | case Instruction::CMPL_DOUBLE: |
| 2553 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2554 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2555 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2556 | break; |
| 2557 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2558 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2559 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2560 | break; |
| 2561 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2562 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2563 | break; |
| 2564 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2565 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2566 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2567 | break; |
| 2568 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2569 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2570 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2571 | break; |
| 2572 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2573 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2574 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2575 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2576 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2577 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2578 | if (res_type.IsUninitializedTypes()) { |
| 2579 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2580 | } else if (!res_type.IsReferenceTypes()) { |
| 2581 | 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] | 2582 | } else { |
| 2583 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2584 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2585 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2586 | } |
| 2587 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2588 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2589 | case Instruction::GOTO: |
| 2590 | case Instruction::GOTO_16: |
| 2591 | case Instruction::GOTO_32: |
| 2592 | /* no effect on or use of registers */ |
| 2593 | break; |
| 2594 | |
| 2595 | case Instruction::PACKED_SWITCH: |
| 2596 | case Instruction::SPARSE_SWITCH: |
| 2597 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2598 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2599 | break; |
| 2600 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2601 | case Instruction::FILL_ARRAY_DATA: { |
| 2602 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2603 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2604 | /* array_type can be null if the reg type is Zero */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2605 | if (!array_type.IsZeroOrNull()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2606 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2607 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2608 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2609 | } else if (array_type.IsUnresolvedTypes()) { |
| 2610 | // If it's an unresolved array type, it must be non-primitive. |
| 2611 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2612 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2613 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2614 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2615 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2616 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2617 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2618 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2619 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2620 | // Now verify if the element width in the table matches the element width declared in |
| 2621 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2622 | const uint16_t* array_data = |
| 2623 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2624 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2625 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2626 | } else { |
| 2627 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2628 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2629 | // in the table and expected from the array class. |
| 2630 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2631 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2632 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2633 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2634 | } |
| 2635 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2639 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2640 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2641 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2642 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2643 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2644 | bool mismatch = false; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2645 | if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2646 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2647 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2648 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2649 | } else { // both integral? |
| 2650 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2651 | } |
| 2652 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2653 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2654 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2655 | } |
| 2656 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2657 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2658 | case Instruction::IF_LT: |
| 2659 | case Instruction::IF_GE: |
| 2660 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2661 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2662 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2663 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2664 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2665 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2666 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2667 | } |
| 2668 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2669 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2670 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2671 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2672 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2673 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2674 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2675 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2676 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2677 | |
| 2678 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2679 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2680 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2681 | instance_of_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2682 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2683 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2684 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2685 | if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2686 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2687 | work_insn_idx_)) { |
| 2688 | break; |
| 2689 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2690 | } else { |
| 2691 | break; |
| 2692 | } |
| 2693 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2694 | const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2695 | |
| 2696 | /* Check for peep-hole pattern of: |
| 2697 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2698 | * instance-of vX, vY, T; |
| 2699 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2700 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2701 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2702 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2703 | * and sharpen the type of vY to be type T. |
| 2704 | * Note, this pattern can't be if: |
| 2705 | * - if there are other branches to this branch, |
| 2706 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2707 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2708 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2709 | (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) && |
| 2710 | (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) && |
| 2711 | (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2712 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2713 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2714 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2715 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2716 | // and we don't want to disallow field and other operations on the object. If the value |
| 2717 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2718 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2719 | // type is assignable to the original then allow optimization. This check is performed to |
| 2720 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2721 | // interface from a class that would lose information relevant to field checks. |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2722 | 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] | 2723 | const RegType& cast_type = ResolveClass<CheckAccess::kYes>( |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2724 | dex::TypeIndex(instance_of_inst.VRegC_22c())); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2725 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2726 | if (!orig_type.Equals(cast_type) && |
| 2727 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2728 | 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] | 2729 | !cast_type.GetClass()->IsInterface() && |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2730 | (orig_type.IsZeroOrNull() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2731 | orig_type.IsStrictlyAssignableFrom( |
| 2732 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2733 | RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(), |
| 2734 | this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2735 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2736 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2737 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2738 | branch_line.reset(update_line); |
| 2739 | } |
| 2740 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2741 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2742 | instance_of_inst.VRegB_22c(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2743 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2744 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2745 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2746 | // register encoding space of instance-of, and propagate type information to the source |
| 2747 | // of the move-object. |
| 2748 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2749 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2750 | move_idx--; |
| 2751 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2752 | if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2753 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2754 | work_insn_idx_)) { |
| 2755 | break; |
| 2756 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2757 | const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2758 | switch (move_inst.Opcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2759 | case Instruction::MOVE_OBJECT: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2760 | if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2761 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2762 | move_inst.VRegB_12x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2763 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2764 | } |
| 2765 | break; |
| 2766 | case Instruction::MOVE_OBJECT_FROM16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2767 | if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2768 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2769 | move_inst.VRegB_22x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2770 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2771 | } |
| 2772 | break; |
| 2773 | case Instruction::MOVE_OBJECT_16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2774 | if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2775 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2776 | move_inst.VRegB_32x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2777 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2778 | } |
| 2779 | break; |
| 2780 | default: |
| 2781 | break; |
| 2782 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2787 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2788 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2789 | case Instruction::IF_LTZ: |
| 2790 | case Instruction::IF_GEZ: |
| 2791 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2792 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2793 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2794 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2795 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2796 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2797 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2798 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2799 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2800 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2801 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2802 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2803 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2804 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2805 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2806 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2807 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2808 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2809 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2810 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2811 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2812 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2813 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2814 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2815 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2816 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2819 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2820 | break; |
| 2821 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2822 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2823 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2826 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2829 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2830 | break; |
| 2831 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2832 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2833 | break; |
| 2834 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2835 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2836 | break; |
| 2837 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2838 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2839 | break; |
| 2840 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2841 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2842 | break; |
| 2843 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2844 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2845 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2846 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2847 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2848 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2849 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2850 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2851 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2852 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2853 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2854 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2855 | break; |
| 2856 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2857 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2858 | break; |
| 2859 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2860 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2861 | break; |
| 2862 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2863 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2864 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2865 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2866 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2867 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2868 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2869 | break; |
| 2870 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2871 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2872 | break; |
| 2873 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2874 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2875 | break; |
| 2876 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2877 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2878 | break; |
| 2879 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2880 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2881 | break; |
| 2882 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2883 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2884 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2885 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2886 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2887 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2888 | break; |
| 2889 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2890 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2891 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2892 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2893 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2894 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2895 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2896 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2897 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2898 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2899 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2900 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2901 | break; |
| 2902 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2903 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2904 | break; |
| 2905 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2906 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2907 | break; |
| 2908 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2909 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2910 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2911 | break; |
| 2912 | |
| 2913 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2914 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2915 | break; |
| 2916 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2917 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2918 | break; |
| 2919 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2920 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2921 | break; |
| 2922 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2923 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2924 | break; |
| 2925 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2926 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2927 | break; |
| 2928 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2929 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2930 | break; |
| 2931 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2932 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2933 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2934 | break; |
| 2935 | |
| 2936 | case Instruction::INVOKE_VIRTUAL: |
| 2937 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2938 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2939 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2940 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2941 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2942 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2943 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2944 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2945 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2946 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2947 | if (called_method != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2948 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2949 | ? called_method->ResolveReturnType() |
| 2950 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2951 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2952 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2953 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2954 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2955 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2956 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2957 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2961 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2962 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2963 | dex::TypeIndex return_type_idx = |
| 2964 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2965 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2966 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2967 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2968 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2969 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2970 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2971 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2972 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2973 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2974 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2975 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2976 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2977 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2978 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2979 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2980 | const char* return_type_descriptor; |
| 2981 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2982 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2983 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2984 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2985 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2986 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2987 | dex::TypeIndex return_type_idx = |
| 2988 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2989 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2990 | } else { |
| 2991 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2992 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2993 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2994 | ? called_method->ResolveReturnType() |
| 2995 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2996 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2997 | return_type = &FromClass(return_type_descriptor, |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2998 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2999 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3000 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3001 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 3002 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3003 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3004 | } |
| 3005 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3006 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3007 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 3008 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 3009 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 3010 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 3011 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3012 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3013 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3014 | if (this_type.IsConflict()) // failure. |
| 3015 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3016 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3017 | /* no null refs allowed (?) */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3018 | if (this_type.IsZeroOrNull()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3019 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 3020 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 3021 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3022 | |
| 3023 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3024 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3025 | // TODO: re-enable constructor type verification |
| 3026 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3027 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3028 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 3029 | // break; |
| 3030 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3031 | |
| 3032 | /* arg must be an uninitialized reference */ |
| 3033 | if (!this_type.IsUninitializedTypes()) { |
| 3034 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 3035 | << this_type; |
| 3036 | break; |
| 3037 | } |
| 3038 | |
| 3039 | /* |
| 3040 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 3041 | * registers that have the same object instance in them, not just the "this" register. |
| 3042 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 3043 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3044 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3045 | if (return_type == nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3046 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3047 | } |
| 3048 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3049 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3050 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3051 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3052 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3053 | just_set_result = true; |
| 3054 | break; |
| 3055 | } |
| 3056 | case Instruction::INVOKE_STATIC: |
| 3057 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3058 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3059 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3060 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3061 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3062 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3063 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3064 | dex::TypeIndex return_type_idx = |
| 3065 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3066 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3067 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3068 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3069 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3070 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3071 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3072 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3073 | } else { |
| 3074 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3075 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3076 | just_set_result = true; |
| 3077 | } |
| 3078 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3079 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3080 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3081 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3082 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3083 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3084 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3085 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3086 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3087 | << abs_method->PrettyMethod() << "'"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3088 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3089 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3090 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3091 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3092 | * interface or Object (see comments in RegType::JoinClass). |
| 3093 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3094 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3095 | if (this_type.IsZeroOrNull()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3096 | /* null pointer always passes (and always fails at runtime) */ |
| 3097 | } else { |
| 3098 | if (this_type.IsUninitializedTypes()) { |
| 3099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3100 | << this_type; |
| 3101 | break; |
| 3102 | } |
| 3103 | // In the past we have tried to assert that "called_interface" is assignable |
| 3104 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3105 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3106 | // implemented by "this_type". For example, two classes may implement the same |
| 3107 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3108 | // join will set "this_type" to the parent class and a test that this implements |
| 3109 | // the interface will incorrectly fail. |
| 3110 | } |
| 3111 | /* |
| 3112 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3113 | * the type information is in the abstract method, so we're good. |
| 3114 | */ |
| 3115 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3116 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3117 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3118 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3119 | dex::TypeIndex return_type_idx = |
| 3120 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3121 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3122 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3123 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3124 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3125 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3126 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3127 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3128 | } else { |
| 3129 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3130 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3131 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3132 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3133 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3134 | case Instruction::INVOKE_POLYMORPHIC: |
| 3135 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3136 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3137 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3138 | if (called_method == nullptr) { |
| 3139 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3140 | if (failure_messages_.size() > 0) { |
| 3141 | std::string message = failure_messages_.back()->str(); |
| 3142 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3143 | } else { |
| 3144 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3145 | } |
| 3146 | break; |
| 3147 | } |
| 3148 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3149 | !CheckSignaturePolymorphicReceiver(inst)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 3150 | DCHECK(HasFailures()); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3151 | break; |
| 3152 | } |
| 3153 | const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3154 | const char* return_descriptor = |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3155 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3156 | const RegType& return_type = |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3157 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3158 | if (!return_type.IsLowHalf()) { |
| 3159 | work_line_->SetResultRegisterType(this, return_type); |
| 3160 | } else { |
| 3161 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3162 | } |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3163 | just_set_result = true; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3164 | break; |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3165 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3166 | case Instruction::INVOKE_CUSTOM: |
| 3167 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3168 | // Verify registers based on method_type in the call site. |
| 3169 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3170 | |
| 3171 | // Step 1. Check the call site that produces the method handle for invocation |
| 3172 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3173 | if (!CheckCallSite(call_site_idx)) { |
| 3174 | DCHECK(HasFailures()); |
| 3175 | break; |
| 3176 | } |
| 3177 | |
| 3178 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3179 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3180 | // the first three arguments and CheckCallSite has checked the method handle type. |
| 3181 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 3182 | it.Next(); // Skip to name. |
| 3183 | it.Next(); // Skip to method type of the method handle |
| 3184 | const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 3185 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
| 3186 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3187 | // Treat method as static as it has yet to be determined. |
| 3188 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3189 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3190 | |
| 3191 | // Step 3. Propagate return type information |
| 3192 | const RegType& return_type = |
| 3193 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
| 3194 | if (!return_type.IsLowHalf()) { |
| 3195 | work_line_->SetResultRegisterType(this, return_type); |
| 3196 | } else { |
| 3197 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3198 | } |
| 3199 | just_set_result = true; |
| 3200 | // TODO: Add compiler support for invoke-custom (b/35337872). |
| 3201 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 3202 | break; |
| 3203 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3204 | case Instruction::NEG_INT: |
| 3205 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3206 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3207 | break; |
| 3208 | case Instruction::NEG_LONG: |
| 3209 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3210 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3211 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3212 | break; |
| 3213 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3214 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3215 | break; |
| 3216 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3217 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3218 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3219 | break; |
| 3220 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3221 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3222 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3223 | break; |
| 3224 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3225 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3226 | break; |
| 3227 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3228 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3229 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3230 | break; |
| 3231 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3232 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3233 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3234 | break; |
| 3235 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3236 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3237 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3240 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3241 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3242 | break; |
| 3243 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3244 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3245 | break; |
| 3246 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3247 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3248 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3249 | break; |
| 3250 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3251 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3252 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3253 | break; |
| 3254 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3255 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3256 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3257 | break; |
| 3258 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3259 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3260 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3261 | break; |
| 3262 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3263 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3264 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3265 | break; |
| 3266 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3267 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3268 | break; |
| 3269 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3270 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3271 | break; |
| 3272 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3273 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3274 | break; |
| 3275 | |
| 3276 | case Instruction::ADD_INT: |
| 3277 | case Instruction::SUB_INT: |
| 3278 | case Instruction::MUL_INT: |
| 3279 | case Instruction::REM_INT: |
| 3280 | case Instruction::DIV_INT: |
| 3281 | case Instruction::SHL_INT: |
| 3282 | case Instruction::SHR_INT: |
| 3283 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3284 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3285 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3286 | break; |
| 3287 | case Instruction::AND_INT: |
| 3288 | case Instruction::OR_INT: |
| 3289 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3290 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3291 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3292 | break; |
| 3293 | case Instruction::ADD_LONG: |
| 3294 | case Instruction::SUB_LONG: |
| 3295 | case Instruction::MUL_LONG: |
| 3296 | case Instruction::DIV_LONG: |
| 3297 | case Instruction::REM_LONG: |
| 3298 | case Instruction::AND_LONG: |
| 3299 | case Instruction::OR_LONG: |
| 3300 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3301 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3302 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3303 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3304 | break; |
| 3305 | case Instruction::SHL_LONG: |
| 3306 | case Instruction::SHR_LONG: |
| 3307 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3308 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3309 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3310 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3311 | break; |
| 3312 | case Instruction::ADD_FLOAT: |
| 3313 | case Instruction::SUB_FLOAT: |
| 3314 | case Instruction::MUL_FLOAT: |
| 3315 | case Instruction::DIV_FLOAT: |
| 3316 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3317 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3318 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3319 | break; |
| 3320 | case Instruction::ADD_DOUBLE: |
| 3321 | case Instruction::SUB_DOUBLE: |
| 3322 | case Instruction::MUL_DOUBLE: |
| 3323 | case Instruction::DIV_DOUBLE: |
| 3324 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3325 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3326 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3327 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3328 | break; |
| 3329 | case Instruction::ADD_INT_2ADDR: |
| 3330 | case Instruction::SUB_INT_2ADDR: |
| 3331 | case Instruction::MUL_INT_2ADDR: |
| 3332 | case Instruction::REM_INT_2ADDR: |
| 3333 | case Instruction::SHL_INT_2ADDR: |
| 3334 | case Instruction::SHR_INT_2ADDR: |
| 3335 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3336 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3337 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3338 | break; |
| 3339 | case Instruction::AND_INT_2ADDR: |
| 3340 | case Instruction::OR_INT_2ADDR: |
| 3341 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3342 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3343 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3344 | break; |
| 3345 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3346 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3347 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3348 | break; |
| 3349 | case Instruction::ADD_LONG_2ADDR: |
| 3350 | case Instruction::SUB_LONG_2ADDR: |
| 3351 | case Instruction::MUL_LONG_2ADDR: |
| 3352 | case Instruction::DIV_LONG_2ADDR: |
| 3353 | case Instruction::REM_LONG_2ADDR: |
| 3354 | case Instruction::AND_LONG_2ADDR: |
| 3355 | case Instruction::OR_LONG_2ADDR: |
| 3356 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3357 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3358 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3359 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3360 | break; |
| 3361 | case Instruction::SHL_LONG_2ADDR: |
| 3362 | case Instruction::SHR_LONG_2ADDR: |
| 3363 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3364 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3365 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3366 | break; |
| 3367 | case Instruction::ADD_FLOAT_2ADDR: |
| 3368 | case Instruction::SUB_FLOAT_2ADDR: |
| 3369 | case Instruction::MUL_FLOAT_2ADDR: |
| 3370 | case Instruction::DIV_FLOAT_2ADDR: |
| 3371 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3372 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3373 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3374 | break; |
| 3375 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3376 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3377 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3378 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3379 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3380 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3381 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3382 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3383 | break; |
| 3384 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3385 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3386 | case Instruction::MUL_INT_LIT16: |
| 3387 | case Instruction::DIV_INT_LIT16: |
| 3388 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3389 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3390 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3391 | break; |
| 3392 | case Instruction::AND_INT_LIT16: |
| 3393 | case Instruction::OR_INT_LIT16: |
| 3394 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3395 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3396 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3397 | break; |
| 3398 | case Instruction::ADD_INT_LIT8: |
| 3399 | case Instruction::RSUB_INT_LIT8: |
| 3400 | case Instruction::MUL_INT_LIT8: |
| 3401 | case Instruction::DIV_INT_LIT8: |
| 3402 | case Instruction::REM_INT_LIT8: |
| 3403 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3404 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3405 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3406 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3407 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3408 | break; |
| 3409 | case Instruction::AND_INT_LIT8: |
| 3410 | case Instruction::OR_INT_LIT8: |
| 3411 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3412 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3413 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3414 | break; |
| 3415 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3416 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3417 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3418 | if (IsConstructor() && !IsStatic()) { |
| 3419 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3420 | if (declaring_class.IsUnresolvedReference()) { |
| 3421 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3422 | // manually over the underlying dex file. |
| 3423 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3424 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3425 | if (first_index != dex::kDexNoIndex) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3426 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3427 | << first_index; |
| 3428 | } |
| 3429 | break; |
| 3430 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3431 | auto* klass = declaring_class.GetClass(); |
| 3432 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3433 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3434 | 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] | 3435 | << klass->GetInstanceField(i)->PrettyField(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3439 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3440 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3441 | // quickened opcodes (otherwise this could be a fall-through). |
| 3442 | if (!IsConstructor()) { |
| 3443 | if (!GetMethodReturnType().IsConflict()) { |
| 3444 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3445 | } |
| 3446 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3447 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3448 | // Note: the following instructions encode offsets derived from class linking. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 3449 | // As such they use Class*/Field*/Executable* as these offsets only have |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3450 | // meaning if the class linking and resolution were successful. |
| 3451 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3452 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3453 | break; |
| 3454 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3455 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3456 | break; |
| 3457 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3458 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3459 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3460 | case Instruction::IGET_BOOLEAN_QUICK: |
| 3461 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 3462 | break; |
| 3463 | case Instruction::IGET_BYTE_QUICK: |
| 3464 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 3465 | break; |
| 3466 | case Instruction::IGET_CHAR_QUICK: |
| 3467 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 3468 | break; |
| 3469 | case Instruction::IGET_SHORT_QUICK: |
| 3470 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 3471 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3472 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3473 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3474 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3475 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3476 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3477 | break; |
| 3478 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3479 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3480 | break; |
| 3481 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3482 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3483 | break; |
| 3484 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3485 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3486 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3487 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3488 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3489 | break; |
| 3490 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3491 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3492 | break; |
| 3493 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 3494 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 3495 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3496 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3497 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3498 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3499 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3500 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3501 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3502 | } else { |
| 3503 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3504 | } |
| 3505 | just_set_result = true; |
| 3506 | } |
| 3507 | break; |
| 3508 | } |
| 3509 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3510 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3511 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3512 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3513 | case Instruction::UNUSED_79: |
| 3514 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3515 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3516 | break; |
| 3517 | |
| 3518 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3519 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3520 | * complain if an instruction is missing (which is desirable). |
| 3521 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3522 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3523 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3524 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3525 | if (Runtime::Current()->IsAotCompiler()) { |
| 3526 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3527 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3528 | LOG(ERROR) << "Pending failures:"; |
| 3529 | for (auto& error : failures_) { |
| 3530 | LOG(ERROR) << error; |
| 3531 | } |
| 3532 | for (auto& error_msg : failure_messages_) { |
| 3533 | LOG(ERROR) << error_msg->str(); |
| 3534 | } |
| 3535 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3536 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3537 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3538 | /* immediate failure, reject class */ |
| 3539 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3540 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3541 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3542 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3543 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3544 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3545 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3546 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3547 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3548 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3549 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3550 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3551 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3552 | */ |
| 3553 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3554 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3557 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3558 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3559 | * |
| 3560 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3561 | * 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] | 3562 | * somebody could get a reference field, check it for zero, and if the |
| 3563 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3564 | * 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] | 3565 | * that, and will reject the code. |
| 3566 | * |
| 3567 | * TODO: avoid re-fetching the branch target |
| 3568 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3569 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3570 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3571 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3572 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3573 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3574 | return false; |
| 3575 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3576 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3577 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), |
| 3578 | work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3579 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3580 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3581 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3582 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3583 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3584 | return false; |
| 3585 | } |
| 3586 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3587 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3588 | return false; |
| 3589 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3590 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3594 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3595 | * |
| 3596 | * We've already verified that the table is structurally sound, so we |
| 3597 | * just need to walk through and tag the targets. |
| 3598 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3599 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3600 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3601 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3602 | int switch_count = switch_insns[1]; |
| 3603 | int offset_to_targets, targ; |
| 3604 | |
| 3605 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3606 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3607 | offset_to_targets = 4; |
| 3608 | } else { |
| 3609 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3610 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3611 | offset_to_targets = 2 + 2 * switch_count; |
| 3612 | } |
| 3613 | |
| 3614 | /* verify each switch target */ |
| 3615 | for (targ = 0; targ < switch_count; targ++) { |
| 3616 | int offset; |
| 3617 | uint32_t abs_offset; |
| 3618 | |
| 3619 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3620 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3621 | (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] | 3622 | abs_offset = work_insn_idx_ + offset; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3623 | DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits()); |
| 3624 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3625 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3626 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3627 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3628 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3629 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3634 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3635 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3636 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3637 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3638 | bool has_catch_all_handler = false; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3639 | const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_); |
| 3640 | CHECK(try_item != nullptr); |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 3641 | CatchHandlerIterator iterator(code_item_accessor_, *try_item); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3642 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3643 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3644 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3645 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3646 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3647 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3648 | if (!handler_type_idx.IsValid()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3649 | has_catch_all_handler = true; |
| 3650 | } else { |
| 3651 | // It is also a catch-all if it is java.lang.Throwable. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3652 | ObjPtr<mirror::Class> klass = |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3653 | linker->ResolveType(handler_type_idx, dex_cache_, class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3654 | if (klass != nullptr) { |
| 3655 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3656 | has_catch_all_handler = true; |
| 3657 | } |
| 3658 | } else { |
| 3659 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3660 | DCHECK(self_->IsExceptionPending()); |
| 3661 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3662 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3663 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3664 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3665 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3666 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3667 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3668 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3669 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3670 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3671 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3675 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3676 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3677 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3678 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3679 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3680 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3681 | * 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] | 3682 | * it will do so before grabbing the lock). |
| 3683 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3684 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3685 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3686 | << "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] | 3687 | return false; |
| 3688 | } |
| 3689 | } |
| 3690 | } |
| 3691 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3692 | /* Handle "continue". Tag the next consecutive instruction. |
| 3693 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3694 | * because it changes work_line_ when performing peephole optimization |
| 3695 | * and this change should not be used in those cases. |
| 3696 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3697 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3698 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3699 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3700 | if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3701 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3702 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3703 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3704 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3705 | // next instruction isn't one. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3706 | if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3707 | return false; |
| 3708 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3709 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3710 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3711 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3712 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3713 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3714 | // 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] | 3715 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3716 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3717 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3718 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3719 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3720 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3721 | // needed. If the merge changes the state of the registers then the work line will be |
| 3722 | // updated. |
| 3723 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3724 | return false; |
| 3725 | } |
| 3726 | } else { |
| 3727 | /* |
| 3728 | * We're not recording register data for the next instruction, so we don't know what the |
| 3729 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3730 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3731 | GetInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3732 | } |
| 3733 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3734 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3735 | /* 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] | 3736 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3737 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3741 | * Update start_guess. Advance to the next instruction of that's |
| 3742 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3743 | * neither of those exists we're in a return or throw; leave start_guess |
| 3744 | * alone and let the caller sort it out. |
| 3745 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3746 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3747 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3748 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3749 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3750 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3751 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3754 | DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits()); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3755 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3756 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3757 | if (have_pending_runtime_throw_failure_) { |
| 3758 | have_any_pending_runtime_throw_failure_ = true; |
| 3759 | // Reset the pending_runtime_throw flag now. |
| 3760 | have_pending_runtime_throw_failure_ = false; |
| 3761 | } |
| 3762 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3763 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3764 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3765 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3766 | void MethodVerifier::UninstantiableError(const char* descriptor) { |
| 3767 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 3768 | << "non-instantiable klass " << descriptor; |
| 3769 | } |
| 3770 | |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3771 | inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3772 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 3773 | } |
| 3774 | |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3775 | template <MethodVerifier::CheckAccess C> |
| 3776 | const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3777 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3778 | ObjPtr<mirror::Class> klass = can_load_classes_ |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3779 | ? linker->ResolveType(class_idx, dex_cache_, class_loader_) |
| 3780 | : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3781 | if (can_load_classes_ && klass == nullptr) { |
| 3782 | DCHECK(self_->IsExceptionPending()); |
| 3783 | self_->ClearException(); |
| 3784 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3785 | const RegType* result = nullptr; |
Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3786 | if (klass != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3787 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3788 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3789 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3790 | UninstantiableError(descriptor); |
| 3791 | precise = false; |
| 3792 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3793 | result = reg_types_.FindClass(klass.Ptr(), precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3794 | if (result == nullptr) { |
| 3795 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3796 | result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3797 | } |
| 3798 | } else { |
| 3799 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3800 | result = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3801 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3802 | DCHECK(result != nullptr); |
| 3803 | if (result->IsConflict()) { |
| 3804 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3805 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3806 | << "' in " << GetDeclaringClass(); |
| 3807 | return *result; |
| 3808 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3809 | |
| 3810 | // Record result of class resolution attempt. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3811 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3812 | |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3813 | // If requested, check if access is allowed. Unresolved types are included in this check, as the |
| 3814 | // interpreter only tests whether access is allowed when a class is not pre-verified and runs in |
| 3815 | // the access-checks interpreter. If result is primitive, skip the access check. |
| 3816 | // |
| 3817 | // Note: we do this for unresolved classes to trigger re-verification at runtime. |
| 3818 | if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3819 | const RegType& referrer = GetDeclaringClass(); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3820 | if (!referrer.CanAccess(*result)) { |
| 3821 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3822 | << referrer << "' -> '" << *result << "'"; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3823 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3824 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3825 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3826 | } |
| 3827 | |
Andreas Gampe | dd30907 | 2017-08-31 07:47:37 -0700 | [diff] [blame] | 3828 | // Instantiate ResolveClass variants. This is required as the -inl file has a function with a call |
| 3829 | // to ResolveClass, and compilers may decide to inline, requiring a symbol. |
Andreas Gampe | ae0b1f4 | 2017-08-30 17:36:26 -0700 | [diff] [blame] | 3830 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>( |
| 3831 | dex::TypeIndex class_idx); |
| 3832 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>( |
| 3833 | dex::TypeIndex class_idx); |
| 3834 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3835 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3836 | const RegType* common_super = nullptr; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3837 | if (code_item_accessor_.TriesSize() != 0) { |
| 3838 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3839 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3840 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3841 | CatchHandlerIterator iterator(handlers_ptr); |
| 3842 | for (; iterator.HasNext(); iterator.Next()) { |
| 3843 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3844 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3845 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3846 | } else { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3847 | const RegType& exception = |
| 3848 | ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3849 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3850 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3851 | if (exception.IsUnresolvedTypes()) { |
| 3852 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3853 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3854 | return exception; |
| 3855 | } else { |
| 3856 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3857 | return reg_types_.Conflict(); |
| 3858 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3859 | } else if (common_super == nullptr) { |
| 3860 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3861 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3862 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3863 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3864 | common_super = &common_super->Merge(exception, ®_types_, this); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3865 | if (FailOrAbort(this, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3866 | reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3867 | *common_super, this), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3868 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3869 | work_insn_idx_)) { |
| 3870 | break; |
| 3871 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3876 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3877 | } |
| 3878 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3879 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3880 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3881 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3882 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3883 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3884 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3885 | } |
| 3886 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3887 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3888 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3889 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3890 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3891 | if (klass_type.IsConflict()) { |
| 3892 | std::string append(" in attempt to access method "); |
| 3893 | append += dex_file_->GetMethodName(method_id); |
| 3894 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3895 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3896 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3897 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3898 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3899 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3900 | ObjPtr<mirror::Class> klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3901 | const RegType& referrer = GetDeclaringClass(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame^] | 3902 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3903 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3904 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3905 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3906 | if (res_method == nullptr) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame^] | 3907 | res_method = class_linker->FindResolvedMethod( |
| 3908 | klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3909 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3910 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3911 | // Record result of method resolution attempt. The klass resolution has recorded whether |
| 3912 | // the class is an interface or not and therefore the type of the lookup performed above. |
| 3913 | // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type. |
| 3914 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method); |
| 3915 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3916 | bool must_fail = false; |
| 3917 | // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method |
| 3918 | // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's |
| 3919 | // hard to see the differences. |
| 3920 | // If we don't have res_method here we must fail. Just use this bool to make sure of that with a |
| 3921 | // DCHECK. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3922 | if (res_method == nullptr) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3923 | must_fail = true; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3924 | // Try to find the method also with the other type for better error reporting below |
| 3925 | // but do not store such bogus lookup result in the DexCache or VerifierDeps. |
| 3926 | if (klass->IsInterface()) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3927 | // NB This is normally not really allowed but we want to get any static or private object |
| 3928 | // methods for error message purposes. This will never be returned. |
| 3929 | // TODO We might want to change the verifier to not require this. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3930 | res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size); |
| 3931 | } else { |
| 3932 | // If there was an interface method with the same signature, |
| 3933 | // we would have found it also in the "copied" methods. |
| 3934 | DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr); |
| 3935 | } |
| 3936 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3937 | |
| 3938 | if (res_method == nullptr) { |
| 3939 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3940 | << klass->PrettyDescriptor() << "." |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3941 | << dex_file_->GetMethodName(method_id) << " " |
| 3942 | << dex_file_->GetMethodSignature(method_id); |
| 3943 | return nullptr; |
| 3944 | } |
| 3945 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3946 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3947 | // enforce them here. |
| 3948 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3949 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3950 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3951 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3952 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3953 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3954 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3955 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3956 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3957 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3958 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3959 | |
| 3960 | // Check that interface methods are static or match interface classes. |
| 3961 | // We only allow statics if we don't have default methods enabled. |
| 3962 | // |
| 3963 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3964 | // while this check implies an IncompatibleClassChangeError. |
| 3965 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3966 | // 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] | 3967 | // default methods are supported for the dex file), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3968 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3969 | method_type != METHOD_STATIC && |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3970 | (!dex_file_->SupportsDefaultMethods() || |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3971 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3972 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3973 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3974 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3975 | << " is in an interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3976 | return nullptr; |
| 3977 | } |
| 3978 | } else { |
| 3979 | if (method_type == METHOD_INTERFACE) { |
| 3980 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3981 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3982 | << " is in a non-interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3983 | return nullptr; |
| 3984 | } |
| 3985 | } |
| 3986 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3987 | // Check specifically for non-public object methods being provided for interface dispatch. This |
| 3988 | // can occur if we failed to find a method with FindInterfaceMethod but later find one with |
| 3989 | // FindClassMethod for error message use. |
| 3990 | if (method_type == METHOD_INTERFACE && |
| 3991 | res_method->GetDeclaringClass()->IsObjectClass() && |
| 3992 | !res_method->IsPublic()) { |
| 3993 | Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "." |
| 3994 | << dex_file_->GetMethodName(method_id) << " " |
| 3995 | << dex_file_->GetMethodSignature(method_id) << " resolved to " |
| 3996 | << "non-public object method " << res_method->PrettyMethod() << " " |
| 3997 | << "but non-public Object methods are excluded from interface " |
| 3998 | << "method resolution."; |
| 3999 | return nullptr; |
| 4000 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4001 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4002 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4003 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 4004 | << res_method->PrettyMethod() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4005 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 4006 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4007 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 4008 | // 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] | 4009 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4010 | 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] | 4011 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4012 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 4013 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4014 | // 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] | 4015 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 4016 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 4017 | // arguments. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 4018 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4019 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4020 | ((method_type == METHOD_SUPER || |
| 4021 | method_type == METHOD_VIRTUAL || |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4022 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 4023 | ((method_type == METHOD_POLYMORPHIC) && |
| 4024 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4025 | 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] | 4026 | "type of " << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4027 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4028 | } |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 4029 | // Make sure we weren't expecting to fail. |
| 4030 | DCHECK(!must_fail) << "invoke type (" << method_type << ")" |
| 4031 | << klass->PrettyDescriptor() << "." |
| 4032 | << dex_file_->GetMethodName(method_id) << " " |
| 4033 | << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to " |
| 4034 | << res_method->PrettyMethod() << " without error. Initially this method was " |
| 4035 | << "not found so we were expecting to fail for some reason."; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4036 | return res_method; |
| 4037 | } |
| 4038 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4039 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4040 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 4041 | T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4042 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4043 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4044 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4045 | const size_t expected_args = inst->VRegA(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4046 | /* caught by static verifier */ |
| 4047 | DCHECK(is_range || expected_args <= 5); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4048 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4049 | if (expected_args > code_item_accessor_.OutsSize()) { |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4050 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4051 | << ") exceeds outsSize (" |
| 4052 | << code_item_accessor_.OutsSize() << ")"; |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4053 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4054 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4055 | |
| 4056 | /* |
| 4057 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4058 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4059 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4060 | */ |
| 4061 | if (method_type != METHOD_STATIC) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4062 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4063 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 4064 | CHECK(have_pending_hard_failure_); |
| 4065 | return nullptr; |
| 4066 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4067 | bool is_init = false; |
| 4068 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4069 | if (res_method) { |
| 4070 | if (!res_method->IsConstructor()) { |
| 4071 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4072 | return nullptr; |
| 4073 | } |
| 4074 | } else { |
| 4075 | // Check whether the name of the called method is "<init>" |
| 4076 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Jeff Hao | 0d08727 | 2014-08-04 14:47:17 -0700 | [diff] [blame] | 4077 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4078 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4079 | return nullptr; |
| 4080 | } |
| 4081 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4082 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4083 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4084 | const RegType& adjusted_type = is_init |
| 4085 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 4086 | : actual_arg_type; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4087 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4088 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 4089 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 4090 | // class. It would be wrong to use this for the type check (interface type checks are |
| 4091 | // postponed to runtime). |
| 4092 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4093 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4094 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4095 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 4096 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4097 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4098 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4099 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4100 | res_method_class = ®_types_.FromDescriptor( |
| 4101 | GetClassLoader(), |
| 4102 | dex_file_->StringByTypeIdx(class_idx), |
| 4103 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4104 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4105 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4106 | Fail(adjusted_type.IsUnresolvedTypes() |
| 4107 | ? VERIFY_ERROR_NO_CLASS |
| 4108 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 4109 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 4110 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4111 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4112 | // the compiler. |
| 4113 | if (have_pending_hard_failure_) { |
| 4114 | return nullptr; |
| 4115 | } |
| 4116 | } |
| 4117 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4120 | uint32_t arg[5]; |
| 4121 | if (!is_range) { |
| 4122 | inst->GetVarArgs(arg); |
| 4123 | } |
| 4124 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4125 | for ( ; it->HasNext(); it->Next()) { |
| 4126 | if (sig_registers >= expected_args) { |
| 4127 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4128 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4129 | return nullptr; |
| 4130 | } |
| 4131 | |
| 4132 | const char* param_descriptor = it->GetDescriptor(); |
| 4133 | |
| 4134 | if (param_descriptor == nullptr) { |
| 4135 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 4136 | "component"; |
| 4137 | return nullptr; |
| 4138 | } |
| 4139 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4140 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4141 | 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] | 4142 | arg[sig_registers]; |
| 4143 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4144 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4145 | if (!src_type.IsIntegralTypes()) { |
| 4146 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4147 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4148 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4149 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4150 | } else { |
| 4151 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4152 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4153 | // the compiler. |
| 4154 | if (have_pending_hard_failure_) { |
| 4155 | return nullptr; |
| 4156 | } |
| 4157 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4158 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4159 | // instructions not specifying register pairs by the first component, but require them |
| 4160 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4161 | // none, this will fail below. |
| 4162 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4163 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4164 | if (second_reg != get_reg + 1) { |
| 4165 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4166 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4167 | << second_reg << "."; |
| 4168 | return nullptr; |
| 4169 | } |
| 4170 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4174 | } |
| 4175 | if (expected_args != sig_registers) { |
| 4176 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4177 | " argument registers, method signature has " << sig_registers; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4178 | return nullptr; |
| 4179 | } |
| 4180 | return res_method; |
| 4181 | } |
| 4182 | |
| 4183 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4184 | MethodType method_type, |
| 4185 | bool is_range) { |
| 4186 | // As the method may not have been resolved, make this static check against what we expect. |
| 4187 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4188 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4189 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4190 | DexFileParameterIterator it(*dex_file_, |
| 4191 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4192 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4195 | bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) { |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4196 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4197 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4198 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4199 | return false; |
| 4200 | } |
| 4201 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4202 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 4203 | // Check essential arguments are provided. The dex file verifier has verified indicies of the |
| 4204 | // main values (method handle, name, method_type). |
| 4205 | if (it.Size() < 3) { |
| 4206 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4207 | << " has too few arguments: " |
| 4208 | << it.Size() << "< 3"; |
| 4209 | return false; |
| 4210 | } |
| 4211 | |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4212 | // Get and check the first argument: the method handle (index range |
| 4213 | // checked by the dex file verifier). |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4214 | uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4215 | it.Next(); |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4216 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4217 | const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx); |
| 4218 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4219 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 4220 | << " argument 0 method handle type is not InvokeStatic: " |
| 4221 | << mh.method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4222 | return false; |
| 4223 | } |
| 4224 | |
| 4225 | // Skip the second argument, the name to resolve, as checked by the |
| 4226 | // dex file verifier. |
| 4227 | it.Next(); |
| 4228 | |
| 4229 | // Skip the third argument, the method type expected, as checked by |
| 4230 | // the dex file verifier. |
| 4231 | it.Next(); |
| 4232 | |
| 4233 | // Check the bootstrap method handle and remaining arguments. |
| 4234 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_); |
| 4235 | uint32_t length; |
| 4236 | const char* shorty = dex_file_->GetMethodShorty(method_id, &length); |
| 4237 | |
| 4238 | if (it.Size() < length - 1) { |
| 4239 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4240 | << " too few arguments for bootstrap method: " |
| 4241 | << it.Size() << " < " << (length - 1); |
| 4242 | return false; |
| 4243 | } |
| 4244 | |
| 4245 | // Check the return type and first 3 arguments are references |
| 4246 | // (CallSite, Lookup, String, MethodType). If they are not of the |
| 4247 | // expected types (or subtypes), it will trigger a |
| 4248 | // WrongMethodTypeException during execution. |
| 4249 | if (shorty[0] != 'L') { |
| 4250 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4251 | << " bootstrap return type is not a reference"; |
| 4252 | return false; |
| 4253 | } |
| 4254 | |
| 4255 | for (uint32_t i = 1; i < 4; ++i) { |
| 4256 | if (shorty[i] != 'L') { |
| 4257 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4258 | << " bootstrap method argument " << (i - 1) |
| 4259 | << " is not a reference"; |
| 4260 | return false; |
| 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | // Check the optional arguments. |
| 4265 | for (uint32_t i = 4; i < length; ++i, it.Next()) { |
| 4266 | bool match = false; |
| 4267 | switch (it.GetValueType()) { |
| 4268 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 4269 | case EncodedArrayValueIterator::ValueType::kByte: |
| 4270 | case EncodedArrayValueIterator::ValueType::kShort: |
| 4271 | case EncodedArrayValueIterator::ValueType::kChar: |
| 4272 | case EncodedArrayValueIterator::ValueType::kInt: |
| 4273 | // These all fit within one register and encoders do not seem |
| 4274 | // too exacting on the encoding type they use (ie using |
| 4275 | // integer for all of these). |
| 4276 | match = (strchr("ZBCSI", shorty[i]) != nullptr); |
| 4277 | break; |
| 4278 | case EncodedArrayValueIterator::ValueType::kLong: |
| 4279 | match = ('J' == shorty[i]); |
| 4280 | break; |
| 4281 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 4282 | match = ('F' == shorty[i]); |
| 4283 | break; |
| 4284 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 4285 | match = ('D' == shorty[i]); |
| 4286 | break; |
| 4287 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| 4288 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| 4289 | case EncodedArrayValueIterator::ValueType::kString: |
| 4290 | case EncodedArrayValueIterator::ValueType::kType: |
| 4291 | case EncodedArrayValueIterator::ValueType::kNull: |
| 4292 | match = ('L' == shorty[i]); |
| 4293 | break; |
| 4294 | case EncodedArrayValueIterator::ValueType::kField: |
| 4295 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 4296 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 4297 | case EncodedArrayValueIterator::ValueType::kArray: |
| 4298 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 4299 | // Unreachable based on current EncodedArrayValueIterator::Next(). |
| 4300 | UNREACHABLE(); |
| 4301 | } |
| 4302 | |
| 4303 | if (!match) { |
| 4304 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4305 | << " bootstrap method argument " << (i - 1) |
| 4306 | << " expected " << shorty[i] |
| 4307 | << " got value type: " << it.GetValueType(); |
| 4308 | return false; |
| 4309 | } |
| 4310 | } |
| 4311 | return true; |
| 4312 | } |
| 4313 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4314 | class MethodParamListDescriptorIterator { |
| 4315 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4316 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4317 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4318 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4319 | } |
| 4320 | |
| 4321 | bool HasNext() { |
| 4322 | return pos_ < params_size_; |
| 4323 | } |
| 4324 | |
| 4325 | void Next() { |
| 4326 | ++pos_; |
| 4327 | } |
| 4328 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4329 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4330 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4331 | } |
| 4332 | |
| 4333 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4334 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4335 | size_t pos_; |
| 4336 | const DexFile::TypeList* params_; |
| 4337 | const size_t params_size_; |
| 4338 | }; |
| 4339 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4340 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4341 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4342 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4343 | // we're making. |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4344 | const uint32_t method_idx = inst->VRegB(); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4345 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4346 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4347 | // Check what we can statically. |
| 4348 | if (!have_pending_hard_failure_) { |
| 4349 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4350 | } |
| 4351 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4352 | } |
| 4353 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4354 | // 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] | 4355 | // 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] | 4356 | if (method_type == METHOD_SUPER) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4357 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4358 | const RegType& reference_type = reg_types_.FromDescriptor( |
| 4359 | GetClassLoader(), |
| 4360 | dex_file_->StringByTypeIdx(class_idx), |
| 4361 | false); |
| 4362 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4363 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4364 | return nullptr; |
| 4365 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4366 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4367 | // TODO Can we verify anything else. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 4368 | if (class_idx == class_def_.class_idx_) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4369 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface"; |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4370 | return nullptr; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4371 | } |
| 4372 | // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS |
| 4373 | // does. |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4374 | if (!GetDeclaringClass().HasClass()) { |
| 4375 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4376 | << "interface invoke-super"; |
| 4377 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4378 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4379 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4380 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4381 | << " in method " |
| 4382 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4383 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4384 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4385 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4386 | } |
| 4387 | } else { |
| 4388 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4389 | if (super.IsUnresolvedTypes()) { |
| 4390 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4391 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4392 | << " to super " << res_method->PrettyMethod(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4393 | return nullptr; |
| 4394 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4395 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4396 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4397 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4398 | << dex_file_->PrettyMethod(dex_method_idx_) |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4399 | << " to super " << super |
| 4400 | << "." << res_method->GetName() |
| 4401 | << res_method->GetSignature(); |
| 4402 | return nullptr; |
| 4403 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4404 | } |
| 4405 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4406 | |
Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4407 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4408 | // Process the signature of the calling site that is invoking the method handle. |
| 4409 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH())); |
| 4410 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4411 | } else { |
| 4412 | // Process the target method's signature. |
| 4413 | MethodParamListDescriptorIterator it(res_method); |
| 4414 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
| 4419 | mirror::Class* klass = method->GetDeclaringClass(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4420 | const char* method_name = method->GetName(); |
| 4421 | |
| 4422 | const char* expected_return_descriptor; |
| 4423 | if (klass == mirror::MethodHandle::StaticClass()) { |
| 4424 | expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name); |
| 4425 | } else if (klass == mirror::VarHandle::StaticClass()) { |
| 4426 | expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name); |
Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 4427 | // TODO: add compiler support for VarHandle accessor methods (b/71781600) |
| 4428 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4429 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4430 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4431 | << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4432 | return false; |
| 4433 | } |
| 4434 | |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4435 | if (expected_return_descriptor == nullptr) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4436 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4437 | << "Signature polymorphic method name invalid: " << method_name; |
| 4438 | return false; |
| 4439 | } |
| 4440 | |
| 4441 | const DexFile::TypeList* types = method->GetParameterTypeList(); |
| 4442 | if (types->Size() != 1) { |
| 4443 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4444 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4445 | return false; |
| 4446 | } |
| 4447 | |
| 4448 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4449 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4450 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4451 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4452 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4453 | return false; |
| 4454 | } |
| 4455 | |
| 4456 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4457 | if (strcmp(return_descriptor, expected_return_descriptor) != 0) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4458 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4459 | << "Signature polymorphic method has unexpected return type: " << return_descriptor |
| 4460 | << " != " << expected_return_descriptor; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4461 | return false; |
| 4462 | } |
| 4463 | |
| 4464 | return true; |
| 4465 | } |
| 4466 | |
| 4467 | bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
| 4468 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4469 | if (this_type.IsZeroOrNull()) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4470 | /* null pointer always passes (and always fails at run time) */ |
| 4471 | return true; |
| 4472 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4473 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4474 | << "invoke-polymorphic receiver is not a reference: " |
| 4475 | << this_type; |
| 4476 | return false; |
| 4477 | } else if (this_type.IsUninitializedReference()) { |
| 4478 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4479 | << "invoke-polymorphic receiver is uninitialized: " |
| 4480 | << this_type; |
| 4481 | return false; |
| 4482 | } else if (!this_type.HasClass()) { |
| 4483 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4484 | << "invoke-polymorphic receiver has no class: " |
| 4485 | << this_type; |
| 4486 | return false; |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4487 | } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) && |
| 4488 | !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4489 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame] | 4490 | << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: " |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4491 | << this_type; |
| 4492 | return false; |
| 4493 | } |
| 4494 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4495 | } |
| 4496 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4497 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 4498 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4499 | if (is_range) { |
| 4500 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 4501 | } else { |
| 4502 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 4503 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4504 | const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4505 | if (!actual_arg_type.HasClass()) { |
| 4506 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 4507 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4508 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4509 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4510 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4511 | if (klass->IsInterface()) { |
| 4512 | // Derive Object.class from Class.class.getSuperclass(). |
| 4513 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4514 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4515 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4516 | return nullptr; |
| 4517 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4518 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4519 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4520 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4521 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4522 | if (!dispatch_class->HasVTable()) { |
| 4523 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 4524 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4525 | return nullptr; |
| 4526 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4527 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4528 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 4529 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4530 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 4531 | FailOrAbort(this, allow_failure, |
| 4532 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 4533 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4534 | return nullptr; |
| 4535 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4536 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4537 | if (self_->IsExceptionPending()) { |
| 4538 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 4539 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4540 | return nullptr; |
| 4541 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4542 | return res_method; |
| 4543 | } |
| 4544 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4545 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4546 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4547 | << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_; |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4548 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4549 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4550 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4551 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4552 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4553 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4554 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 4555 | work_insn_idx_)) { |
| 4556 | return nullptr; |
| 4557 | } |
| 4558 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 4559 | work_insn_idx_)) { |
| 4560 | return nullptr; |
| 4561 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4562 | |
| 4563 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4564 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4565 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4566 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4567 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4568 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4569 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4570 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4571 | /* caught by static verifier */ |
| 4572 | DCHECK(is_range || expected_args <= 5); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4573 | if (expected_args > code_item_accessor_.OutsSize()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4574 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4575 | << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4576 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | /* |
| 4580 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4581 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4582 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4583 | */ |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4584 | // Note: given an uninitialized type, this should always fail. Constructors aren't virtual. |
| 4585 | if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4586 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4587 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4588 | } |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4589 | if (!actual_arg_type.IsZeroOrNull()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4590 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4591 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4592 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4593 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4594 | if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4595 | Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never |
| 4596 | ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this. |
| 4597 | : actual_arg_type.IsUnresolvedTypes() |
| 4598 | ? VERIFY_ERROR_NO_CLASS |
| 4599 | : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4600 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4601 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4602 | } |
| 4603 | } |
| 4604 | /* |
| 4605 | * Process the target method's signature. This signature may or may not |
| 4606 | * have been verified, so we can't assume it's properly formed. |
| 4607 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4608 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4609 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4610 | uint32_t arg[5]; |
| 4611 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4612 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4613 | } |
| 4614 | size_t actual_args = 1; |
| 4615 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 4616 | if (actual_args >= expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4617 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" |
| 4618 | << res_method->PrettyMethod() |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4619 | << "'. Expected " << expected_args |
| 4620 | << " arguments, processing argument " << actual_args |
| 4621 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4622 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4623 | } |
| 4624 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4625 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4626 | if (descriptor == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4627 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4628 | << res_method->PrettyMethod() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4629 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4630 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4631 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4632 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4633 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4634 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4635 | return res_method; |
| 4636 | } |
| 4637 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 4638 | } |
| 4639 | if (actual_args != expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4640 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4641 | << res_method->PrettyMethod() << " expected " |
| 4642 | << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4643 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4644 | } else { |
| 4645 | return res_method; |
| 4646 | } |
| 4647 | } |
| 4648 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 4649 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4650 | dex::TypeIndex type_idx; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4651 | if (!is_filled) { |
| 4652 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4653 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4654 | } else if (!is_range) { |
| 4655 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4656 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4657 | } else { |
| 4658 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4659 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4660 | } |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4661 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4662 | if (res_type.IsConflict()) { // bad class |
| 4663 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4664 | } else { |
| 4665 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4666 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4667 | 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] | 4668 | } else if (!is_filled) { |
| 4669 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4670 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4671 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4672 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4673 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4674 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4675 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4676 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4677 | // 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] | 4678 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4679 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4680 | uint32_t arg[5]; |
| 4681 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4682 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4683 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4684 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4685 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4686 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4687 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4688 | return; |
| 4689 | } |
| 4690 | } |
| 4691 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4692 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4693 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | } |
| 4697 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4698 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4699 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4700 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4701 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4702 | 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] | 4703 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4704 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4705 | if (array_type.IsZeroOrNull()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4706 | // 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] | 4707 | // instruction type. |
| 4708 | if (!is_primitive) { |
| 4709 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null()); |
| 4710 | } else if (insn_type.IsInteger()) { |
| 4711 | // Pick a non-zero constant (to distinguish with null) that can fit in any primitive. |
| 4712 | // We cannot use 'insn_type' as it could be a float array or an int array. |
| 4713 | work_line_->SetRegisterType<LockOp::kClear>( |
| 4714 | this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_)); |
| 4715 | } else if (insn_type.IsCategory1Types()) { |
| 4716 | // Category 1 |
| 4717 | // The 'insn_type' is exactly the type we need. |
| 4718 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4719 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4720 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4721 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4722 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4723 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4724 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4725 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4726 | 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] | 4727 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4728 | // Unresolved array types must be reference array types. |
| 4729 | if (is_primitive) { |
| 4730 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4731 | << " source for category 1 aget"; |
| 4732 | } else { |
| 4733 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4734 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4735 | // Approximate with java.lang.Object[]. |
| 4736 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4737 | inst->VRegA_23x(), |
| 4738 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4739 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4740 | } else { |
| 4741 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4742 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4743 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4744 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4745 | << " source for aget-object"; |
| 4746 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4747 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4748 | << " source for category 1 aget"; |
| 4749 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4750 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4751 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4752 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4753 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4754 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4755 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4756 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4757 | // doubles. |
| 4758 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4759 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4760 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4761 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4762 | component_type.HighHalf(®_types_)); |
| 4763 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | } |
| 4767 | } |
| 4768 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4769 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4770 | const uint32_t vregA) { |
| 4771 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4772 | bool instruction_compatible; |
| 4773 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4774 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4775 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4776 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4777 | value_compatible = value_type.IsIntegralTypes(); |
| 4778 | } else if (target_type.IsFloat()) { |
| 4779 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4780 | value_compatible = value_type.IsFloatTypes(); |
| 4781 | } else if (target_type.IsLong()) { |
| 4782 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4783 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4784 | // as target_type depends on the resolved type of the field. |
| 4785 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4786 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4787 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4788 | } else { |
| 4789 | value_compatible = false; |
| 4790 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4791 | } else if (target_type.IsDouble()) { |
| 4792 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4793 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4794 | // as target_type depends on the resolved type of the field. |
| 4795 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4796 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4797 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4798 | } else { |
| 4799 | value_compatible = false; |
| 4800 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4801 | } else { |
| 4802 | instruction_compatible = false; // reference with primitive store |
| 4803 | value_compatible = false; // unused |
| 4804 | } |
| 4805 | if (!instruction_compatible) { |
| 4806 | // This is a global failure rather than a class change failure as the instructions and |
| 4807 | // the descriptors for the type should have been consistent within the same file at |
| 4808 | // compile time. |
| 4809 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4810 | << "' but expected type '" << target_type << "'"; |
| 4811 | return; |
| 4812 | } |
| 4813 | if (!value_compatible) { |
| 4814 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4815 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4816 | return; |
| 4817 | } |
| 4818 | } |
| 4819 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4820 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4821 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4822 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4823 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4824 | 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] | 4825 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4826 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4827 | if (array_type.IsZeroOrNull()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4828 | // Null array type; this code path will fail at runtime. |
| 4829 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4830 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4831 | // and fits multiple register types. |
| 4832 | const RegType* modified_reg_type = &insn_type; |
| 4833 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4834 | (modified_reg_type == ®_types_.LongLo())) { |
| 4835 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4836 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4837 | if (modified_reg_type == ®_types_.Integer()) { |
| 4838 | if (&value_type == ®_types_.Float()) { |
| 4839 | modified_reg_type = &value_type; |
| 4840 | } |
| 4841 | } else { |
| 4842 | if (&value_type == ®_types_.DoubleLo()) { |
| 4843 | modified_reg_type = &value_type; |
| 4844 | } |
| 4845 | } |
| 4846 | } |
| 4847 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4848 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4849 | 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] | 4850 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4851 | // Unresolved array types must be reference array types. |
| 4852 | if (is_primitive) { |
| 4853 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4854 | << "' but unresolved type '" << array_type << "'"; |
| 4855 | } else { |
| 4856 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4857 | << " because of missing class"; |
| 4858 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4859 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4860 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4861 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4862 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4863 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4864 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4865 | if (!component_type.IsReferenceTypes()) { |
| 4866 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4867 | << " source for aput-object"; |
| 4868 | } else { |
| 4869 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4870 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4871 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4872 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4873 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | } |
| 4878 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4879 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4880 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4881 | // Check access to class |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4882 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4883 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4884 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4885 | field_idx, dex_file_->GetFieldName(field_id), |
| 4886 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4887 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4888 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4889 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4890 | // Accessibility checks depend on resolved fields. |
| 4891 | DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty()); |
| 4892 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4893 | 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] | 4894 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4895 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4896 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4897 | |
| 4898 | // Record result of the field resolution attempt. |
| 4899 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4900 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4901 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4902 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4903 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4904 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4905 | DCHECK(self_->IsExceptionPending()); |
| 4906 | self_->ClearException(); |
| 4907 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4908 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4909 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4910 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4911 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4912 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4913 | } else if (!field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4914 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4915 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4916 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4917 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4920 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4921 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4922 | // Check access to class. |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4923 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4924 | if (klass_type.IsConflict()) { |
| 4925 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4926 | field_idx, dex_file_->GetFieldName(field_id), |
| 4927 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4928 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4929 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4930 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4931 | // Accessibility checks depend on resolved fields. |
| 4932 | DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty()); |
| 4933 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4934 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4935 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4936 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4937 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4938 | |
| 4939 | // Record result of the field resolution attempt. |
| 4940 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4941 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4942 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4943 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4944 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4945 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4946 | DCHECK(self_->IsExceptionPending()); |
| 4947 | self_->ClearException(); |
| 4948 | return nullptr; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4949 | } else if (obj_type.IsZeroOrNull()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4950 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4951 | // Fall through into a few last soft failure checks below. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4952 | } else if (!obj_type.IsReferenceTypes()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4953 | // Trying to read a field from something that isn't a reference. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4954 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4955 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4956 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4957 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4958 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4959 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4960 | const RegType& field_klass = |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4961 | FromClass(klass->GetDescriptor(&temp), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 4962 | klass.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4963 | klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4964 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4965 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4966 | // the field is declared in this class. |
| 4967 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4968 | // appear in constructors. |
| 4969 | if (!obj_type.IsUninitializedThisReference() || |
| 4970 | !IsConstructor() || |
| 4971 | !field_klass.Equals(GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4972 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4973 | << " of a not fully initialized object within the context" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4974 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4975 | return nullptr; |
| 4976 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4977 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4978 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4979 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4980 | // 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] | 4981 | VerifyError type; |
| 4982 | bool is_aot = Runtime::Current()->IsAotCompiler(); |
| 4983 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4984 | // Compiler & unresolved types involved, retry at runtime. |
| 4985 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4986 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4987 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4988 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4989 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4990 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4991 | Fail(type) << "cannot access instance field " << field->PrettyField() |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4992 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4993 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4994 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4995 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4996 | |
| 4997 | // Few last soft failure checks. |
| 4998 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4999 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5000 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5001 | << " from " << GetDeclaringClass(); |
| 5002 | return nullptr; |
| 5003 | } else if (field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5004 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5005 | << " to not be static"; |
| 5006 | return nullptr; |
| 5007 | } |
| 5008 | |
| 5009 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5010 | } |
| 5011 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5012 | template <MethodVerifier::FieldAccessType kAccType> |
| 5013 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 5014 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 5015 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5016 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5017 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 5018 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5019 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5020 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 5021 | |
| 5022 | // One is not allowed to access fields on uninitialized references, except to write to |
| 5023 | // fields in the constructor (before calling another constructor). |
| 5024 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 5025 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 5026 | // checks at the same time that we're verifying a constructor). |
| 5027 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 5028 | object_type.IsUninitializedThisReference(); |
| 5029 | const RegType& adjusted_type = should_adjust |
| 5030 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 5031 | : object_type; |
| 5032 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5033 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 5034 | return; |
| 5035 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5036 | if (should_adjust) { |
| 5037 | if (field == nullptr) { |
| 5038 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 5039 | << "to the superclass being initialized in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5040 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5041 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 5042 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5043 | << field->PrettyField() << " of a not fully initialized " |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5044 | << "object within the context of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5045 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5046 | return; |
| 5047 | } |
| 5048 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5049 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5050 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5051 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5052 | if (kAccType == FieldAccessType::kAccPut) { |
| 5053 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5054 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5055 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 5056 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5057 | } |
| 5058 | } |
| 5059 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 5060 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 5061 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5062 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 5063 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 5064 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5065 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 5066 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5067 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5068 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5069 | } |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 5070 | } else { |
| 5071 | // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to |
| 5072 | // redo verification at runtime as the field may be final, unless the field id shows it's in |
| 5073 | // the same class. |
| 5074 | // |
| 5075 | // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an |
| 5076 | // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class |
| 5077 | // to the access-checks interpreter. |
| 5078 | // |
| 5079 | // Note: see b/34966607. This and above may be changed in the future. |
| 5080 | if (kAccType == FieldAccessType::kAccPut) { |
| 5081 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 5082 | const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 5083 | const RegType* field_class_type = ®_types_.FromDescriptor(GetClassLoader(), |
| 5084 | field_class_descriptor, |
| 5085 | false); |
| 5086 | if (!field_class_type->Equals(GetDeclaringClass())) { |
| 5087 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of " |
| 5088 | << field_class_descriptor |
| 5089 | << "." |
| 5090 | << dex_file_->GetFieldName(field_id) |
| 5091 | << " from other class " |
| 5092 | << GetDeclaringClass(); |
| 5093 | } |
| 5094 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5095 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5096 | if (field_type == nullptr) { |
| 5097 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 5098 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5099 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5100 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 5101 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 5102 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5103 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5104 | "Unexpected third access type"); |
| 5105 | if (kAccType == FieldAccessType::kAccPut) { |
| 5106 | // sput or iput. |
| 5107 | if (is_primitive) { |
| 5108 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5109 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5110 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5111 | // If the field type is not a reference, this is a global failure rather than |
| 5112 | // a class change failure as the instructions and the descriptors for the type |
| 5113 | // should have been consistent within the same file at compile time. |
| 5114 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5115 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5116 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5117 | << " to be compatible with type '" << insn_type |
| 5118 | << "' but found type '" << *field_type |
| 5119 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5120 | return; |
| 5121 | } |
| 5122 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5123 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5124 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5125 | // sget or iget. |
| 5126 | if (is_primitive) { |
| 5127 | if (field_type->Equals(insn_type) || |
| 5128 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 5129 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 5130 | // expected that read is of the correct primitive type or that int reads are reading |
| 5131 | // floats or long reads are reading doubles |
| 5132 | } else { |
| 5133 | // This is a global failure rather than a class change failure as the instructions and |
| 5134 | // the descriptors for the type should have been consistent within the same file at |
| 5135 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5136 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5137 | << " to be of type '" << insn_type |
| 5138 | << "' but found type '" << *field_type << "' in get"; |
| 5139 | return; |
| 5140 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 5141 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5142 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5143 | // If the field type is not a reference, this is a global failure rather than |
| 5144 | // a class change failure as the instructions and the descriptors for the type |
| 5145 | // should have been consistent within the same file at compile time. |
| 5146 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5147 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5148 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5149 | << " to be compatible with type '" << insn_type |
| 5150 | << "' but found type '" << *field_type |
| 5151 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5152 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5153 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5154 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5155 | return; |
| 5156 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5157 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5158 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5159 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5160 | } else { |
| 5161 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 5162 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5163 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5164 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5165 | } |
| 5166 | } |
| 5167 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 5168 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5169 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5170 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 5171 | if (!object_type.HasClass()) { |
| 5172 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 5173 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5174 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5175 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5176 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5177 | if (f == nullptr) { |
| 5178 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5179 | << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'"; |
Andreas Gampe | 8ddb7f5 | 2017-12-04 10:14:08 -0800 | [diff] [blame] | 5180 | } else { |
| 5181 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5182 | } |
| 5183 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5184 | } |
| 5185 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5186 | template <MethodVerifier::FieldAccessType kAccType> |
| 5187 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 5188 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 5189 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5190 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5191 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5192 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5193 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 5194 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5195 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5196 | |
| 5197 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 5198 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5199 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5200 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5201 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5202 | return; |
| 5203 | } |
| 5204 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5205 | |
| 5206 | // Get the field type. |
| 5207 | const RegType* field_type; |
| 5208 | { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 5209 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 5210 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5211 | |
| 5212 | if (field_type_class != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5213 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 5214 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5215 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5216 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5217 | Thread* self = Thread::Current(); |
| 5218 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 5219 | self->ClearException(); |
| 5220 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5221 | field->GetTypeDescriptor(), |
| 5222 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5223 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5224 | if (field_type == nullptr) { |
| 5225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5226 | return; |
| 5227 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | const uint32_t vregA = inst->VRegA_22c(); |
| 5231 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5232 | "Unexpected third access type"); |
| 5233 | if (kAccType == FieldAccessType::kAccPut) { |
| 5234 | if (is_primitive) { |
| 5235 | // Primitive field assignability rules are weaker than regular assignability rules |
| 5236 | bool instruction_compatible; |
| 5237 | bool value_compatible; |
| 5238 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 5239 | if (field_type->IsIntegralTypes()) { |
| 5240 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 5241 | value_compatible = value_type.IsIntegralTypes(); |
| 5242 | } else if (field_type->IsFloat()) { |
| 5243 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 5244 | value_compatible = value_type.IsFloatTypes(); |
| 5245 | } else if (field_type->IsLong()) { |
| 5246 | instruction_compatible = insn_type.IsLong(); |
| 5247 | value_compatible = value_type.IsLongTypes(); |
| 5248 | } else if (field_type->IsDouble()) { |
| 5249 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 5250 | value_compatible = value_type.IsDoubleTypes(); |
| 5251 | } else { |
| 5252 | instruction_compatible = false; // reference field with primitive store |
| 5253 | value_compatible = false; // unused |
| 5254 | } |
| 5255 | if (!instruction_compatible) { |
| 5256 | // This is a global failure rather than a class change failure as the instructions and |
| 5257 | // the descriptors for the type should have been consistent within the same file at |
| 5258 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5259 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5260 | << " to be of type '" << insn_type |
| 5261 | << "' but found type '" << *field_type |
| 5262 | << "' in put"; |
| 5263 | return; |
| 5264 | } |
| 5265 | if (!value_compatible) { |
| 5266 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 5267 | << " of type " << value_type |
| 5268 | << " but expected " << *field_type |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5269 | << " for store to " << ArtField::PrettyField(field) << " in put"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5270 | return; |
| 5271 | } |
| 5272 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5273 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5274 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5275 | << " to be compatible with type '" << insn_type |
| 5276 | << "' but found type '" << *field_type |
| 5277 | << "' in put-object"; |
| 5278 | return; |
| 5279 | } |
| 5280 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 5281 | } |
| 5282 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5283 | if (is_primitive) { |
| 5284 | if (field_type->Equals(insn_type) || |
| 5285 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 5286 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 5287 | // expected that read is of the correct primitive type or that int reads are reading |
| 5288 | // floats or long reads are reading doubles |
| 5289 | } else { |
| 5290 | // This is a global failure rather than a class change failure as the instructions and |
| 5291 | // the descriptors for the type should have been consistent within the same file at |
| 5292 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5293 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5294 | << " to be of type '" << insn_type |
| 5295 | << "' but found type '" << *field_type << "' in Get"; |
| 5296 | return; |
| 5297 | } |
| 5298 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5299 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5300 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5301 | << " to be compatible with type '" << insn_type |
| 5302 | << "' but found type '" << *field_type |
| 5303 | << "' in get-object"; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5304 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5305 | return; |
| 5306 | } |
| 5307 | } |
| 5308 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5309 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5310 | } else { |
| 5311 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5312 | } |
| 5313 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5314 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5315 | } |
| 5316 | } |
| 5317 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5318 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5319 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 5320 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5321 | return false; |
| 5322 | } |
| 5323 | return true; |
| 5324 | } |
| 5325 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 5326 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 5327 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 5328 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 5329 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 5330 | return false; |
| 5331 | } |
| 5332 | return true; |
| 5333 | } |
| 5334 | |
| 5335 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 5336 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 5337 | } |
| 5338 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5339 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 5340 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5341 | bool changed = true; |
| 5342 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5343 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5344 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5345 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 5346 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 5347 | * 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] | 5348 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5349 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5350 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 5351 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5352 | merge_line->VerifyMonitorStackEmpty(this); |
| 5353 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5354 | // For returns we only care about the operand to the return, all other registers are dead. |
| 5355 | // 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] | 5356 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5357 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5358 | // Directly bail if a hard failure was found. |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 5359 | if (have_pending_hard_failure_) { |
| 5360 | return false; |
| 5361 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5362 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5363 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 5364 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 5365 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5366 | copy.reset(RegisterLine::Create(target_line->NumRegs(), this)); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5367 | copy->CopyFromLine(target_line); |
| 5368 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5369 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5370 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5371 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5372 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 5373 | if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 5374 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 5375 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5376 | << copy->Dump(this) << " MERGE\n" |
| 5377 | << merge_line->Dump(this) << " ==\n" |
| 5378 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5379 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5380 | if (update_merge_line && changed) { |
| 5381 | merge_line->CopyFromLine(target_line); |
| 5382 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5383 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5384 | if (changed) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5385 | GetInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5386 | } |
| 5387 | return true; |
| 5388 | } |
| 5389 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5390 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5391 | return &GetInstructionFlags(work_insn_idx_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5392 | } |
| 5393 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5394 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5395 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5396 | if (mirror_method_ != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5397 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 5398 | ? mirror_method_->ResolveReturnType() |
| 5399 | : mirror_method_->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5400 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5401 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5402 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5403 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5404 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5405 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5406 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5407 | } |
| 5408 | } |
| 5409 | if (return_type_ == nullptr) { |
| 5410 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 5411 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5412 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5413 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5414 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5415 | } |
| 5416 | } |
| 5417 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5418 | } |
| 5419 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5420 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5421 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5422 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 5423 | const char* descriptor |
| 5424 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5425 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5426 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5427 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5428 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5429 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5430 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5431 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5432 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5433 | } |
| 5434 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5435 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 5436 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 5437 | 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] | 5438 | std::vector<int32_t> result; |
| 5439 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5440 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5441 | if (type.IsConstant()) { |
| 5442 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5443 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5444 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5445 | } else if (type.IsConstantLo()) { |
| 5446 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5447 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5448 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5449 | } else if (type.IsConstantHi()) { |
| 5450 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5451 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5452 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5453 | } else if (type.IsIntegralTypes()) { |
| 5454 | result.push_back(kIntVReg); |
| 5455 | result.push_back(0); |
| 5456 | } else if (type.IsFloat()) { |
| 5457 | result.push_back(kFloatVReg); |
| 5458 | result.push_back(0); |
| 5459 | } else if (type.IsLong()) { |
| 5460 | result.push_back(kLongLoVReg); |
| 5461 | result.push_back(0); |
| 5462 | result.push_back(kLongHiVReg); |
| 5463 | result.push_back(0); |
| 5464 | ++i; |
| 5465 | } else if (type.IsDouble()) { |
| 5466 | result.push_back(kDoubleLoVReg); |
| 5467 | result.push_back(0); |
| 5468 | result.push_back(kDoubleHiVReg); |
| 5469 | result.push_back(0); |
| 5470 | ++i; |
| 5471 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 5472 | result.push_back(kUndefined); |
| 5473 | result.push_back(0); |
| 5474 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5475 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5476 | result.push_back(kReferenceVReg); |
| 5477 | result.push_back(0); |
| 5478 | } |
| 5479 | } |
| 5480 | return result; |
| 5481 | } |
| 5482 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5483 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5484 | if (precise) { |
| 5485 | // Precise constant type. |
| 5486 | return reg_types_.FromCat1Const(value, true); |
| 5487 | } else { |
| 5488 | // Imprecise constant type. |
| 5489 | if (value < -32768) { |
| 5490 | return reg_types_.IntConstant(); |
| 5491 | } else if (value < -128) { |
| 5492 | return reg_types_.ShortConstant(); |
| 5493 | } else if (value < 0) { |
| 5494 | return reg_types_.ByteConstant(); |
| 5495 | } else if (value == 0) { |
| 5496 | return reg_types_.Zero(); |
| 5497 | } else if (value == 1) { |
| 5498 | return reg_types_.One(); |
| 5499 | } else if (value < 128) { |
| 5500 | return reg_types_.PosByteConstant(); |
| 5501 | } else if (value < 32768) { |
| 5502 | return reg_types_.PosShortConstant(); |
| 5503 | } else if (value < 65536) { |
| 5504 | return reg_types_.CharConstant(); |
| 5505 | } else { |
| 5506 | return reg_types_.IntConstant(); |
| 5507 | } |
| 5508 | } |
| 5509 | } |
| 5510 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5511 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5512 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5513 | } |
| 5514 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5515 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5516 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5517 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5518 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5519 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5520 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5521 | } |
| 5522 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5523 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5524 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5525 | } |
| 5526 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5527 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 5528 | mirror::Class* klass, |
| 5529 | bool precise) { |
| 5530 | DCHECK(klass != nullptr); |
| 5531 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 5532 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 5533 | << "non-instantiable klass " << descriptor; |
| 5534 | precise = false; |
| 5535 | } |
| 5536 | return reg_types_.FromClass(descriptor, klass, precise); |
| 5537 | } |
| 5538 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5539 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5540 | } // namespace art |