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" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "dex_file-inl.h" |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 35 | #include "dex_file_exception_helpers.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 36 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | #include "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 | 808c7a5 | 2017-12-15 11:19:33 -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(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1688 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1689 | if (reg_line != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1690 | vios->Stream() << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1691 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1692 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1693 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1694 | const bool kDumpHexOfInstruction = false; |
| 1695 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1696 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1697 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1698 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1699 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1702 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1703 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1704 | case 'I': |
| 1705 | case 'C': |
| 1706 | case 'S': |
| 1707 | case 'B': |
| 1708 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1709 | case 'F': |
| 1710 | case 'D': |
| 1711 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1712 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1713 | default: |
| 1714 | return false; |
| 1715 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1718 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1719 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1720 | |
| 1721 | // Should have been verified earlier. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1722 | DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize()); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1723 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1724 | uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize(); |
| 1725 | size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1726 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1727 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1728 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1729 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1730 | if (expected_args == 0) { |
| 1731 | // Expect at least a receiver. |
| 1732 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1733 | return false; |
| 1734 | } |
| 1735 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1736 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1737 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1738 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1739 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1740 | if (IsConstructor()) { |
| 1741 | if (declaring_class.IsJavaLangObject()) { |
| 1742 | // "this" is implicitly initialized. |
| 1743 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1744 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1745 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1746 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1747 | this, |
| 1748 | arg_start + cur_arg, |
| 1749 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1750 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1751 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1752 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1753 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1754 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1757 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1758 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1759 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1760 | |
| 1761 | for (; iterator.HasNext(); iterator.Next()) { |
| 1762 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1763 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1764 | LOG(FATAL) << "Null descriptor"; |
| 1765 | } |
| 1766 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1767 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1768 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1769 | return false; |
| 1770 | } |
| 1771 | switch (descriptor[0]) { |
| 1772 | case 'L': |
| 1773 | case '[': |
| 1774 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1775 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1776 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1777 | // can return without doing anything or call virtual methods). |
| 1778 | { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 1779 | // Note: don't check access. No error would be thrown for declaring or passing an |
| 1780 | // inaccessible class. Only actual accesses to fields or methods will. |
| 1781 | const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1782 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1783 | DCHECK(HasFailures()); |
| 1784 | return false; |
| 1785 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1786 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1787 | } |
| 1788 | break; |
| 1789 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1790 | 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] | 1791 | break; |
| 1792 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1793 | 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] | 1794 | break; |
| 1795 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1796 | 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] | 1797 | break; |
| 1798 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1799 | 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] | 1800 | break; |
| 1801 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1802 | 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] | 1803 | break; |
| 1804 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1805 | 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] | 1806 | break; |
| 1807 | case 'J': |
| 1808 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1809 | if (cur_arg + 1 >= expected_args) { |
| 1810 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1811 | << " args, found more (" << descriptor << ")"; |
| 1812 | return false; |
| 1813 | } |
| 1814 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1815 | const RegType* lo_half; |
| 1816 | const RegType* hi_half; |
| 1817 | if (descriptor[0] == 'J') { |
| 1818 | lo_half = ®_types_.LongLo(); |
| 1819 | hi_half = ®_types_.LongHi(); |
| 1820 | } else { |
| 1821 | lo_half = ®_types_.DoubleLo(); |
| 1822 | hi_half = ®_types_.DoubleHi(); |
| 1823 | } |
| 1824 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1825 | cur_arg++; |
| 1826 | break; |
| 1827 | } |
| 1828 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1829 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1830 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1831 | return false; |
| 1832 | } |
| 1833 | cur_arg++; |
| 1834 | } |
| 1835 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1836 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1837 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1838 | return false; |
| 1839 | } |
| 1840 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1841 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1842 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1843 | bool result; |
| 1844 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1845 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1846 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1847 | size_t i = 0; |
| 1848 | do { |
| 1849 | i++; |
| 1850 | } while (descriptor[i] == '['); // process leading [ |
| 1851 | if (descriptor[i] == 'L') { // object array |
| 1852 | do { |
| 1853 | i++; // find closing ; |
| 1854 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1855 | result = descriptor[i] == ';'; |
| 1856 | } else { // primitive array |
| 1857 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1858 | } |
| 1859 | } else if (descriptor[0] == 'L') { |
| 1860 | // could be more thorough here, but shouldn't be required |
| 1861 | size_t i = 0; |
| 1862 | do { |
| 1863 | i++; |
| 1864 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1865 | result = descriptor[i] == ';'; |
| 1866 | } else { |
| 1867 | result = false; |
| 1868 | } |
| 1869 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1870 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1871 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1872 | } |
| 1873 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1876 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1877 | const uint16_t* insns = code_item_accessor_.Insns(); |
| 1878 | const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits(); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1879 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1880 | /* Begin by marking the first instruction as "changed". */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1881 | GetInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1882 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1883 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1884 | /* Continue until no instructions are marked "changed". */ |
| 1885 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1886 | if (allow_thread_suspension_) { |
| 1887 | self_->AllowThreadSuspension(); |
| 1888 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1889 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1890 | uint32_t insn_idx = start_guess; |
| 1891 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1892 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1893 | break; |
| 1894 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1895 | if (insn_idx == insns_size) { |
| 1896 | if (start_guess != 0) { |
| 1897 | /* try again, starting from the top */ |
| 1898 | start_guess = 0; |
| 1899 | continue; |
| 1900 | } else { |
| 1901 | /* all flags are clear */ |
| 1902 | break; |
| 1903 | } |
| 1904 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1905 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1906 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1907 | // "changed" flags, we need to load the set of registers from the table. |
| 1908 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1909 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1910 | // target. |
| 1911 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1912 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1913 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1914 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1915 | /* |
| 1916 | * Sanity check: retrieve the stored register line (assuming |
| 1917 | * a full table) and make sure it actually matches. |
| 1918 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1919 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1920 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1921 | if (work_line_->CompareLine(register_line) != 0) { |
| 1922 | Dump(std::cout); |
| 1923 | std::cout << info_messages_.str(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1924 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1925 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1926 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1927 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1928 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1929 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1930 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1931 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1932 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1933 | prepend += " failed to verify: "; |
| 1934 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1935 | return false; |
| 1936 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1937 | /* Clear "changed" and mark as visited. */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1938 | GetInstructionFlags(insn_idx).SetVisited(); |
| 1939 | GetInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1940 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1941 | |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 1942 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1943 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1944 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1945 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1946 | * down the line, possibly in the verifier. |
| 1947 | * |
| 1948 | * If we've substituted "always throw" instructions into the stream, |
| 1949 | * we are almost certainly going to have some dead code. |
| 1950 | */ |
| 1951 | int dead_start = -1; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1952 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1953 | for (const DexInstructionPcPair& inst : code_item_accessor_) { |
Mathieu Chartier | 2b2bef2 | 2017-10-26 17:10:19 -0700 | [diff] [blame] | 1954 | const uint32_t insn_idx = inst.DexPc(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1955 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1956 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1957 | * may or may not be preceded by a padding NOP (for alignment). |
| 1958 | */ |
| 1959 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1960 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1961 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1962 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1963 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1964 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1965 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1966 | GetInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1969 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1970 | if (dead_start < 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1971 | dead_start = insn_idx; |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1972 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1973 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1974 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1975 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1976 | dead_start = -1; |
| 1977 | } |
| 1978 | } |
| 1979 | if (dead_start >= 0) { |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 1980 | LogVerifyInfo() |
| 1981 | << "dead code " << reinterpret_cast<void*>(dead_start) |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1982 | << "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1983 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1984 | // 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] | 1985 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1986 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1987 | // LOG(INFO) << info_messages_.str(); |
| 1988 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1989 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1990 | return true; |
| 1991 | } |
| 1992 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1993 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1994 | // is no such field. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1995 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1996 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 1997 | DCHECK(class_def != nullptr); |
| 1998 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 1999 | DCHECK(class_data != nullptr); |
| 2000 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | e17cf24 | 2017-06-19 11:05:51 -0700 | [diff] [blame] | 2001 | it.SkipStaticFields(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2002 | while (it.HasNextInstanceField()) { |
| 2003 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 2004 | return it.GetMemberIndex(); |
| 2005 | } |
| 2006 | it.Next(); |
| 2007 | } |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 2008 | return dex::kDexNoIndex; |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2011 | // Setup a register line for the given return instruction. |
| 2012 | static void AdjustReturnLine(MethodVerifier* verifier, |
| 2013 | const Instruction* ret_inst, |
| 2014 | RegisterLine* line) { |
| 2015 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2016 | |
| 2017 | switch (opcode) { |
| 2018 | case Instruction::RETURN_VOID: |
| 2019 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2020 | SafelyMarkAllRegistersAsConflicts(verifier, line); |
| 2021 | break; |
| 2022 | |
| 2023 | case Instruction::RETURN: |
| 2024 | case Instruction::RETURN_OBJECT: |
| 2025 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 2026 | break; |
| 2027 | |
| 2028 | case Instruction::RETURN_WIDE: |
| 2029 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 2030 | break; |
| 2031 | |
| 2032 | default: |
| 2033 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 2034 | UNREACHABLE(); |
| 2035 | } |
| 2036 | } |
| 2037 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2038 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2039 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 2040 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 2041 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 2042 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2043 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2044 | 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] | 2045 | |
| 2046 | std::map<uint32_t, DexLockInfo> depth_to_lock_info; |
| 2047 | auto collector = [&](uint32_t dex_reg, uint32_t depth) { |
| 2048 | auto insert_pair = depth_to_lock_info.emplace(depth, DexLockInfo(depth)); |
| 2049 | auto it = insert_pair.first; |
| 2050 | auto set_insert_pair = it->second.dex_registers.insert(dex_reg); |
| 2051 | DCHECK(set_insert_pair.second); |
| 2052 | }; |
| 2053 | work_line_->IterateRegToLockDepths(collector); |
| 2054 | for (auto& pair : depth_to_lock_info) { |
| 2055 | monitor_enter_dex_pcs_->push_back(pair.second); |
| 2056 | // Map depth to dex PC. |
| 2057 | (*monitor_enter_dex_pcs_)[monitor_enter_dex_pcs_->size() - 1].dex_pc = |
| 2058 | work_line_->GetMonitorEnterDexPc(pair.second.dex_pc); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2064 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2065 | * control to another statement: |
| 2066 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2067 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2068 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2069 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2070 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2071 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2072 | * throw an exception that is handled by an encompassing "try" |
| 2073 | * block. |
| 2074 | * |
| 2075 | * We can also return, in which case there is no successor instruction |
| 2076 | * from this point. |
| 2077 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2078 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2079 | */ |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2080 | const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2081 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2082 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2083 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2084 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2085 | bool just_set_result = false; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 2086 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2087 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2088 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2089 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2090 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2091 | |
| 2092 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2093 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2094 | * can throw an exception, we will copy/merge this into the "catch" |
| 2095 | * address rather than work_line, because we don't want the result |
| 2096 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2097 | * a type) to be visible to the exception handler. |
| 2098 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2099 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2100 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2101 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2102 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2103 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 2104 | 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] | 2105 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2106 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2107 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2108 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2109 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2110 | RegisterLineArenaUniquePtr branch_line; |
| 2111 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2112 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2113 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2114 | case Instruction::NOP: |
| 2115 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2116 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2117 | * a signature that looks like a NOP; if we see one of these in |
| 2118 | * the course of executing code then we have a problem. |
| 2119 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2120 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2121 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2122 | } |
| 2123 | break; |
| 2124 | |
| 2125 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2126 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2127 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2128 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2129 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2130 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2131 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2132 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2133 | break; |
| 2134 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2135 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2136 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2137 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2138 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2139 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2140 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2141 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2142 | break; |
| 2143 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2144 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2145 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2146 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2147 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2148 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2149 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2150 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2151 | break; |
| 2152 | |
| 2153 | /* |
| 2154 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2155 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2156 | * might want to hold the result in an actual CPU register, so the |
| 2157 | * Dalvik spec requires that these only appear immediately after an |
| 2158 | * invoke or filled-new-array. |
| 2159 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2160 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2161 | * redundant with the reset done below, but it can make the debug info |
| 2162 | * easier to read in some cases.) |
| 2163 | */ |
| 2164 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2165 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2166 | break; |
| 2167 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2168 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2169 | break; |
| 2170 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2171 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2172 | break; |
| 2173 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2174 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2175 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 2176 | // where one entrypoint to the catch block is not actually an exception path. |
| 2177 | if (work_insn_idx_ == 0) { |
| 2178 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 2179 | break; |
| 2180 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2181 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2182 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2183 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2184 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2185 | const RegType& res_type = GetCaughtExceptionType(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2186 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2187 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2188 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2189 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2190 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2191 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2192 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
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 | } |
| 2195 | break; |
| 2196 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2197 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2198 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2199 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2200 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2201 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2202 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2203 | } else { |
| 2204 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2205 | // 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] | 2206 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2207 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2208 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2209 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2210 | return_type.IsShort() || return_type.IsChar()) && |
| 2211 | src_type.IsInteger())); |
| 2212 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2213 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2214 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2215 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2216 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2217 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | break; |
| 2221 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2222 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2223 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2224 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2225 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2226 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2227 | } else { |
| 2228 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2229 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2230 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2231 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2232 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2233 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | break; |
| 2237 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2238 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2239 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2240 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2241 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2242 | } else { |
| 2243 | /* return_type is the *expected* return type, not register value */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2244 | DCHECK(!return_type.IsZeroOrNull()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2245 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2246 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2247 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2248 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2249 | // reference in vAA is an instance of the "return_type." |
| 2250 | if (reg_type.IsUndefined()) { |
| 2251 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2252 | } else if (reg_type.IsConflict()) { |
| 2253 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2254 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2255 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2256 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2257 | } else if (!reg_type.IsReferenceTypes()) { |
| 2258 | // We really do expect a reference here. |
| 2259 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2260 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2261 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2262 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 2263 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 2264 | << "' or '" << reg_type << "'"; |
| 2265 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2266 | bool soft_error = false; |
| 2267 | // Check whether arrays are involved. They will show a valid class status, even |
| 2268 | // if their components are erroneous. |
| 2269 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2270 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2271 | if (soft_error) { |
| 2272 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2273 | << reg_type << " vs " << return_type; |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | if (!soft_error) { |
| 2278 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2279 | << "', but expected from declaration '" << return_type << "'"; |
| 2280 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2281 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2282 | } |
| 2283 | } |
| 2284 | } |
| 2285 | break; |
| 2286 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2287 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2288 | case Instruction::CONST_4: { |
| 2289 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2290 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2291 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2292 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2293 | } |
| 2294 | case Instruction::CONST_16: { |
| 2295 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2296 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2297 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2298 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2299 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2300 | case Instruction::CONST: { |
| 2301 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2302 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2303 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2304 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2305 | } |
| 2306 | case Instruction::CONST_HIGH16: { |
| 2307 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2308 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2309 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2310 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2311 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2312 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2313 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2314 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2315 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2316 | 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] | 2317 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2318 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2319 | } |
| 2320 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2321 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2322 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2323 | 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] | 2324 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2328 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2329 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2330 | 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] | 2331 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2332 | break; |
| 2333 | } |
| 2334 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2335 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2336 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2337 | 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] | 2338 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2339 | break; |
| 2340 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2341 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2342 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2343 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2344 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2345 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2346 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2347 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2348 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2349 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2350 | // Get type from instruction if unresolved then we need an access check |
| 2351 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2352 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2353 | // 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] | 2354 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2355 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2356 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2357 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2358 | } |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 2359 | case Instruction::CONST_METHOD_HANDLE: |
| 2360 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2361 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle()); |
| 2362 | // TODO: add compiler support for const-method-{handle,type} (b/66890674) |
| 2363 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 2364 | break; |
| 2365 | case Instruction::CONST_METHOD_TYPE: |
| 2366 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2367 | this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType()); |
| 2368 | // TODO: add compiler support for const-method-{handle,type} (b/66890674) |
| 2369 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 2370 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2371 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2372 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2373 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2374 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2375 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2376 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2377 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2378 | prev_idx--; |
| 2379 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2380 | const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx); |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2381 | switch (prev_inst.Opcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2382 | case Instruction::MOVE_OBJECT: |
| 2383 | case Instruction::MOVE_OBJECT_16: |
| 2384 | case Instruction::MOVE_OBJECT_FROM16: |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2385 | if (prev_inst.VRegB() == inst->VRegA_11x()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2386 | // Redo the copy. This won't change the register types, but update the lock status |
| 2387 | // for the aliased register. |
| 2388 | work_line_->CopyRegister1(this, |
Mathieu Chartier | 1d2d4ff | 2017-09-23 16:11:06 -0700 | [diff] [blame] | 2389 | prev_inst.VRegA(), |
| 2390 | prev_inst.VRegB(), |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2391 | kTypeCategoryRef); |
| 2392 | } |
| 2393 | break; |
| 2394 | |
| 2395 | default: // Other instruction types ignored. |
| 2396 | break; |
| 2397 | } |
| 2398 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2399 | break; |
| 2400 | case Instruction::MONITOR_EXIT: |
| 2401 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2402 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2403 | * 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] | 2404 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2405 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2406 | * feature that Dalvik doesn't support.) |
| 2407 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2408 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2409 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2410 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2411 | * structured locking checks are working, the former would have |
| 2412 | * failed on the -enter instruction, and the latter is impossible. |
| 2413 | * |
| 2414 | * This is fortunate, because issue 3221411 prevents us from |
| 2415 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2416 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2417 | * some catch blocks (which will show up as "dead" code when |
| 2418 | * we skip them here); if we can't, then the code path could be |
| 2419 | * "live" so we still need to check it. |
| 2420 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2421 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2422 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2423 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2424 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2425 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2426 | /* |
| 2427 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2428 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2429 | * |
| 2430 | * 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] | 2431 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2432 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2433 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2434 | 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] | 2435 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2436 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2437 | // If this is a primitive type, fail HARD. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 2438 | ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->LookupResolvedType( |
| 2439 | type_idx, dex_cache_.Get(), class_loader_.Get()); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2440 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2441 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2442 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2443 | << GetDeclaringClass(); |
| 2444 | break; |
| 2445 | } |
| 2446 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2447 | DCHECK_NE(failures_.size(), 0U); |
| 2448 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2449 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2450 | inst->VRegA_22c(), |
| 2451 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2452 | } |
| 2453 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2454 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2455 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2456 | 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] | 2457 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2458 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2459 | if (is_checkcast) { |
| 2460 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2461 | } else { |
| 2462 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2463 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2464 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2465 | if (is_checkcast) { |
| 2466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2467 | } else { |
| 2468 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2469 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2470 | } else if (orig_type.IsUninitializedTypes()) { |
| 2471 | if (is_checkcast) { |
| 2472 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2473 | << orig_type_reg; |
| 2474 | } else { |
| 2475 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2476 | << orig_type_reg; |
| 2477 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2478 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2479 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2480 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2481 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2482 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2483 | inst->VRegA_22c(), |
| 2484 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2485 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2486 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2487 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2488 | } |
| 2489 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2490 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2491 | if (res_type.IsReferenceTypes()) { |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2492 | if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) { |
| 2493 | // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2494 | 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] | 2495 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2496 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2497 | inst->VRegA_12x(), |
| 2498 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2499 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2500 | } else { |
| 2501 | 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] | 2502 | } |
| 2503 | break; |
| 2504 | } |
| 2505 | case Instruction::NEW_INSTANCE: { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 2506 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2507 | if (res_type.IsConflict()) { |
| 2508 | DCHECK_NE(failures_.size(), 0U); |
| 2509 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2510 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2511 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2512 | // can't create an instance of an interface or abstract class */ |
| 2513 | if (!res_type.IsInstantiableTypes()) { |
| 2514 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2515 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2516 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2517 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2518 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2519 | // Any registers holding previous allocations from this address that have not yet been |
| 2520 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2521 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2522 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2523 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2524 | break; |
| 2525 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2526 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2527 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2528 | break; |
| 2529 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2530 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2531 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2532 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2533 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2534 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2535 | just_set_result = true; // Filled new array range sets result register |
| 2536 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2537 | case Instruction::CMPL_FLOAT: |
| 2538 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2539 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2540 | break; |
| 2541 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2542 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2543 | break; |
| 2544 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2545 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2546 | break; |
| 2547 | case Instruction::CMPL_DOUBLE: |
| 2548 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2549 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2550 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2551 | break; |
| 2552 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2553 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2554 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2555 | break; |
| 2556 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2557 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2558 | break; |
| 2559 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2560 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2561 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2562 | break; |
| 2563 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2564 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2565 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2566 | break; |
| 2567 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2568 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2569 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2570 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2571 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2572 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2573 | if (res_type.IsUninitializedTypes()) { |
| 2574 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2575 | } else if (!res_type.IsReferenceTypes()) { |
| 2576 | 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] | 2577 | } else { |
| 2578 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2579 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2580 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2581 | } |
| 2582 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2583 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2584 | case Instruction::GOTO: |
| 2585 | case Instruction::GOTO_16: |
| 2586 | case Instruction::GOTO_32: |
| 2587 | /* no effect on or use of registers */ |
| 2588 | break; |
| 2589 | |
| 2590 | case Instruction::PACKED_SWITCH: |
| 2591 | case Instruction::SPARSE_SWITCH: |
| 2592 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2593 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2594 | break; |
| 2595 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2596 | case Instruction::FILL_ARRAY_DATA: { |
| 2597 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2598 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2599 | /* array_type can be null if the reg type is Zero */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2600 | if (!array_type.IsZeroOrNull()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2601 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2602 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2603 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2604 | } else if (array_type.IsUnresolvedTypes()) { |
| 2605 | // If it's an unresolved array type, it must be non-primitive. |
| 2606 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2607 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2608 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2609 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2610 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2611 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2612 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2613 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2614 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2615 | // Now verify if the element width in the table matches the element width declared in |
| 2616 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2617 | const uint16_t* array_data = |
| 2618 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2619 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2620 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2621 | } else { |
| 2622 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2623 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2624 | // in the table and expected from the array class. |
| 2625 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2626 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2627 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2628 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2629 | } |
| 2630 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2634 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2635 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2636 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2637 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2638 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2639 | bool mismatch = false; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2640 | if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2641 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2642 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2643 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2644 | } else { // both integral? |
| 2645 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2646 | } |
| 2647 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2648 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2649 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2650 | } |
| 2651 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2652 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2653 | case Instruction::IF_LT: |
| 2654 | case Instruction::IF_GE: |
| 2655 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2656 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2657 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2658 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2659 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2660 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2661 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2662 | } |
| 2663 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2664 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2665 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2666 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2667 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2668 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2669 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2670 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2671 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2672 | |
| 2673 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2674 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2675 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2676 | instance_of_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2677 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2678 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2679 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2680 | if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2681 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2682 | work_insn_idx_)) { |
| 2683 | break; |
| 2684 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2685 | } else { |
| 2686 | break; |
| 2687 | } |
| 2688 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2689 | const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2690 | |
| 2691 | /* Check for peep-hole pattern of: |
| 2692 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2693 | * instance-of vX, vY, T; |
| 2694 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2695 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2696 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2697 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2698 | * and sharpen the type of vY to be type T. |
| 2699 | * Note, this pattern can't be if: |
| 2700 | * - if there are other branches to this branch, |
| 2701 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2702 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2703 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2704 | (Instruction::INSTANCE_OF == instance_of_inst.Opcode()) && |
| 2705 | (inst->VRegA_21t() == instance_of_inst.VRegA_22c()) && |
| 2706 | (instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2707 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2708 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2709 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2710 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2711 | // and we don't want to disallow field and other operations on the object. If the value |
| 2712 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2713 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2714 | // type is assignable to the original then allow optimization. This check is performed to |
| 2715 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2716 | // interface from a class that would lose information relevant to field checks. |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2717 | 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] | 2718 | const RegType& cast_type = ResolveClass<CheckAccess::kYes>( |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2719 | dex::TypeIndex(instance_of_inst.VRegC_22c())); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2720 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2721 | if (!orig_type.Equals(cast_type) && |
| 2722 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2723 | 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] | 2724 | !cast_type.GetClass()->IsInterface() && |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 2725 | (orig_type.IsZeroOrNull() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2726 | orig_type.IsStrictlyAssignableFrom( |
| 2727 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2728 | RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(), |
| 2729 | this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2730 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2731 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2732 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2733 | branch_line.reset(update_line); |
| 2734 | } |
| 2735 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2736 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2737 | instance_of_inst.VRegB_22c(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2738 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2739 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2740 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2741 | // register encoding space of instance-of, and propagate type information to the source |
| 2742 | // of the move-object. |
| 2743 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2744 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2745 | move_idx--; |
| 2746 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2747 | if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2748 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2749 | work_insn_idx_)) { |
| 2750 | break; |
| 2751 | } |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 2752 | const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx); |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2753 | switch (move_inst.Opcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2754 | case Instruction::MOVE_OBJECT: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2755 | if (move_inst.VRegA_12x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2756 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2757 | move_inst.VRegB_12x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2758 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2759 | } |
| 2760 | break; |
| 2761 | case Instruction::MOVE_OBJECT_FROM16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2762 | if (move_inst.VRegA_22x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2763 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2764 | move_inst.VRegB_22x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2765 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2766 | } |
| 2767 | break; |
| 2768 | case Instruction::MOVE_OBJECT_16: |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2769 | if (move_inst.VRegA_32x() == instance_of_inst.VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2770 | update_line->SetRegisterType<LockOp::kKeep>(this, |
Vladimir Marko | d7559b7 | 2017-09-28 13:50:37 +0100 | [diff] [blame] | 2771 | move_inst.VRegB_32x(), |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2772 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2773 | } |
| 2774 | break; |
| 2775 | default: |
| 2776 | break; |
| 2777 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2782 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2783 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2784 | case Instruction::IF_LTZ: |
| 2785 | case Instruction::IF_GEZ: |
| 2786 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2787 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2788 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2789 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2790 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2791 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2792 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2793 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2794 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2795 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2796 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2797 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2798 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2799 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2800 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2801 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2802 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2803 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2804 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2805 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2806 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2807 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2808 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2809 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2810 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2811 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2812 | break; |
| 2813 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2814 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2815 | break; |
| 2816 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2817 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2818 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2819 | break; |
| 2820 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2821 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2822 | break; |
| 2823 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2824 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2825 | break; |
| 2826 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2827 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2828 | break; |
| 2829 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2830 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2831 | break; |
| 2832 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2833 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2834 | break; |
| 2835 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2836 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2837 | break; |
| 2838 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2839 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2840 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2841 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2842 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2843 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2844 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2845 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2846 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2847 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2848 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2849 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2850 | break; |
| 2851 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2852 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2855 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2856 | break; |
| 2857 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2858 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2859 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2860 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2861 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2862 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2863 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2864 | break; |
| 2865 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2866 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2867 | break; |
| 2868 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2869 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2870 | break; |
| 2871 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2872 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2873 | break; |
| 2874 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2875 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2876 | break; |
| 2877 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2878 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2879 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2880 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2881 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2882 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2883 | break; |
| 2884 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2885 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2886 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2887 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2888 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2889 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2890 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2891 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2892 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2893 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2894 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2895 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2896 | break; |
| 2897 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2898 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2899 | break; |
| 2900 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2901 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2902 | break; |
| 2903 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2904 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2905 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2906 | break; |
| 2907 | |
| 2908 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2909 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2910 | break; |
| 2911 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2912 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2913 | break; |
| 2914 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2915 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2916 | break; |
| 2917 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2918 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2919 | break; |
| 2920 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2921 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2922 | break; |
| 2923 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2924 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2925 | break; |
| 2926 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2927 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2928 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2929 | break; |
| 2930 | |
| 2931 | case Instruction::INVOKE_VIRTUAL: |
| 2932 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2933 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2934 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2935 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2936 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2937 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2938 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2939 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2940 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2941 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2942 | if (called_method != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2943 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2944 | ? called_method->ResolveReturnType() |
| 2945 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2946 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2947 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2948 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2949 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2950 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2951 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2952 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2956 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2957 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2958 | dex::TypeIndex return_type_idx = |
| 2959 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2960 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2961 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2962 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2963 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2964 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2965 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2966 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2967 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2968 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2969 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2970 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2971 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2972 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2973 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2974 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2975 | const char* return_type_descriptor; |
| 2976 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2977 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2978 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2979 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2980 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2981 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2982 | dex::TypeIndex return_type_idx = |
| 2983 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2984 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2985 | } else { |
| 2986 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2987 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2988 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 2989 | ? called_method->ResolveReturnType() |
| 2990 | : called_method->LookupResolvedReturnType(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2991 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2992 | return_type = &FromClass(return_type_descriptor, |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 2993 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2994 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2995 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2996 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2997 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2998 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2999 | } |
| 3000 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3001 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3002 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 3003 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 3004 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 3005 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 3006 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3007 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3008 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3009 | if (this_type.IsConflict()) // failure. |
| 3010 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3011 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3012 | /* no null refs allowed (?) */ |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3013 | if (this_type.IsZeroOrNull()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3014 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 3015 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 3016 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3017 | |
| 3018 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3019 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3020 | // TODO: re-enable constructor type verification |
| 3021 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3022 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 3023 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 3024 | // break; |
| 3025 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3026 | |
| 3027 | /* arg must be an uninitialized reference */ |
| 3028 | if (!this_type.IsUninitializedTypes()) { |
| 3029 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 3030 | << this_type; |
| 3031 | break; |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 3036 | * registers that have the same object instance in them, not just the "this" register. |
| 3037 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 3038 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3039 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3040 | if (return_type == nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3041 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3042 | } |
| 3043 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3044 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3045 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3046 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3047 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3048 | just_set_result = true; |
| 3049 | break; |
| 3050 | } |
| 3051 | case Instruction::INVOKE_STATIC: |
| 3052 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3053 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3054 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3055 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3056 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3057 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3058 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3059 | dex::TypeIndex return_type_idx = |
| 3060 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3061 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3062 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3063 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3064 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3065 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3066 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3067 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3068 | } else { |
| 3069 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3070 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3071 | just_set_result = true; |
| 3072 | } |
| 3073 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3074 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3075 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3076 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3077 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3078 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3079 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3080 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3081 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3082 | << abs_method->PrettyMethod() << "'"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3083 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3084 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3085 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3086 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3087 | * interface or Object (see comments in RegType::JoinClass). |
| 3088 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3089 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 3090 | if (this_type.IsZeroOrNull()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3091 | /* null pointer always passes (and always fails at runtime) */ |
| 3092 | } else { |
| 3093 | if (this_type.IsUninitializedTypes()) { |
| 3094 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3095 | << this_type; |
| 3096 | break; |
| 3097 | } |
| 3098 | // In the past we have tried to assert that "called_interface" is assignable |
| 3099 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3100 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3101 | // implemented by "this_type". For example, two classes may implement the same |
| 3102 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3103 | // join will set "this_type" to the parent class and a test that this implements |
| 3104 | // the interface will incorrectly fail. |
| 3105 | } |
| 3106 | /* |
| 3107 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3108 | * the type information is in the abstract method, so we're good. |
| 3109 | */ |
| 3110 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3111 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3112 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3113 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3114 | dex::TypeIndex return_type_idx = |
| 3115 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3116 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3117 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3118 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3119 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3120 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3121 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3122 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3123 | } else { |
| 3124 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3125 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3126 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3127 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3128 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3129 | case Instruction::INVOKE_POLYMORPHIC: |
| 3130 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3131 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3132 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3133 | if (called_method == nullptr) { |
| 3134 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3135 | if (failure_messages_.size() > 0) { |
| 3136 | std::string message = failure_messages_.back()->str(); |
| 3137 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3138 | } else { |
| 3139 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3140 | } |
| 3141 | break; |
| 3142 | } |
| 3143 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3144 | !CheckSignaturePolymorphicReceiver(inst)) { |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 3145 | DCHECK(HasFailures()); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3146 | break; |
| 3147 | } |
| 3148 | 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] | 3149 | const char* return_descriptor = |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3150 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3151 | const RegType& return_type = |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3152 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3153 | if (!return_type.IsLowHalf()) { |
| 3154 | work_line_->SetResultRegisterType(this, return_type); |
| 3155 | } else { |
| 3156 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3157 | } |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3158 | just_set_result = true; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3159 | break; |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3160 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3161 | case Instruction::INVOKE_CUSTOM: |
| 3162 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3163 | // Verify registers based on method_type in the call site. |
| 3164 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3165 | |
| 3166 | // Step 1. Check the call site that produces the method handle for invocation |
| 3167 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3168 | if (!CheckCallSite(call_site_idx)) { |
| 3169 | DCHECK(HasFailures()); |
| 3170 | break; |
| 3171 | } |
| 3172 | |
| 3173 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3174 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3175 | // the first three arguments and CheckCallSite has checked the method handle type. |
| 3176 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 3177 | it.Next(); // Skip to name. |
| 3178 | it.Next(); // Skip to method type of the method handle |
| 3179 | const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 3180 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
| 3181 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3182 | // Treat method as static as it has yet to be determined. |
| 3183 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3184 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3185 | |
| 3186 | // Step 3. Propagate return type information |
| 3187 | const RegType& return_type = |
| 3188 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
| 3189 | if (!return_type.IsLowHalf()) { |
| 3190 | work_line_->SetResultRegisterType(this, return_type); |
| 3191 | } else { |
| 3192 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3193 | } |
| 3194 | just_set_result = true; |
| 3195 | // TODO: Add compiler support for invoke-custom (b/35337872). |
| 3196 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 3197 | break; |
| 3198 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3199 | case Instruction::NEG_INT: |
| 3200 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3201 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3202 | break; |
| 3203 | case Instruction::NEG_LONG: |
| 3204 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3205 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3206 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3207 | break; |
| 3208 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3209 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3210 | break; |
| 3211 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3212 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3213 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3214 | break; |
| 3215 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3216 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3217 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3218 | break; |
| 3219 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3220 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3221 | break; |
| 3222 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3223 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3224 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3225 | break; |
| 3226 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3227 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3228 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3229 | break; |
| 3230 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3231 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3232 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3233 | break; |
| 3234 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3235 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3236 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3237 | break; |
| 3238 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3239 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3240 | break; |
| 3241 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3242 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3243 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3244 | break; |
| 3245 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3246 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3247 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3248 | break; |
| 3249 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3250 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3251 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3252 | break; |
| 3253 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3254 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3255 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3256 | break; |
| 3257 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3258 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3259 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3260 | break; |
| 3261 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3262 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3263 | break; |
| 3264 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3265 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3266 | break; |
| 3267 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3268 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3269 | break; |
| 3270 | |
| 3271 | case Instruction::ADD_INT: |
| 3272 | case Instruction::SUB_INT: |
| 3273 | case Instruction::MUL_INT: |
| 3274 | case Instruction::REM_INT: |
| 3275 | case Instruction::DIV_INT: |
| 3276 | case Instruction::SHL_INT: |
| 3277 | case Instruction::SHR_INT: |
| 3278 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3279 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3280 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3281 | break; |
| 3282 | case Instruction::AND_INT: |
| 3283 | case Instruction::OR_INT: |
| 3284 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3285 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3286 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3287 | break; |
| 3288 | case Instruction::ADD_LONG: |
| 3289 | case Instruction::SUB_LONG: |
| 3290 | case Instruction::MUL_LONG: |
| 3291 | case Instruction::DIV_LONG: |
| 3292 | case Instruction::REM_LONG: |
| 3293 | case Instruction::AND_LONG: |
| 3294 | case Instruction::OR_LONG: |
| 3295 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3296 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3297 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3298 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3299 | break; |
| 3300 | case Instruction::SHL_LONG: |
| 3301 | case Instruction::SHR_LONG: |
| 3302 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3303 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3304 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3305 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3306 | break; |
| 3307 | case Instruction::ADD_FLOAT: |
| 3308 | case Instruction::SUB_FLOAT: |
| 3309 | case Instruction::MUL_FLOAT: |
| 3310 | case Instruction::DIV_FLOAT: |
| 3311 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3312 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3313 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3314 | break; |
| 3315 | case Instruction::ADD_DOUBLE: |
| 3316 | case Instruction::SUB_DOUBLE: |
| 3317 | case Instruction::MUL_DOUBLE: |
| 3318 | case Instruction::DIV_DOUBLE: |
| 3319 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3320 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3321 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3322 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3323 | break; |
| 3324 | case Instruction::ADD_INT_2ADDR: |
| 3325 | case Instruction::SUB_INT_2ADDR: |
| 3326 | case Instruction::MUL_INT_2ADDR: |
| 3327 | case Instruction::REM_INT_2ADDR: |
| 3328 | case Instruction::SHL_INT_2ADDR: |
| 3329 | case Instruction::SHR_INT_2ADDR: |
| 3330 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3331 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3332 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3333 | break; |
| 3334 | case Instruction::AND_INT_2ADDR: |
| 3335 | case Instruction::OR_INT_2ADDR: |
| 3336 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3337 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3338 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3339 | break; |
| 3340 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3341 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3342 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3343 | break; |
| 3344 | case Instruction::ADD_LONG_2ADDR: |
| 3345 | case Instruction::SUB_LONG_2ADDR: |
| 3346 | case Instruction::MUL_LONG_2ADDR: |
| 3347 | case Instruction::DIV_LONG_2ADDR: |
| 3348 | case Instruction::REM_LONG_2ADDR: |
| 3349 | case Instruction::AND_LONG_2ADDR: |
| 3350 | case Instruction::OR_LONG_2ADDR: |
| 3351 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3352 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3353 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3354 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3355 | break; |
| 3356 | case Instruction::SHL_LONG_2ADDR: |
| 3357 | case Instruction::SHR_LONG_2ADDR: |
| 3358 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3359 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3360 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3361 | break; |
| 3362 | case Instruction::ADD_FLOAT_2ADDR: |
| 3363 | case Instruction::SUB_FLOAT_2ADDR: |
| 3364 | case Instruction::MUL_FLOAT_2ADDR: |
| 3365 | case Instruction::DIV_FLOAT_2ADDR: |
| 3366 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3367 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3368 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3369 | break; |
| 3370 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3371 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3372 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3373 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3374 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3375 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3376 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3377 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3378 | break; |
| 3379 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3380 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3381 | case Instruction::MUL_INT_LIT16: |
| 3382 | case Instruction::DIV_INT_LIT16: |
| 3383 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3384 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3385 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3386 | break; |
| 3387 | case Instruction::AND_INT_LIT16: |
| 3388 | case Instruction::OR_INT_LIT16: |
| 3389 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3390 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3391 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3392 | break; |
| 3393 | case Instruction::ADD_INT_LIT8: |
| 3394 | case Instruction::RSUB_INT_LIT8: |
| 3395 | case Instruction::MUL_INT_LIT8: |
| 3396 | case Instruction::DIV_INT_LIT8: |
| 3397 | case Instruction::REM_INT_LIT8: |
| 3398 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3399 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3400 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3401 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3402 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3403 | break; |
| 3404 | case Instruction::AND_INT_LIT8: |
| 3405 | case Instruction::OR_INT_LIT8: |
| 3406 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3407 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3408 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3409 | break; |
| 3410 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3411 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3412 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3413 | if (IsConstructor() && !IsStatic()) { |
| 3414 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3415 | if (declaring_class.IsUnresolvedReference()) { |
| 3416 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3417 | // manually over the underlying dex file. |
| 3418 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3419 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 3420 | if (first_index != dex::kDexNoIndex) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3421 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3422 | << first_index; |
| 3423 | } |
| 3424 | break; |
| 3425 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3426 | auto* klass = declaring_class.GetClass(); |
| 3427 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3428 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3429 | 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] | 3430 | << klass->GetInstanceField(i)->PrettyField(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3431 | break; |
| 3432 | } |
| 3433 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3434 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3435 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3436 | // quickened opcodes (otherwise this could be a fall-through). |
| 3437 | if (!IsConstructor()) { |
| 3438 | if (!GetMethodReturnType().IsConflict()) { |
| 3439 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3440 | } |
| 3441 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3442 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3443 | // Note: the following instructions encode offsets derived from class linking. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 3444 | // As such they use Class*/Field*/Executable* as these offsets only have |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3445 | // meaning if the class linking and resolution were successful. |
| 3446 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3447 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3448 | break; |
| 3449 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3450 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3451 | break; |
| 3452 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3453 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3454 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3455 | case Instruction::IGET_BOOLEAN_QUICK: |
| 3456 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 3457 | break; |
| 3458 | case Instruction::IGET_BYTE_QUICK: |
| 3459 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 3460 | break; |
| 3461 | case Instruction::IGET_CHAR_QUICK: |
| 3462 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 3463 | break; |
| 3464 | case Instruction::IGET_SHORT_QUICK: |
| 3465 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 3466 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3467 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3468 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3469 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3470 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3471 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3472 | break; |
| 3473 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3474 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3475 | break; |
| 3476 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3477 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3478 | break; |
| 3479 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3480 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3481 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3482 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3483 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3484 | break; |
| 3485 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3486 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3487 | break; |
| 3488 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 3489 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 3490 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3491 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3492 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3493 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3494 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3495 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3496 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3497 | } else { |
| 3498 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3499 | } |
| 3500 | just_set_result = true; |
| 3501 | } |
| 3502 | break; |
| 3503 | } |
| 3504 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3505 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3506 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3507 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3508 | case Instruction::UNUSED_79: |
| 3509 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3510 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3511 | break; |
| 3512 | |
| 3513 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3514 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3515 | * complain if an instruction is missing (which is desirable). |
| 3516 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3517 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3518 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3519 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3520 | if (Runtime::Current()->IsAotCompiler()) { |
| 3521 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3522 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3523 | LOG(ERROR) << "Pending failures:"; |
| 3524 | for (auto& error : failures_) { |
| 3525 | LOG(ERROR) << error; |
| 3526 | } |
| 3527 | for (auto& error_msg : failure_messages_) { |
| 3528 | LOG(ERROR) << error_msg->str(); |
| 3529 | } |
| 3530 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3531 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3532 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3533 | /* immediate failure, reject class */ |
| 3534 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3535 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3536 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3537 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3538 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3539 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3540 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3541 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3542 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3543 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3544 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3545 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3546 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3547 | */ |
| 3548 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3549 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3550 | } |
| 3551 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3552 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3553 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3554 | * |
| 3555 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3556 | * 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] | 3557 | * somebody could get a reference field, check it for zero, and if the |
| 3558 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3559 | * 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] | 3560 | * that, and will reject the code. |
| 3561 | * |
| 3562 | * TODO: avoid re-fetching the branch target |
| 3563 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3564 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3565 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3566 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3567 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3568 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3569 | return false; |
| 3570 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3571 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3572 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), |
| 3573 | work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3574 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3575 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3576 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3577 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3578 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3579 | return false; |
| 3580 | } |
| 3581 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3582 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3583 | return false; |
| 3584 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3585 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3589 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3590 | * |
| 3591 | * We've already verified that the table is structurally sound, so we |
| 3592 | * just need to walk through and tag the targets. |
| 3593 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3594 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3595 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3596 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3597 | int switch_count = switch_insns[1]; |
| 3598 | int offset_to_targets, targ; |
| 3599 | |
| 3600 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3601 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3602 | offset_to_targets = 4; |
| 3603 | } else { |
| 3604 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3605 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3606 | offset_to_targets = 2 + 2 * switch_count; |
| 3607 | } |
| 3608 | |
| 3609 | /* verify each switch target */ |
| 3610 | for (targ = 0; targ < switch_count; targ++) { |
| 3611 | int offset; |
| 3612 | uint32_t abs_offset; |
| 3613 | |
| 3614 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3615 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3616 | (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] | 3617 | abs_offset = work_insn_idx_ + offset; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3618 | DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits()); |
| 3619 | if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3620 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3621 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3622 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3623 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3624 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | |
| 3628 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3629 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3630 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3631 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3632 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3633 | bool has_catch_all_handler = false; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3634 | const DexFile::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_); |
| 3635 | CHECK(try_item != nullptr); |
Mathieu Chartier | dc578c7 | 2017-12-27 11:51:45 -0800 | [diff] [blame] | 3636 | CatchHandlerIterator iterator(code_item_accessor_, *try_item); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3637 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3638 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3639 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3640 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3641 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3642 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3643 | if (!handler_type_idx.IsValid()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3644 | has_catch_all_handler = true; |
| 3645 | } else { |
| 3646 | // It is also a catch-all if it is java.lang.Throwable. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3647 | ObjPtr<mirror::Class> klass = |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3648 | linker->ResolveType(handler_type_idx, dex_cache_, class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3649 | if (klass != nullptr) { |
| 3650 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3651 | has_catch_all_handler = true; |
| 3652 | } |
| 3653 | } else { |
| 3654 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3655 | DCHECK(self_->IsExceptionPending()); |
| 3656 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3657 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3658 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3659 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3660 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3661 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3662 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3663 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3664 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3665 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3666 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3670 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3671 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3672 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3673 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3674 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3675 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3676 | * 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] | 3677 | * it will do so before grabbing the lock). |
| 3678 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3679 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3680 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3681 | << "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] | 3682 | return false; |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3687 | /* Handle "continue". Tag the next consecutive instruction. |
| 3688 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3689 | * because it changes work_line_ when performing peephole optimization |
| 3690 | * and this change should not be used in those cases. |
| 3691 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3692 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3693 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3694 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3695 | if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3696 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3697 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3698 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3699 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3700 | // next instruction isn't one. |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3701 | if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3702 | return false; |
| 3703 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3704 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3705 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3706 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3707 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3708 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3709 | // 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] | 3710 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3711 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3712 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3713 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3714 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3715 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3716 | // needed. If the merge changes the state of the registers then the work line will be |
| 3717 | // updated. |
| 3718 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3719 | return false; |
| 3720 | } |
| 3721 | } else { |
| 3722 | /* |
| 3723 | * We're not recording register data for the next instruction, so we don't know what the |
| 3724 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3725 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3726 | GetInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3727 | } |
| 3728 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3729 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3730 | /* 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] | 3731 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3732 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3736 | * Update start_guess. Advance to the next instruction of that's |
| 3737 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3738 | * neither of those exists we're in a return or throw; leave start_guess |
| 3739 | * alone and let the caller sort it out. |
| 3740 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3741 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3742 | DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3743 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3744 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3745 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3746 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3749 | DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits()); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3750 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3751 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3752 | if (have_pending_runtime_throw_failure_) { |
| 3753 | have_any_pending_runtime_throw_failure_ = true; |
| 3754 | // Reset the pending_runtime_throw flag now. |
| 3755 | have_pending_runtime_throw_failure_ = false; |
| 3756 | } |
| 3757 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3758 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3759 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3760 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3761 | void MethodVerifier::UninstantiableError(const char* descriptor) { |
| 3762 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 3763 | << "non-instantiable klass " << descriptor; |
| 3764 | } |
| 3765 | |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3766 | inline bool MethodVerifier::IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3767 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 3768 | } |
| 3769 | |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3770 | template <MethodVerifier::CheckAccess C> |
| 3771 | const RegType& MethodVerifier::ResolveClass(dex::TypeIndex class_idx) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3772 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3773 | ObjPtr<mirror::Class> klass = can_load_classes_ |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 3774 | ? linker->ResolveType(class_idx, dex_cache_, class_loader_) |
| 3775 | : linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3776 | if (can_load_classes_ && klass == nullptr) { |
| 3777 | DCHECK(self_->IsExceptionPending()); |
| 3778 | self_->ClearException(); |
| 3779 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3780 | const RegType* result = nullptr; |
Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3781 | if (klass != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3782 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3783 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3784 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3785 | UninstantiableError(descriptor); |
| 3786 | precise = false; |
| 3787 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3788 | result = reg_types_.FindClass(klass.Ptr(), precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3789 | if (result == nullptr) { |
| 3790 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3791 | result = reg_types_.InsertClass(descriptor, klass.Ptr(), precise); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3792 | } |
| 3793 | } else { |
| 3794 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3795 | result = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3796 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3797 | DCHECK(result != nullptr); |
| 3798 | if (result->IsConflict()) { |
| 3799 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3800 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3801 | << "' in " << GetDeclaringClass(); |
| 3802 | return *result; |
| 3803 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3804 | |
| 3805 | // Record result of class resolution attempt. |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 3806 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass.Ptr()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3807 | |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3808 | // If requested, check if access is allowed. Unresolved types are included in this check, as the |
| 3809 | // interpreter only tests whether access is allowed when a class is not pre-verified and runs in |
| 3810 | // the access-checks interpreter. If result is primitive, skip the access check. |
| 3811 | // |
| 3812 | // Note: we do this for unresolved classes to trigger re-verification at runtime. |
| 3813 | if (C == CheckAccess::kYes && result->IsNonZeroReferenceTypes()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3814 | const RegType& referrer = GetDeclaringClass(); |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 3815 | if (!referrer.CanAccess(*result)) { |
| 3816 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3817 | << referrer << "' -> '" << *result << "'"; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3818 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3819 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3820 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Andreas Gampe | dd30907 | 2017-08-31 07:47:37 -0700 | [diff] [blame] | 3823 | // Instantiate ResolveClass variants. This is required as the -inl file has a function with a call |
| 3824 | // to ResolveClass, and compilers may decide to inline, requiring a symbol. |
Andreas Gampe | ae0b1f4 | 2017-08-30 17:36:26 -0700 | [diff] [blame] | 3825 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kNo>( |
| 3826 | dex::TypeIndex class_idx); |
| 3827 | template const RegType& MethodVerifier::ResolveClass<MethodVerifier::CheckAccess::kYes>( |
| 3828 | dex::TypeIndex class_idx); |
| 3829 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3830 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3831 | const RegType* common_super = nullptr; |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 3832 | if (code_item_accessor_.TriesSize() != 0) { |
| 3833 | const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3834 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3835 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3836 | CatchHandlerIterator iterator(handlers_ptr); |
| 3837 | for (; iterator.HasNext(); iterator.Next()) { |
| 3838 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3839 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3840 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3841 | } else { |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3842 | const RegType& exception = |
| 3843 | ResolveClass<CheckAccess::kYes>(iterator.GetHandlerTypeIndex()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3844 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3845 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3846 | if (exception.IsUnresolvedTypes()) { |
| 3847 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3848 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3849 | return exception; |
| 3850 | } else { |
| 3851 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3852 | return reg_types_.Conflict(); |
| 3853 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3854 | } else if (common_super == nullptr) { |
| 3855 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3856 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3857 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3858 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3859 | common_super = &common_super->Merge(exception, ®_types_, this); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3860 | if (FailOrAbort(this, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3861 | reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3862 | *common_super, this), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3863 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3864 | work_insn_idx_)) { |
| 3865 | break; |
| 3866 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | } |
| 3870 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3871 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3872 | } |
| 3873 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3874 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3875 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3876 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3877 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3878 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3879 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3882 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3883 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3884 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 3885 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3886 | if (klass_type.IsConflict()) { |
| 3887 | std::string append(" in attempt to access method "); |
| 3888 | append += dex_file_->GetMethodName(method_id); |
| 3889 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3890 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3891 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3892 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3893 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3894 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3895 | ObjPtr<mirror::Class> klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3896 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3897 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3898 | auto pointer_size = cl->GetImagePointerSize(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3899 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3900 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3901 | if (res_method == nullptr) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3902 | // Try to find the method with the appropriate lookup for the klass type (interface or not). |
| 3903 | // If this lookup does not match `method_type`, errors shall be reported below. |
| 3904 | if (klass->IsInterface()) { |
| 3905 | res_method = klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3906 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3907 | res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3908 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3909 | if (res_method != nullptr) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3910 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3911 | } |
| 3912 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3913 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3914 | // Record result of method resolution attempt. The klass resolution has recorded whether |
| 3915 | // the class is an interface or not and therefore the type of the lookup performed above. |
| 3916 | // TODO: Maybe we should not record dependency if the invoke type does not match the lookup type. |
| 3917 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method); |
| 3918 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3919 | bool must_fail = false; |
| 3920 | // This is traditional and helps with screwy bytecode. It will tell you that, yes, a method |
| 3921 | // exists, but that it's called incorrectly. This significantly helps debugging, as locally it's |
| 3922 | // hard to see the differences. |
| 3923 | // If we don't have res_method here we must fail. Just use this bool to make sure of that with a |
| 3924 | // DCHECK. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3925 | if (res_method == nullptr) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3926 | must_fail = true; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3927 | // Try to find the method also with the other type for better error reporting below |
| 3928 | // but do not store such bogus lookup result in the DexCache or VerifierDeps. |
| 3929 | if (klass->IsInterface()) { |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3930 | // NB This is normally not really allowed but we want to get any static or private object |
| 3931 | // methods for error message purposes. This will never be returned. |
| 3932 | // TODO We might want to change the verifier to not require this. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 3933 | res_method = klass->FindClassMethod(dex_cache_.Get(), dex_method_idx, pointer_size); |
| 3934 | } else { |
| 3935 | // If there was an interface method with the same signature, |
| 3936 | // we would have found it also in the "copied" methods. |
| 3937 | DCHECK(klass->FindInterfaceMethod(dex_cache_.Get(), dex_method_idx, pointer_size) == nullptr); |
| 3938 | } |
| 3939 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3940 | |
| 3941 | if (res_method == nullptr) { |
| 3942 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3943 | << klass->PrettyDescriptor() << "." |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3944 | << dex_file_->GetMethodName(method_id) << " " |
| 3945 | << dex_file_->GetMethodSignature(method_id); |
| 3946 | return nullptr; |
| 3947 | } |
| 3948 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3949 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3950 | // enforce them here. |
| 3951 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3952 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3953 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3954 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3955 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3956 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3957 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3958 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3959 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3960 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3961 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3962 | |
| 3963 | // Check that interface methods are static or match interface classes. |
| 3964 | // We only allow statics if we don't have default methods enabled. |
| 3965 | // |
| 3966 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3967 | // while this check implies an IncompatibleClassChangeError. |
| 3968 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3969 | // 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] | 3970 | // default methods are supported for the dex file), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3971 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3972 | method_type != METHOD_STATIC && |
Mathieu Chartier | f6e3147 | 2017-12-28 13:32:08 -0800 | [diff] [blame] | 3973 | (!dex_file_->SupportsDefaultMethods() || |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3974 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3975 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3976 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3977 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3978 | << " is in an interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3979 | return nullptr; |
| 3980 | } |
| 3981 | } else { |
| 3982 | if (method_type == METHOD_INTERFACE) { |
| 3983 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3984 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3985 | << " is in a non-interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3986 | return nullptr; |
| 3987 | } |
| 3988 | } |
| 3989 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 3990 | // Check specifically for non-public object methods being provided for interface dispatch. This |
| 3991 | // can occur if we failed to find a method with FindInterfaceMethod but later find one with |
| 3992 | // FindClassMethod for error message use. |
| 3993 | if (method_type == METHOD_INTERFACE && |
| 3994 | res_method->GetDeclaringClass()->IsObjectClass() && |
| 3995 | !res_method->IsPublic()) { |
| 3996 | Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "." |
| 3997 | << dex_file_->GetMethodName(method_id) << " " |
| 3998 | << dex_file_->GetMethodSignature(method_id) << " resolved to " |
| 3999 | << "non-public object method " << res_method->PrettyMethod() << " " |
| 4000 | << "but non-public Object methods are excluded from interface " |
| 4001 | << "method resolution."; |
| 4002 | return nullptr; |
| 4003 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4004 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4005 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4006 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 4007 | << res_method->PrettyMethod() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4008 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 4009 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4010 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 4011 | // 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] | 4012 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4013 | 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] | 4014 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4015 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 4016 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4017 | // 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] | 4018 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 4019 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 4020 | // arguments. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 4021 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4022 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4023 | ((method_type == METHOD_SUPER || |
| 4024 | method_type == METHOD_VIRTUAL || |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4025 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 4026 | ((method_type == METHOD_POLYMORPHIC) && |
| 4027 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4028 | 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] | 4029 | "type of " << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4030 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4031 | } |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 4032 | // Make sure we weren't expecting to fail. |
| 4033 | DCHECK(!must_fail) << "invoke type (" << method_type << ")" |
| 4034 | << klass->PrettyDescriptor() << "." |
| 4035 | << dex_file_->GetMethodName(method_id) << " " |
| 4036 | << dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to " |
| 4037 | << res_method->PrettyMethod() << " without error. Initially this method was " |
| 4038 | << "not found so we were expecting to fail for some reason."; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4039 | return res_method; |
| 4040 | } |
| 4041 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4042 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4043 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 4044 | 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] | 4045 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4046 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4047 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4048 | const size_t expected_args = inst->VRegA(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4049 | /* caught by static verifier */ |
| 4050 | DCHECK(is_range || expected_args <= 5); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4051 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4052 | if (expected_args > code_item_accessor_.OutsSize()) { |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4053 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4054 | << ") exceeds outsSize (" |
| 4055 | << code_item_accessor_.OutsSize() << ")"; |
Orion Hodson | 1cda7c2 | 2017-08-10 13:06:45 +0100 | [diff] [blame] | 4056 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4057 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4058 | |
| 4059 | /* |
| 4060 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4061 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4062 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4063 | */ |
| 4064 | if (method_type != METHOD_STATIC) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4065 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4066 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 4067 | CHECK(have_pending_hard_failure_); |
| 4068 | return nullptr; |
| 4069 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4070 | bool is_init = false; |
| 4071 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4072 | if (res_method) { |
| 4073 | if (!res_method->IsConstructor()) { |
| 4074 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4075 | return nullptr; |
| 4076 | } |
| 4077 | } else { |
| 4078 | // Check whether the name of the called method is "<init>" |
| 4079 | 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] | 4080 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4081 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4082 | return nullptr; |
| 4083 | } |
| 4084 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4085 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4086 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4087 | const RegType& adjusted_type = is_init |
| 4088 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 4089 | : actual_arg_type; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4090 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4091 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 4092 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 4093 | // class. It would be wrong to use this for the type check (interface type checks are |
| 4094 | // postponed to runtime). |
| 4095 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4096 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4097 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4098 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 4099 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4100 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4101 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4102 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4103 | res_method_class = ®_types_.FromDescriptor( |
| 4104 | GetClassLoader(), |
| 4105 | dex_file_->StringByTypeIdx(class_idx), |
| 4106 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4107 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4108 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4109 | Fail(adjusted_type.IsUnresolvedTypes() |
| 4110 | ? VERIFY_ERROR_NO_CLASS |
| 4111 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 4112 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 4113 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4114 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4115 | // the compiler. |
| 4116 | if (have_pending_hard_failure_) { |
| 4117 | return nullptr; |
| 4118 | } |
| 4119 | } |
| 4120 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4121 | } |
| 4122 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4123 | uint32_t arg[5]; |
| 4124 | if (!is_range) { |
| 4125 | inst->GetVarArgs(arg); |
| 4126 | } |
| 4127 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4128 | for ( ; it->HasNext(); it->Next()) { |
| 4129 | if (sig_registers >= expected_args) { |
| 4130 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4131 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4132 | return nullptr; |
| 4133 | } |
| 4134 | |
| 4135 | const char* param_descriptor = it->GetDescriptor(); |
| 4136 | |
| 4137 | if (param_descriptor == nullptr) { |
| 4138 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 4139 | "component"; |
| 4140 | return nullptr; |
| 4141 | } |
| 4142 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4143 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4144 | 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] | 4145 | arg[sig_registers]; |
| 4146 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4147 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4148 | if (!src_type.IsIntegralTypes()) { |
| 4149 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4150 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4151 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4152 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4153 | } else { |
| 4154 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4155 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4156 | // the compiler. |
| 4157 | if (have_pending_hard_failure_) { |
| 4158 | return nullptr; |
| 4159 | } |
| 4160 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4161 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4162 | // instructions not specifying register pairs by the first component, but require them |
| 4163 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4164 | // none, this will fail below. |
| 4165 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4166 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4167 | if (second_reg != get_reg + 1) { |
| 4168 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4169 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4170 | << second_reg << "."; |
| 4171 | return nullptr; |
| 4172 | } |
| 4173 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4174 | } |
| 4175 | } |
| 4176 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4177 | } |
| 4178 | if (expected_args != sig_registers) { |
| 4179 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4180 | " argument registers, method signature has " << sig_registers; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4181 | return nullptr; |
| 4182 | } |
| 4183 | return res_method; |
| 4184 | } |
| 4185 | |
| 4186 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4187 | MethodType method_type, |
| 4188 | bool is_range) { |
| 4189 | // As the method may not have been resolved, make this static check against what we expect. |
| 4190 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4191 | // 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] | 4192 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4193 | DexFileParameterIterator it(*dex_file_, |
| 4194 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4195 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4198 | bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) { |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4199 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4200 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4201 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4202 | return false; |
| 4203 | } |
| 4204 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4205 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 4206 | // Check essential arguments are provided. The dex file verifier has verified indicies of the |
| 4207 | // main values (method handle, name, method_type). |
| 4208 | if (it.Size() < 3) { |
| 4209 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4210 | << " has too few arguments: " |
| 4211 | << it.Size() << "< 3"; |
| 4212 | return false; |
| 4213 | } |
| 4214 | |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4215 | // Get and check the first argument: the method handle (index range |
| 4216 | // checked by the dex file verifier). |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4217 | uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4218 | it.Next(); |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4219 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4220 | const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx); |
| 4221 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4222 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 4223 | << " argument 0 method handle type is not InvokeStatic: " |
| 4224 | << mh.method_handle_type_; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4225 | return false; |
| 4226 | } |
| 4227 | |
| 4228 | // Skip the second argument, the name to resolve, as checked by the |
| 4229 | // dex file verifier. |
| 4230 | it.Next(); |
| 4231 | |
| 4232 | // Skip the third argument, the method type expected, as checked by |
| 4233 | // the dex file verifier. |
| 4234 | it.Next(); |
| 4235 | |
| 4236 | // Check the bootstrap method handle and remaining arguments. |
| 4237 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_); |
| 4238 | uint32_t length; |
| 4239 | const char* shorty = dex_file_->GetMethodShorty(method_id, &length); |
| 4240 | |
| 4241 | if (it.Size() < length - 1) { |
| 4242 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4243 | << " too few arguments for bootstrap method: " |
| 4244 | << it.Size() << " < " << (length - 1); |
| 4245 | return false; |
| 4246 | } |
| 4247 | |
| 4248 | // Check the return type and first 3 arguments are references |
| 4249 | // (CallSite, Lookup, String, MethodType). If they are not of the |
| 4250 | // expected types (or subtypes), it will trigger a |
| 4251 | // WrongMethodTypeException during execution. |
| 4252 | if (shorty[0] != 'L') { |
| 4253 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4254 | << " bootstrap return type is not a reference"; |
| 4255 | return false; |
| 4256 | } |
| 4257 | |
| 4258 | for (uint32_t i = 1; i < 4; ++i) { |
| 4259 | if (shorty[i] != 'L') { |
| 4260 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4261 | << " bootstrap method argument " << (i - 1) |
| 4262 | << " is not a reference"; |
| 4263 | return false; |
| 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | // Check the optional arguments. |
| 4268 | for (uint32_t i = 4; i < length; ++i, it.Next()) { |
| 4269 | bool match = false; |
| 4270 | switch (it.GetValueType()) { |
| 4271 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 4272 | case EncodedArrayValueIterator::ValueType::kByte: |
| 4273 | case EncodedArrayValueIterator::ValueType::kShort: |
| 4274 | case EncodedArrayValueIterator::ValueType::kChar: |
| 4275 | case EncodedArrayValueIterator::ValueType::kInt: |
| 4276 | // These all fit within one register and encoders do not seem |
| 4277 | // too exacting on the encoding type they use (ie using |
| 4278 | // integer for all of these). |
| 4279 | match = (strchr("ZBCSI", shorty[i]) != nullptr); |
| 4280 | break; |
| 4281 | case EncodedArrayValueIterator::ValueType::kLong: |
| 4282 | match = ('J' == shorty[i]); |
| 4283 | break; |
| 4284 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 4285 | match = ('F' == shorty[i]); |
| 4286 | break; |
| 4287 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 4288 | match = ('D' == shorty[i]); |
| 4289 | break; |
| 4290 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| 4291 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| 4292 | case EncodedArrayValueIterator::ValueType::kString: |
| 4293 | case EncodedArrayValueIterator::ValueType::kType: |
| 4294 | case EncodedArrayValueIterator::ValueType::kNull: |
| 4295 | match = ('L' == shorty[i]); |
| 4296 | break; |
| 4297 | case EncodedArrayValueIterator::ValueType::kField: |
| 4298 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 4299 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 4300 | case EncodedArrayValueIterator::ValueType::kArray: |
| 4301 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 4302 | // Unreachable based on current EncodedArrayValueIterator::Next(). |
| 4303 | UNREACHABLE(); |
| 4304 | } |
| 4305 | |
| 4306 | if (!match) { |
| 4307 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4308 | << " bootstrap method argument " << (i - 1) |
| 4309 | << " expected " << shorty[i] |
| 4310 | << " got value type: " << it.GetValueType(); |
| 4311 | return false; |
| 4312 | } |
| 4313 | } |
| 4314 | return true; |
| 4315 | } |
| 4316 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4317 | class MethodParamListDescriptorIterator { |
| 4318 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4319 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4320 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4321 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4322 | } |
| 4323 | |
| 4324 | bool HasNext() { |
| 4325 | return pos_ < params_size_; |
| 4326 | } |
| 4327 | |
| 4328 | void Next() { |
| 4329 | ++pos_; |
| 4330 | } |
| 4331 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4332 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4333 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4334 | } |
| 4335 | |
| 4336 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4337 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4338 | size_t pos_; |
| 4339 | const DexFile::TypeList* params_; |
| 4340 | const size_t params_size_; |
| 4341 | }; |
| 4342 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4343 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4344 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4345 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4346 | // we're making. |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4347 | const uint32_t method_idx = inst->VRegB(); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4348 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4349 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4350 | // Check what we can statically. |
| 4351 | if (!have_pending_hard_failure_) { |
| 4352 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4353 | } |
| 4354 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4355 | } |
| 4356 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4357 | // 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] | 4358 | // 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] | 4359 | if (method_type == METHOD_SUPER) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4360 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4361 | const RegType& reference_type = reg_types_.FromDescriptor( |
| 4362 | GetClassLoader(), |
| 4363 | dex_file_->StringByTypeIdx(class_idx), |
| 4364 | false); |
| 4365 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4366 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4367 | return nullptr; |
| 4368 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4369 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4370 | // TODO Can we verify anything else. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 4371 | if (class_idx == class_def_.class_idx_) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4372 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface"; |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4373 | return nullptr; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4374 | } |
| 4375 | // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS |
| 4376 | // does. |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4377 | if (!GetDeclaringClass().HasClass()) { |
| 4378 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4379 | << "interface invoke-super"; |
| 4380 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4381 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4382 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4383 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4384 | << " in method " |
| 4385 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4386 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4387 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4388 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4389 | } |
| 4390 | } else { |
| 4391 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4392 | if (super.IsUnresolvedTypes()) { |
| 4393 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4394 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4395 | << " to super " << res_method->PrettyMethod(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4396 | return nullptr; |
| 4397 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4398 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4399 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4400 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4401 | << dex_file_->PrettyMethod(dex_method_idx_) |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4402 | << " to super " << super |
| 4403 | << "." << res_method->GetName() |
| 4404 | << res_method->GetSignature(); |
| 4405 | return nullptr; |
| 4406 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4407 | } |
| 4408 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4409 | |
Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4410 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4411 | // Process the signature of the calling site that is invoking the method handle. |
| 4412 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH())); |
| 4413 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4414 | } else { |
| 4415 | // Process the target method's signature. |
| 4416 | MethodParamListDescriptorIterator it(res_method); |
| 4417 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
| 4422 | mirror::Class* klass = method->GetDeclaringClass(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4423 | const char* method_name = method->GetName(); |
| 4424 | |
| 4425 | const char* expected_return_descriptor; |
| 4426 | if (klass == mirror::MethodHandle::StaticClass()) { |
| 4427 | expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name); |
| 4428 | } else if (klass == mirror::VarHandle::StaticClass()) { |
| 4429 | expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name); |
| 4430 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4431 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4432 | << "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor(); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4433 | return false; |
| 4434 | } |
| 4435 | |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4436 | if (expected_return_descriptor == nullptr) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4437 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4438 | << "Signature polymorphic method name invalid: " << method_name; |
| 4439 | return false; |
| 4440 | } |
| 4441 | |
| 4442 | const DexFile::TypeList* types = method->GetParameterTypeList(); |
| 4443 | if (types->Size() != 1) { |
| 4444 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4445 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4446 | return false; |
| 4447 | } |
| 4448 | |
| 4449 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4450 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4451 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4452 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4453 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4454 | return false; |
| 4455 | } |
| 4456 | |
| 4457 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4458 | if (strcmp(return_descriptor, expected_return_descriptor) != 0) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4459 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4460 | << "Signature polymorphic method has unexpected return type: " << return_descriptor |
| 4461 | << " != " << expected_return_descriptor; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4462 | return false; |
| 4463 | } |
| 4464 | |
| 4465 | return true; |
| 4466 | } |
| 4467 | |
| 4468 | bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
| 4469 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4470 | if (this_type.IsZeroOrNull()) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4471 | /* null pointer always passes (and always fails at run time) */ |
| 4472 | return true; |
| 4473 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4474 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4475 | << "invoke-polymorphic receiver is not a reference: " |
| 4476 | << this_type; |
| 4477 | return false; |
| 4478 | } else if (this_type.IsUninitializedReference()) { |
| 4479 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4480 | << "invoke-polymorphic receiver is uninitialized: " |
| 4481 | << this_type; |
| 4482 | return false; |
| 4483 | } else if (!this_type.HasClass()) { |
| 4484 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4485 | << "invoke-polymorphic receiver has no class: " |
| 4486 | << this_type; |
| 4487 | return false; |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4488 | } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass()) && |
| 4489 | !this_type.GetClass()->IsSubClass(mirror::VarHandle::StaticClass())) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4490 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Orion Hodson | fe92d12 | 2018-01-02 10:45:17 +0000 | [diff] [blame^] | 4491 | << "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: " |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4492 | << this_type; |
| 4493 | return false; |
| 4494 | } |
| 4495 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4498 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 4499 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4500 | if (is_range) { |
| 4501 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 4502 | } else { |
| 4503 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 4504 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4505 | const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4506 | if (!actual_arg_type.HasClass()) { |
| 4507 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 4508 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4509 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4510 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4511 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4512 | if (klass->IsInterface()) { |
| 4513 | // Derive Object.class from Class.class.getSuperclass(). |
| 4514 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4515 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4516 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4517 | return nullptr; |
| 4518 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4519 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4520 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4521 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4522 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4523 | if (!dispatch_class->HasVTable()) { |
| 4524 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 4525 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4526 | return nullptr; |
| 4527 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4528 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4529 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 4530 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4531 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 4532 | FailOrAbort(this, allow_failure, |
| 4533 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 4534 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4535 | return nullptr; |
| 4536 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4537 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4538 | if (self_->IsExceptionPending()) { |
| 4539 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 4540 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4541 | return nullptr; |
| 4542 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4543 | return res_method; |
| 4544 | } |
| 4545 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4546 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4547 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4548 | << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_; |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4549 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4550 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4551 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4552 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4553 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4554 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4555 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 4556 | work_insn_idx_)) { |
| 4557 | return nullptr; |
| 4558 | } |
| 4559 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 4560 | work_insn_idx_)) { |
| 4561 | return nullptr; |
| 4562 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4563 | |
| 4564 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4565 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4566 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4567 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4568 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4569 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4570 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4571 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4572 | /* caught by static verifier */ |
| 4573 | DCHECK(is_range || expected_args <= 5); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4574 | if (expected_args > code_item_accessor_.OutsSize()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4575 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 4576 | << ") exceeds outsSize (" << code_item_accessor_.OutsSize() << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4577 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | /* |
| 4581 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4582 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4583 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4584 | */ |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4585 | // Note: given an uninitialized type, this should always fail. Constructors aren't virtual. |
| 4586 | if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4587 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4588 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4589 | } |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4590 | if (!actual_arg_type.IsZeroOrNull()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4591 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4592 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4593 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4594 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4595 | if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4596 | Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never |
| 4597 | ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this. |
| 4598 | : actual_arg_type.IsUnresolvedTypes() |
| 4599 | ? VERIFY_ERROR_NO_CLASS |
| 4600 | : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4601 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4602 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | /* |
| 4606 | * Process the target method's signature. This signature may or may not |
| 4607 | * have been verified, so we can't assume it's properly formed. |
| 4608 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4609 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4610 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4611 | uint32_t arg[5]; |
| 4612 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4613 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4614 | } |
| 4615 | size_t actual_args = 1; |
| 4616 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 4617 | if (actual_args >= expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4618 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" |
| 4619 | << res_method->PrettyMethod() |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4620 | << "'. Expected " << expected_args |
| 4621 | << " arguments, processing argument " << actual_args |
| 4622 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4623 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4624 | } |
| 4625 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4626 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4627 | if (descriptor == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4628 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4629 | << res_method->PrettyMethod() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4630 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4631 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4632 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4633 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4634 | 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] | 4635 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4636 | return res_method; |
| 4637 | } |
| 4638 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 4639 | } |
| 4640 | if (actual_args != expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4641 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4642 | << res_method->PrettyMethod() << " expected " |
| 4643 | << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4644 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4645 | } else { |
| 4646 | return res_method; |
| 4647 | } |
| 4648 | } |
| 4649 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 4650 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4651 | dex::TypeIndex type_idx; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4652 | if (!is_filled) { |
| 4653 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4654 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4655 | } else if (!is_range) { |
| 4656 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4657 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4658 | } else { |
| 4659 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4660 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4661 | } |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4662 | const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4663 | if (res_type.IsConflict()) { // bad class |
| 4664 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4665 | } else { |
| 4666 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4667 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4668 | 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] | 4669 | } else if (!is_filled) { |
| 4670 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4671 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4672 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4673 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4674 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4675 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4676 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4677 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4678 | // 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] | 4679 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4680 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4681 | uint32_t arg[5]; |
| 4682 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4683 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4684 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4685 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4686 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4687 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4688 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4689 | return; |
| 4690 | } |
| 4691 | } |
| 4692 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4693 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4694 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4695 | } |
| 4696 | } |
| 4697 | } |
| 4698 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4699 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4700 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4701 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4702 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4703 | 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] | 4704 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4705 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4706 | if (array_type.IsZeroOrNull()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4707 | // 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] | 4708 | // instruction type. |
| 4709 | if (!is_primitive) { |
| 4710 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null()); |
| 4711 | } else if (insn_type.IsInteger()) { |
| 4712 | // Pick a non-zero constant (to distinguish with null) that can fit in any primitive. |
| 4713 | // We cannot use 'insn_type' as it could be a float array or an int array. |
| 4714 | work_line_->SetRegisterType<LockOp::kClear>( |
| 4715 | this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_)); |
| 4716 | } else if (insn_type.IsCategory1Types()) { |
| 4717 | // Category 1 |
| 4718 | // The 'insn_type' is exactly the type we need. |
| 4719 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4720 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4721 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4722 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4723 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4724 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4725 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4726 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4727 | 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] | 4728 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4729 | // Unresolved array types must be reference array types. |
| 4730 | if (is_primitive) { |
| 4731 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4732 | << " source for category 1 aget"; |
| 4733 | } else { |
| 4734 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4735 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4736 | // Approximate with java.lang.Object[]. |
| 4737 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4738 | inst->VRegA_23x(), |
| 4739 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4740 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4741 | } else { |
| 4742 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4743 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4744 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4745 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4746 | << " source for aget-object"; |
| 4747 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4748 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4749 | << " source for category 1 aget"; |
| 4750 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4751 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4752 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4753 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4754 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4755 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4756 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4757 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4758 | // doubles. |
| 4759 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4760 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4761 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4762 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4763 | component_type.HighHalf(®_types_)); |
| 4764 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | } |
| 4768 | } |
| 4769 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4770 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4771 | const uint32_t vregA) { |
| 4772 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4773 | bool instruction_compatible; |
| 4774 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4775 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4776 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4777 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4778 | value_compatible = value_type.IsIntegralTypes(); |
| 4779 | } else if (target_type.IsFloat()) { |
| 4780 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4781 | value_compatible = value_type.IsFloatTypes(); |
| 4782 | } else if (target_type.IsLong()) { |
| 4783 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4784 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4785 | // as target_type depends on the resolved type of the field. |
| 4786 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4787 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4788 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4789 | } else { |
| 4790 | value_compatible = false; |
| 4791 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4792 | } else if (target_type.IsDouble()) { |
| 4793 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4794 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4795 | // as target_type depends on the resolved type of the field. |
| 4796 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4797 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4798 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4799 | } else { |
| 4800 | value_compatible = false; |
| 4801 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4802 | } else { |
| 4803 | instruction_compatible = false; // reference with primitive store |
| 4804 | value_compatible = false; // unused |
| 4805 | } |
| 4806 | if (!instruction_compatible) { |
| 4807 | // This is a global failure rather than a class change failure as the instructions and |
| 4808 | // the descriptors for the type should have been consistent within the same file at |
| 4809 | // compile time. |
| 4810 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4811 | << "' but expected type '" << target_type << "'"; |
| 4812 | return; |
| 4813 | } |
| 4814 | if (!value_compatible) { |
| 4815 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4816 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4817 | return; |
| 4818 | } |
| 4819 | } |
| 4820 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4821 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4822 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4823 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4824 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4825 | 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] | 4826 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4827 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4828 | if (array_type.IsZeroOrNull()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4829 | // Null array type; this code path will fail at runtime. |
| 4830 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4831 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4832 | // and fits multiple register types. |
| 4833 | const RegType* modified_reg_type = &insn_type; |
| 4834 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4835 | (modified_reg_type == ®_types_.LongLo())) { |
| 4836 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4837 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4838 | if (modified_reg_type == ®_types_.Integer()) { |
| 4839 | if (&value_type == ®_types_.Float()) { |
| 4840 | modified_reg_type = &value_type; |
| 4841 | } |
| 4842 | } else { |
| 4843 | if (&value_type == ®_types_.DoubleLo()) { |
| 4844 | modified_reg_type = &value_type; |
| 4845 | } |
| 4846 | } |
| 4847 | } |
| 4848 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4849 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4850 | 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] | 4851 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4852 | // Unresolved array types must be reference array types. |
| 4853 | if (is_primitive) { |
| 4854 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4855 | << "' but unresolved type '" << array_type << "'"; |
| 4856 | } else { |
| 4857 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4858 | << " because of missing class"; |
| 4859 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4860 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4861 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4862 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4863 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4864 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4865 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4866 | if (!component_type.IsReferenceTypes()) { |
| 4867 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4868 | << " source for aput-object"; |
| 4869 | } else { |
| 4870 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4871 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4872 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4873 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4874 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4875 | } |
| 4876 | } |
| 4877 | } |
| 4878 | } |
| 4879 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4880 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4881 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4882 | // Check access to class |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4883 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4884 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4885 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4886 | field_idx, dex_file_->GetFieldName(field_id), |
| 4887 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4888 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4889 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4890 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4891 | // Accessibility checks depend on resolved fields. |
| 4892 | DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty()); |
| 4893 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4894 | 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] | 4895 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4896 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4897 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4898 | |
| 4899 | // Record result of the field resolution attempt. |
| 4900 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4901 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4902 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4903 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4904 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4905 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4906 | DCHECK(self_->IsExceptionPending()); |
| 4907 | self_->ClearException(); |
| 4908 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4909 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4910 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4911 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4912 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4913 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4914 | } else if (!field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4915 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4916 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4917 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4918 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4919 | } |
| 4920 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4921 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4922 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4923 | // Check access to class. |
Andreas Gampe | 98be1a9 | 2017-08-28 08:25:45 -0700 | [diff] [blame] | 4924 | const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4925 | if (klass_type.IsConflict()) { |
| 4926 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4927 | field_idx, dex_file_->GetFieldName(field_id), |
| 4928 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4929 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4930 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4931 | if (klass_type.IsUnresolvedTypes()) { |
Andreas Gampe | 629be51 | 2017-08-25 17:09:32 -0700 | [diff] [blame] | 4932 | // Accessibility checks depend on resolved fields. |
| 4933 | DCHECK(klass_type.Equals(GetDeclaringClass()) || !failures_.empty()); |
| 4934 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4935 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4936 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4937 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4938 | ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4939 | |
| 4940 | // Record result of the field resolution attempt. |
| 4941 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4942 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4943 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4944 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4945 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4946 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4947 | DCHECK(self_->IsExceptionPending()); |
| 4948 | self_->ClearException(); |
| 4949 | return nullptr; |
Andreas Gampe | eed3a5d | 2017-11-29 14:58:34 -0800 | [diff] [blame] | 4950 | } else if (obj_type.IsZeroOrNull()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4951 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4952 | // Fall through into a few last soft failure checks below. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4953 | } else if (!obj_type.IsReferenceTypes()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4954 | // Trying to read a field from something that isn't a reference. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4955 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4956 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4957 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4958 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4959 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4960 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4961 | const RegType& field_klass = |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4962 | FromClass(klass->GetDescriptor(&temp), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 4963 | klass.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4964 | klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4965 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4966 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4967 | // the field is declared in this class. |
| 4968 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4969 | // appear in constructors. |
| 4970 | if (!obj_type.IsUninitializedThisReference() || |
| 4971 | !IsConstructor() || |
| 4972 | !field_klass.Equals(GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4973 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4974 | << " of a not fully initialized object within the context" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4975 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4976 | return nullptr; |
| 4977 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4978 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4979 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4980 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4981 | // 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] | 4982 | VerifyError type; |
| 4983 | bool is_aot = Runtime::Current()->IsAotCompiler(); |
| 4984 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4985 | // Compiler & unresolved types involved, retry at runtime. |
| 4986 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4987 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4988 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4989 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4990 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4991 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4992 | Fail(type) << "cannot access instance field " << field->PrettyField() |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4993 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4994 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4995 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4996 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4997 | |
| 4998 | // Few last soft failure checks. |
| 4999 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 5000 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5001 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5002 | << " from " << GetDeclaringClass(); |
| 5003 | return nullptr; |
| 5004 | } else if (field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5005 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5006 | << " to not be static"; |
| 5007 | return nullptr; |
| 5008 | } |
| 5009 | |
| 5010 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5013 | template <MethodVerifier::FieldAccessType kAccType> |
| 5014 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 5015 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 5016 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5017 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5018 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 5019 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5020 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5021 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 5022 | |
| 5023 | // One is not allowed to access fields on uninitialized references, except to write to |
| 5024 | // fields in the constructor (before calling another constructor). |
| 5025 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 5026 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 5027 | // checks at the same time that we're verifying a constructor). |
| 5028 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 5029 | object_type.IsUninitializedThisReference(); |
| 5030 | const RegType& adjusted_type = should_adjust |
| 5031 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 5032 | : object_type; |
| 5033 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5034 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 5035 | return; |
| 5036 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5037 | if (should_adjust) { |
| 5038 | if (field == nullptr) { |
| 5039 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 5040 | << "to the superclass being initialized in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5041 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5042 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 5043 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5044 | << field->PrettyField() << " of a not fully initialized " |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5045 | << "object within the context of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5046 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 5047 | return; |
| 5048 | } |
| 5049 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 5050 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5051 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5052 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5053 | if (kAccType == FieldAccessType::kAccPut) { |
| 5054 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5055 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5056 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 5057 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5058 | } |
| 5059 | } |
| 5060 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 5061 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 5062 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5063 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 5064 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 5065 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5066 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 5067 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5068 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5069 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5070 | } |
Andreas Gampe | 7da4c40 | 2017-08-25 11:30:48 -0700 | [diff] [blame] | 5071 | } else { |
| 5072 | // If we don't have the field (it seems we failed resolution) and this is a PUT, we need to |
| 5073 | // redo verification at runtime as the field may be final, unless the field id shows it's in |
| 5074 | // the same class. |
| 5075 | // |
| 5076 | // For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an |
| 5077 | // ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class |
| 5078 | // to the access-checks interpreter. |
| 5079 | // |
| 5080 | // Note: see b/34966607. This and above may be changed in the future. |
| 5081 | if (kAccType == FieldAccessType::kAccPut) { |
| 5082 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 5083 | const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
| 5084 | const RegType* field_class_type = ®_types_.FromDescriptor(GetClassLoader(), |
| 5085 | field_class_descriptor, |
| 5086 | false); |
| 5087 | if (!field_class_type->Equals(GetDeclaringClass())) { |
| 5088 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of " |
| 5089 | << field_class_descriptor |
| 5090 | << "." |
| 5091 | << dex_file_->GetFieldName(field_id) |
| 5092 | << " from other class " |
| 5093 | << GetDeclaringClass(); |
| 5094 | } |
| 5095 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5096 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5097 | if (field_type == nullptr) { |
| 5098 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 5099 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5100 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5101 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 5102 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 5103 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5104 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5105 | "Unexpected third access type"); |
| 5106 | if (kAccType == FieldAccessType::kAccPut) { |
| 5107 | // sput or iput. |
| 5108 | if (is_primitive) { |
| 5109 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5110 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5111 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5112 | // If the field type is not a reference, this is a global failure rather than |
| 5113 | // a class change failure as the instructions and the descriptors for the type |
| 5114 | // should have been consistent within the same file at compile time. |
| 5115 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5116 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5117 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5118 | << " to be compatible with type '" << insn_type |
| 5119 | << "' but found type '" << *field_type |
| 5120 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5121 | return; |
| 5122 | } |
| 5123 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5124 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5125 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5126 | // sget or iget. |
| 5127 | if (is_primitive) { |
| 5128 | if (field_type->Equals(insn_type) || |
| 5129 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 5130 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 5131 | // expected that read is of the correct primitive type or that int reads are reading |
| 5132 | // floats or long reads are reading doubles |
| 5133 | } else { |
| 5134 | // This is a global failure rather than a class change failure as the instructions and |
| 5135 | // the descriptors for the type should have been consistent within the same file at |
| 5136 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5137 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5138 | << " to be of type '" << insn_type |
| 5139 | << "' but found type '" << *field_type << "' in get"; |
| 5140 | return; |
| 5141 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 5142 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5143 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5144 | // If the field type is not a reference, this is a global failure rather than |
| 5145 | // a class change failure as the instructions and the descriptors for the type |
| 5146 | // should have been consistent within the same file at compile time. |
| 5147 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5148 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5149 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5150 | << " to be compatible with type '" << insn_type |
| 5151 | << "' but found type '" << *field_type |
| 5152 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5153 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5154 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5155 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5156 | return; |
| 5157 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5158 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5159 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5160 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5161 | } else { |
| 5162 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 5163 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5164 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5165 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 5169 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5170 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5171 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 5172 | if (!object_type.HasClass()) { |
| 5173 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 5174 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5175 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5176 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5177 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5178 | if (f == nullptr) { |
| 5179 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5180 | << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'"; |
Andreas Gampe | 8ddb7f5 | 2017-12-04 10:14:08 -0800 | [diff] [blame] | 5181 | } else { |
| 5182 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5183 | } |
| 5184 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5185 | } |
| 5186 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5187 | template <MethodVerifier::FieldAccessType kAccType> |
| 5188 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 5189 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 5190 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5191 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5192 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5193 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5194 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 5195 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5196 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5197 | |
| 5198 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 5199 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5200 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5201 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5202 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5203 | return; |
| 5204 | } |
| 5205 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5206 | |
| 5207 | // Get the field type. |
| 5208 | const RegType* field_type; |
| 5209 | { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 5210 | ObjPtr<mirror::Class> field_type_class = |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 5211 | can_load_classes_ ? field->ResolveType() : field->LookupResolvedType(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5212 | |
| 5213 | if (field_type_class != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5214 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 5215 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5216 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5217 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5218 | Thread* self = Thread::Current(); |
| 5219 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 5220 | self->ClearException(); |
| 5221 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5222 | field->GetTypeDescriptor(), |
| 5223 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5224 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5225 | if (field_type == nullptr) { |
| 5226 | 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] | 5227 | return; |
| 5228 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | const uint32_t vregA = inst->VRegA_22c(); |
| 5232 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5233 | "Unexpected third access type"); |
| 5234 | if (kAccType == FieldAccessType::kAccPut) { |
| 5235 | if (is_primitive) { |
| 5236 | // Primitive field assignability rules are weaker than regular assignability rules |
| 5237 | bool instruction_compatible; |
| 5238 | bool value_compatible; |
| 5239 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 5240 | if (field_type->IsIntegralTypes()) { |
| 5241 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 5242 | value_compatible = value_type.IsIntegralTypes(); |
| 5243 | } else if (field_type->IsFloat()) { |
| 5244 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 5245 | value_compatible = value_type.IsFloatTypes(); |
| 5246 | } else if (field_type->IsLong()) { |
| 5247 | instruction_compatible = insn_type.IsLong(); |
| 5248 | value_compatible = value_type.IsLongTypes(); |
| 5249 | } else if (field_type->IsDouble()) { |
| 5250 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 5251 | value_compatible = value_type.IsDoubleTypes(); |
| 5252 | } else { |
| 5253 | instruction_compatible = false; // reference field with primitive store |
| 5254 | value_compatible = false; // unused |
| 5255 | } |
| 5256 | if (!instruction_compatible) { |
| 5257 | // This is a global failure rather than a class change failure as the instructions and |
| 5258 | // the descriptors for the type should have been consistent within the same file at |
| 5259 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5260 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5261 | << " to be of type '" << insn_type |
| 5262 | << "' but found type '" << *field_type |
| 5263 | << "' in put"; |
| 5264 | return; |
| 5265 | } |
| 5266 | if (!value_compatible) { |
| 5267 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 5268 | << " of type " << value_type |
| 5269 | << " but expected " << *field_type |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5270 | << " for store to " << ArtField::PrettyField(field) << " in put"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5271 | return; |
| 5272 | } |
| 5273 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5274 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5275 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5276 | << " to be compatible with type '" << insn_type |
| 5277 | << "' but found type '" << *field_type |
| 5278 | << "' in put-object"; |
| 5279 | return; |
| 5280 | } |
| 5281 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 5282 | } |
| 5283 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5284 | if (is_primitive) { |
| 5285 | if (field_type->Equals(insn_type) || |
| 5286 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 5287 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 5288 | // expected that read is of the correct primitive type or that int reads are reading |
| 5289 | // floats or long reads are reading doubles |
| 5290 | } else { |
| 5291 | // This is a global failure rather than a class change failure as the instructions and |
| 5292 | // the descriptors for the type should have been consistent within the same file at |
| 5293 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5294 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5295 | << " to be of type '" << insn_type |
| 5296 | << "' but found type '" << *field_type << "' in Get"; |
| 5297 | return; |
| 5298 | } |
| 5299 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5300 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5301 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5302 | << " to be compatible with type '" << insn_type |
| 5303 | << "' but found type '" << *field_type |
| 5304 | << "' in get-object"; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5305 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5306 | return; |
| 5307 | } |
| 5308 | } |
| 5309 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5310 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5311 | } else { |
| 5312 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5313 | } |
| 5314 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5315 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5316 | } |
| 5317 | } |
| 5318 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5319 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5320 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 5321 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5322 | return false; |
| 5323 | } |
| 5324 | return true; |
| 5325 | } |
| 5326 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 5327 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 5328 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 5329 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 5330 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 5331 | return false; |
| 5332 | } |
| 5333 | return true; |
| 5334 | } |
| 5335 | |
| 5336 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 5337 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 5338 | } |
| 5339 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5340 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 5341 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5342 | bool changed = true; |
| 5343 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5344 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5345 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5346 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 5347 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 5348 | * 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] | 5349 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5350 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5351 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 5352 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5353 | merge_line->VerifyMonitorStackEmpty(this); |
| 5354 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5355 | // For returns we only care about the operand to the return, all other registers are dead. |
| 5356 | // 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] | 5357 | const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5358 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5359 | // Directly bail if a hard failure was found. |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 5360 | if (have_pending_hard_failure_) { |
| 5361 | return false; |
| 5362 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5363 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5364 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 5365 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 5366 | if (UNLIKELY(VLOG_IS_ON(verifier_debug))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5367 | copy.reset(RegisterLine::Create(target_line->NumRegs(), this)); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5368 | copy->CopyFromLine(target_line); |
| 5369 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5370 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5371 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5372 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5373 | } |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 5374 | if (UNLIKELY(VLOG_IS_ON(verifier_debug)) && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 5375 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 5376 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5377 | << copy->Dump(this) << " MERGE\n" |
| 5378 | << merge_line->Dump(this) << " ==\n" |
| 5379 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5380 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5381 | if (update_merge_line && changed) { |
| 5382 | merge_line->CopyFromLine(target_line); |
| 5383 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5384 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5385 | if (changed) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5386 | GetInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5387 | } |
| 5388 | return true; |
| 5389 | } |
| 5390 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5391 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5392 | return &GetInstructionFlags(work_insn_idx_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5393 | } |
| 5394 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5395 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5396 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5397 | if (mirror_method_ != nullptr) { |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5398 | ObjPtr<mirror::Class> return_type_class = can_load_classes_ |
| 5399 | ? mirror_method_->ResolveReturnType() |
| 5400 | : mirror_method_->LookupResolvedReturnType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5401 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5402 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5403 | return_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5404 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5405 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5406 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5407 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | if (return_type_ == nullptr) { |
| 5411 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 5412 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5413 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5414 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5415 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5416 | } |
| 5417 | } |
| 5418 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5421 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5422 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5423 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 5424 | const char* descriptor |
| 5425 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5426 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5427 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5428 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5429 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5430 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5431 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5432 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5433 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5436 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 5437 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 5438 | 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] | 5439 | std::vector<int32_t> result; |
| 5440 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5441 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5442 | if (type.IsConstant()) { |
| 5443 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5444 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5445 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5446 | } else if (type.IsConstantLo()) { |
| 5447 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5448 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5449 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5450 | } else if (type.IsConstantHi()) { |
| 5451 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5452 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5453 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5454 | } else if (type.IsIntegralTypes()) { |
| 5455 | result.push_back(kIntVReg); |
| 5456 | result.push_back(0); |
| 5457 | } else if (type.IsFloat()) { |
| 5458 | result.push_back(kFloatVReg); |
| 5459 | result.push_back(0); |
| 5460 | } else if (type.IsLong()) { |
| 5461 | result.push_back(kLongLoVReg); |
| 5462 | result.push_back(0); |
| 5463 | result.push_back(kLongHiVReg); |
| 5464 | result.push_back(0); |
| 5465 | ++i; |
| 5466 | } else if (type.IsDouble()) { |
| 5467 | result.push_back(kDoubleLoVReg); |
| 5468 | result.push_back(0); |
| 5469 | result.push_back(kDoubleHiVReg); |
| 5470 | result.push_back(0); |
| 5471 | ++i; |
| 5472 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 5473 | result.push_back(kUndefined); |
| 5474 | result.push_back(0); |
| 5475 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5476 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5477 | result.push_back(kReferenceVReg); |
| 5478 | result.push_back(0); |
| 5479 | } |
| 5480 | } |
| 5481 | return result; |
| 5482 | } |
| 5483 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5484 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5485 | if (precise) { |
| 5486 | // Precise constant type. |
| 5487 | return reg_types_.FromCat1Const(value, true); |
| 5488 | } else { |
| 5489 | // Imprecise constant type. |
| 5490 | if (value < -32768) { |
| 5491 | return reg_types_.IntConstant(); |
| 5492 | } else if (value < -128) { |
| 5493 | return reg_types_.ShortConstant(); |
| 5494 | } else if (value < 0) { |
| 5495 | return reg_types_.ByteConstant(); |
| 5496 | } else if (value == 0) { |
| 5497 | return reg_types_.Zero(); |
| 5498 | } else if (value == 1) { |
| 5499 | return reg_types_.One(); |
| 5500 | } else if (value < 128) { |
| 5501 | return reg_types_.PosByteConstant(); |
| 5502 | } else if (value < 32768) { |
| 5503 | return reg_types_.PosShortConstant(); |
| 5504 | } else if (value < 65536) { |
| 5505 | return reg_types_.CharConstant(); |
| 5506 | } else { |
| 5507 | return reg_types_.IntConstant(); |
| 5508 | } |
| 5509 | } |
| 5510 | } |
| 5511 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5512 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5513 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5514 | } |
| 5515 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5516 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5517 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5518 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5519 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5520 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5521 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5522 | } |
| 5523 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5524 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5525 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5526 | } |
| 5527 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5528 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 5529 | mirror::Class* klass, |
| 5530 | bool precise) { |
| 5531 | DCHECK(klass != nullptr); |
| 5532 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 5533 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 5534 | << "non-instantiable klass " << descriptor; |
| 5535 | precise = false; |
| 5536 | } |
| 5537 | return reg_types_.FromClass(descriptor, klass, precise); |
| 5538 | } |
| 5539 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5540 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5541 | } // namespace art |