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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 21 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 23 | #include "base/enums.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 25 | #include "base/mutex-inl.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 27 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 28 | #include "base/time_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 29 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 30 | #include "compiler_callbacks.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 31 | #include "dex_file-inl.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 32 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 33 | #include "dex_instruction_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 34 | #include "dex_instruction_visitor.h" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 35 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 37 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 38 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 39 | #include "leb128.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 40 | #include "method_resolution_kind.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | #include "mirror/class.h" |
| 42 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 43 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 44 | #include "mirror/object-inl.h" |
| 45 | #include "mirror/object_array-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 46 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 47 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 48 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 49 | #include "scoped_thread_state_change-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 50 | #include "utils.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 51 | #include "verifier_deps.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 52 | #include "handle_scope-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 53 | |
| 54 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 55 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 56 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 57 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 58 | static constexpr bool kDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 59 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 60 | |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 61 | // On VLOG(verifier), should we dump the whole state when we run into a hard failure? |
| 62 | static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true; |
| 63 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 64 | // We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make |
| 65 | // sure we only print this once. |
| 66 | static bool gPrintedDxMonitorText = false; |
| 67 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 68 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena) |
| 69 | : register_lines_(arena.Adapter(kArenaAllocVerifier)) {} |
| 70 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 71 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 72 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 73 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 74 | DCHECK_GT(insns_size, 0U); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 75 | register_lines_.resize(insns_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 76 | for (uint32_t i = 0; i < insns_size; i++) { |
| 77 | bool interesting = false; |
| 78 | switch (mode) { |
| 79 | case kTrackRegsAll: |
| 80 | interesting = flags[i].IsOpcode(); |
| 81 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 82 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 83 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 84 | break; |
| 85 | case kTrackRegsBranches: |
| 86 | interesting = flags[i].IsBranchTarget(); |
| 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | if (interesting) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 92 | register_lines_[i].reset(RegisterLine::Create(registers_size, verifier)); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 97 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 99 | // Note: returns true on failure. |
| 100 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 101 | const char* error_msg, uint32_t work_insn_idx) { |
| 102 | if (kIsDebugBuild) { |
| 103 | // In a debug build, abort if the error condition is wrong. |
| 104 | DCHECK(condition) << error_msg << work_insn_idx; |
| 105 | } else { |
| 106 | // In a non-debug build, just fail the class. |
| 107 | if (!condition) { |
| 108 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 116 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 117 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 118 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 119 | reg_line->CheckConstructorReturn(verifier); |
| 120 | } |
| 121 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 122 | } |
| 123 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 124 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 125 | mirror::Class* klass, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 126 | CompilerCallbacks* callbacks, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 127 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 128 | HardFailLogMode log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 129 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 130 | if (klass->IsVerified()) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 131 | return kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 132 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 133 | bool early_failure = false; |
| 134 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 135 | const DexFile& dex_file = klass->GetDexFile(); |
| 136 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 137 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 138 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 139 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 140 | early_failure = true; |
| 141 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 142 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 143 | early_failure = true; |
| 144 | failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 145 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 146 | early_failure = true; |
| 147 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 148 | } |
| 149 | if (early_failure) { |
| 150 | *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 151 | if (callbacks != nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 152 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 153 | callbacks->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 154 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 155 | return kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 156 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 157 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 158 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 159 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 160 | return VerifyClass(self, |
| 161 | &dex_file, |
| 162 | dex_cache, |
| 163 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 164 | *class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 165 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 166 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 167 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 168 | error); |
| 169 | } |
| 170 | |
| 171 | template <bool kDirect> |
| 172 | static bool HasNextMethod(ClassDataItemIterator* it) { |
| 173 | return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod(); |
| 174 | } |
| 175 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 176 | static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1, |
| 177 | MethodVerifier::FailureKind fk2) { |
| 178 | static_assert(MethodVerifier::FailureKind::kNoFailure < |
| 179 | MethodVerifier::FailureKind::kSoftFailure |
| 180 | && MethodVerifier::FailureKind::kSoftFailure < |
| 181 | MethodVerifier::FailureKind::kHardFailure, |
| 182 | "Unexpected FailureKind order"); |
| 183 | return std::max(fk1, fk2); |
| 184 | } |
| 185 | |
| 186 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 187 | kind = FailureKindMax(kind, fd.kind); |
| 188 | types |= fd.types; |
| 189 | } |
| 190 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 191 | template <bool kDirect> |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 192 | MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self, |
| 193 | ClassLinker* linker, |
| 194 | const DexFile* dex_file, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 195 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 196 | ClassDataItemIterator* it, |
| 197 | Handle<mirror::DexCache> dex_cache, |
| 198 | Handle<mirror::ClassLoader> class_loader, |
| 199 | CompilerCallbacks* callbacks, |
| 200 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 201 | HardFailLogMode log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 202 | bool need_precise_constants, |
| 203 | std::string* error_string) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 204 | DCHECK(it != nullptr); |
| 205 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 206 | MethodVerifier::FailureData failure_data; |
| 207 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 208 | int64_t previous_method_idx = -1; |
| 209 | while (HasNextMethod<kDirect>(it)) { |
| 210 | self->AllowThreadSuspension(); |
| 211 | uint32_t method_idx = it->GetMemberIndex(); |
| 212 | if (method_idx == previous_method_idx) { |
| 213 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 214 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 215 | it->Next(); |
| 216 | continue; |
| 217 | } |
| 218 | previous_method_idx = method_idx; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 219 | InvokeType type = it->GetMethodInvokeType(class_def); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 220 | ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 221 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
| 222 | if (method == nullptr) { |
| 223 | DCHECK(self->IsExceptionPending()); |
| 224 | // We couldn't resolve the method, but continue regardless. |
| 225 | self->ClearException(); |
| 226 | } else { |
| 227 | DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type; |
| 228 | } |
| 229 | StackHandleScope<1> hs(self); |
| 230 | std::string hard_failure_msg; |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 231 | MethodVerifier::FailureData result = VerifyMethod(self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 232 | method_idx, |
| 233 | dex_file, |
| 234 | dex_cache, |
| 235 | class_loader, |
| 236 | class_def, |
| 237 | it->GetMethodCodeItem(), |
| 238 | method, |
| 239 | it->GetMethodAccessFlags(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 240 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 241 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 242 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 243 | need_precise_constants, |
| 244 | &hard_failure_msg); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 245 | if (result.kind == kHardFailure) { |
| 246 | if (failure_data.kind == kHardFailure) { |
| 247 | // If we logged an error before, we need a newline. |
| 248 | *error_string += "\n"; |
| 249 | } else { |
| 250 | // If we didn't log a hard failure before, print the header of the message. |
| 251 | *error_string += "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 252 | *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 253 | *error_string += ":"; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 254 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 255 | *error_string += " "; |
| 256 | *error_string += hard_failure_msg; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 257 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 258 | failure_data.Merge(result); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 259 | it->Next(); |
| 260 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 261 | |
| 262 | return failure_data; |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 265 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 266 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 267 | Handle<mirror::DexCache> dex_cache, |
| 268 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 269 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 270 | CompilerCallbacks* callbacks, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 271 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 272 | HardFailLogMode log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 273 | std::string* error) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 274 | ScopedTrace trace(__FUNCTION__); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 275 | |
| 276 | // A class must not be abstract and final. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 277 | if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 278 | *error = "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 279 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 280 | *error += ": class is abstract and final."; |
| 281 | return kHardFailure; |
| 282 | } |
| 283 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 284 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 285 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 286 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 287 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 288 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 289 | ClassDataItemIterator it(*dex_file, class_data); |
| 290 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 291 | it.Next(); |
| 292 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 293 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 294 | // Direct methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 295 | MethodVerifier::FailureData data1 = VerifyMethods<true>(self, |
| 296 | linker, |
| 297 | dex_file, |
| 298 | class_def, |
| 299 | &it, |
| 300 | dex_cache, |
| 301 | class_loader, |
| 302 | callbacks, |
| 303 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 304 | log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 305 | false /* need precise constants */, |
| 306 | error); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 307 | // Virtual methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 308 | MethodVerifier::FailureData data2 = VerifyMethods<false>(self, |
| 309 | linker, |
| 310 | dex_file, |
| 311 | class_def, |
| 312 | &it, |
| 313 | dex_cache, |
| 314 | class_loader, |
| 315 | callbacks, |
| 316 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 317 | log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 318 | false /* need precise constants */, |
| 319 | error); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 320 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 321 | data1.Merge(data2); |
| 322 | |
| 323 | if (data1.kind == kNoFailure) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 324 | return kNoFailure; |
| 325 | } else { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 326 | if ((data1.types & VERIFY_ERROR_LOCKING) != 0) { |
| 327 | // Print a warning about expected slow-down. Use a string temporary to print one contiguous |
| 328 | // warning. |
| 329 | std::string tmp = |
| 330 | StringPrintf("Class %s failed lock verification and will run slower.", |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 331 | PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str()); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 332 | if (!gPrintedDxMonitorText) { |
| 333 | tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n" |
| 334 | "and incorrect proguard optimizations."; |
| 335 | gPrintedDxMonitorText = true; |
| 336 | } |
| 337 | LOG(WARNING) << tmp; |
| 338 | } |
| 339 | return data1.kind; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 340 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 343 | static bool IsLargeMethod(const DexFile::CodeItem* const code_item) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 344 | if (code_item == nullptr) { |
| 345 | return false; |
| 346 | } |
| 347 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 348 | uint16_t registers_size = code_item->registers_size_; |
| 349 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 350 | |
| 351 | return registers_size * insns_size > 4*1024*1024; |
| 352 | } |
| 353 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 354 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 355 | uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 356 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 357 | Handle<mirror::DexCache> dex_cache, |
| 358 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 359 | const DexFile::ClassDef& class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 360 | const DexFile::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 361 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 362 | uint32_t method_access_flags, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 363 | CompilerCallbacks* callbacks, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 364 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 365 | HardFailLogMode log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 366 | bool need_precise_constants, |
| 367 | std::string* hard_failure_msg) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 368 | MethodVerifier::FailureData result; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 369 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 370 | |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 371 | MethodVerifier verifier(self, |
| 372 | dex_file, |
| 373 | dex_cache, |
| 374 | class_loader, |
| 375 | class_def, |
| 376 | code_item, |
| 377 | method_idx, |
| 378 | method, |
| 379 | method_access_flags, |
| 380 | true /* can_load_classes */, |
| 381 | allow_soft_failures, |
| 382 | need_precise_constants, |
| 383 | false /* verify to dump */, |
| 384 | true /* allow_thread_suspension */); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 385 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 386 | // Verification completed, however failures may be pending that didn't cause the verification |
| 387 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 388 | CHECK(!verifier.have_pending_hard_failure_); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 389 | |
| 390 | if (code_item != nullptr && callbacks != nullptr) { |
| 391 | // Let the interested party know that the method was verified. |
| 392 | callbacks->MethodVerified(&verifier); |
| 393 | } |
| 394 | |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 395 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 396 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 397 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
| 398 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 399 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 400 | result.kind = kSoftFailure; |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 401 | if (method != nullptr && |
| 402 | !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) { |
| 403 | method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother); |
| 404 | } |
| 405 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 406 | if (method != nullptr) { |
| 407 | if (verifier.HasInstructionThatWillThrow()) { |
| 408 | method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother); |
| 409 | } |
| 410 | if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
| 411 | method->SetAccessFlags(method->GetAccessFlags() | kAccMustCountLocks); |
| 412 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 413 | } |
| 414 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 415 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 416 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 417 | |
| 418 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 419 | // Failed due to being forced into interpreter. This is ok because |
| 420 | // we just want to skip verification. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 421 | result.kind = kSoftFailure; |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 422 | } else { |
| 423 | CHECK(verifier.have_pending_hard_failure_); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 424 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 425 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 426 | } |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 427 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 428 | LogSeverity severity; |
| 429 | switch (log_level) { |
| 430 | case HardFailLogMode::kLogVerbose: |
| 431 | severity = LogSeverity::VERBOSE; |
| 432 | break; |
| 433 | case HardFailLogMode::kLogWarning: |
| 434 | severity = LogSeverity::WARNING; |
| 435 | break; |
| 436 | case HardFailLogMode::kLogInternalFatal: |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 437 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 438 | break; |
| 439 | default: |
| 440 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 441 | UNREACHABLE(); |
| 442 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 443 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
| 444 | << PrettyMethod(method_idx, *dex_file) |
| 445 | << "\n"); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 446 | } |
| 447 | if (hard_failure_msg != nullptr) { |
| 448 | CHECK(!verifier.failure_messages_.empty()); |
| 449 | *hard_failure_msg = |
| 450 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 451 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 452 | result.kind = kHardFailure; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 453 | |
| 454 | if (callbacks != nullptr) { |
| 455 | // Let the interested party know that we failed the class. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 456 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 457 | callbacks->ClassRejected(ref); |
| 458 | } |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 459 | } |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 460 | if (VLOG_IS_ON(verifier)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 461 | std::cout << "\n" << verifier.info_messages_.str(); |
| 462 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 463 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 464 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 465 | if (kTimeVerifyMethod) { |
| 466 | uint64_t duration_ns = NanoTime() - start_ns; |
| 467 | if (duration_ns > MsToNs(100)) { |
| 468 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 469 | << " took " << PrettyDuration(duration_ns) |
| 470 | << (IsLargeMethod(code_item) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 471 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 472 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 473 | result.types = verifier.encountered_failure_types_; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 474 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 477 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 478 | VariableIndentationOutputStream* vios, |
| 479 | uint32_t dex_method_idx, |
| 480 | const DexFile* dex_file, |
| 481 | Handle<mirror::DexCache> dex_cache, |
| 482 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 483 | const DexFile::ClassDef& class_def, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 484 | const DexFile::CodeItem* code_item, |
| 485 | ArtMethod* method, |
| 486 | uint32_t method_access_flags) { |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 487 | MethodVerifier* verifier = new MethodVerifier(self, |
| 488 | dex_file, |
| 489 | dex_cache, |
| 490 | class_loader, |
| 491 | class_def, |
| 492 | code_item, |
| 493 | dex_method_idx, |
| 494 | method, |
| 495 | method_access_flags, |
| 496 | true /* can_load_classes */, |
| 497 | true /* allow_soft_failures */, |
| 498 | true /* need_precise_constants */, |
| 499 | true /* verify_to_dump */, |
| 500 | true /* allow_thread_suspension */); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 501 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 502 | verifier->DumpFailures(vios->Stream()); |
| 503 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 504 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 505 | // and querying any info is dangerous/can abort. |
| 506 | if (verifier->have_pending_hard_failure_) { |
| 507 | delete verifier; |
| 508 | return nullptr; |
| 509 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 510 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 511 | return verifier; |
| 512 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 515 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 516 | const DexFile* dex_file, |
| 517 | Handle<mirror::DexCache> dex_cache, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 518 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 519 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 520 | const DexFile::CodeItem* code_item, |
| 521 | uint32_t dex_method_idx, |
| 522 | ArtMethod* method, |
| 523 | uint32_t method_access_flags, |
| 524 | bool can_load_classes, |
| 525 | bool allow_soft_failures, |
| 526 | bool need_precise_constants, |
| 527 | bool verify_to_dump, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 528 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 529 | : self_(self), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 530 | arena_stack_(Runtime::Current()->GetArenaPool()), |
| 531 | arena_(&arena_stack_), |
| 532 | reg_types_(can_load_classes, arena_), |
| 533 | reg_table_(arena_), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 534 | work_insn_idx_(DexFile::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 535 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 536 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 537 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 538 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 539 | dex_file_(dex_file), |
| 540 | dex_cache_(dex_cache), |
| 541 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 542 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 543 | code_item_(code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 544 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 545 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 546 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 547 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 548 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 549 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 550 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 551 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 552 | monitor_enter_count_(0), |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 553 | encountered_failure_types_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 554 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 555 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 556 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 557 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 558 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 559 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 560 | allow_thread_suspension_(allow_thread_suspension), |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 561 | is_constructor_(false), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 562 | link_(nullptr) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 563 | self->PushVerifier(this); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 566 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 567 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 568 | STLDeleteElements(&failure_messages_); |
| 569 | } |
| 570 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 571 | void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 572 | std::vector<uint32_t>* monitor_enter_dex_pcs) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 573 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 574 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 575 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 576 | MethodVerifier verifier(hs.Self(), |
| 577 | m->GetDexFile(), |
| 578 | dex_cache, |
| 579 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 580 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 581 | m->GetCodeItem(), |
| 582 | m->GetDexMethodIndex(), |
| 583 | m, |
| 584 | m->GetAccessFlags(), |
| 585 | false /* can_load_classes */, |
| 586 | true /* allow_soft_failures */, |
| 587 | false /* need_precise_constants */, |
| 588 | false /* verify_to_dump */, |
| 589 | false /* allow_thread_suspension */); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 590 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 591 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 592 | verifier.FindLocksAtDexPc(); |
| 593 | } |
| 594 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 595 | static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) { |
| 596 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 597 | |
| 598 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 599 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 600 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | dex_pc += inst->SizeInCodeUnits(); |
| 605 | inst = inst->Next(); |
| 606 | } |
| 607 | |
| 608 | return false; |
| 609 | } |
| 610 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 611 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 612 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| 613 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 614 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 615 | // Quick check whether there are any monitor_enter instructions at all. |
| 616 | if (!HasMonitorEnterInstructions(code_item_)) { |
| 617 | return; |
| 618 | } |
| 619 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 620 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 621 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 622 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 623 | // got what we wanted. |
| 624 | Verify(); |
| 625 | } |
| 626 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 627 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 628 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 629 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 630 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 631 | MethodVerifier verifier(hs.Self(), |
| 632 | m->GetDexFile(), |
| 633 | dex_cache, |
| 634 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 635 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 636 | m->GetCodeItem(), |
| 637 | m->GetDexMethodIndex(), |
| 638 | m, |
| 639 | m->GetAccessFlags(), |
| 640 | true /* can_load_classes */, |
| 641 | true /* allow_soft_failures */, |
| 642 | false /* need_precise_constants */, |
| 643 | false /* verify_to_dump */, |
| 644 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 645 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 646 | } |
| 647 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 648 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 649 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 650 | |
| 651 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 652 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 653 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 654 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 655 | bool success = Verify(); |
| 656 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 657 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 658 | } |
| 659 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 660 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 661 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 662 | } |
| 663 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 664 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 667 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 668 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 669 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 670 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 671 | MethodVerifier verifier(hs.Self(), |
| 672 | m->GetDexFile(), |
| 673 | dex_cache, |
| 674 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 675 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 676 | m->GetCodeItem(), |
| 677 | m->GetDexMethodIndex(), |
| 678 | m, |
| 679 | m->GetAccessFlags(), |
| 680 | true /* can_load_classes */, |
| 681 | true /* allow_soft_failures */, |
| 682 | false /* need_precise_constants */, |
| 683 | false /* verify_to_dump */, |
| 684 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 685 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 686 | } |
| 687 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 688 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 689 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 690 | |
| 691 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 692 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 693 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 694 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 695 | bool success = Verify(); |
| 696 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 697 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 698 | } |
| 699 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 700 | if (register_line == nullptr) { |
| 701 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 702 | } |
| 703 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 704 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 705 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 706 | } |
| 707 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 708 | bool MethodVerifier::Verify() { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 709 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 710 | // the name. |
| 711 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 712 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 713 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 714 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 715 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 716 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 717 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 718 | if (!constructor_by_name) { |
| 719 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 720 | << "method is marked as constructor, but not named accordingly"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 721 | return false; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 722 | } |
| 723 | is_constructor_ = true; |
| 724 | } else if (constructor_by_name) { |
| 725 | LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_) |
| 726 | << " not marked as constructor."; |
| 727 | is_constructor_ = true; |
| 728 | } |
| 729 | // If it's a constructor, check whether IsStatic() matches the name. |
| 730 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 731 | if (kIsDebugBuild) { |
| 732 | if (IsConstructor()) { |
| 733 | if (IsStatic() ^ static_constructor_by_name) { |
| 734 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 735 | << "constructor name doesn't match static flag"; |
| 736 | return false; |
| 737 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 738 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 739 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 740 | |
| 741 | // Methods may only have one of public/protected/private. |
| 742 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 743 | if (kIsDebugBuild) { |
| 744 | size_t access_mod_count = |
| 745 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 746 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 747 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 748 | if (access_mod_count > 1) { |
| 749 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 750 | return false; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 755 | if (code_item_ == nullptr) { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 756 | // Only native or abstract methods may not have code. |
| 757 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 758 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 759 | return false; |
| 760 | } |
| 761 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 762 | // 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] | 763 | // 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] | 764 | if (kIsDebugBuild) { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 765 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 766 | // Abstract methods are not allowed to have the following flags. |
| 767 | static constexpr uint32_t kForbidden = |
| 768 | kAccPrivate | |
| 769 | kAccStatic | |
| 770 | kAccFinal | |
| 771 | kAccNative | |
| 772 | kAccStrict | |
| 773 | kAccSynchronized; |
| 774 | if ((method_access_flags_ & kForbidden) != 0) { |
| 775 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 776 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 777 | return false; |
| 778 | } |
| 779 | } |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 780 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 781 | // Interface methods must be public and abstract (if default methods are disabled). |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 782 | uint32_t kRequired = kAccPublic; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 783 | if ((method_access_flags_ & kRequired) != kRequired) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 784 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 785 | return false; |
| 786 | } |
| 787 | // In addition to the above, interface methods must not be protected. |
| 788 | static constexpr uint32_t kForbidden = kAccProtected; |
| 789 | if ((method_access_flags_ & kForbidden) != 0) { |
| 790 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 791 | return false; |
| 792 | } |
| 793 | } |
| 794 | // We also don't allow constructors to be abstract or native. |
| 795 | if (IsConstructor()) { |
| 796 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 797 | return false; |
| 798 | } |
| 799 | } |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 804 | if (kIsDebugBuild) { |
| 805 | // When there's code, the method must not be native or abstract. |
| 806 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 807 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 808 | return false; |
| 809 | } |
| 810 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 811 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 812 | // Interfaces may always have static initializers for their fields. If we are running with |
| 813 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 814 | // Otherwise that is the only type of method allowed. |
Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 815 | if (!(IsConstructor() && IsStatic())) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 816 | if (IsInstanceConstructor()) { |
| 817 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 818 | return false; |
| 819 | } else if (method_access_flags_ & kAccFinal) { |
| 820 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 821 | return false; |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 822 | } else { |
| 823 | uint32_t access_flag_options = kAccPublic; |
| 824 | if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) { |
| 825 | access_flag_options |= kAccPrivate; |
| 826 | } |
| 827 | if (!(method_access_flags_ & access_flag_options)) { |
| 828 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 829 | << "interfaces may not have protected or package-private members"; |
| 830 | return false; |
| 831 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 832 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
| 836 | // Instance constructors must not be synchronized. |
| 837 | if (IsInstanceConstructor()) { |
| 838 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 839 | if ((method_access_flags_ & kForbidden) != 0) { |
| 840 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 841 | return false; |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 846 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 847 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 848 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 849 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 850 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 851 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 852 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 853 | // Allocate and initialize an array to hold instruction data. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 854 | insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_)); |
| 855 | DCHECK(insn_flags_ != nullptr); |
| 856 | std::uninitialized_fill_n(insn_flags_.get(), |
| 857 | code_item_->insns_size_in_code_units_, |
| 858 | InstructionFlags()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 859 | // Run through the instructions and see if the width checks out. |
| 860 | bool result = ComputeWidthsAndCountOps(); |
| 861 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 862 | result = result && ScanTryCatchBlocks(); |
| 863 | // Perform static instruction verification. |
| 864 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 865 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 866 | result = result && VerifyCodeFlow(); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 867 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 868 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 871 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 872 | // Mark the error type as encountered. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 873 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 874 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 875 | switch (error) { |
| 876 | case VERIFY_ERROR_NO_CLASS: |
| 877 | case VERIFY_ERROR_NO_FIELD: |
| 878 | case VERIFY_ERROR_NO_METHOD: |
| 879 | case VERIFY_ERROR_ACCESS_CLASS: |
| 880 | case VERIFY_ERROR_ACCESS_FIELD: |
| 881 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 882 | case VERIFY_ERROR_INSTANTIATION: |
| 883 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 884 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 885 | case VERIFY_ERROR_LOCKING: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 886 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 887 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 888 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 889 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 890 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 891 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 892 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 893 | // to an interpreter. |
| 894 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 895 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 896 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 897 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 898 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 899 | |
| 900 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 901 | // try to merge garbage. |
| 902 | // 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] | 903 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 904 | // add a check. |
| 905 | if (work_insn_idx_ < DexFile::kDexNoIndex) { |
| 906 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 907 | const Instruction* inst = Instruction::At(insns); |
| 908 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 909 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 910 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 911 | saved_line_->CopyFromLine(work_line_.get()); |
| 912 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 913 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 914 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 915 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 916 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 917 | // Indication that verification should be retried at runtime. |
| 918 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 919 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 920 | have_pending_hard_failure_ = true; |
| 921 | } |
| 922 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 923 | |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 924 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 925 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 926 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 927 | have_pending_hard_failure_ = true; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 928 | if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) { |
| 929 | ScopedObjectAccess soa(Thread::Current()); |
| 930 | std::ostringstream oss; |
| 931 | Dump(oss); |
| 932 | LOG(ERROR) << oss.str(); |
| 933 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 934 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 935 | } |
| 936 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 937 | failures_.push_back(error); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 938 | std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 939 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 940 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 941 | failure_messages_.push_back(failure_message); |
| 942 | return *failure_message; |
| 943 | } |
| 944 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 945 | std::ostream& MethodVerifier::LogVerifyInfo() { |
| 946 | return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) |
| 947 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 948 | } |
| 949 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 950 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 951 | size_t failure_num = failure_messages_.size(); |
| 952 | DCHECK_NE(failure_num, 0U); |
| 953 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 954 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 955 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 956 | delete last_fail_message; |
| 957 | } |
| 958 | |
| 959 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 960 | size_t failure_num = failure_messages_.size(); |
| 961 | DCHECK_NE(failure_num, 0U); |
| 962 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 963 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 966 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 967 | const uint16_t* insns = code_item_->insns_; |
| 968 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 969 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 970 | size_t new_instance_count = 0; |
| 971 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 972 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 973 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 974 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 975 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 976 | switch (opcode) { |
| 977 | case Instruction::APUT_OBJECT: |
| 978 | case Instruction::CHECK_CAST: |
| 979 | has_check_casts_ = true; |
| 980 | break; |
| 981 | case Instruction::INVOKE_VIRTUAL: |
| 982 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 983 | case Instruction::INVOKE_INTERFACE: |
| 984 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 985 | has_virtual_or_interface_invokes_ = true; |
| 986 | break; |
| 987 | case Instruction::MONITOR_ENTER: |
| 988 | monitor_enter_count++; |
| 989 | break; |
| 990 | case Instruction::NEW_INSTANCE: |
| 991 | new_instance_count++; |
| 992 | break; |
| 993 | default: |
| 994 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 995 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 996 | size_t inst_size = inst->SizeInCodeUnits(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 997 | GetInstructionFlags(dex_pc).SetIsOpcode(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 998 | dex_pc += inst_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 999 | inst = inst->RelativeAt(inst_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1002 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1003 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 1004 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
| 1007 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1008 | new_instance_count_ = new_instance_count; |
| 1009 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1010 | return true; |
| 1011 | } |
| 1012 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1013 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1014 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1015 | if (tries_size == 0) { |
| 1016 | return true; |
| 1017 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1018 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1019 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1020 | |
| 1021 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 1022 | const DexFile::TryItem* try_item = &tries[idx]; |
| 1023 | uint32_t start = try_item->start_addr_; |
| 1024 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1025 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1027 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1030 | if (!GetInstructionFlags(start).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1031 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1032 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1035 | uint32_t dex_pc = start; |
| 1036 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 1037 | while (dex_pc < end) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1038 | GetInstructionFlags(dex_pc).SetInTry(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1039 | size_t insn_size = inst->SizeInCodeUnits(); |
| 1040 | dex_pc += insn_size; |
| 1041 | inst = inst->RelativeAt(insn_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1044 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1045 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1046 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1047 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1048 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1049 | CatchHandlerIterator iterator(handlers_ptr); |
| 1050 | for (; iterator.HasNext(); iterator.Next()) { |
| 1051 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1052 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1053 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1054 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1055 | return false; |
| 1056 | } |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 1057 | if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) { |
| 1058 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1059 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 1060 | return false; |
| 1061 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1062 | GetInstructionFlags(dex_pc).SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1063 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1064 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1065 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1066 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 1067 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1068 | dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1069 | if (exception_type == nullptr) { |
| 1070 | DCHECK(self_->IsExceptionPending()); |
| 1071 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1072 | } |
| 1073 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1074 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1075 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1076 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1077 | return true; |
| 1078 | } |
| 1079 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1080 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1081 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1082 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1083 | /* 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] | 1084 | GetInstructionFlags(0).SetBranchTarget(); |
| 1085 | GetInstructionFlags(0).SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1086 | |
| 1087 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1088 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1089 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1090 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1091 | return false; |
| 1092 | } |
| 1093 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1094 | // All invoke points are marked as "Throw" points already. |
| 1095 | // 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] | 1096 | if (inst->IsBranch()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1097 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1098 | // The compiler also needs safepoints for fall-through to loop heads. |
| 1099 | // Such a loop head must be a target of a branch. |
| 1100 | int32_t offset = 0; |
| 1101 | bool cond, self_ok; |
| 1102 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 1103 | DCHECK(target_ok); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1104 | GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1105 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1106 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 1107 | } else if (inst->IsReturn()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1108 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1109 | } |
| 1110 | dex_pc += inst->SizeInCodeUnits(); |
| 1111 | inst = inst->Next(); |
| 1112 | } |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1116 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1117 | if (UNLIKELY(inst->IsExperimental())) { |
| 1118 | // Experimental instructions don't yet have verifier support implementation. |
| 1119 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 1120 | // with a virtual register that was created by an experimental instruction, |
| 1121 | // the data flow analysis will fail. |
| 1122 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 1123 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 1124 | have_pending_experimental_failure_ = true; |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1128 | bool result = true; |
| 1129 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1130 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1131 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1132 | break; |
| 1133 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1134 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1138 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1139 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1142 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1145 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1146 | break; |
| 1147 | case Instruction::kVerifyRegBNewInstance: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1148 | result = result && CheckNewInstance(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1149 | break; |
| 1150 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1151 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | case Instruction::kVerifyRegBType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1154 | result = result && CheckTypeIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1155 | break; |
| 1156 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1157 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1158 | break; |
| 1159 | } |
| 1160 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1161 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1162 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1163 | break; |
| 1164 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1165 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1166 | break; |
| 1167 | case Instruction::kVerifyRegCNewArray: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1168 | result = result && CheckNewArray(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1169 | break; |
| 1170 | case Instruction::kVerifyRegCType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1171 | result = result && CheckTypeIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1172 | break; |
| 1173 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1174 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1175 | break; |
| 1176 | } |
| 1177 | switch (inst->GetVerifyExtraFlags()) { |
| 1178 | case Instruction::kVerifyArrayData: |
| 1179 | result = result && CheckArrayData(code_offset); |
| 1180 | break; |
| 1181 | case Instruction::kVerifyBranchTarget: |
| 1182 | result = result && CheckBranchTarget(code_offset); |
| 1183 | break; |
| 1184 | case Instruction::kVerifySwitchTargets: |
| 1185 | result = result && CheckSwitchTargets(code_offset); |
| 1186 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1187 | case Instruction::kVerifyVarArgNonZero: |
| 1188 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1189 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1190 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1191 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1192 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1193 | v_a > Instruction::kMaxVarArgRegs) { |
| 1194 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1195 | "non-range invoke"; |
| 1196 | return false; |
| 1197 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1198 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1199 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1200 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1201 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1202 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1203 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1204 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1205 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1206 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1207 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1208 | inst->VRegA() <= 0) { |
| 1209 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1210 | "range invoke"; |
| 1211 | return false; |
| 1212 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1213 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1214 | break; |
| 1215 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1216 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1217 | result = false; |
| 1218 | break; |
| 1219 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1220 | if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1221 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1222 | result = false; |
| 1223 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1224 | return result; |
| 1225 | } |
| 1226 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1227 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1228 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1229 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 1230 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1231 | return false; |
| 1232 | } |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1236 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1237 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1238 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 1239 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
| 1242 | return true; |
| 1243 | } |
| 1244 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1245 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1246 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1247 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 1248 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | return true; |
| 1252 | } |
| 1253 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1254 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1255 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1256 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 1257 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1263 | inline bool MethodVerifier::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1264 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1265 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1266 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
| 1269 | // 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] | 1270 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1271 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1272 | 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] | 1273 | return false; |
Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1274 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
| 1275 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1276 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1277 | Fail(VERIFY_ERROR_INSTANTIATION); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1278 | } |
| 1279 | return true; |
| 1280 | } |
| 1281 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1282 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1283 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1284 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 1285 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1286 | return false; |
| 1287 | } |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1291 | inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1292 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1293 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1294 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1300 | bool MethodVerifier::CheckNewArray(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1302 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1303 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1307 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1308 | const char* cp = descriptor; |
| 1309 | while (*cp++ == '[') { |
| 1310 | bracket_count++; |
| 1311 | } |
| 1312 | if (bracket_count == 0) { |
| 1313 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1314 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1315 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1316 | return false; |
| 1317 | } else if (bracket_count > 255) { |
| 1318 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1319 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1320 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1326 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1327 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1328 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1329 | const uint16_t* array_data; |
| 1330 | int32_t array_data_offset; |
| 1331 | |
| 1332 | DCHECK_LT(cur_offset, insn_count); |
| 1333 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1334 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| 1335 | if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1336 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1337 | 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] | 1338 | << ", data offset " << array_data_offset |
| 1339 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1340 | return false; |
| 1341 | } |
| 1342 | /* offset to array data table is a relative branch-style offset */ |
| 1343 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1344 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1345 | if (!IsAligned<4>(array_data)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1346 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1347 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1348 | return false; |
| 1349 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1350 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1351 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1352 | if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1353 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1354 | << ", data offset " << array_data_offset |
| 1355 | << " not correctly visited, probably bad padding."; |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1359 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1360 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1361 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1362 | /* make sure the end of the switch is in range */ |
| 1363 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1364 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1365 | << ", data offset " << array_data_offset << ", end " |
| 1366 | << cur_offset + array_data_offset + table_size |
| 1367 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
| 1370 | return true; |
| 1371 | } |
| 1372 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1373 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1374 | int32_t offset; |
| 1375 | bool isConditional, selfOkay; |
| 1376 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1377 | return false; |
| 1378 | } |
| 1379 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1380 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1381 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1382 | return false; |
| 1383 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1384 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1385 | // to have identical "wrap-around" behavior, but it's unwise to depend on that. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1386 | if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1387 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1388 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
| 1391 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1392 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1393 | if (abs_offset < 0 || |
| 1394 | (uint32_t) abs_offset >= insn_count || |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1395 | !GetInstructionFlags(abs_offset).IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1396 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1397 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1398 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1399 | return false; |
| 1400 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1401 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1402 | return true; |
| 1403 | } |
| 1404 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1405 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1406 | bool* selfOkay) { |
| 1407 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1408 | *pConditional = false; |
| 1409 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1410 | switch (*insns & 0xff) { |
| 1411 | case Instruction::GOTO: |
| 1412 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1413 | break; |
| 1414 | case Instruction::GOTO_32: |
| 1415 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1416 | *selfOkay = true; |
| 1417 | break; |
| 1418 | case Instruction::GOTO_16: |
| 1419 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1420 | break; |
| 1421 | case Instruction::IF_EQ: |
| 1422 | case Instruction::IF_NE: |
| 1423 | case Instruction::IF_LT: |
| 1424 | case Instruction::IF_GE: |
| 1425 | case Instruction::IF_GT: |
| 1426 | case Instruction::IF_LE: |
| 1427 | case Instruction::IF_EQZ: |
| 1428 | case Instruction::IF_NEZ: |
| 1429 | case Instruction::IF_LTZ: |
| 1430 | case Instruction::IF_GEZ: |
| 1431 | case Instruction::IF_GTZ: |
| 1432 | case Instruction::IF_LEZ: |
| 1433 | *pOffset = (int16_t) insns[1]; |
| 1434 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1435 | break; |
| 1436 | default: |
| 1437 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1438 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1439 | return true; |
| 1440 | } |
| 1441 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1442 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1443 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1444 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1445 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1446 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1447 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| 1448 | if (static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1449 | cur_offset + switch_offset + 2 > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1450 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1451 | << ", switch offset " << switch_offset |
| 1452 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1455 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1456 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1457 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1458 | if (!IsAligned<4>(switch_insns)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1459 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1460 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1461 | return false; |
| 1462 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1463 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1464 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1465 | if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1467 | << ", switch offset " << switch_offset |
| 1468 | << " not correctly visited, probably bad padding."; |
| 1469 | return false; |
| 1470 | } |
| 1471 | |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1472 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1473 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1474 | uint32_t switch_count = switch_insns[1]; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1475 | int32_t targets_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1476 | uint16_t expected_signature; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1477 | if (is_packed_switch) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1478 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1479 | targets_offset = 4; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1480 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1481 | } else { |
| 1482 | /* 0=sig, 1=count, 2..count*2 = keys */ |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1483 | targets_offset = 2 + 2 * switch_count; |
| 1484 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1485 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1486 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1487 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1488 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1489 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1490 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1491 | return false; |
| 1492 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1493 | /* make sure the end of the switch is in range */ |
| 1494 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1495 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1496 | << ", switch offset " << switch_offset |
| 1497 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1498 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1499 | return false; |
| 1500 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1501 | |
| 1502 | constexpr int32_t keys_offset = 2; |
| 1503 | if (switch_count > 1) { |
| 1504 | if (is_packed_switch) { |
| 1505 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1506 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1507 | int32_t max_first_key = |
| 1508 | std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1); |
| 1509 | if (first_key > max_first_key) { |
| 1510 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1511 | << ", switch_count=" << switch_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1512 | return false; |
| 1513 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1514 | } else { |
| 1515 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1516 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1517 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1518 | int32_t key = |
| 1519 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1520 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 1521 | if (key <= last_key) { |
| 1522 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1523 | << ", this=" << key; |
| 1524 | return false; |
| 1525 | } |
| 1526 | last_key = key; |
| 1527 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1530 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1531 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1532 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1533 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1534 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1535 | if (abs_offset < 0 || |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1536 | abs_offset >= static_cast<int32_t>(insn_count) || |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1537 | !GetInstructionFlags(abs_offset).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1538 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1539 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1540 | << reinterpret_cast<void*>(cur_offset) |
| 1541 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1542 | return false; |
| 1543 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1544 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1545 | } |
| 1546 | return true; |
| 1547 | } |
| 1548 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1549 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1550 | uint16_t registers_size = code_item_->registers_size_; |
| 1551 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1552 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1553 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1554 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1555 | return false; |
| 1556 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | return true; |
| 1560 | } |
| 1561 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1562 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1563 | uint16_t registers_size = code_item_->registers_size_; |
| 1564 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1565 | // integer overflow when adding them here. |
| 1566 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1567 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1568 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1569 | return false; |
| 1570 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1571 | return true; |
| 1572 | } |
| 1573 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1574 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1575 | uint16_t registers_size = code_item_->registers_size_; |
| 1576 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1577 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1578 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1579 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1580 | insn_flags_.get(), |
| 1581 | insns_size, |
| 1582 | registers_size, |
| 1583 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1584 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1585 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1586 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1588 | /* Initialize register types of method arguments. */ |
| 1589 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1590 | DCHECK_NE(failures_.size(), 0U); |
| 1591 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1592 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1593 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1594 | return false; |
| 1595 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1596 | // We may have a runtime failure here, clear. |
| 1597 | have_pending_runtime_throw_failure_ = false; |
| 1598 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1599 | /* Perform code flow verification. */ |
| 1600 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1601 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1602 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1603 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1604 | return true; |
| 1605 | } |
| 1606 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1607 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1608 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1609 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1610 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1611 | } |
| 1612 | return os; |
| 1613 | } |
| 1614 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1615 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1616 | VariableIndentationOutputStream vios(&os); |
| 1617 | Dump(&vios); |
| 1618 | } |
| 1619 | |
| 1620 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1621 | if (code_item_ == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1622 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1623 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1624 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1625 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1626 | vios->Stream() << "Register Types:\n"; |
| 1627 | ScopedIndentation indent1(vios); |
| 1628 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1629 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1630 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1631 | ScopedIndentation indent1(vios); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1632 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1633 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 1634 | dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1635 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1636 | if (reg_line != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1637 | vios->Stream() << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1638 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1639 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1640 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1641 | const bool kDumpHexOfInstruction = false; |
| 1642 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1643 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1644 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1645 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1646 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1649 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1650 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1651 | case 'I': |
| 1652 | case 'C': |
| 1653 | case 'S': |
| 1654 | case 'B': |
| 1655 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1656 | case 'F': |
| 1657 | case 'D': |
| 1658 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1659 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1660 | default: |
| 1661 | return false; |
| 1662 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1665 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1666 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1667 | |
| 1668 | // Should have been verified earlier. |
| 1669 | DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_); |
| 1670 | |
| 1671 | uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1672 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1673 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1674 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1675 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1676 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1677 | if (expected_args == 0) { |
| 1678 | // Expect at least a receiver. |
| 1679 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1680 | return false; |
| 1681 | } |
| 1682 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1683 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1684 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1685 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1686 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1687 | if (IsConstructor()) { |
| 1688 | if (declaring_class.IsJavaLangObject()) { |
| 1689 | // "this" is implicitly initialized. |
| 1690 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1691 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1692 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1693 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1694 | this, |
| 1695 | arg_start + cur_arg, |
| 1696 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1697 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1698 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1699 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1700 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1701 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1704 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1705 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1706 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1707 | |
| 1708 | for (; iterator.HasNext(); iterator.Next()) { |
| 1709 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1710 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1711 | LOG(FATAL) << "Null descriptor"; |
| 1712 | } |
| 1713 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1714 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1715 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1716 | return false; |
| 1717 | } |
| 1718 | switch (descriptor[0]) { |
| 1719 | case 'L': |
| 1720 | case '[': |
| 1721 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1722 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1723 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1724 | // can return without doing anything or call virtual methods). |
| 1725 | { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1726 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1727 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1728 | DCHECK(HasFailures()); |
| 1729 | return false; |
| 1730 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1731 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1732 | } |
| 1733 | break; |
| 1734 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1735 | 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] | 1736 | break; |
| 1737 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1738 | 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] | 1739 | break; |
| 1740 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1741 | 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] | 1742 | break; |
| 1743 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1744 | 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] | 1745 | break; |
| 1746 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1747 | 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] | 1748 | break; |
| 1749 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1750 | 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] | 1751 | break; |
| 1752 | case 'J': |
| 1753 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1754 | if (cur_arg + 1 >= expected_args) { |
| 1755 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1756 | << " args, found more (" << descriptor << ")"; |
| 1757 | return false; |
| 1758 | } |
| 1759 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1760 | const RegType* lo_half; |
| 1761 | const RegType* hi_half; |
| 1762 | if (descriptor[0] == 'J') { |
| 1763 | lo_half = ®_types_.LongLo(); |
| 1764 | hi_half = ®_types_.LongHi(); |
| 1765 | } else { |
| 1766 | lo_half = ®_types_.DoubleLo(); |
| 1767 | hi_half = ®_types_.DoubleHi(); |
| 1768 | } |
| 1769 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1770 | cur_arg++; |
| 1771 | break; |
| 1772 | } |
| 1773 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1774 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1775 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
| 1778 | cur_arg++; |
| 1779 | } |
| 1780 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1781 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1782 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1783 | return false; |
| 1784 | } |
| 1785 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1786 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1787 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1788 | bool result; |
| 1789 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1790 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1791 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1792 | size_t i = 0; |
| 1793 | do { |
| 1794 | i++; |
| 1795 | } while (descriptor[i] == '['); // process leading [ |
| 1796 | if (descriptor[i] == 'L') { // object array |
| 1797 | do { |
| 1798 | i++; // find closing ; |
| 1799 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1800 | result = descriptor[i] == ';'; |
| 1801 | } else { // primitive array |
| 1802 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1803 | } |
| 1804 | } else if (descriptor[0] == 'L') { |
| 1805 | // could be more thorough here, but shouldn't be required |
| 1806 | size_t i = 0; |
| 1807 | do { |
| 1808 | i++; |
| 1809 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1810 | result = descriptor[i] == ';'; |
| 1811 | } else { |
| 1812 | result = false; |
| 1813 | } |
| 1814 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1815 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1816 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1817 | } |
| 1818 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1821 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1822 | const uint16_t* insns = code_item_->insns_; |
| 1823 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1824 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1825 | /* Begin by marking the first instruction as "changed". */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1826 | GetInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1827 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1828 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1829 | /* Continue until no instructions are marked "changed". */ |
| 1830 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1831 | if (allow_thread_suspension_) { |
| 1832 | self_->AllowThreadSuspension(); |
| 1833 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1834 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1835 | uint32_t insn_idx = start_guess; |
| 1836 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1837 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1838 | break; |
| 1839 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1840 | if (insn_idx == insns_size) { |
| 1841 | if (start_guess != 0) { |
| 1842 | /* try again, starting from the top */ |
| 1843 | start_guess = 0; |
| 1844 | continue; |
| 1845 | } else { |
| 1846 | /* all flags are clear */ |
| 1847 | break; |
| 1848 | } |
| 1849 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1850 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1851 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1852 | // "changed" flags, we need to load the set of registers from the table. |
| 1853 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1854 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1855 | // target. |
| 1856 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1857 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1858 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1859 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1860 | /* |
| 1861 | * Sanity check: retrieve the stored register line (assuming |
| 1862 | * a full table) and make sure it actually matches. |
| 1863 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1864 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1865 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1866 | if (work_line_->CompareLine(register_line) != 0) { |
| 1867 | Dump(std::cout); |
| 1868 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1869 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1870 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1871 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1872 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1873 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1874 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1875 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1876 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1877 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1878 | prepend += " failed to verify: "; |
| 1879 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1880 | return false; |
| 1881 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1882 | /* Clear "changed" and mark as visited. */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1883 | GetInstructionFlags(insn_idx).SetVisited(); |
| 1884 | GetInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1885 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1886 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 1887 | if (kDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1888 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1889 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1890 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1891 | * down the line, possibly in the verifier. |
| 1892 | * |
| 1893 | * If we've substituted "always throw" instructions into the stream, |
| 1894 | * we are almost certainly going to have some dead code. |
| 1895 | */ |
| 1896 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1897 | uint32_t insn_idx = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1898 | for (; insn_idx < insns_size; |
| 1899 | insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1900 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1901 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1902 | * may or may not be preceded by a padding NOP (for alignment). |
| 1903 | */ |
| 1904 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1905 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1906 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1907 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1908 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1909 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1910 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1911 | GetInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1914 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1915 | if (dead_start < 0) |
| 1916 | dead_start = insn_idx; |
| 1917 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1918 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1919 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1920 | dead_start = -1; |
| 1921 | } |
| 1922 | } |
| 1923 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1924 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1925 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1926 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1927 | // To dump the state of the verify after a method, do something like: |
| 1928 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1929 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1930 | // LOG(INFO) << info_messages_.str(); |
| 1931 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1932 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1933 | return true; |
| 1934 | } |
| 1935 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1936 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1937 | // is no such field. |
| 1938 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) { |
| 1939 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 1940 | DCHECK(class_def != nullptr); |
| 1941 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 1942 | DCHECK(class_data != nullptr); |
| 1943 | ClassDataItemIterator it(dex_file, class_data); |
| 1944 | // Skip static fields. |
| 1945 | while (it.HasNextStaticField()) { |
| 1946 | it.Next(); |
| 1947 | } |
| 1948 | while (it.HasNextInstanceField()) { |
| 1949 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 1950 | return it.GetMemberIndex(); |
| 1951 | } |
| 1952 | it.Next(); |
| 1953 | } |
| 1954 | return DexFile::kDexNoIndex; |
| 1955 | } |
| 1956 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 1957 | // Setup a register line for the given return instruction. |
| 1958 | static void AdjustReturnLine(MethodVerifier* verifier, |
| 1959 | const Instruction* ret_inst, |
| 1960 | RegisterLine* line) { |
| 1961 | Instruction::Code opcode = ret_inst->Opcode(); |
| 1962 | |
| 1963 | switch (opcode) { |
| 1964 | case Instruction::RETURN_VOID: |
| 1965 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 1966 | SafelyMarkAllRegistersAsConflicts(verifier, line); |
| 1967 | break; |
| 1968 | |
| 1969 | case Instruction::RETURN: |
| 1970 | case Instruction::RETURN_OBJECT: |
| 1971 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 1972 | break; |
| 1973 | |
| 1974 | case Instruction::RETURN_WIDE: |
| 1975 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 1976 | break; |
| 1977 | |
| 1978 | default: |
| 1979 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 1980 | UNREACHABLE(); |
| 1981 | } |
| 1982 | } |
| 1983 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1984 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1985 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1986 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1987 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1988 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1989 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1990 | monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1991 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1992 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1993 | } |
| 1994 | } |
| 1995 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1996 | /* |
| 1997 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1998 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1999 | * control to another statement: |
| 2000 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2001 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2002 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2003 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2004 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2005 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2006 | * throw an exception that is handled by an encompassing "try" |
| 2007 | * block. |
| 2008 | * |
| 2009 | * We can also return, in which case there is no successor instruction |
| 2010 | * from this point. |
| 2011 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2012 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2013 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2014 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 2015 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2016 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2017 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2018 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2019 | bool just_set_result = false; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 2020 | if (kDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2021 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2022 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2023 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2024 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2025 | |
| 2026 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2027 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | * can throw an exception, we will copy/merge this into the "catch" |
| 2029 | * address rather than work_line, because we don't want the result |
| 2030 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2031 | * a type) to be visible to the exception handler. |
| 2032 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2033 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2034 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2035 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2036 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2037 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 2038 | 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] | 2039 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2040 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2041 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2042 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2043 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2044 | RegisterLineArenaUniquePtr branch_line; |
| 2045 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2046 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2047 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2048 | case Instruction::NOP: |
| 2049 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2050 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2051 | * a signature that looks like a NOP; if we see one of these in |
| 2052 | * the course of executing code then we have a problem. |
| 2053 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2054 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2055 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2056 | } |
| 2057 | break; |
| 2058 | |
| 2059 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2060 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2061 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2062 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2063 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2064 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2065 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2066 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | break; |
| 2068 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2069 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2070 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2071 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2072 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2073 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2074 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2075 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2076 | break; |
| 2077 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2078 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2079 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2080 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2081 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2082 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2083 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2084 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2085 | break; |
| 2086 | |
| 2087 | /* |
| 2088 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2089 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2090 | * might want to hold the result in an actual CPU register, so the |
| 2091 | * Dalvik spec requires that these only appear immediately after an |
| 2092 | * invoke or filled-new-array. |
| 2093 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2094 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2095 | * redundant with the reset done below, but it can make the debug info |
| 2096 | * easier to read in some cases.) |
| 2097 | */ |
| 2098 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2099 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2100 | break; |
| 2101 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2102 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2103 | break; |
| 2104 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2105 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2106 | break; |
| 2107 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2108 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2109 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 2110 | // where one entrypoint to the catch block is not actually an exception path. |
| 2111 | if (work_insn_idx_ == 0) { |
| 2112 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 2113 | break; |
| 2114 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2115 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2116 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2117 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2118 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2119 | const RegType& res_type = GetCaughtExceptionType(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2120 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2121 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2122 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2123 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2124 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2125 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2126 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2127 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2128 | } |
| 2129 | break; |
| 2130 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2131 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2132 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2133 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2134 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2135 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2136 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2137 | } else { |
| 2138 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2139 | // 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] | 2140 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2141 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2142 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2143 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2144 | return_type.IsShort() || return_type.IsChar()) && |
| 2145 | src_type.IsInteger())); |
| 2146 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2147 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2148 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2149 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2150 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2151 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | break; |
| 2155 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2156 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2157 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2158 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2159 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2160 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2161 | } else { |
| 2162 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2163 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2164 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2165 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2166 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2167 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2168 | } |
| 2169 | } |
| 2170 | break; |
| 2171 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2172 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2173 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2174 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2175 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2176 | } else { |
| 2177 | /* return_type is the *expected* return type, not register value */ |
| 2178 | DCHECK(!return_type.IsZero()); |
| 2179 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2180 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2181 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2182 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2183 | // reference in vAA is an instance of the "return_type." |
| 2184 | if (reg_type.IsUndefined()) { |
| 2185 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2186 | } else if (reg_type.IsConflict()) { |
| 2187 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2188 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2189 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2190 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2191 | } else if (!reg_type.IsReferenceTypes()) { |
| 2192 | // We really do expect a reference here. |
| 2193 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2194 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2195 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2196 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 2197 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 2198 | << "' or '" << reg_type << "'"; |
| 2199 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2200 | bool soft_error = false; |
| 2201 | // Check whether arrays are involved. They will show a valid class status, even |
| 2202 | // if their components are erroneous. |
| 2203 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2204 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2205 | if (soft_error) { |
| 2206 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2207 | << reg_type << " vs " << return_type; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | if (!soft_error) { |
| 2212 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2213 | << "', but expected from declaration '" << return_type << "'"; |
| 2214 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2215 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | break; |
| 2220 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2221 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2222 | case Instruction::CONST_4: { |
| 2223 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2224 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2225 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2226 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2227 | } |
| 2228 | case Instruction::CONST_16: { |
| 2229 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2230 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2231 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2232 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2233 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2234 | case Instruction::CONST: { |
| 2235 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2236 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2237 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2238 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2239 | } |
| 2240 | case Instruction::CONST_HIGH16: { |
| 2241 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2242 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2243 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2244 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2245 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2246 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2247 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2248 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2249 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2250 | 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] | 2251 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2252 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2253 | } |
| 2254 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2255 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2256 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2257 | 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] | 2258 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2259 | break; |
| 2260 | } |
| 2261 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2262 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2263 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2264 | 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] | 2265 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2266 | break; |
| 2267 | } |
| 2268 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2269 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2270 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2271 | 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] | 2272 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2273 | break; |
| 2274 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2275 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2276 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2277 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2278 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2279 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2280 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2281 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2282 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2283 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2284 | // Get type from instruction if unresolved then we need an access check |
| 2285 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2286 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2287 | // 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] | 2288 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2289 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2290 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2291 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2292 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2293 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2294 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2295 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2296 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2297 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2298 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2299 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2300 | prev_idx--; |
| 2301 | } |
| 2302 | const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx); |
| 2303 | switch (prev_inst->Opcode()) { |
| 2304 | case Instruction::MOVE_OBJECT: |
| 2305 | case Instruction::MOVE_OBJECT_16: |
| 2306 | case Instruction::MOVE_OBJECT_FROM16: |
| 2307 | if (prev_inst->VRegB() == inst->VRegA_11x()) { |
| 2308 | // Redo the copy. This won't change the register types, but update the lock status |
| 2309 | // for the aliased register. |
| 2310 | work_line_->CopyRegister1(this, |
| 2311 | prev_inst->VRegA(), |
| 2312 | prev_inst->VRegB(), |
| 2313 | kTypeCategoryRef); |
| 2314 | } |
| 2315 | break; |
| 2316 | |
| 2317 | default: // Other instruction types ignored. |
| 2318 | break; |
| 2319 | } |
| 2320 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2321 | break; |
| 2322 | case Instruction::MONITOR_EXIT: |
| 2323 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2324 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2325 | * 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] | 2326 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2327 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2328 | * feature that Dalvik doesn't support.) |
| 2329 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2330 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2331 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2332 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2333 | * structured locking checks are working, the former would have |
| 2334 | * failed on the -enter instruction, and the latter is impossible. |
| 2335 | * |
| 2336 | * This is fortunate, because issue 3221411 prevents us from |
| 2337 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2338 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2339 | * some catch blocks (which will show up as "dead" code when |
| 2340 | * we skip them here); if we can't, then the code path could be |
| 2341 | * "live" so we still need to check it. |
| 2342 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2343 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2344 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2345 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2346 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2347 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2348 | /* |
| 2349 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2350 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2351 | * |
| 2352 | * 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] | 2353 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2354 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2355 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 2356 | const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2357 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2358 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2359 | // If this is a primitive type, fail HARD. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2360 | mirror::Class* klass = dex_cache_->GetResolvedType(type_idx); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2361 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2362 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2363 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2364 | << GetDeclaringClass(); |
| 2365 | break; |
| 2366 | } |
| 2367 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2368 | DCHECK_NE(failures_.size(), 0U); |
| 2369 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2370 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2371 | inst->VRegA_22c(), |
| 2372 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2373 | } |
| 2374 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2375 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2376 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2377 | 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] | 2378 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2379 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2380 | if (is_checkcast) { |
| 2381 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2382 | } else { |
| 2383 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2384 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2385 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2386 | if (is_checkcast) { |
| 2387 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2388 | } else { |
| 2389 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2390 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2391 | } else if (orig_type.IsUninitializedTypes()) { |
| 2392 | if (is_checkcast) { |
| 2393 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2394 | << orig_type_reg; |
| 2395 | } else { |
| 2396 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2397 | << orig_type_reg; |
| 2398 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2399 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2400 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2401 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2402 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2403 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2404 | inst->VRegA_22c(), |
| 2405 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2406 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2407 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2408 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2409 | } |
| 2410 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2411 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2412 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2413 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2414 | 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] | 2415 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2416 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2417 | inst->VRegA_12x(), |
| 2418 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2419 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2420 | } else { |
| 2421 | 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] | 2422 | } |
| 2423 | break; |
| 2424 | } |
| 2425 | case Instruction::NEW_INSTANCE: { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2426 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2427 | if (res_type.IsConflict()) { |
| 2428 | DCHECK_NE(failures_.size(), 0U); |
| 2429 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2430 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2431 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2432 | // can't create an instance of an interface or abstract class */ |
| 2433 | if (!res_type.IsInstantiableTypes()) { |
| 2434 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2435 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2436 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2437 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2438 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2439 | // Any registers holding previous allocations from this address that have not yet been |
| 2440 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2441 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2442 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2443 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2444 | break; |
| 2445 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2446 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2447 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2448 | break; |
| 2449 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2450 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2451 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2452 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2453 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2454 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2455 | just_set_result = true; // Filled new array range sets result register |
| 2456 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2457 | case Instruction::CMPL_FLOAT: |
| 2458 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2459 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2460 | break; |
| 2461 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2462 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2463 | break; |
| 2464 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2465 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2466 | break; |
| 2467 | case Instruction::CMPL_DOUBLE: |
| 2468 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2469 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2470 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2471 | break; |
| 2472 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2473 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2474 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2475 | break; |
| 2476 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2477 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2478 | break; |
| 2479 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2480 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2481 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2482 | break; |
| 2483 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2484 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2485 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2486 | break; |
| 2487 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2488 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2489 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2490 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2491 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2492 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2493 | if (res_type.IsUninitializedTypes()) { |
| 2494 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2495 | } else if (!res_type.IsReferenceTypes()) { |
| 2496 | 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] | 2497 | } else { |
| 2498 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2499 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2500 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2501 | } |
| 2502 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2503 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2504 | case Instruction::GOTO: |
| 2505 | case Instruction::GOTO_16: |
| 2506 | case Instruction::GOTO_32: |
| 2507 | /* no effect on or use of registers */ |
| 2508 | break; |
| 2509 | |
| 2510 | case Instruction::PACKED_SWITCH: |
| 2511 | case Instruction::SPARSE_SWITCH: |
| 2512 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2513 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2514 | break; |
| 2515 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2516 | case Instruction::FILL_ARRAY_DATA: { |
| 2517 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2518 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2519 | /* array_type can be null if the reg type is Zero */ |
| 2520 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2521 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2522 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2523 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2524 | } else if (array_type.IsUnresolvedTypes()) { |
| 2525 | // If it's an unresolved array type, it must be non-primitive. |
| 2526 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2527 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2528 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2529 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2530 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2531 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2532 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2533 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2534 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2535 | // Now verify if the element width in the table matches the element width declared in |
| 2536 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2537 | const uint16_t* array_data = |
| 2538 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2539 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2540 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2541 | } else { |
| 2542 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2543 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2544 | // in the table and expected from the array class. |
| 2545 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2546 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2547 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2548 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2549 | } |
| 2550 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2554 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2555 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2556 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2557 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2558 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2559 | bool mismatch = false; |
| 2560 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2561 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2562 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2563 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2564 | } else { // both integral? |
| 2565 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2566 | } |
| 2567 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2568 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2569 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2570 | } |
| 2571 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2572 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2573 | case Instruction::IF_LT: |
| 2574 | case Instruction::IF_GE: |
| 2575 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2576 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2577 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2578 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2579 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2580 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2581 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2582 | } |
| 2583 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2584 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2585 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2586 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2587 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2588 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2589 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2590 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2591 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2592 | |
| 2593 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2594 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2595 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2596 | instance_of_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2597 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2598 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2599 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2600 | if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2601 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2602 | work_insn_idx_)) { |
| 2603 | break; |
| 2604 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2605 | } else { |
| 2606 | break; |
| 2607 | } |
| 2608 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2609 | const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2610 | |
| 2611 | /* Check for peep-hole pattern of: |
| 2612 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2613 | * instance-of vX, vY, T; |
| 2614 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2615 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2616 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2617 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2618 | * and sharpen the type of vY to be type T. |
| 2619 | * Note, this pattern can't be if: |
| 2620 | * - if there are other branches to this branch, |
| 2621 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2622 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2623 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2624 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 2625 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 2626 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2627 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2628 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2629 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2630 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2631 | // and we don't want to disallow field and other operations on the object. If the value |
| 2632 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2633 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2634 | // type is assignable to the original then allow optimization. This check is performed to |
| 2635 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2636 | // interface from a class that would lose information relevant to field checks. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2637 | const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2638 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2639 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2640 | if (!orig_type.Equals(cast_type) && |
| 2641 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2642 | 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] | 2643 | !cast_type.GetClass()->IsInterface() && |
| 2644 | (orig_type.IsZero() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2645 | orig_type.IsStrictlyAssignableFrom( |
| 2646 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 2647 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2648 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2649 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2650 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2651 | branch_line.reset(update_line); |
| 2652 | } |
| 2653 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2654 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2655 | instance_of_inst->VRegB_22c(), |
| 2656 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2657 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2658 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2659 | // register encoding space of instance-of, and propagate type information to the source |
| 2660 | // of the move-object. |
| 2661 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2662 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2663 | move_idx--; |
| 2664 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2665 | if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2666 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2667 | work_insn_idx_)) { |
| 2668 | break; |
| 2669 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2670 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 2671 | switch (move_inst->Opcode()) { |
| 2672 | case Instruction::MOVE_OBJECT: |
| 2673 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2674 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2675 | move_inst->VRegB_12x(), |
| 2676 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2677 | } |
| 2678 | break; |
| 2679 | case Instruction::MOVE_OBJECT_FROM16: |
| 2680 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2681 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2682 | move_inst->VRegB_22x(), |
| 2683 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2684 | } |
| 2685 | break; |
| 2686 | case Instruction::MOVE_OBJECT_16: |
| 2687 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2688 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2689 | move_inst->VRegB_32x(), |
| 2690 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2691 | } |
| 2692 | break; |
| 2693 | default: |
| 2694 | break; |
| 2695 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | } |
| 2699 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2700 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2701 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2702 | case Instruction::IF_LTZ: |
| 2703 | case Instruction::IF_GEZ: |
| 2704 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2705 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2706 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2707 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2708 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2709 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2710 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2711 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2712 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2713 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2714 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2715 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2716 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2717 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2718 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2719 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2720 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2721 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2722 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2723 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2725 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2726 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2727 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2728 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2729 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2730 | break; |
| 2731 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2732 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2733 | break; |
| 2734 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2735 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2736 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2739 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2740 | break; |
| 2741 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2742 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2743 | break; |
| 2744 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2745 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2746 | break; |
| 2747 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2748 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2749 | break; |
| 2750 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2751 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2752 | break; |
| 2753 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2754 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2755 | break; |
| 2756 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2757 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2758 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2759 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2760 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2761 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2762 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2763 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2764 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2765 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2766 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2767 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2768 | break; |
| 2769 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2770 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2771 | break; |
| 2772 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2773 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2774 | break; |
| 2775 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2776 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2777 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2778 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2779 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2780 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2781 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2784 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2785 | break; |
| 2786 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2787 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2788 | break; |
| 2789 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2790 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2791 | break; |
| 2792 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2793 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2794 | break; |
| 2795 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2796 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
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::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2799 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2800 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2801 | break; |
| 2802 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2803 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2804 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2805 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2806 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2807 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2808 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2809 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2810 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2811 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2812 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2813 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2814 | break; |
| 2815 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2816 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2819 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2820 | break; |
| 2821 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2822 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2823 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | |
| 2826 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2827 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2828 | break; |
| 2829 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2830 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2831 | break; |
| 2832 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2833 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2834 | break; |
| 2835 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2836 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2837 | break; |
| 2838 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2839 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2840 | break; |
| 2841 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2842 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2843 | break; |
| 2844 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2845 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2846 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2847 | break; |
| 2848 | |
| 2849 | case Instruction::INVOKE_VIRTUAL: |
| 2850 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2851 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2852 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2853 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2854 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2855 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2856 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2857 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2858 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2859 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2860 | if (called_method != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2861 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2862 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_, |
| 2863 | pointer_size); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2864 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2865 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
| 2866 | return_type_class, |
| 2867 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2868 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2869 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2870 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2874 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2875 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2876 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2877 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2878 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2879 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2880 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2881 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2882 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2883 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2884 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2885 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2886 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2887 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2888 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2889 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2890 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2891 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2892 | const char* return_type_descriptor; |
| 2893 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2894 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2895 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2896 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2897 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2898 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2899 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2900 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2901 | } else { |
| 2902 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2903 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2904 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2905 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_, |
| 2906 | pointer_size); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2907 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2908 | return_type = &FromClass(return_type_descriptor, |
| 2909 | return_type_class, |
| 2910 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2911 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2912 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2913 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2914 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2915 | } |
| 2916 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2917 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2918 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2919 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2920 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2921 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2922 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2923 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2924 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2925 | if (this_type.IsConflict()) // failure. |
| 2926 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2927 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2928 | /* no null refs allowed (?) */ |
| 2929 | if (this_type.IsZero()) { |
| 2930 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2931 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2932 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2933 | |
| 2934 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2935 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2936 | // TODO: re-enable constructor type verification |
| 2937 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2938 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2939 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2940 | // break; |
| 2941 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2942 | |
| 2943 | /* arg must be an uninitialized reference */ |
| 2944 | if (!this_type.IsUninitializedTypes()) { |
| 2945 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2946 | << this_type; |
| 2947 | break; |
| 2948 | } |
| 2949 | |
| 2950 | /* |
| 2951 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2952 | * registers that have the same object instance in them, not just the "this" register. |
| 2953 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 2954 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2955 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2956 | if (return_type == nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2957 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2958 | } |
| 2959 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2960 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2961 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2962 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2963 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2964 | just_set_result = true; |
| 2965 | break; |
| 2966 | } |
| 2967 | case Instruction::INVOKE_STATIC: |
| 2968 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2969 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2970 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2971 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2972 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2973 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2974 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2975 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2976 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2977 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2978 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2979 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2980 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2981 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2982 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2983 | } else { |
| 2984 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2985 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2986 | just_set_result = true; |
| 2987 | } |
| 2988 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2989 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2990 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2991 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2992 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2993 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2994 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2995 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2996 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2997 | << PrettyMethod(abs_method) << "'"; |
| 2998 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2999 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3000 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3001 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3002 | * interface or Object (see comments in RegType::JoinClass). |
| 3003 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3004 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3005 | if (this_type.IsZero()) { |
| 3006 | /* null pointer always passes (and always fails at runtime) */ |
| 3007 | } else { |
| 3008 | if (this_type.IsUninitializedTypes()) { |
| 3009 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3010 | << this_type; |
| 3011 | break; |
| 3012 | } |
| 3013 | // In the past we have tried to assert that "called_interface" is assignable |
| 3014 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3015 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3016 | // implemented by "this_type". For example, two classes may implement the same |
| 3017 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3018 | // join will set "this_type" to the parent class and a test that this implements |
| 3019 | // the interface will incorrectly fail. |
| 3020 | } |
| 3021 | /* |
| 3022 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3023 | * the type information is in the abstract method, so we're good. |
| 3024 | */ |
| 3025 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3026 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3027 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3028 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 3029 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3030 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3031 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3032 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3033 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3034 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3035 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3036 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3037 | } else { |
| 3038 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3039 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3040 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3041 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3042 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3043 | case Instruction::NEG_INT: |
| 3044 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3045 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3046 | break; |
| 3047 | case Instruction::NEG_LONG: |
| 3048 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3049 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3050 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3051 | break; |
| 3052 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3053 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3054 | break; |
| 3055 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3056 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3057 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3058 | break; |
| 3059 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3060 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3061 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3062 | break; |
| 3063 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3064 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3065 | break; |
| 3066 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3067 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3068 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3069 | break; |
| 3070 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3071 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3072 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3073 | break; |
| 3074 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3075 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3076 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3077 | break; |
| 3078 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3079 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3080 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3081 | break; |
| 3082 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3083 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3084 | break; |
| 3085 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3086 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3087 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3088 | break; |
| 3089 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3090 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3091 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3092 | break; |
| 3093 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3094 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3095 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3096 | break; |
| 3097 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3098 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3099 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3100 | break; |
| 3101 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3102 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3103 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3104 | break; |
| 3105 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3106 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3107 | break; |
| 3108 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3109 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3110 | break; |
| 3111 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3112 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3113 | break; |
| 3114 | |
| 3115 | case Instruction::ADD_INT: |
| 3116 | case Instruction::SUB_INT: |
| 3117 | case Instruction::MUL_INT: |
| 3118 | case Instruction::REM_INT: |
| 3119 | case Instruction::DIV_INT: |
| 3120 | case Instruction::SHL_INT: |
| 3121 | case Instruction::SHR_INT: |
| 3122 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3123 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3124 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3125 | break; |
| 3126 | case Instruction::AND_INT: |
| 3127 | case Instruction::OR_INT: |
| 3128 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3129 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3130 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3131 | break; |
| 3132 | case Instruction::ADD_LONG: |
| 3133 | case Instruction::SUB_LONG: |
| 3134 | case Instruction::MUL_LONG: |
| 3135 | case Instruction::DIV_LONG: |
| 3136 | case Instruction::REM_LONG: |
| 3137 | case Instruction::AND_LONG: |
| 3138 | case Instruction::OR_LONG: |
| 3139 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3140 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3141 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3142 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3143 | break; |
| 3144 | case Instruction::SHL_LONG: |
| 3145 | case Instruction::SHR_LONG: |
| 3146 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3147 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3148 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3149 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3150 | break; |
| 3151 | case Instruction::ADD_FLOAT: |
| 3152 | case Instruction::SUB_FLOAT: |
| 3153 | case Instruction::MUL_FLOAT: |
| 3154 | case Instruction::DIV_FLOAT: |
| 3155 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3156 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3157 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3158 | break; |
| 3159 | case Instruction::ADD_DOUBLE: |
| 3160 | case Instruction::SUB_DOUBLE: |
| 3161 | case Instruction::MUL_DOUBLE: |
| 3162 | case Instruction::DIV_DOUBLE: |
| 3163 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3164 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3165 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3166 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3167 | break; |
| 3168 | case Instruction::ADD_INT_2ADDR: |
| 3169 | case Instruction::SUB_INT_2ADDR: |
| 3170 | case Instruction::MUL_INT_2ADDR: |
| 3171 | case Instruction::REM_INT_2ADDR: |
| 3172 | case Instruction::SHL_INT_2ADDR: |
| 3173 | case Instruction::SHR_INT_2ADDR: |
| 3174 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3175 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3176 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3177 | break; |
| 3178 | case Instruction::AND_INT_2ADDR: |
| 3179 | case Instruction::OR_INT_2ADDR: |
| 3180 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3181 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3182 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3183 | break; |
| 3184 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3185 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3186 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3187 | break; |
| 3188 | case Instruction::ADD_LONG_2ADDR: |
| 3189 | case Instruction::SUB_LONG_2ADDR: |
| 3190 | case Instruction::MUL_LONG_2ADDR: |
| 3191 | case Instruction::DIV_LONG_2ADDR: |
| 3192 | case Instruction::REM_LONG_2ADDR: |
| 3193 | case Instruction::AND_LONG_2ADDR: |
| 3194 | case Instruction::OR_LONG_2ADDR: |
| 3195 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3196 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3197 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3198 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3199 | break; |
| 3200 | case Instruction::SHL_LONG_2ADDR: |
| 3201 | case Instruction::SHR_LONG_2ADDR: |
| 3202 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3203 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3204 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3205 | break; |
| 3206 | case Instruction::ADD_FLOAT_2ADDR: |
| 3207 | case Instruction::SUB_FLOAT_2ADDR: |
| 3208 | case Instruction::MUL_FLOAT_2ADDR: |
| 3209 | case Instruction::DIV_FLOAT_2ADDR: |
| 3210 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3211 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3212 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3213 | break; |
| 3214 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3215 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3216 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3217 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3218 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3219 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3220 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3221 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3222 | break; |
| 3223 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3224 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3225 | case Instruction::MUL_INT_LIT16: |
| 3226 | case Instruction::DIV_INT_LIT16: |
| 3227 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3228 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3229 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3230 | break; |
| 3231 | case Instruction::AND_INT_LIT16: |
| 3232 | case Instruction::OR_INT_LIT16: |
| 3233 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3234 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3235 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3236 | break; |
| 3237 | case Instruction::ADD_INT_LIT8: |
| 3238 | case Instruction::RSUB_INT_LIT8: |
| 3239 | case Instruction::MUL_INT_LIT8: |
| 3240 | case Instruction::DIV_INT_LIT8: |
| 3241 | case Instruction::REM_INT_LIT8: |
| 3242 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3243 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3244 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3245 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3246 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3247 | break; |
| 3248 | case Instruction::AND_INT_LIT8: |
| 3249 | case Instruction::OR_INT_LIT8: |
| 3250 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3251 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3252 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3253 | break; |
| 3254 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3255 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3256 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3257 | if (IsConstructor() && !IsStatic()) { |
| 3258 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3259 | if (declaring_class.IsUnresolvedReference()) { |
| 3260 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3261 | // manually over the underlying dex file. |
| 3262 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3263 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
| 3264 | if (first_index != DexFile::kDexNoIndex) { |
| 3265 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3266 | << first_index; |
| 3267 | } |
| 3268 | break; |
| 3269 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3270 | auto* klass = declaring_class.GetClass(); |
| 3271 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3272 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3273 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
| 3274 | << PrettyField(klass->GetInstanceField(i)); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3275 | break; |
| 3276 | } |
| 3277 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3278 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3279 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3280 | // quickened opcodes (otherwise this could be a fall-through). |
| 3281 | if (!IsConstructor()) { |
| 3282 | if (!GetMethodReturnType().IsConflict()) { |
| 3283 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3284 | } |
| 3285 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3286 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3287 | // Note: the following instructions encode offsets derived from class linking. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 3288 | // As such they use Class*/Field*/Executable* as these offsets only have |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3289 | // meaning if the class linking and resolution were successful. |
| 3290 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3291 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3292 | break; |
| 3293 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3294 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3295 | break; |
| 3296 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3297 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3298 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3299 | case Instruction::IGET_BOOLEAN_QUICK: |
| 3300 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 3301 | break; |
| 3302 | case Instruction::IGET_BYTE_QUICK: |
| 3303 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 3304 | break; |
| 3305 | case Instruction::IGET_CHAR_QUICK: |
| 3306 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 3307 | break; |
| 3308 | case Instruction::IGET_SHORT_QUICK: |
| 3309 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 3310 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3311 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3312 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3313 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3314 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3315 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3316 | break; |
| 3317 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3318 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3319 | break; |
| 3320 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3321 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3322 | break; |
| 3323 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3324 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3325 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3326 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3327 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3328 | break; |
| 3329 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3330 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3331 | break; |
| 3332 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 3333 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 3334 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3335 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3336 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3337 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3338 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3339 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3340 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3341 | } else { |
| 3342 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3343 | } |
| 3344 | just_set_result = true; |
| 3345 | } |
| 3346 | break; |
| 3347 | } |
| 3348 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3349 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3350 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3351 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
| 3352 | case Instruction::UNUSED_FC ... Instruction::UNUSED_FF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3353 | case Instruction::UNUSED_79: |
| 3354 | case Instruction::UNUSED_7A: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3355 | case Instruction::INVOKE_POLYMORPHIC: |
| 3356 | case Instruction::INVOKE_POLYMORPHIC_RANGE: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3357 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3358 | break; |
| 3359 | |
| 3360 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3361 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3362 | * complain if an instruction is missing (which is desirable). |
| 3363 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3364 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3365 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3366 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3367 | if (Runtime::Current()->IsAotCompiler()) { |
| 3368 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3369 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3370 | LOG(ERROR) << "Pending failures:"; |
| 3371 | for (auto& error : failures_) { |
| 3372 | LOG(ERROR) << error; |
| 3373 | } |
| 3374 | for (auto& error_msg : failure_messages_) { |
| 3375 | LOG(ERROR) << error_msg->str(); |
| 3376 | } |
| 3377 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3378 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3379 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3380 | /* immediate failure, reject class */ |
| 3381 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3382 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3383 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3384 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3385 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3386 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3387 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3388 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3389 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3390 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3391 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3392 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3393 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3394 | */ |
| 3395 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3396 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3399 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3400 | |
| 3401 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3402 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3403 | * |
| 3404 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3405 | * 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] | 3406 | * somebody could get a reference field, check it for zero, and if the |
| 3407 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3408 | * 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] | 3409 | * that, and will reject the code. |
| 3410 | * |
| 3411 | * TODO: avoid re-fetching the branch target |
| 3412 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3413 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3414 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3415 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3416 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3417 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3418 | return false; |
| 3419 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3420 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3421 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3422 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3423 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3424 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3425 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3426 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3427 | return false; |
| 3428 | } |
| 3429 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3430 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3431 | return false; |
| 3432 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3433 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3437 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3438 | * |
| 3439 | * We've already verified that the table is structurally sound, so we |
| 3440 | * just need to walk through and tag the targets. |
| 3441 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3442 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3443 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3444 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3445 | int switch_count = switch_insns[1]; |
| 3446 | int offset_to_targets, targ; |
| 3447 | |
| 3448 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3449 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3450 | offset_to_targets = 4; |
| 3451 | } else { |
| 3452 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3453 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3454 | offset_to_targets = 2 + 2 * switch_count; |
| 3455 | } |
| 3456 | |
| 3457 | /* verify each switch target */ |
| 3458 | for (targ = 0; targ < switch_count; targ++) { |
| 3459 | int offset; |
| 3460 | uint32_t abs_offset; |
| 3461 | |
| 3462 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3463 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3464 | (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] | 3465 | abs_offset = work_insn_idx_ + offset; |
| 3466 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3467 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3468 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3469 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3470 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3471 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3472 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3477 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3478 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3479 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3480 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3481 | bool has_catch_all_handler = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3482 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3483 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3484 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3485 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3486 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3487 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3488 | uint16_t handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3489 | if (handler_type_idx == DexFile::kDexNoIndex16) { |
| 3490 | has_catch_all_handler = true; |
| 3491 | } else { |
| 3492 | // It is also a catch-all if it is java.lang.Throwable. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3493 | mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_, |
| 3494 | class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3495 | if (klass != nullptr) { |
| 3496 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3497 | has_catch_all_handler = true; |
| 3498 | } |
| 3499 | } else { |
| 3500 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3501 | DCHECK(self_->IsExceptionPending()); |
| 3502 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3503 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3504 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3505 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3506 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3507 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3508 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3509 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3510 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3511 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3512 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3516 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3517 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3518 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3519 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3520 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3521 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3522 | * 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] | 3523 | * it will do so before grabbing the lock). |
| 3524 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3525 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3526 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3527 | << "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] | 3528 | return false; |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3533 | /* Handle "continue". Tag the next consecutive instruction. |
| 3534 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3535 | * because it changes work_line_ when performing peephole optimization |
| 3536 | * and this change should not be used in those cases. |
| 3537 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3538 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3539 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3540 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3541 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 3542 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3543 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3544 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3545 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3546 | // next instruction isn't one. |
| 3547 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 3548 | return false; |
| 3549 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3550 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3551 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3552 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3553 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3554 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3555 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3556 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3557 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3558 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3559 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3560 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3561 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3562 | // needed. If the merge changes the state of the registers then the work line will be |
| 3563 | // updated. |
| 3564 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3565 | return false; |
| 3566 | } |
| 3567 | } else { |
| 3568 | /* |
| 3569 | * We're not recording register data for the next instruction, so we don't know what the |
| 3570 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3571 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3572 | GetInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3573 | } |
| 3574 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3575 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3576 | /* 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] | 3577 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3578 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3582 | * Update start_guess. Advance to the next instruction of that's |
| 3583 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3584 | * neither of those exists we're in a return or throw; leave start_guess |
| 3585 | * alone and let the caller sort it out. |
| 3586 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3587 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3588 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3589 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3590 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3591 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3592 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3593 | } |
| 3594 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3595 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3596 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3597 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3598 | if (have_pending_runtime_throw_failure_) { |
| 3599 | have_any_pending_runtime_throw_failure_ = true; |
| 3600 | // Reset the pending_runtime_throw flag now. |
| 3601 | have_pending_runtime_throw_failure_ = false; |
| 3602 | } |
| 3603 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3604 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3605 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3606 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3607 | void MethodVerifier::UninstantiableError(const char* descriptor) { |
| 3608 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 3609 | << "non-instantiable klass " << descriptor; |
| 3610 | } |
| 3611 | |
| 3612 | inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) { |
| 3613 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 3614 | } |
| 3615 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3616 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3617 | mirror::Class* klass = dex_cache_->GetResolvedType(class_idx); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3618 | const RegType* result = nullptr; |
| 3619 | if (klass != nullptr) { |
| 3620 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3621 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3622 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3623 | UninstantiableError(descriptor); |
| 3624 | precise = false; |
| 3625 | } |
| 3626 | result = reg_types_.FindClass(klass, precise); |
| 3627 | if (result == nullptr) { |
| 3628 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3629 | result = reg_types_.InsertClass(descriptor, klass, precise); |
| 3630 | } |
| 3631 | } else { |
| 3632 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3633 | result = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3634 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3635 | DCHECK(result != nullptr); |
| 3636 | if (result->IsConflict()) { |
| 3637 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3638 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3639 | << "' in " << GetDeclaringClass(); |
| 3640 | return *result; |
| 3641 | } |
| 3642 | if (klass == nullptr && !result->IsUnresolvedTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3643 | klass = result->GetClass(); |
| 3644 | dex_cache_->SetResolvedType(class_idx, klass); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3645 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3646 | |
| 3647 | // Record result of class resolution attempt. |
| 3648 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass); |
| 3649 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3650 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3651 | // check at runtime if access is allowed and so pass here. If result is |
| 3652 | // primitive, skip the access check. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3653 | if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) { |
| 3654 | const RegType& referrer = GetDeclaringClass(); |
| 3655 | if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) { |
| 3656 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
| 3657 | << referrer << "' -> '" << result << "'"; |
| 3658 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3659 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3660 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3663 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3664 | const RegType* common_super = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3665 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3666 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3667 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3668 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3669 | CatchHandlerIterator iterator(handlers_ptr); |
| 3670 | for (; iterator.HasNext(); iterator.Next()) { |
| 3671 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3672 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3673 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3674 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3675 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3676 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3677 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3678 | if (exception.IsUnresolvedTypes()) { |
| 3679 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3680 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3681 | return exception; |
| 3682 | } else { |
| 3683 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3684 | return reg_types_.Conflict(); |
| 3685 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3686 | } else if (common_super == nullptr) { |
| 3687 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3688 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3689 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3690 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3691 | common_super = &common_super->Merge(exception, ®_types_, this); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3692 | if (FailOrAbort(this, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3693 | reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3694 | *common_super, this), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3695 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3696 | work_insn_idx_)) { |
| 3697 | break; |
| 3698 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | } |
| 3702 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3703 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3704 | } |
| 3705 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3706 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3707 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3708 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3709 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3710 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3711 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3712 | } |
| 3713 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3714 | inline static MethodResolutionKind GetMethodResolutionKind( |
| 3715 | MethodType method_type, bool is_interface) { |
| 3716 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
| 3717 | return kDirectMethodResolution; |
| 3718 | } else if (method_type == METHOD_INTERFACE) { |
| 3719 | return kInterfaceMethodResolution; |
| 3720 | } else if (method_type == METHOD_SUPER && is_interface) { |
| 3721 | return kInterfaceMethodResolution; |
| 3722 | } else { |
| 3723 | DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER); |
| 3724 | return kVirtualMethodResolution; |
| 3725 | } |
| 3726 | } |
| 3727 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3728 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3729 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3730 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3731 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3732 | if (klass_type.IsConflict()) { |
| 3733 | std::string append(" in attempt to access method "); |
| 3734 | append += dex_file_->GetMethodName(method_id); |
| 3735 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3736 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3737 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3738 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3739 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3740 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3741 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3742 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3743 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3744 | auto pointer_size = cl->GetImagePointerSize(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3745 | MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface()); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3746 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3747 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3748 | bool stash_method = false; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3749 | if (res_method == nullptr) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3750 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3751 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3752 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3753 | if (res_kind == kDirectMethodResolution) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3754 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3755 | } else if (res_kind == kVirtualMethodResolution) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3756 | res_method = klass->FindVirtualMethod(name, signature, pointer_size); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3757 | } else { |
| 3758 | DCHECK_EQ(res_kind, kInterfaceMethodResolution); |
| 3759 | res_method = klass->FindInterfaceMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3760 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3761 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3762 | if (res_method != nullptr) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3763 | stash_method = true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3764 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3765 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3766 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3767 | // a class has changed, and will be flagged as an error in later checks. |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3768 | // Note that in this case, we do not put the resolved method in the Dex cache |
| 3769 | // because it was not discovered using the expected type of method resolution. |
| 3770 | if (res_kind != kDirectMethodResolution) { |
| 3771 | // Record result of the initial resolution attempt. |
| 3772 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr); |
| 3773 | // Change resolution type to 'direct' and try to resolve again. |
| 3774 | res_kind = kDirectMethodResolution; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3775 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3776 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3777 | } |
| 3778 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3779 | |
| 3780 | // Record result of method resolution attempt. |
| 3781 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method); |
| 3782 | |
| 3783 | if (res_method == nullptr) { |
| 3784 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 3785 | << PrettyDescriptor(klass) << "." |
| 3786 | << dex_file_->GetMethodName(method_id) << " " |
| 3787 | << dex_file_->GetMethodSignature(method_id); |
| 3788 | return nullptr; |
| 3789 | } |
| 3790 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3791 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3792 | // enforce them here. |
| 3793 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3794 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 3795 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3796 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3797 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3798 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3799 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3800 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 3801 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3802 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3803 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3804 | |
| 3805 | // Check that interface methods are static or match interface classes. |
| 3806 | // We only allow statics if we don't have default methods enabled. |
| 3807 | // |
| 3808 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3809 | // while this check implies an IncompatibleClassChangeError. |
| 3810 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3811 | // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if |
| 3812 | // dex file version is 37 or greater), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3813 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3814 | method_type != METHOD_STATIC && |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3815 | ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) || |
| 3816 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3817 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3818 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
| 3819 | << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_) |
| 3820 | << " is in an interface class " << PrettyClass(klass); |
| 3821 | return nullptr; |
| 3822 | } |
| 3823 | } else { |
| 3824 | if (method_type == METHOD_INTERFACE) { |
| 3825 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
| 3826 | << "interface method " << PrettyMethod(dex_method_idx, *dex_file_) |
| 3827 | << " is in a non-interface class " << PrettyClass(klass); |
| 3828 | return nullptr; |
| 3829 | } |
| 3830 | } |
| 3831 | |
| 3832 | // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct. |
| 3833 | if (stash_method) { |
| 3834 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size); |
| 3835 | } |
| 3836 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3837 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3838 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3839 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3840 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3841 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3842 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3843 | // 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] | 3844 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3845 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3846 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3847 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3848 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3849 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3850 | // target method. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3851 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3852 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3853 | ((method_type == METHOD_SUPER || |
| 3854 | method_type == METHOD_VIRTUAL || |
| 3855 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3856 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3857 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3858 | " type of " << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3859 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3860 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3861 | return res_method; |
| 3862 | } |
| 3863 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3864 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3865 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 3866 | 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] | 3867 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3868 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3869 | // definition (which doesn't have register count values). |
| 3870 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3871 | /* caught by static verifier */ |
| 3872 | DCHECK(is_range || expected_args <= 5); |
| 3873 | if (expected_args > code_item_->outs_size_) { |
| 3874 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3875 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3876 | return nullptr; |
| 3877 | } |
| 3878 | |
| 3879 | uint32_t arg[5]; |
| 3880 | if (!is_range) { |
| 3881 | inst->GetVarArgs(arg); |
| 3882 | } |
| 3883 | uint32_t sig_registers = 0; |
| 3884 | |
| 3885 | /* |
| 3886 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3887 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3888 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3889 | */ |
| 3890 | if (method_type != METHOD_STATIC) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3891 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3892 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3893 | CHECK(have_pending_hard_failure_); |
| 3894 | return nullptr; |
| 3895 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3896 | bool is_init = false; |
| 3897 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3898 | if (res_method) { |
| 3899 | if (!res_method->IsConstructor()) { |
| 3900 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3901 | return nullptr; |
| 3902 | } |
| 3903 | } else { |
| 3904 | // Check whether the name of the called method is "<init>" |
| 3905 | 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] | 3906 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3907 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3908 | return nullptr; |
| 3909 | } |
| 3910 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3911 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3912 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3913 | const RegType& adjusted_type = is_init |
| 3914 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 3915 | : actual_arg_type; |
| 3916 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3917 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 3918 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 3919 | // class. It would be wrong to use this for the type check (interface type checks are |
| 3920 | // postponed to runtime). |
| 3921 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3922 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3923 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3924 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 3925 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3926 | } else { |
| 3927 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3928 | const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3929 | res_method_class = ®_types_.FromDescriptor( |
| 3930 | GetClassLoader(), |
| 3931 | dex_file_->StringByTypeIdx(class_idx), |
| 3932 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3933 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3934 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3935 | Fail(adjusted_type.IsUnresolvedTypes() |
| 3936 | ? VERIFY_ERROR_NO_CLASS |
| 3937 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 3938 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 3939 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3940 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3941 | // the compiler. |
| 3942 | if (have_pending_hard_failure_) { |
| 3943 | return nullptr; |
| 3944 | } |
| 3945 | } |
| 3946 | } |
| 3947 | sig_registers = 1; |
| 3948 | } |
| 3949 | |
| 3950 | for ( ; it->HasNext(); it->Next()) { |
| 3951 | if (sig_registers >= expected_args) { |
| 3952 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
| 3953 | " arguments, found " << sig_registers << " or more."; |
| 3954 | return nullptr; |
| 3955 | } |
| 3956 | |
| 3957 | const char* param_descriptor = it->GetDescriptor(); |
| 3958 | |
| 3959 | if (param_descriptor == nullptr) { |
| 3960 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 3961 | "component"; |
| 3962 | return nullptr; |
| 3963 | } |
| 3964 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3965 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3966 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) : |
| 3967 | arg[sig_registers]; |
| 3968 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3969 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3970 | if (!src_type.IsIntegralTypes()) { |
| 3971 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3972 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3973 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3974 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 3975 | } else { |
| 3976 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 3977 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3978 | // the compiler. |
| 3979 | if (have_pending_hard_failure_) { |
| 3980 | return nullptr; |
| 3981 | } |
| 3982 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 3983 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 3984 | // instructions not specifying register pairs by the first component, but require them |
| 3985 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 3986 | // none, this will fail below. |
| 3987 | if (!is_range && sig_registers + 1 < expected_args) { |
| 3988 | uint32_t second_reg = arg[sig_registers + 1]; |
| 3989 | if (second_reg != get_reg + 1) { |
| 3990 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 3991 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 3992 | << second_reg << "."; |
| 3993 | return nullptr; |
| 3994 | } |
| 3995 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3996 | } |
| 3997 | } |
| 3998 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 3999 | } |
| 4000 | if (expected_args != sig_registers) { |
| 4001 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
| 4002 | " arguments, found " << sig_registers; |
| 4003 | return nullptr; |
| 4004 | } |
| 4005 | return res_method; |
| 4006 | } |
| 4007 | |
| 4008 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4009 | MethodType method_type, |
| 4010 | bool is_range) { |
| 4011 | // As the method may not have been resolved, make this static check against what we expect. |
| 4012 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4013 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
| 4014 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 4015 | DexFileParameterIterator it(*dex_file_, |
| 4016 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
| 4017 | VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range, |
| 4018 | nullptr); |
| 4019 | } |
| 4020 | |
| 4021 | class MethodParamListDescriptorIterator { |
| 4022 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4023 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4024 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4025 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4026 | } |
| 4027 | |
| 4028 | bool HasNext() { |
| 4029 | return pos_ < params_size_; |
| 4030 | } |
| 4031 | |
| 4032 | void Next() { |
| 4033 | ++pos_; |
| 4034 | } |
| 4035 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4036 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4037 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4038 | } |
| 4039 | |
| 4040 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4041 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4042 | size_t pos_; |
| 4043 | const DexFile::TypeList* params_; |
| 4044 | const size_t params_size_; |
| 4045 | }; |
| 4046 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4047 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4048 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4049 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4050 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4051 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | acc4d2f | 2014-06-12 19:35:05 -0700 | [diff] [blame] | 4052 | |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4053 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4054 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4055 | // Check what we can statically. |
| 4056 | if (!have_pending_hard_failure_) { |
| 4057 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4058 | } |
| 4059 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4060 | } |
| 4061 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4062 | // 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] | 4063 | // 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] | 4064 | if (method_type == METHOD_SUPER) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4065 | uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4066 | const RegType& reference_type = reg_types_.FromDescriptor( |
| 4067 | GetClassLoader(), |
| 4068 | dex_file_->StringByTypeIdx(class_idx), |
| 4069 | false); |
| 4070 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4071 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4072 | return nullptr; |
| 4073 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4074 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4075 | // TODO Can we verify anything else. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 4076 | if (class_idx == class_def_.class_idx_) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4077 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface"; |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4078 | return nullptr; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4079 | } |
| 4080 | // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS |
| 4081 | // does. |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4082 | if (!GetDeclaringClass().HasClass()) { |
| 4083 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4084 | << "interface invoke-super"; |
| 4085 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4086 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4087 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4088 | << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method " |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4089 | << PrettyMethod(dex_method_idx_, *dex_file_) << " to method " |
| 4090 | << PrettyMethod(method_idx, *dex_file_) << " references " |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4091 | << "non-super-interface type " << PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4092 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4093 | } |
| 4094 | } else { |
| 4095 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4096 | if (super.IsUnresolvedTypes()) { |
| 4097 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
| 4098 | << PrettyMethod(dex_method_idx_, *dex_file_) |
| 4099 | << " to super " << PrettyMethod(res_method); |
| 4100 | return nullptr; |
| 4101 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4102 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4103 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4104 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
| 4105 | << PrettyMethod(dex_method_idx_, *dex_file_) |
| 4106 | << " to super " << super |
| 4107 | << "." << res_method->GetName() |
| 4108 | << res_method->GetSignature(); |
| 4109 | return nullptr; |
| 4110 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4111 | } |
| 4112 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4113 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4114 | // Process the target method's signature. This signature may or may not |
| 4115 | MethodParamListDescriptorIterator it(res_method); |
| 4116 | return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type, |
| 4117 | is_range, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4120 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 4121 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4122 | if (is_range) { |
| 4123 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 4124 | } else { |
| 4125 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 4126 | } |
| 4127 | const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4128 | if (!actual_arg_type.HasClass()) { |
| 4129 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 4130 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4131 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4132 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4133 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4134 | if (klass->IsInterface()) { |
| 4135 | // Derive Object.class from Class.class.getSuperclass(). |
| 4136 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4137 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4138 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4139 | return nullptr; |
| 4140 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4141 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4142 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4143 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4144 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4145 | if (!dispatch_class->HasVTable()) { |
| 4146 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 4147 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4148 | return nullptr; |
| 4149 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4150 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4151 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 4152 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4153 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 4154 | FailOrAbort(this, allow_failure, |
| 4155 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 4156 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4157 | return nullptr; |
| 4158 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4159 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4160 | if (self_->IsExceptionPending()) { |
| 4161 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 4162 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4163 | return nullptr; |
| 4164 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4165 | return res_method; |
| 4166 | } |
| 4167 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4168 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4169 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 4170 | << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_; |
| 4171 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4172 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4173 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4174 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4175 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4176 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4177 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 4178 | work_insn_idx_)) { |
| 4179 | return nullptr; |
| 4180 | } |
| 4181 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 4182 | work_insn_idx_)) { |
| 4183 | return nullptr; |
| 4184 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4185 | |
| 4186 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4187 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4188 | // definition (which doesn't have register count values). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4189 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4190 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4191 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4192 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4193 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4194 | /* caught by static verifier */ |
| 4195 | DCHECK(is_range || expected_args <= 5); |
| 4196 | if (expected_args > code_item_->outs_size_) { |
| 4197 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 4198 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4199 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | /* |
| 4203 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4204 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4205 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4206 | */ |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4207 | // Note: given an uninitialized type, this should always fail. Constructors aren't virtual. |
| 4208 | if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4209 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4210 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4211 | } |
| 4212 | if (!actual_arg_type.IsZero()) { |
| 4213 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4214 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4215 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4216 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4217 | if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4218 | Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never |
| 4219 | ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this. |
| 4220 | : actual_arg_type.IsUnresolvedTypes() |
| 4221 | ? VERIFY_ERROR_NO_CLASS |
| 4222 | : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4223 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4224 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | /* |
| 4228 | * Process the target method's signature. This signature may or may not |
| 4229 | * have been verified, so we can't assume it's properly formed. |
| 4230 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4231 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4232 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4233 | uint32_t arg[5]; |
| 4234 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4235 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4236 | } |
| 4237 | size_t actual_args = 1; |
| 4238 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 4239 | if (actual_args >= expected_args) { |
| 4240 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4241 | << "'. Expected " << expected_args |
| 4242 | << " arguments, processing argument " << actual_args |
| 4243 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4244 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4245 | } |
| 4246 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4247 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4248 | if (descriptor == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4249 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4250 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4251 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4252 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4253 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4254 | 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] | 4255 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4256 | return res_method; |
| 4257 | } |
| 4258 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 4259 | } |
| 4260 | if (actual_args != expected_args) { |
| 4261 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 4262 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4263 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4264 | } else { |
| 4265 | return res_method; |
| 4266 | } |
| 4267 | } |
| 4268 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 4269 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4270 | uint32_t type_idx; |
| 4271 | if (!is_filled) { |
| 4272 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 4273 | type_idx = inst->VRegC_22c(); |
| 4274 | } else if (!is_range) { |
| 4275 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 4276 | type_idx = inst->VRegB_35c(); |
| 4277 | } else { |
| 4278 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 4279 | type_idx = inst->VRegB_3rc(); |
| 4280 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4281 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4282 | if (res_type.IsConflict()) { // bad class |
| 4283 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4284 | } else { |
| 4285 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4286 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4287 | 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] | 4288 | } else if (!is_filled) { |
| 4289 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4290 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4291 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4292 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4293 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4294 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4295 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4296 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4297 | // 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] | 4298 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4299 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4300 | uint32_t arg[5]; |
| 4301 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4302 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4303 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4304 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4305 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4306 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4307 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4308 | return; |
| 4309 | } |
| 4310 | } |
| 4311 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4312 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4313 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4314 | } |
| 4315 | } |
| 4316 | } |
| 4317 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4318 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4319 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4320 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4321 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4322 | 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] | 4323 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4324 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4325 | if (array_type.IsZero()) { |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 4326 | have_pending_runtime_throw_failure_ = true; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4327 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 4328 | // instruction type. TODO: have a proper notion of bottom here. |
| 4329 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 4330 | // Reference or category 1 |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4331 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4332 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4333 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4334 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4335 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4336 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4337 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4338 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4339 | 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] | 4340 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4341 | // Unresolved array types must be reference array types. |
| 4342 | if (is_primitive) { |
| 4343 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4344 | << " source for category 1 aget"; |
| 4345 | } else { |
| 4346 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4347 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4348 | // Approximate with java.lang.Object[]. |
| 4349 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4350 | inst->VRegA_23x(), |
| 4351 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4352 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4353 | } else { |
| 4354 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4355 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4356 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4357 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4358 | << " source for aget-object"; |
| 4359 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4360 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4361 | << " source for category 1 aget"; |
| 4362 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4363 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4364 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4365 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4366 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4367 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4368 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4369 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4370 | // doubles. |
| 4371 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4372 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4373 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4374 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4375 | component_type.HighHalf(®_types_)); |
| 4376 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4382 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4383 | const uint32_t vregA) { |
| 4384 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4385 | bool instruction_compatible; |
| 4386 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4387 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4388 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4389 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4390 | value_compatible = value_type.IsIntegralTypes(); |
| 4391 | } else if (target_type.IsFloat()) { |
| 4392 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4393 | value_compatible = value_type.IsFloatTypes(); |
| 4394 | } else if (target_type.IsLong()) { |
| 4395 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4396 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4397 | // as target_type depends on the resolved type of the field. |
| 4398 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4399 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4400 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4401 | } else { |
| 4402 | value_compatible = false; |
| 4403 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4404 | } else if (target_type.IsDouble()) { |
| 4405 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4406 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4407 | // as target_type depends on the resolved type of the field. |
| 4408 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4409 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4410 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4411 | } else { |
| 4412 | value_compatible = false; |
| 4413 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4414 | } else { |
| 4415 | instruction_compatible = false; // reference with primitive store |
| 4416 | value_compatible = false; // unused |
| 4417 | } |
| 4418 | if (!instruction_compatible) { |
| 4419 | // This is a global failure rather than a class change failure as the instructions and |
| 4420 | // the descriptors for the type should have been consistent within the same file at |
| 4421 | // compile time. |
| 4422 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4423 | << "' but expected type '" << target_type << "'"; |
| 4424 | return; |
| 4425 | } |
| 4426 | if (!value_compatible) { |
| 4427 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4428 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4429 | return; |
| 4430 | } |
| 4431 | } |
| 4432 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4433 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4434 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4435 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4436 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4437 | 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] | 4438 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4439 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4440 | if (array_type.IsZero()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4441 | // Null array type; this code path will fail at runtime. |
| 4442 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4443 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4444 | // and fits multiple register types. |
| 4445 | const RegType* modified_reg_type = &insn_type; |
| 4446 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4447 | (modified_reg_type == ®_types_.LongLo())) { |
| 4448 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4449 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4450 | if (modified_reg_type == ®_types_.Integer()) { |
| 4451 | if (&value_type == ®_types_.Float()) { |
| 4452 | modified_reg_type = &value_type; |
| 4453 | } |
| 4454 | } else { |
| 4455 | if (&value_type == ®_types_.DoubleLo()) { |
| 4456 | modified_reg_type = &value_type; |
| 4457 | } |
| 4458 | } |
| 4459 | } |
| 4460 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4461 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4462 | 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] | 4463 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4464 | // Unresolved array types must be reference array types. |
| 4465 | if (is_primitive) { |
| 4466 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4467 | << "' but unresolved type '" << array_type << "'"; |
| 4468 | } else { |
| 4469 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4470 | << " because of missing class"; |
| 4471 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4472 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4473 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4474 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4475 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4476 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4477 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4478 | if (!component_type.IsReferenceTypes()) { |
| 4479 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4480 | << " source for aput-object"; |
| 4481 | } else { |
| 4482 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4483 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4484 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4485 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4486 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | } |
| 4491 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4492 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4493 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4494 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4495 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4496 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4497 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4498 | field_idx, dex_file_->GetFieldName(field_id), |
| 4499 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4500 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4501 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4502 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4503 | 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] | 4504 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4505 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4506 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_); |
| 4507 | |
| 4508 | // Record result of the field resolution attempt. |
| 4509 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4510 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4511 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4512 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4513 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4514 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4515 | DCHECK(self_->IsExceptionPending()); |
| 4516 | self_->ClearException(); |
| 4517 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4518 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4519 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4520 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4521 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4522 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4523 | } else if (!field->IsStatic()) { |
| 4524 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4525 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4526 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4527 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4528 | } |
| 4529 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4530 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4531 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4532 | // Check access to class. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4533 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4534 | if (klass_type.IsConflict()) { |
| 4535 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4536 | field_idx, dex_file_->GetFieldName(field_id), |
| 4537 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4538 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4539 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4540 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4541 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4542 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4543 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4544 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_); |
| 4545 | |
| 4546 | // Record result of the field resolution attempt. |
| 4547 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4548 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4549 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4550 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4551 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4552 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4553 | DCHECK(self_->IsExceptionPending()); |
| 4554 | self_->ClearException(); |
| 4555 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4556 | } else if (obj_type.IsZero()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4557 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4558 | // Fall through into a few last soft failure checks below. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4559 | } else if (!obj_type.IsReferenceTypes()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4560 | // Trying to read a field from something that isn't a reference. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4561 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4562 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4563 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4564 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4565 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4566 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4567 | const RegType& field_klass = |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4568 | FromClass(klass->GetDescriptor(&temp), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame^] | 4569 | klass.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4570 | klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4571 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4572 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4573 | // the field is declared in this class. |
| 4574 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4575 | // appear in constructors. |
| 4576 | if (!obj_type.IsUninitializedThisReference() || |
| 4577 | !IsConstructor() || |
| 4578 | !field_klass.Equals(GetDeclaringClass())) { |
| 4579 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
| 4580 | << " of a not fully initialized object within the context" |
| 4581 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
| 4582 | return nullptr; |
| 4583 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4584 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4585 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4586 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4587 | // 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] | 4588 | VerifyError type; |
| 4589 | bool is_aot = Runtime::Current()->IsAotCompiler(); |
| 4590 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4591 | // Compiler & unresolved types involved, retry at runtime. |
| 4592 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4593 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4594 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4595 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4596 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4597 | } |
| 4598 | Fail(type) << "cannot access instance field " << PrettyField(field) |
| 4599 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4600 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4601 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4602 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4603 | |
| 4604 | // Few last soft failure checks. |
| 4605 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4606 | field->GetAccessFlags())) { |
| 4607 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
| 4608 | << " from " << GetDeclaringClass(); |
| 4609 | return nullptr; |
| 4610 | } else if (field->IsStatic()) { |
| 4611 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 4612 | << " to not be static"; |
| 4613 | return nullptr; |
| 4614 | } |
| 4615 | |
| 4616 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4619 | template <MethodVerifier::FieldAccessType kAccType> |
| 4620 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4621 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4622 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4623 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4624 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4625 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4626 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4627 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4628 | |
| 4629 | // One is not allowed to access fields on uninitialized references, except to write to |
| 4630 | // fields in the constructor (before calling another constructor). |
| 4631 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 4632 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 4633 | // checks at the same time that we're verifying a constructor). |
| 4634 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 4635 | object_type.IsUninitializedThisReference(); |
| 4636 | const RegType& adjusted_type = should_adjust |
| 4637 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 4638 | : object_type; |
| 4639 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4640 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 4641 | return; |
| 4642 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4643 | if (should_adjust) { |
| 4644 | if (field == nullptr) { |
| 4645 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 4646 | << "to the superclass being initialized in " |
| 4647 | << PrettyMethod(dex_method_idx_, *dex_file_); |
| 4648 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4649 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
| 4650 | << PrettyField(field) << " of a not fully initialized " |
| 4651 | << "object within the context of " |
| 4652 | << PrettyMethod(dex_method_idx_, *dex_file_); |
| 4653 | return; |
| 4654 | } |
| 4655 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4656 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4657 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4658 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4659 | if (kAccType == FieldAccessType::kAccPut) { |
| 4660 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4661 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 4662 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 4663 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4664 | } |
| 4665 | } |
| 4666 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4667 | ObjPtr<mirror::Class> field_type_class = |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4668 | can_load_classes_ ? field->GetType<true>() : field->GetType<false>(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4669 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4670 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame^] | 4671 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4672 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4673 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4674 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4675 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4676 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4677 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4678 | if (field_type == nullptr) { |
| 4679 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4680 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4681 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4682 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4683 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4684 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4685 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4686 | "Unexpected third access type"); |
| 4687 | if (kAccType == FieldAccessType::kAccPut) { |
| 4688 | // sput or iput. |
| 4689 | if (is_primitive) { |
| 4690 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4691 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4692 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4693 | // If the field type is not a reference, this is a global failure rather than |
| 4694 | // a class change failure as the instructions and the descriptors for the type |
| 4695 | // should have been consistent within the same file at compile time. |
| 4696 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4697 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4698 | Fail(error) << "expected field " << PrettyField(field) |
| 4699 | << " to be compatible with type '" << insn_type |
| 4700 | << "' but found type '" << *field_type |
| 4701 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4702 | return; |
| 4703 | } |
| 4704 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4705 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4706 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4707 | // sget or iget. |
| 4708 | if (is_primitive) { |
| 4709 | if (field_type->Equals(insn_type) || |
| 4710 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4711 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4712 | // expected that read is of the correct primitive type or that int reads are reading |
| 4713 | // floats or long reads are reading doubles |
| 4714 | } else { |
| 4715 | // This is a global failure rather than a class change failure as the instructions and |
| 4716 | // the descriptors for the type should have been consistent within the same file at |
| 4717 | // compile time |
| 4718 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4719 | << " to be of type '" << insn_type |
| 4720 | << "' but found type '" << *field_type << "' in get"; |
| 4721 | return; |
| 4722 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4723 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4724 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4725 | // If the field type is not a reference, this is a global failure rather than |
| 4726 | // a class change failure as the instructions and the descriptors for the type |
| 4727 | // should have been consistent within the same file at compile time. |
| 4728 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4729 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4730 | Fail(error) << "expected field " << PrettyField(field) |
| 4731 | << " to be compatible with type '" << insn_type |
| 4732 | << "' but found type '" << *field_type |
| 4733 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4734 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4735 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4736 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4737 | return; |
| 4738 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4739 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4740 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4741 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4742 | } else { |
| 4743 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4744 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4745 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4746 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4750 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4751 | RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4752 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4753 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4754 | if (!object_type.HasClass()) { |
| 4755 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 4756 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4757 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4758 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4759 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4760 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 4761 | if (f == nullptr) { |
| 4762 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
| 4763 | << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; |
| 4764 | } |
| 4765 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4768 | template <MethodVerifier::FieldAccessType kAccType> |
| 4769 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4770 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 4771 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4772 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4773 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4774 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4775 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 4776 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4777 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4778 | |
| 4779 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 4780 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4781 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4782 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4783 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4784 | return; |
| 4785 | } |
| 4786 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4787 | |
| 4788 | // Get the field type. |
| 4789 | const RegType* field_type; |
| 4790 | { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4791 | ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() : |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4792 | field->GetType<false>(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4793 | |
| 4794 | if (field_type_class != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4795 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame^] | 4796 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4797 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4798 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4799 | Thread* self = Thread::Current(); |
| 4800 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 4801 | self->ClearException(); |
| 4802 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4803 | field->GetTypeDescriptor(), |
| 4804 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4805 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4806 | if (field_type == nullptr) { |
| 4807 | 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] | 4808 | return; |
| 4809 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | const uint32_t vregA = inst->VRegA_22c(); |
| 4813 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4814 | "Unexpected third access type"); |
| 4815 | if (kAccType == FieldAccessType::kAccPut) { |
| 4816 | if (is_primitive) { |
| 4817 | // Primitive field assignability rules are weaker than regular assignability rules |
| 4818 | bool instruction_compatible; |
| 4819 | bool value_compatible; |
| 4820 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 4821 | if (field_type->IsIntegralTypes()) { |
| 4822 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 4823 | value_compatible = value_type.IsIntegralTypes(); |
| 4824 | } else if (field_type->IsFloat()) { |
| 4825 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 4826 | value_compatible = value_type.IsFloatTypes(); |
| 4827 | } else if (field_type->IsLong()) { |
| 4828 | instruction_compatible = insn_type.IsLong(); |
| 4829 | value_compatible = value_type.IsLongTypes(); |
| 4830 | } else if (field_type->IsDouble()) { |
| 4831 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 4832 | value_compatible = value_type.IsDoubleTypes(); |
| 4833 | } else { |
| 4834 | instruction_compatible = false; // reference field with primitive store |
| 4835 | value_compatible = false; // unused |
| 4836 | } |
| 4837 | if (!instruction_compatible) { |
| 4838 | // This is a global failure rather than a class change failure as the instructions and |
| 4839 | // the descriptors for the type should have been consistent within the same file at |
| 4840 | // compile time |
| 4841 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4842 | << " to be of type '" << insn_type |
| 4843 | << "' but found type '" << *field_type |
| 4844 | << "' in put"; |
| 4845 | return; |
| 4846 | } |
| 4847 | if (!value_compatible) { |
| 4848 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4849 | << " of type " << value_type |
| 4850 | << " but expected " << *field_type |
| 4851 | << " for store to " << PrettyField(field) << " in put"; |
| 4852 | return; |
| 4853 | } |
| 4854 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4855 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4856 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4857 | << " to be compatible with type '" << insn_type |
| 4858 | << "' but found type '" << *field_type |
| 4859 | << "' in put-object"; |
| 4860 | return; |
| 4861 | } |
| 4862 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 4863 | } |
| 4864 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4865 | if (is_primitive) { |
| 4866 | if (field_type->Equals(insn_type) || |
| 4867 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 4868 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 4869 | // expected that read is of the correct primitive type or that int reads are reading |
| 4870 | // floats or long reads are reading doubles |
| 4871 | } else { |
| 4872 | // This is a global failure rather than a class change failure as the instructions and |
| 4873 | // the descriptors for the type should have been consistent within the same file at |
| 4874 | // compile time |
| 4875 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4876 | << " to be of type '" << insn_type |
| 4877 | << "' but found type '" << *field_type << "' in Get"; |
| 4878 | return; |
| 4879 | } |
| 4880 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4881 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4882 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4883 | << " to be compatible with type '" << insn_type |
| 4884 | << "' but found type '" << *field_type |
| 4885 | << "' in get-object"; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4886 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4887 | return; |
| 4888 | } |
| 4889 | } |
| 4890 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4891 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4892 | } else { |
| 4893 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4894 | } |
| 4895 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4896 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4900 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4901 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4902 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4903 | return false; |
| 4904 | } |
| 4905 | return true; |
| 4906 | } |
| 4907 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 4908 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 4909 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 4910 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 4911 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 4912 | return false; |
| 4913 | } |
| 4914 | return true; |
| 4915 | } |
| 4916 | |
| 4917 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 4918 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 4919 | } |
| 4920 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4921 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 4922 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4923 | bool changed = true; |
| 4924 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4925 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4926 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4927 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4928 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4929 | * 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] | 4930 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4931 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4932 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4933 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4934 | merge_line->VerifyMonitorStackEmpty(this); |
| 4935 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4936 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4937 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 4938 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 4939 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4940 | // Directly bail if a hard failure was found. |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 4941 | if (have_pending_hard_failure_) { |
| 4942 | return false; |
| 4943 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4944 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4945 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 4946 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4947 | if (kDebugVerify) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4948 | copy.reset(RegisterLine::Create(target_line->NumRegs(), this)); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4949 | copy->CopyFromLine(target_line); |
| 4950 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4951 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4952 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4953 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4954 | } |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4955 | if (kDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4956 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4957 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4958 | << copy->Dump(this) << " MERGE\n" |
| 4959 | << merge_line->Dump(this) << " ==\n" |
| 4960 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4961 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4962 | if (update_merge_line && changed) { |
| 4963 | merge_line->CopyFromLine(target_line); |
| 4964 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4965 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4966 | if (changed) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4967 | GetInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4968 | } |
| 4969 | return true; |
| 4970 | } |
| 4971 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4972 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4973 | return &GetInstructionFlags(work_insn_idx_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4974 | } |
| 4975 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4976 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4977 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4978 | if (mirror_method_ != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 4979 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4980 | mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_, |
| 4981 | pointer_size); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4982 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4983 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
| 4984 | return_type_class, |
| 4985 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4986 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4987 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4988 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | if (return_type_ == nullptr) { |
| 4992 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4993 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 4994 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 4995 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4996 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4997 | } |
| 4998 | } |
| 4999 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5000 | } |
| 5001 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5002 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5003 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5004 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 5005 | const char* descriptor |
| 5006 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5007 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5008 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5009 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5010 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5011 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5012 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5013 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5014 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5017 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 5018 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 5019 | 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] | 5020 | std::vector<int32_t> result; |
| 5021 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5022 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5023 | if (type.IsConstant()) { |
| 5024 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5025 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5026 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5027 | } else if (type.IsConstantLo()) { |
| 5028 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5029 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5030 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5031 | } else if (type.IsConstantHi()) { |
| 5032 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5033 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5034 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5035 | } else if (type.IsIntegralTypes()) { |
| 5036 | result.push_back(kIntVReg); |
| 5037 | result.push_back(0); |
| 5038 | } else if (type.IsFloat()) { |
| 5039 | result.push_back(kFloatVReg); |
| 5040 | result.push_back(0); |
| 5041 | } else if (type.IsLong()) { |
| 5042 | result.push_back(kLongLoVReg); |
| 5043 | result.push_back(0); |
| 5044 | result.push_back(kLongHiVReg); |
| 5045 | result.push_back(0); |
| 5046 | ++i; |
| 5047 | } else if (type.IsDouble()) { |
| 5048 | result.push_back(kDoubleLoVReg); |
| 5049 | result.push_back(0); |
| 5050 | result.push_back(kDoubleHiVReg); |
| 5051 | result.push_back(0); |
| 5052 | ++i; |
| 5053 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 5054 | result.push_back(kUndefined); |
| 5055 | result.push_back(0); |
| 5056 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5057 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5058 | result.push_back(kReferenceVReg); |
| 5059 | result.push_back(0); |
| 5060 | } |
| 5061 | } |
| 5062 | return result; |
| 5063 | } |
| 5064 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5065 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5066 | if (precise) { |
| 5067 | // Precise constant type. |
| 5068 | return reg_types_.FromCat1Const(value, true); |
| 5069 | } else { |
| 5070 | // Imprecise constant type. |
| 5071 | if (value < -32768) { |
| 5072 | return reg_types_.IntConstant(); |
| 5073 | } else if (value < -128) { |
| 5074 | return reg_types_.ShortConstant(); |
| 5075 | } else if (value < 0) { |
| 5076 | return reg_types_.ByteConstant(); |
| 5077 | } else if (value == 0) { |
| 5078 | return reg_types_.Zero(); |
| 5079 | } else if (value == 1) { |
| 5080 | return reg_types_.One(); |
| 5081 | } else if (value < 128) { |
| 5082 | return reg_types_.PosByteConstant(); |
| 5083 | } else if (value < 32768) { |
| 5084 | return reg_types_.PosShortConstant(); |
| 5085 | } else if (value < 65536) { |
| 5086 | return reg_types_.CharConstant(); |
| 5087 | } else { |
| 5088 | return reg_types_.IntConstant(); |
| 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5093 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5094 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5095 | } |
| 5096 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5097 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5098 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5099 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5100 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5101 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5102 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5103 | } |
| 5104 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5105 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5106 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5109 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 5110 | mirror::Class* klass, |
| 5111 | bool precise) { |
| 5112 | DCHECK(klass != nullptr); |
| 5113 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 5114 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 5115 | << "non-instantiable klass " << descriptor; |
| 5116 | precise = false; |
| 5117 | } |
| 5118 | return reg_types_.FromClass(descriptor, klass, precise); |
| 5119 | } |
| 5120 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5121 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5122 | } // namespace art |