Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 17 | #include "method_verifier-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | #include "base/logging.h" |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 27 | #include "base/mutex-inl.h" |
Vladimir Marko | 637ee0b | 2015-09-04 12:47:41 +0100 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 29 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | #include "base/time_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 31 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 32 | #include "compiler_callbacks.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "dex_file-inl.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 34 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | #include "dex_instruction_utils.h" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 36 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 38 | #include "handle_scope-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 39 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 40 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 41 | #include "leb128.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 42 | #include "method_resolution_kind.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "mirror/class.h" |
| 44 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 45 | #include "mirror/dex_cache-inl.h" |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 46 | #include "mirror/method_handle_impl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "mirror/object-inl.h" |
| 48 | #include "mirror/object_array-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 49 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 50 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 51 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 52 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame^] | 53 | #include "stack.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 54 | #include "utils.h" |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 55 | #include "verifier_deps.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 56 | #include "verifier_compiler_binding.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 57 | |
| 58 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 59 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 60 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 61 | using android::base::StringPrintf; |
| 62 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 63 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 64 | static constexpr bool kDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 65 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 66 | |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 67 | // On VLOG(verifier), should we dump the whole state when we run into a hard failure? |
| 68 | static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true; |
| 69 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 70 | // We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make |
| 71 | // sure we only print this once. |
| 72 | static bool gPrintedDxMonitorText = false; |
| 73 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 74 | PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena) |
| 75 | : register_lines_(arena.Adapter(kArenaAllocVerifier)) {} |
| 76 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 77 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 78 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 79 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 80 | DCHECK_GT(insns_size, 0U); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 81 | register_lines_.resize(insns_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 82 | for (uint32_t i = 0; i < insns_size; i++) { |
| 83 | bool interesting = false; |
| 84 | switch (mode) { |
| 85 | case kTrackRegsAll: |
| 86 | interesting = flags[i].IsOpcode(); |
| 87 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 88 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 89 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 90 | break; |
| 91 | case kTrackRegsBranches: |
| 92 | interesting = flags[i].IsBranchTarget(); |
| 93 | break; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | if (interesting) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 98 | register_lines_[i].reset(RegisterLine::Create(registers_size, verifier)); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 103 | PcToRegisterLineTable::~PcToRegisterLineTable() {} |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 104 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 105 | // Note: returns true on failure. |
| 106 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 107 | const char* error_msg, uint32_t work_insn_idx) { |
| 108 | if (kIsDebugBuild) { |
Andreas Gampe | f8f36c2 | 2016-11-19 14:51:52 -0800 | [diff] [blame] | 109 | // In a debug build, abort if the error condition is wrong. Only warn if |
| 110 | // we are already aborting (as this verification is likely run to print |
| 111 | // lock information). |
| 112 | if (LIKELY(gAborting == 0)) { |
| 113 | DCHECK(condition) << error_msg << work_insn_idx; |
| 114 | } else { |
| 115 | if (!condition) { |
| 116 | LOG(ERROR) << error_msg << work_insn_idx; |
| 117 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 118 | return true; |
| 119 | } |
| 120 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 121 | } else { |
| 122 | // In a non-debug build, just fail the class. |
| 123 | if (!condition) { |
| 124 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 132 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 133 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 134 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 135 | reg_line->CheckConstructorReturn(verifier); |
| 136 | } |
| 137 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 138 | } |
| 139 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 140 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 141 | mirror::Class* klass, |
| 142 | CompilerCallbacks* callbacks, |
| 143 | bool allow_soft_failures, |
| 144 | HardFailLogMode log_level, |
| 145 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 146 | if (klass->IsVerified()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 147 | return FailureKind::kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 148 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 149 | bool early_failure = false; |
| 150 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 151 | const DexFile& dex_file = klass->GetDexFile(); |
| 152 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 153 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 154 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 155 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 156 | early_failure = true; |
| 157 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 158 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 159 | early_failure = true; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 160 | failure_message = " that attempts to sub-class final class " + super->PrettyDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 161 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 162 | early_failure = true; |
| 163 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 164 | } |
| 165 | if (early_failure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 166 | *error = "Verifier rejected class " + klass->PrettyDescriptor() + failure_message; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 167 | if (callbacks != nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 168 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 169 | callbacks->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 170 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 171 | return FailureKind::kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 172 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 173 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 174 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 175 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 176 | return VerifyClass(self, |
| 177 | &dex_file, |
| 178 | dex_cache, |
| 179 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 180 | *class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 181 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 182 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 183 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 184 | error); |
| 185 | } |
| 186 | |
| 187 | template <bool kDirect> |
| 188 | static bool HasNextMethod(ClassDataItemIterator* it) { |
| 189 | return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod(); |
| 190 | } |
| 191 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 192 | static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) { |
| 193 | static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure |
| 194 | && FailureKind::kSoftFailure < FailureKind::kHardFailure, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 195 | "Unexpected FailureKind order"); |
| 196 | return std::max(fk1, fk2); |
| 197 | } |
| 198 | |
| 199 | void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) { |
| 200 | kind = FailureKindMax(kind, fd.kind); |
| 201 | types |= fd.types; |
| 202 | } |
| 203 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 204 | template <bool kDirect> |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 205 | MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self, |
| 206 | ClassLinker* linker, |
| 207 | const DexFile* dex_file, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 208 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 209 | ClassDataItemIterator* it, |
| 210 | Handle<mirror::DexCache> dex_cache, |
| 211 | Handle<mirror::ClassLoader> class_loader, |
| 212 | CompilerCallbacks* callbacks, |
| 213 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 214 | HardFailLogMode log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 215 | bool need_precise_constants, |
| 216 | std::string* error_string) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 217 | DCHECK(it != nullptr); |
| 218 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 219 | MethodVerifier::FailureData failure_data; |
| 220 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 221 | int64_t previous_method_idx = -1; |
| 222 | while (HasNextMethod<kDirect>(it)) { |
| 223 | self->AllowThreadSuspension(); |
| 224 | uint32_t method_idx = it->GetMemberIndex(); |
| 225 | if (method_idx == previous_method_idx) { |
| 226 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 227 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 228 | it->Next(); |
| 229 | continue; |
| 230 | } |
| 231 | previous_method_idx = method_idx; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 232 | InvokeType type = it->GetMethodInvokeType(class_def); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 233 | ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 234 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
| 235 | if (method == nullptr) { |
| 236 | DCHECK(self->IsExceptionPending()); |
| 237 | // We couldn't resolve the method, but continue regardless. |
| 238 | self->ClearException(); |
| 239 | } else { |
| 240 | DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type; |
| 241 | } |
| 242 | StackHandleScope<1> hs(self); |
| 243 | std::string hard_failure_msg; |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 244 | MethodVerifier::FailureData result = VerifyMethod(self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 245 | method_idx, |
| 246 | dex_file, |
| 247 | dex_cache, |
| 248 | class_loader, |
| 249 | class_def, |
| 250 | it->GetMethodCodeItem(), |
| 251 | method, |
| 252 | it->GetMethodAccessFlags(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 253 | callbacks, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 254 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 255 | log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 256 | need_precise_constants, |
| 257 | &hard_failure_msg); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 258 | if (result.kind == FailureKind::kHardFailure) { |
| 259 | if (failure_data.kind == FailureKind::kHardFailure) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 260 | // If we logged an error before, we need a newline. |
| 261 | *error_string += "\n"; |
| 262 | } else { |
| 263 | // If we didn't log a hard failure before, print the header of the message. |
| 264 | *error_string += "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 265 | *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 266 | *error_string += ":"; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 267 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 268 | *error_string += " "; |
| 269 | *error_string += hard_failure_msg; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 270 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 271 | failure_data.Merge(result); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 272 | it->Next(); |
| 273 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 274 | |
| 275 | return failure_data; |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 278 | FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 279 | const DexFile* dex_file, |
| 280 | Handle<mirror::DexCache> dex_cache, |
| 281 | Handle<mirror::ClassLoader> class_loader, |
| 282 | const DexFile::ClassDef& class_def, |
| 283 | CompilerCallbacks* callbacks, |
| 284 | bool allow_soft_failures, |
| 285 | HardFailLogMode log_level, |
| 286 | std::string* error) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 287 | ScopedTrace trace(__FUNCTION__); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 288 | |
| 289 | // A class must not be abstract and final. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 290 | if ((class_def.access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 291 | *error = "Verifier rejected class "; |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 292 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def)); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 293 | *error += ": class is abstract and final."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 294 | return FailureKind::kHardFailure; |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 295 | } |
| 296 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 297 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 298 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 299 | // empty class, probably a marker interface |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 300 | return FailureKind::kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 301 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 302 | ClassDataItemIterator it(*dex_file, class_data); |
| 303 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 304 | it.Next(); |
| 305 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 306 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 307 | // Direct methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 308 | MethodVerifier::FailureData data1 = VerifyMethods<true>(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 | // Virtual methods. |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 321 | MethodVerifier::FailureData data2 = VerifyMethods<false>(self, |
| 322 | linker, |
| 323 | dex_file, |
| 324 | class_def, |
| 325 | &it, |
| 326 | dex_cache, |
| 327 | class_loader, |
| 328 | callbacks, |
| 329 | allow_soft_failures, |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 330 | log_level, |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 331 | false /* need precise constants */, |
| 332 | error); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 333 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 334 | data1.Merge(data2); |
| 335 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 336 | if (data1.kind == FailureKind::kNoFailure) { |
| 337 | return FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 338 | } else { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 339 | if ((data1.types & VERIFY_ERROR_LOCKING) != 0) { |
| 340 | // Print a warning about expected slow-down. Use a string temporary to print one contiguous |
| 341 | // warning. |
| 342 | std::string tmp = |
| 343 | StringPrintf("Class %s failed lock verification and will run slower.", |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 344 | PrettyDescriptor(dex_file->GetClassDescriptor(class_def)).c_str()); |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 345 | if (!gPrintedDxMonitorText) { |
| 346 | tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n" |
| 347 | "and incorrect proguard optimizations."; |
| 348 | gPrintedDxMonitorText = true; |
| 349 | } |
| 350 | LOG(WARNING) << tmp; |
| 351 | } |
| 352 | return data1.kind; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 353 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 356 | static bool IsLargeMethod(const DexFile::CodeItem* const code_item) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 357 | if (code_item == nullptr) { |
| 358 | return false; |
| 359 | } |
| 360 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 361 | uint16_t registers_size = code_item->registers_size_; |
| 362 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 363 | |
| 364 | return registers_size * insns_size > 4*1024*1024; |
| 365 | } |
| 366 | |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 367 | MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 368 | uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 369 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 370 | Handle<mirror::DexCache> dex_cache, |
| 371 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 372 | const DexFile::ClassDef& class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | const DexFile::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 374 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 375 | uint32_t method_access_flags, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 376 | CompilerCallbacks* callbacks, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 377 | bool allow_soft_failures, |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 378 | HardFailLogMode log_level, |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 379 | bool need_precise_constants, |
| 380 | std::string* hard_failure_msg) { |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 381 | MethodVerifier::FailureData result; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 382 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 383 | |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 384 | MethodVerifier verifier(self, |
| 385 | dex_file, |
| 386 | dex_cache, |
| 387 | class_loader, |
| 388 | class_def, |
| 389 | code_item, |
| 390 | method_idx, |
| 391 | method, |
| 392 | method_access_flags, |
| 393 | true /* can_load_classes */, |
| 394 | allow_soft_failures, |
| 395 | need_precise_constants, |
| 396 | false /* verify to dump */, |
| 397 | true /* allow_thread_suspension */); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 398 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 399 | // Verification completed, however failures may be pending that didn't cause the verification |
| 400 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 401 | CHECK(!verifier.have_pending_hard_failure_); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 402 | |
| 403 | if (code_item != nullptr && callbacks != nullptr) { |
| 404 | // Let the interested party know that the method was verified. |
| 405 | callbacks->MethodVerified(&verifier); |
| 406 | } |
| 407 | |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 408 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 409 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 410 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 411 | << dex_file->PrettyMethod(method_idx) << "\n"); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 412 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 413 | result.kind = FailureKind::kSoftFailure; |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 414 | if (method != nullptr && |
| 415 | !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 416 | method->SetDontCompile(); |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 417 | } |
| 418 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 419 | if (method != nullptr) { |
| 420 | if (verifier.HasInstructionThatWillThrow()) { |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 421 | method->SetDontCompile(); |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 422 | if (Runtime::Current()->IsAotCompiler() && |
| 423 | (callbacks != nullptr) && !callbacks->IsBootImage()) { |
| 424 | // When compiling apps, make HasInstructionThatWillThrow a soft error to trigger |
| 425 | // re-verification at runtime. |
| 426 | // The dead code after the throw is not verified and might be invalid. This may cause |
| 427 | // the JIT compiler to crash since it assumes that all the code is valid. |
| 428 | // |
| 429 | // There's a strong assumption that the entire boot image is verified and all its dex |
| 430 | // code is valid (even the dead and unverified one). As such this is done only for apps. |
| 431 | // (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are |
| 432 | // fully verified). |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 433 | result.kind = FailureKind::kSoftFailure; |
Calin Juravle | 857f058 | 2016-12-20 14:36:59 +0000 | [diff] [blame] | 434 | } |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 435 | } |
| 436 | if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 437 | method->AddAccessFlags(kAccMustCountLocks); |
Andreas Gampe | 56fdd0e | 2016-04-28 14:56:54 -0700 | [diff] [blame] | 438 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 439 | } |
| 440 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 441 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 442 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 443 | |
| 444 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 445 | // Failed due to being forced into interpreter. This is ok because |
| 446 | // we just want to skip verification. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 447 | result.kind = FailureKind::kSoftFailure; |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 448 | } else { |
| 449 | CHECK(verifier.have_pending_hard_failure_); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 450 | if (VLOG_IS_ON(verifier)) { |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 451 | log_level = std::max(HardFailLogMode::kLogVerbose, log_level); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 452 | } |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 453 | if (log_level >= HardFailLogMode::kLogVerbose) { |
| 454 | LogSeverity severity; |
| 455 | switch (log_level) { |
| 456 | case HardFailLogMode::kLogVerbose: |
| 457 | severity = LogSeverity::VERBOSE; |
| 458 | break; |
| 459 | case HardFailLogMode::kLogWarning: |
| 460 | severity = LogSeverity::WARNING; |
| 461 | break; |
| 462 | case HardFailLogMode::kLogInternalFatal: |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 463 | severity = LogSeverity::FATAL_WITHOUT_ABORT; |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 464 | break; |
| 465 | default: |
| 466 | LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level); |
| 467 | UNREACHABLE(); |
| 468 | } |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 469 | verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 470 | << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 471 | << "\n"); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 472 | } |
| 473 | if (hard_failure_msg != nullptr) { |
| 474 | CHECK(!verifier.failure_messages_.empty()); |
| 475 | *hard_failure_msg = |
| 476 | verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str(); |
| 477 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 478 | result.kind = FailureKind::kHardFailure; |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 479 | |
| 480 | if (callbacks != nullptr) { |
| 481 | // Let the interested party know that we failed the class. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 482 | ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def)); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 483 | callbacks->ClassRejected(ref); |
| 484 | } |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 485 | } |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 486 | if (VLOG_IS_ON(verifier)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 487 | std::cout << "\n" << verifier.info_messages_.str(); |
| 488 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 489 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 490 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 491 | if (kTimeVerifyMethod) { |
| 492 | uint64_t duration_ns = NanoTime() - start_ns; |
| 493 | if (duration_ns > MsToNs(100)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 494 | LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 495 | << " took " << PrettyDuration(duration_ns) |
| 496 | << (IsLargeMethod(code_item) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 497 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 498 | } |
Andreas Gampe | 9fcfb8a | 2016-02-04 20:52:54 -0800 | [diff] [blame] | 499 | result.types = verifier.encountered_failure_types_; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 500 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 503 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 504 | VariableIndentationOutputStream* vios, |
| 505 | uint32_t dex_method_idx, |
| 506 | const DexFile* dex_file, |
| 507 | Handle<mirror::DexCache> dex_cache, |
| 508 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 509 | const DexFile::ClassDef& class_def, |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 510 | const DexFile::CodeItem* code_item, |
| 511 | ArtMethod* method, |
| 512 | uint32_t method_access_flags) { |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 513 | MethodVerifier* verifier = new MethodVerifier(self, |
| 514 | dex_file, |
| 515 | dex_cache, |
| 516 | class_loader, |
| 517 | class_def, |
| 518 | code_item, |
| 519 | dex_method_idx, |
| 520 | method, |
| 521 | method_access_flags, |
| 522 | true /* can_load_classes */, |
| 523 | true /* allow_soft_failures */, |
| 524 | true /* need_precise_constants */, |
| 525 | true /* verify_to_dump */, |
| 526 | true /* allow_thread_suspension */); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 527 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 528 | verifier->DumpFailures(vios->Stream()); |
| 529 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 530 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 531 | // and querying any info is dangerous/can abort. |
| 532 | if (verifier->have_pending_hard_failure_) { |
| 533 | delete verifier; |
| 534 | return nullptr; |
| 535 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 536 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 537 | return verifier; |
| 538 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 541 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 542 | const DexFile* dex_file, |
| 543 | Handle<mirror::DexCache> dex_cache, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 544 | Handle<mirror::ClassLoader> class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 545 | const DexFile::ClassDef& class_def, |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 546 | const DexFile::CodeItem* code_item, |
| 547 | uint32_t dex_method_idx, |
| 548 | ArtMethod* method, |
| 549 | uint32_t method_access_flags, |
| 550 | bool can_load_classes, |
| 551 | bool allow_soft_failures, |
| 552 | bool need_precise_constants, |
| 553 | bool verify_to_dump, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 554 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 555 | : self_(self), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 556 | arena_stack_(Runtime::Current()->GetArenaPool()), |
| 557 | arena_(&arena_stack_), |
| 558 | reg_types_(can_load_classes, arena_), |
| 559 | reg_table_(arena_), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 560 | work_insn_idx_(DexFile::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 561 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 562 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 563 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 564 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 565 | dex_file_(dex_file), |
| 566 | dex_cache_(dex_cache), |
| 567 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 568 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 569 | code_item_(code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 570 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 571 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 572 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 573 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 574 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 575 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 576 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 577 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 578 | monitor_enter_count_(0), |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 579 | encountered_failure_types_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 580 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 581 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 582 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 583 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 584 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 585 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 586 | allow_thread_suspension_(allow_thread_suspension), |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 587 | is_constructor_(false), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 588 | link_(nullptr) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 589 | self->PushVerifier(this); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 592 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 593 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 594 | STLDeleteElements(&failure_messages_); |
| 595 | } |
| 596 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 597 | void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 598 | std::vector<uint32_t>* monitor_enter_dex_pcs) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 599 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 600 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 601 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 602 | MethodVerifier verifier(hs.Self(), |
| 603 | m->GetDexFile(), |
| 604 | dex_cache, |
| 605 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 606 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 607 | m->GetCodeItem(), |
| 608 | m->GetDexMethodIndex(), |
| 609 | m, |
| 610 | m->GetAccessFlags(), |
| 611 | false /* can_load_classes */, |
| 612 | true /* allow_soft_failures */, |
| 613 | false /* need_precise_constants */, |
| 614 | false /* verify_to_dump */, |
| 615 | false /* allow_thread_suspension */); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 616 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 617 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 618 | verifier.FindLocksAtDexPc(); |
| 619 | } |
| 620 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 621 | static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) { |
| 622 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 623 | |
| 624 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 625 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 626 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | dex_pc += inst->SizeInCodeUnits(); |
| 631 | inst = inst->Next(); |
| 632 | } |
| 633 | |
| 634 | return false; |
| 635 | } |
| 636 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 637 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 638 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| 639 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 640 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 641 | // Quick check whether there are any monitor_enter instructions at all. |
| 642 | if (!HasMonitorEnterInstructions(code_item_)) { |
| 643 | return; |
| 644 | } |
| 645 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 646 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 647 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 648 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 649 | // got what we wanted. |
| 650 | Verify(); |
| 651 | } |
| 652 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 653 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 654 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 655 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 656 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 657 | MethodVerifier verifier(hs.Self(), |
| 658 | m->GetDexFile(), |
| 659 | dex_cache, |
| 660 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 661 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 662 | m->GetCodeItem(), |
| 663 | m->GetDexMethodIndex(), |
| 664 | m, |
| 665 | m->GetAccessFlags(), |
| 666 | true /* can_load_classes */, |
| 667 | true /* allow_soft_failures */, |
| 668 | false /* need_precise_constants */, |
| 669 | false /* verify_to_dump */, |
| 670 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 671 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 674 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 675 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 676 | |
| 677 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 678 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 679 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 680 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 681 | bool success = Verify(); |
| 682 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 683 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 684 | } |
| 685 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 686 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 687 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 688 | } |
| 689 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 690 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 693 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 694 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 695 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 696 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 697 | MethodVerifier verifier(hs.Self(), |
| 698 | m->GetDexFile(), |
| 699 | dex_cache, |
| 700 | class_loader, |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 701 | m->GetClassDef(), |
Andreas Gampe | bf9611f | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 702 | m->GetCodeItem(), |
| 703 | m->GetDexMethodIndex(), |
| 704 | m, |
| 705 | m->GetAccessFlags(), |
| 706 | true /* can_load_classes */, |
| 707 | true /* allow_soft_failures */, |
| 708 | false /* need_precise_constants */, |
| 709 | false /* verify_to_dump */, |
| 710 | true /* allow_thread_suspension */); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 711 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 714 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 715 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 716 | |
| 717 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 718 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 719 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 720 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 721 | bool success = Verify(); |
| 722 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 723 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 724 | } |
| 725 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 726 | if (register_line == nullptr) { |
| 727 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 728 | } |
| 729 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 730 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 731 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 732 | } |
| 733 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 734 | bool MethodVerifier::Verify() { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 735 | // Some older code doesn't correctly mark constructors as such. Test for this case by looking at |
| 736 | // the name. |
| 737 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 738 | const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_); |
| 739 | bool instance_constructor_by_name = strcmp("<init>", method_name) == 0; |
| 740 | bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0; |
| 741 | bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name; |
| 742 | // Check that only constructors are tagged, and check for bad code that doesn't tag constructors. |
| 743 | if ((method_access_flags_ & kAccConstructor) != 0) { |
| 744 | if (!constructor_by_name) { |
| 745 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 746 | << "method is marked as constructor, but not named accordingly"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 747 | return false; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 748 | } |
| 749 | is_constructor_ = true; |
| 750 | } else if (constructor_by_name) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 751 | LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_) |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 752 | << " not marked as constructor."; |
| 753 | is_constructor_ = true; |
| 754 | } |
| 755 | // If it's a constructor, check whether IsStatic() matches the name. |
| 756 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 757 | if (kIsDebugBuild) { |
| 758 | if (IsConstructor()) { |
| 759 | if (IsStatic() ^ static_constructor_by_name) { |
| 760 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 761 | << "constructor name doesn't match static flag"; |
| 762 | return false; |
| 763 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 764 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 765 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 766 | |
| 767 | // Methods may only have one of public/protected/private. |
| 768 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 769 | if (kIsDebugBuild) { |
| 770 | size_t access_mod_count = |
| 771 | (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) + |
| 772 | (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) + |
| 773 | (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1); |
| 774 | if (access_mod_count > 1) { |
| 775 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private"; |
| 776 | return false; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 781 | if (code_item_ == nullptr) { |
Andreas Gampe | 76ed99d | 2016-03-28 18:31:29 -0700 | [diff] [blame] | 782 | // Only native or abstract methods may not have code. |
| 783 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
| 784 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
| 785 | return false; |
| 786 | } |
| 787 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 788 | // 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] | 789 | // 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] | 790 | if (kIsDebugBuild) { |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 791 | if ((method_access_flags_ & kAccAbstract) != 0) { |
| 792 | // Abstract methods are not allowed to have the following flags. |
| 793 | static constexpr uint32_t kForbidden = |
| 794 | kAccPrivate | |
| 795 | kAccStatic | |
| 796 | kAccFinal | |
| 797 | kAccNative | |
| 798 | kAccStrict | |
| 799 | kAccSynchronized; |
| 800 | if ((method_access_flags_ & kForbidden) != 0) { |
| 801 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 802 | << "method can't be abstract and private/static/final/native/strict/synchronized"; |
| 803 | return false; |
| 804 | } |
| 805 | } |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 806 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 807 | // Interface methods must be public and abstract (if default methods are disabled). |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 808 | uint32_t kRequired = kAccPublic; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 809 | if ((method_access_flags_ & kRequired) != kRequired) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 810 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"; |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 811 | return false; |
| 812 | } |
| 813 | // In addition to the above, interface methods must not be protected. |
| 814 | static constexpr uint32_t kForbidden = kAccProtected; |
| 815 | if ((method_access_flags_ & kForbidden) != 0) { |
| 816 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected"; |
| 817 | return false; |
| 818 | } |
| 819 | } |
| 820 | // We also don't allow constructors to be abstract or native. |
| 821 | if (IsConstructor()) { |
| 822 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native"; |
| 823 | return false; |
| 824 | } |
| 825 | } |
| 826 | return true; |
| 827 | } |
| 828 | |
| 829 | // This should have been rejected by the dex file verifier. Only do in debug build. |
| 830 | if (kIsDebugBuild) { |
| 831 | // When there's code, the method must not be native or abstract. |
| 832 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) { |
| 833 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method"; |
| 834 | return false; |
| 835 | } |
| 836 | |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 837 | if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 838 | // Interfaces may always have static initializers for their fields. If we are running with |
| 839 | // default methods enabled we also allow other public, static, non-final methods to have code. |
| 840 | // Otherwise that is the only type of method allowed. |
Alex Light | 0db36b3 | 2015-10-27 14:06:34 -0700 | [diff] [blame] | 841 | if (!(IsConstructor() && IsStatic())) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 842 | if (IsInstanceConstructor()) { |
| 843 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor"; |
| 844 | return false; |
| 845 | } else if (method_access_flags_ & kAccFinal) { |
| 846 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods"; |
| 847 | return false; |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 848 | } else { |
| 849 | uint32_t access_flag_options = kAccPublic; |
| 850 | if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) { |
| 851 | access_flag_options |= kAccPrivate; |
| 852 | } |
| 853 | if (!(method_access_flags_ & access_flag_options)) { |
| 854 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 855 | << "interfaces may not have protected or package-private members"; |
| 856 | return false; |
| 857 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 858 | } |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | // Instance constructors must not be synchronized. |
| 863 | if (IsInstanceConstructor()) { |
| 864 | static constexpr uint32_t kForbidden = kAccSynchronized; |
| 865 | if ((method_access_flags_ & kForbidden) != 0) { |
| 866 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized"; |
| 867 | return false; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 872 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 873 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 874 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 875 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 876 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 877 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 878 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 879 | // Allocate and initialize an array to hold instruction data. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 880 | insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_)); |
| 881 | DCHECK(insn_flags_ != nullptr); |
| 882 | std::uninitialized_fill_n(insn_flags_.get(), |
| 883 | code_item_->insns_size_in_code_units_, |
| 884 | InstructionFlags()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 885 | // Run through the instructions and see if the width checks out. |
| 886 | bool result = ComputeWidthsAndCountOps(); |
| 887 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 888 | result = result && ScanTryCatchBlocks(); |
| 889 | // Perform static instruction verification. |
| 890 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 891 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 892 | result = result && VerifyCodeFlow(); |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 893 | |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 894 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 897 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 898 | // Mark the error type as encountered. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 899 | encountered_failure_types_ |= static_cast<uint32_t>(error); |
Andreas Gampe | 0760a81 | 2015-08-26 17:12:51 -0700 | [diff] [blame] | 900 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 901 | switch (error) { |
| 902 | case VERIFY_ERROR_NO_CLASS: |
| 903 | case VERIFY_ERROR_NO_FIELD: |
| 904 | case VERIFY_ERROR_NO_METHOD: |
| 905 | case VERIFY_ERROR_ACCESS_CLASS: |
| 906 | case VERIFY_ERROR_ACCESS_FIELD: |
| 907 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 908 | case VERIFY_ERROR_INSTANTIATION: |
| 909 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 910 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 911 | case VERIFY_ERROR_LOCKING: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 912 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 913 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 914 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 915 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 916 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 917 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 918 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 919 | // to an interpreter. |
| 920 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 921 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 922 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 923 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 924 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 925 | |
| 926 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 927 | // try to merge garbage. |
| 928 | // 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] | 929 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 930 | // add a check. |
| 931 | if (work_insn_idx_ < DexFile::kDexNoIndex) { |
| 932 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 933 | const Instruction* inst = Instruction::At(insns); |
| 934 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 935 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 936 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 937 | saved_line_->CopyFromLine(work_line_.get()); |
| 938 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 939 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 940 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 941 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 942 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 943 | // Indication that verification should be retried at runtime. |
| 944 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 945 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 946 | have_pending_hard_failure_ = true; |
| 947 | } |
| 948 | break; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 949 | |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 950 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 951 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 952 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 953 | have_pending_hard_failure_ = true; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 954 | if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) { |
| 955 | ScopedObjectAccess soa(Thread::Current()); |
| 956 | std::ostringstream oss; |
| 957 | Dump(oss); |
| 958 | LOG(ERROR) << oss.str(); |
| 959 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 960 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 963 | failures_.push_back(error); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 964 | std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 965 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 966 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 967 | failure_messages_.push_back(failure_message); |
| 968 | return *failure_message; |
| 969 | } |
| 970 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 971 | std::ostream& MethodVerifier::LogVerifyInfo() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 972 | return info_messages_ << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_) |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 973 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 974 | } |
| 975 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 976 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 977 | size_t failure_num = failure_messages_.size(); |
| 978 | DCHECK_NE(failure_num, 0U); |
| 979 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 980 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 981 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 982 | delete last_fail_message; |
| 983 | } |
| 984 | |
Vladimir Marko | 5c657fe | 2016-11-03 15:12:29 +0000 | [diff] [blame] | 985 | void MethodVerifier::AppendToLastFailMessage(const std::string& append) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 986 | size_t failure_num = failure_messages_.size(); |
| 987 | DCHECK_NE(failure_num, 0U); |
| 988 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 989 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 992 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 993 | const uint16_t* insns = code_item_->insns_; |
| 994 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 995 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 996 | size_t new_instance_count = 0; |
| 997 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 998 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 999 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1000 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1001 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 1002 | switch (opcode) { |
| 1003 | case Instruction::APUT_OBJECT: |
| 1004 | case Instruction::CHECK_CAST: |
| 1005 | has_check_casts_ = true; |
| 1006 | break; |
| 1007 | case Instruction::INVOKE_VIRTUAL: |
| 1008 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 1009 | case Instruction::INVOKE_INTERFACE: |
| 1010 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 1011 | has_virtual_or_interface_invokes_ = true; |
| 1012 | break; |
| 1013 | case Instruction::MONITOR_ENTER: |
| 1014 | monitor_enter_count++; |
| 1015 | break; |
| 1016 | case Instruction::NEW_INSTANCE: |
| 1017 | new_instance_count++; |
| 1018 | break; |
| 1019 | default: |
| 1020 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1021 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1022 | size_t inst_size = inst->SizeInCodeUnits(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1023 | GetInstructionFlags(dex_pc).SetIsOpcode(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1024 | dex_pc += inst_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1025 | inst = inst->RelativeAt(inst_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1028 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1029 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 1030 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
| 1033 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1034 | new_instance_count_ = new_instance_count; |
| 1035 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1036 | return true; |
| 1037 | } |
| 1038 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1039 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1040 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1041 | if (tries_size == 0) { |
| 1042 | return true; |
| 1043 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1044 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1045 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1046 | |
| 1047 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 1048 | const DexFile::TryItem* try_item = &tries[idx]; |
| 1049 | uint32_t start = try_item->start_addr_; |
| 1050 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1051 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1052 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 1053 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1056 | if (!GetInstructionFlags(start).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1057 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1058 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1059 | return false; |
| 1060 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1061 | uint32_t dex_pc = start; |
| 1062 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 1063 | while (dex_pc < end) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1064 | GetInstructionFlags(dex_pc).SetInTry(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1065 | size_t insn_size = inst->SizeInCodeUnits(); |
| 1066 | dex_pc += insn_size; |
| 1067 | inst = inst->RelativeAt(insn_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1070 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1071 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1072 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1073 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1074 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1075 | CatchHandlerIterator iterator(handlers_ptr); |
| 1076 | for (; iterator.HasNext(); iterator.Next()) { |
| 1077 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1078 | if (!GetInstructionFlags(dex_pc).IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1079 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1080 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1081 | return false; |
| 1082 | } |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 1083 | if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) { |
| 1084 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1085 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 1086 | return false; |
| 1087 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1088 | GetInstructionFlags(dex_pc).SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1089 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 1090 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1091 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1092 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 1093 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1094 | dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1095 | if (exception_type == nullptr) { |
| 1096 | DCHECK(self_->IsExceptionPending()); |
| 1097 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1100 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1101 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1102 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1103 | return true; |
| 1104 | } |
| 1105 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1106 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1107 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1108 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 1109 | /* 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] | 1110 | GetInstructionFlags(0).SetBranchTarget(); |
| 1111 | GetInstructionFlags(0).SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1112 | |
| 1113 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Andreas Gampe | 1189516 | 2017-05-15 10:25:02 -0700 | [diff] [blame] | 1114 | bool allow_runtime_only_instructions = !Runtime::Current()->IsAotCompiler() || verify_to_dump_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1115 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Andreas Gampe | 1189516 | 2017-05-15 10:25:02 -0700 | [diff] [blame] | 1116 | if (!VerifyInstruction(inst, dex_pc, allow_runtime_only_instructions)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1117 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
| 1120 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1121 | // All invoke points are marked as "Throw" points already. |
| 1122 | // 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] | 1123 | if (inst->IsBranch()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1124 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1125 | // The compiler also needs safepoints for fall-through to loop heads. |
| 1126 | // Such a loop head must be a target of a branch. |
| 1127 | int32_t offset = 0; |
| 1128 | bool cond, self_ok; |
| 1129 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 1130 | DCHECK(target_ok); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1131 | GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint(); |
Vladimir Marko | 8b858e1 | 2014-11-27 14:52:37 +0000 | [diff] [blame] | 1132 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1133 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 1134 | } else if (inst->IsReturn()) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1135 | GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1136 | } |
| 1137 | dex_pc += inst->SizeInCodeUnits(); |
| 1138 | inst = inst->Next(); |
| 1139 | } |
| 1140 | return true; |
| 1141 | } |
| 1142 | |
Andreas Gampe | 1189516 | 2017-05-15 10:25:02 -0700 | [diff] [blame] | 1143 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, |
| 1144 | uint32_t code_offset, |
| 1145 | bool allow_runtime_only_instructions) { |
Andreas Gampe | e05cc66 | 2017-05-15 10:17:30 -0700 | [diff] [blame] | 1146 | if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 1147 | // Experimental instructions don't yet have verifier support implementation. |
| 1148 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 1149 | // with a virtual register that was created by an experimental instruction, |
| 1150 | // the data flow analysis will fail. |
| 1151 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 1152 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 1153 | have_pending_experimental_failure_ = true; |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1157 | bool result = true; |
| 1158 | switch (inst->GetVerifyTypeArgumentA()) { |
| 1159 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1160 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1161 | break; |
| 1162 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1163 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1164 | break; |
| 1165 | } |
| 1166 | switch (inst->GetVerifyTypeArgumentB()) { |
| 1167 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1168 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1169 | break; |
| 1170 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1171 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1172 | break; |
| 1173 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1174 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1175 | break; |
| 1176 | case Instruction::kVerifyRegBNewInstance: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1177 | result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1178 | break; |
| 1179 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1180 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1181 | break; |
| 1182 | case Instruction::kVerifyRegBType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1183 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | break; |
| 1185 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1186 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | switch (inst->GetVerifyTypeArgumentC()) { |
| 1190 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1191 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1192 | break; |
| 1193 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1194 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1195 | break; |
| 1196 | case Instruction::kVerifyRegCNewArray: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1197 | result = result && CheckNewArray(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1198 | break; |
| 1199 | case Instruction::kVerifyRegCType: |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1200 | result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC())); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1201 | break; |
| 1202 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1203 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1204 | break; |
| 1205 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1206 | switch (inst->GetVerifyTypeArgumentH()) { |
| 1207 | case Instruction::kVerifyRegHPrototype: |
| 1208 | result = result && CheckPrototypeIndex(inst->VRegH()); |
| 1209 | break; |
| 1210 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1211 | switch (inst->GetVerifyExtraFlags()) { |
| 1212 | case Instruction::kVerifyArrayData: |
| 1213 | result = result && CheckArrayData(code_offset); |
| 1214 | break; |
| 1215 | case Instruction::kVerifyBranchTarget: |
| 1216 | result = result && CheckBranchTarget(code_offset); |
| 1217 | break; |
| 1218 | case Instruction::kVerifySwitchTargets: |
| 1219 | result = result && CheckSwitchTargets(code_offset); |
| 1220 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1221 | case Instruction::kVerifyVarArgNonZero: |
| 1222 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1223 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1224 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 1225 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 1226 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 1227 | v_a > Instruction::kMaxVarArgRegs) { |
| 1228 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1229 | "non-range invoke"; |
| 1230 | return false; |
| 1231 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1232 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1233 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 1234 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 1235 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1236 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1237 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1238 | case Instruction::kVerifyVarArgRangeNonZero: |
| 1239 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1240 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 1241 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 1242 | inst->VRegA() <= 0) { |
| 1243 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 1244 | "range invoke"; |
| 1245 | return false; |
| 1246 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1247 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1248 | break; |
| 1249 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1250 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1251 | result = false; |
| 1252 | break; |
| 1253 | } |
Andreas Gampe | 1189516 | 2017-05-15 10:25:02 -0700 | [diff] [blame] | 1254 | if (!allow_runtime_only_instructions && inst->GetVerifyIsRuntimeOnly()) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 1255 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 1256 | result = false; |
| 1257 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1258 | return result; |
| 1259 | } |
| 1260 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1261 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1262 | if (UNLIKELY(idx >= code_item_->registers_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1263 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 1264 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1270 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1271 | if (UNLIKELY(idx + 1 >= code_item_->registers_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1272 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 1273 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1274 | return false; |
| 1275 | } |
| 1276 | return true; |
| 1277 | } |
| 1278 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1279 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1280 | if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1281 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 1282 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1283 | return false; |
| 1284 | } |
| 1285 | return true; |
| 1286 | } |
| 1287 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1288 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1289 | if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1290 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 1291 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1292 | return false; |
| 1293 | } |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1297 | inline bool MethodVerifier::CheckNewInstance(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1298 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1299 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1300 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | return false; |
| 1302 | } |
| 1303 | // 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] | 1304 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1305 | if (UNLIKELY(descriptor[0] != 'L')) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1306 | 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] | 1307 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1308 | } else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) { |
Aart Bik | db698f1 | 2016-07-25 17:52:22 -0700 | [diff] [blame] | 1309 | // An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an |
| 1310 | // exception is thrown when this statement is executed (compiled code would not do that). |
| 1311 | Fail(VERIFY_ERROR_INSTANTIATION); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1312 | } |
| 1313 | return true; |
| 1314 | } |
| 1315 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1316 | inline bool MethodVerifier::CheckPrototypeIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1317 | if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 1318 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max " |
| 1319 | << dex_file_->GetHeader().proto_ids_size_ << ")"; |
| 1320 | return false; |
| 1321 | } |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1325 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1326 | if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1327 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 1328 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1334 | inline bool MethodVerifier::CheckTypeIndex(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1335 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1336 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1337 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
| 1340 | return true; |
| 1341 | } |
| 1342 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1343 | bool MethodVerifier::CheckNewArray(dex::TypeIndex idx) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1344 | if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1345 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max " |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1346 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1347 | return false; |
| 1348 | } |
| 1349 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1350 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1351 | const char* cp = descriptor; |
| 1352 | while (*cp++ == '[') { |
| 1353 | bracket_count++; |
| 1354 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1355 | if (UNLIKELY(bracket_count == 0)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1356 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1357 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1358 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1359 | return false; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1360 | } else if (UNLIKELY(bracket_count > 255)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1361 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1362 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1363 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
| 1366 | return true; |
| 1367 | } |
| 1368 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1369 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1370 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1371 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1372 | const uint16_t* array_data; |
| 1373 | int32_t array_data_offset; |
| 1374 | |
| 1375 | DCHECK_LT(cur_offset, insn_count); |
| 1376 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1377 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1378 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
| 1379 | cur_offset + array_data_offset + 2 >= insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1380 | 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] | 1381 | << ", data offset " << array_data_offset |
| 1382 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
| 1385 | /* offset to array data table is a relative branch-style offset */ |
| 1386 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1387 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1388 | if (UNLIKELY(!IsAligned<4>(array_data))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1389 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1390 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1391 | return false; |
| 1392 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1393 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1394 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1395 | if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1396 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1397 | << ", data offset " << array_data_offset |
| 1398 | << " not correctly visited, probably bad padding."; |
| 1399 | return false; |
| 1400 | } |
| 1401 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1402 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1403 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1404 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1405 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1406 | if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1407 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1408 | << ", data offset " << array_data_offset << ", end " |
| 1409 | << cur_offset + array_data_offset + table_size |
| 1410 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1411 | return false; |
| 1412 | } |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1416 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1417 | int32_t offset; |
| 1418 | bool isConditional, selfOkay; |
| 1419 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1420 | return false; |
| 1421 | } |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1422 | if (UNLIKELY(!selfOkay && offset == 0)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1423 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1424 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1425 | return false; |
| 1426 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1427 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1428 | // to have identical "wrap-around" behavior, but it's unwise to depend on that. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1429 | if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1430 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1431 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1432 | return false; |
| 1433 | } |
| 1434 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1435 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1436 | if (UNLIKELY(abs_offset < 0 || |
| 1437 | (uint32_t) abs_offset >= insn_count || |
| 1438 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1439 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1440 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1441 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1444 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1445 | return true; |
| 1446 | } |
| 1447 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1448 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1449 | bool* selfOkay) { |
| 1450 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1451 | *pConditional = false; |
| 1452 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1453 | switch (*insns & 0xff) { |
| 1454 | case Instruction::GOTO: |
| 1455 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1456 | break; |
| 1457 | case Instruction::GOTO_32: |
| 1458 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1459 | *selfOkay = true; |
| 1460 | break; |
| 1461 | case Instruction::GOTO_16: |
| 1462 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | case Instruction::IF_EQ: |
| 1465 | case Instruction::IF_NE: |
| 1466 | case Instruction::IF_LT: |
| 1467 | case Instruction::IF_GE: |
| 1468 | case Instruction::IF_GT: |
| 1469 | case Instruction::IF_LE: |
| 1470 | case Instruction::IF_EQZ: |
| 1471 | case Instruction::IF_NEZ: |
| 1472 | case Instruction::IF_LTZ: |
| 1473 | case Instruction::IF_GEZ: |
| 1474 | case Instruction::IF_GTZ: |
| 1475 | case Instruction::IF_LEZ: |
| 1476 | *pOffset = (int16_t) insns[1]; |
| 1477 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1478 | break; |
| 1479 | default: |
| 1480 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1481 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1482 | return true; |
| 1483 | } |
| 1484 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1485 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1486 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1487 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1488 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1489 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1490 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1491 | if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1492 | cur_offset + switch_offset + 2 > insn_count)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1493 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1494 | << ", switch offset " << switch_offset |
| 1495 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1496 | return false; |
| 1497 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1498 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1499 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1500 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1501 | if (UNLIKELY(!IsAligned<4>(switch_insns))) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1502 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1503 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1504 | return false; |
| 1505 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1506 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1507 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1508 | if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) { |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1509 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1510 | << ", switch offset " << switch_offset |
| 1511 | << " not correctly visited, probably bad padding."; |
| 1512 | return false; |
| 1513 | } |
| 1514 | |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1515 | bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH; |
| 1516 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1517 | uint32_t switch_count = switch_insns[1]; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1518 | int32_t targets_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1519 | uint16_t expected_signature; |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1520 | if (is_packed_switch) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1521 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1522 | targets_offset = 4; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1523 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1524 | } else { |
| 1525 | /* 0=sig, 1=count, 2..count*2 = keys */ |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1526 | targets_offset = 2 + 2 * switch_count; |
| 1527 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1528 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1529 | uint32_t table_size = targets_offset + switch_count * 2; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1530 | if (UNLIKELY(switch_insns[0] != expected_signature)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1531 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1532 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1533 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1534 | return false; |
| 1535 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1536 | /* make sure the end of the switch is in range */ |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1537 | if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1538 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1539 | << ", switch offset " << switch_offset |
| 1540 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1541 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1542 | return false; |
| 1543 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1544 | |
| 1545 | constexpr int32_t keys_offset = 2; |
| 1546 | if (switch_count > 1) { |
| 1547 | if (is_packed_switch) { |
| 1548 | /* for a packed switch, verify that keys do not overflow int32 */ |
| 1549 | int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1550 | int32_t max_first_key = |
| 1551 | std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1552 | if (UNLIKELY(first_key > max_first_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1553 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key |
| 1554 | << ", switch_count=" << switch_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1555 | return false; |
| 1556 | } |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1557 | } else { |
| 1558 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1559 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1560 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
| 1561 | int32_t key = |
| 1562 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1563 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1564 | if (UNLIKELY(key <= last_key)) { |
David Brazdil | 5469d34 | 2015-09-25 16:57:53 +0100 | [diff] [blame] | 1565 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key |
| 1566 | << ", this=" << key; |
| 1567 | return false; |
| 1568 | } |
| 1569 | last_key = key; |
| 1570 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1573 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1574 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1575 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1576 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1577 | int32_t abs_offset = cur_offset + offset; |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1578 | if (UNLIKELY(abs_offset < 0 || |
| 1579 | abs_offset >= static_cast<int32_t>(insn_count) || |
| 1580 | !GetInstructionFlags(abs_offset).IsOpcode())) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1581 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1582 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1583 | << reinterpret_cast<void*>(cur_offset) |
| 1584 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1585 | return false; |
| 1586 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1587 | GetInstructionFlags(abs_offset).SetBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1588 | } |
| 1589 | return true; |
| 1590 | } |
| 1591 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1592 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1593 | uint16_t registers_size = code_item_->registers_size_; |
| 1594 | for (uint32_t idx = 0; idx < vA; idx++) { |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1595 | if (UNLIKELY(arg[idx] >= registers_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1596 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1597 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1598 | return false; |
| 1599 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1605 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1606 | uint16_t registers_size = code_item_->registers_size_; |
| 1607 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1608 | // integer overflow when adding them here. |
Andreas Gampe | 29e8142 | 2017-05-15 16:29:32 -0700 | [diff] [blame] | 1609 | if (UNLIKELY(vA + vC > registers_size)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1610 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1611 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1612 | return false; |
| 1613 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1614 | return true; |
| 1615 | } |
| 1616 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1617 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1618 | uint16_t registers_size = code_item_->registers_size_; |
| 1619 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1620 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1621 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1622 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1623 | insn_flags_.get(), |
| 1624 | insns_size, |
| 1625 | registers_size, |
| 1626 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1627 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1628 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1629 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1630 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1631 | /* Initialize register types of method arguments. */ |
| 1632 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1633 | DCHECK_NE(failures_.size(), 0U); |
| 1634 | std::string prepend("Bad signature in "); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1635 | prepend += dex_file_->PrettyMethod(dex_method_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1636 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1637 | return false; |
| 1638 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1639 | // We may have a runtime failure here, clear. |
| 1640 | have_pending_runtime_throw_failure_ = false; |
| 1641 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1642 | /* Perform code flow verification. */ |
| 1643 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1644 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1645 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1646 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1647 | return true; |
| 1648 | } |
| 1649 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1650 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1651 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1652 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1653 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1654 | } |
| 1655 | return os; |
| 1656 | } |
| 1657 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1658 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1659 | VariableIndentationOutputStream vios(&os); |
| 1660 | Dump(&vios); |
| 1661 | } |
| 1662 | |
| 1663 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1664 | if (code_item_ == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1665 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1666 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1667 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1668 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1669 | vios->Stream() << "Register Types:\n"; |
| 1670 | ScopedIndentation indent1(vios); |
| 1671 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1672 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1673 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1674 | ScopedIndentation indent1(vios); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1675 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1676 | 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] | 1677 | dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1678 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1679 | if (reg_line != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1680 | vios->Stream() << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1681 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1682 | vios->Stream() |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1683 | << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1684 | const bool kDumpHexOfInstruction = false; |
| 1685 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1686 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1687 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1688 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1689 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1692 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1693 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1694 | case 'I': |
| 1695 | case 'C': |
| 1696 | case 'S': |
| 1697 | case 'B': |
| 1698 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1699 | case 'F': |
| 1700 | case 'D': |
| 1701 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1702 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1703 | default: |
| 1704 | return false; |
| 1705 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1708 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1709 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1710 | |
| 1711 | // Should have been verified earlier. |
| 1712 | DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_); |
| 1713 | |
| 1714 | uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1715 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1716 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1717 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1718 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1719 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1720 | if (expected_args == 0) { |
| 1721 | // Expect at least a receiver. |
| 1722 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1726 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1727 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1728 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1729 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1730 | if (IsConstructor()) { |
| 1731 | if (declaring_class.IsJavaLangObject()) { |
| 1732 | // "this" is implicitly initialized. |
| 1733 | reg_line->SetThisInitialized(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1734 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1735 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1736 | reg_line->SetRegisterType<LockOp::kClear>( |
| 1737 | this, |
| 1738 | arg_start + cur_arg, |
| 1739 | reg_types_.UninitializedThisArgument(declaring_class)); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1740 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1741 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1742 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1743 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1744 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1747 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1748 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1749 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1750 | |
| 1751 | for (; iterator.HasNext(); iterator.Next()) { |
| 1752 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1753 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1754 | LOG(FATAL) << "Null descriptor"; |
| 1755 | } |
| 1756 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1757 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1758 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1759 | return false; |
| 1760 | } |
| 1761 | switch (descriptor[0]) { |
| 1762 | case 'L': |
| 1763 | case '[': |
| 1764 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1765 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1766 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1767 | // can return without doing anything or call virtual methods). |
| 1768 | { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1769 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1770 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1771 | DCHECK(HasFailures()); |
| 1772 | return false; |
| 1773 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1774 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1775 | } |
| 1776 | break; |
| 1777 | case 'Z': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1778 | 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] | 1779 | break; |
| 1780 | case 'C': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1781 | 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] | 1782 | break; |
| 1783 | case 'B': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1784 | 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] | 1785 | break; |
| 1786 | case 'I': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1787 | 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] | 1788 | break; |
| 1789 | case 'S': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1790 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1791 | break; |
| 1792 | case 'F': |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 1793 | reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1794 | break; |
| 1795 | case 'J': |
| 1796 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1797 | if (cur_arg + 1 >= expected_args) { |
| 1798 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1799 | << " args, found more (" << descriptor << ")"; |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1803 | const RegType* lo_half; |
| 1804 | const RegType* hi_half; |
| 1805 | if (descriptor[0] == 'J') { |
| 1806 | lo_half = ®_types_.LongLo(); |
| 1807 | hi_half = ®_types_.LongHi(); |
| 1808 | } else { |
| 1809 | lo_half = ®_types_.DoubleLo(); |
| 1810 | hi_half = ®_types_.DoubleHi(); |
| 1811 | } |
| 1812 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1813 | cur_arg++; |
| 1814 | break; |
| 1815 | } |
| 1816 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1817 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1818 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1819 | return false; |
| 1820 | } |
| 1821 | cur_arg++; |
| 1822 | } |
| 1823 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1824 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1825 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1826 | return false; |
| 1827 | } |
| 1828 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1829 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1830 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1831 | bool result; |
| 1832 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1833 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1834 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1835 | size_t i = 0; |
| 1836 | do { |
| 1837 | i++; |
| 1838 | } while (descriptor[i] == '['); // process leading [ |
| 1839 | if (descriptor[i] == 'L') { // object array |
| 1840 | do { |
| 1841 | i++; // find closing ; |
| 1842 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1843 | result = descriptor[i] == ';'; |
| 1844 | } else { // primitive array |
| 1845 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1846 | } |
| 1847 | } else if (descriptor[0] == 'L') { |
| 1848 | // could be more thorough here, but shouldn't be required |
| 1849 | size_t i = 0; |
| 1850 | do { |
| 1851 | i++; |
| 1852 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1853 | result = descriptor[i] == ';'; |
| 1854 | } else { |
| 1855 | result = false; |
| 1856 | } |
| 1857 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1858 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1859 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1860 | } |
| 1861 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1864 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1865 | const uint16_t* insns = code_item_->insns_; |
| 1866 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1867 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1868 | /* Begin by marking the first instruction as "changed". */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1869 | GetInstructionFlags(0).SetChanged(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1870 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1871 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1872 | /* Continue until no instructions are marked "changed". */ |
| 1873 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1874 | if (allow_thread_suspension_) { |
| 1875 | self_->AllowThreadSuspension(); |
| 1876 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1877 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1878 | uint32_t insn_idx = start_guess; |
| 1879 | for (; insn_idx < insns_size; insn_idx++) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1880 | if (GetInstructionFlags(insn_idx).IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1881 | break; |
| 1882 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1883 | if (insn_idx == insns_size) { |
| 1884 | if (start_guess != 0) { |
| 1885 | /* try again, starting from the top */ |
| 1886 | start_guess = 0; |
| 1887 | continue; |
| 1888 | } else { |
| 1889 | /* all flags are clear */ |
| 1890 | break; |
| 1891 | } |
| 1892 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1893 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1894 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1895 | // "changed" flags, we need to load the set of registers from the table. |
| 1896 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1897 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1898 | // target. |
| 1899 | work_insn_idx_ = insn_idx; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1900 | if (GetInstructionFlags(insn_idx).IsBranchTarget()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1901 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1902 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Sanity check: retrieve the stored register line (assuming |
| 1905 | * a full table) and make sure it actually matches. |
| 1906 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1907 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1908 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1909 | if (work_line_->CompareLine(register_line) != 0) { |
| 1910 | Dump(std::cout); |
| 1911 | std::cout << info_messages_.str(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1912 | LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1913 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1914 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1915 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1916 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1917 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1918 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1919 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1920 | std::string prepend(dex_file_->PrettyMethod(dex_method_idx_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1921 | prepend += " failed to verify: "; |
| 1922 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1923 | return false; |
| 1924 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1925 | /* Clear "changed" and mark as visited. */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1926 | GetInstructionFlags(insn_idx).SetVisited(); |
| 1927 | GetInstructionFlags(insn_idx).ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1928 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1929 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 1930 | if (kDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1931 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1932 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1933 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1934 | * down the line, possibly in the verifier. |
| 1935 | * |
| 1936 | * If we've substituted "always throw" instructions into the stream, |
| 1937 | * we are almost certainly going to have some dead code. |
| 1938 | */ |
| 1939 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1940 | uint32_t insn_idx = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1941 | for (; insn_idx < insns_size; |
| 1942 | insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1943 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1944 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1945 | * may or may not be preceded by a padding NOP (for alignment). |
| 1946 | */ |
| 1947 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1948 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1949 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1950 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1951 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1952 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1953 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1954 | GetInstructionFlags(insn_idx).SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 1957 | if (!GetInstructionFlags(insn_idx).IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1958 | if (dead_start < 0) |
| 1959 | dead_start = insn_idx; |
| 1960 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1961 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1962 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1963 | dead_start = -1; |
| 1964 | } |
| 1965 | } |
| 1966 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1967 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1968 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1969 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1970 | // To dump the state of the verify after a method, do something like: |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1971 | // if (dex_file_->PrettyMethod(dex_method_idx_) == |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1972 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1973 | // LOG(INFO) << info_messages_.str(); |
| 1974 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1975 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1976 | return true; |
| 1977 | } |
| 1978 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1979 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1980 | // is no such field. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1981 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) { |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1982 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 1983 | DCHECK(class_def != nullptr); |
| 1984 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 1985 | DCHECK(class_data != nullptr); |
| 1986 | ClassDataItemIterator it(dex_file, class_data); |
| 1987 | // Skip static fields. |
| 1988 | while (it.HasNextStaticField()) { |
| 1989 | it.Next(); |
| 1990 | } |
| 1991 | while (it.HasNextInstanceField()) { |
| 1992 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 1993 | return it.GetMemberIndex(); |
| 1994 | } |
| 1995 | it.Next(); |
| 1996 | } |
| 1997 | return DexFile::kDexNoIndex; |
| 1998 | } |
| 1999 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 2000 | // Setup a register line for the given return instruction. |
| 2001 | static void AdjustReturnLine(MethodVerifier* verifier, |
| 2002 | const Instruction* ret_inst, |
| 2003 | RegisterLine* line) { |
| 2004 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2005 | |
| 2006 | switch (opcode) { |
| 2007 | case Instruction::RETURN_VOID: |
| 2008 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2009 | SafelyMarkAllRegistersAsConflicts(verifier, line); |
| 2010 | break; |
| 2011 | |
| 2012 | case Instruction::RETURN: |
| 2013 | case Instruction::RETURN_OBJECT: |
| 2014 | line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x()); |
| 2015 | break; |
| 2016 | |
| 2017 | case Instruction::RETURN_WIDE: |
| 2018 | line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x()); |
| 2019 | break; |
| 2020 | |
| 2021 | default: |
| 2022 | LOG(FATAL) << "Unknown return opcode " << opcode; |
| 2023 | UNREACHABLE(); |
| 2024 | } |
| 2025 | } |
| 2026 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2027 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 2028 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 2029 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 2030 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 2031 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2032 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2033 | 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] | 2034 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 2035 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 2036 | } |
| 2037 | } |
| 2038 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2039 | /* |
| 2040 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2041 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2042 | * control to another statement: |
| 2043 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2044 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2046 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2047 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2048 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2049 | * throw an exception that is handled by an encompassing "try" |
| 2050 | * block. |
| 2051 | * |
| 2052 | * We can also return, in which case there is no successor instruction |
| 2053 | * from this point. |
| 2054 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2055 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2056 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2057 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 2058 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 2059 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2060 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2061 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2062 | bool just_set_result = false; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 2063 | if (kDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2064 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 2065 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2066 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2067 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2068 | |
| 2069 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2070 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2071 | * can throw an exception, we will copy/merge this into the "catch" |
| 2072 | * address rather than work_line, because we don't want the result |
| 2073 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 2074 | * a type) to be visible to the exception handler. |
| 2075 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2076 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2077 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2078 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2079 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2080 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 2081 | 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] | 2082 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 2083 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2084 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 2085 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 2086 | // branch target. |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 2087 | RegisterLineArenaUniquePtr branch_line; |
| 2088 | RegisterLineArenaUniquePtr fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2089 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2090 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2091 | case Instruction::NOP: |
| 2092 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2093 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2094 | * a signature that looks like a NOP; if we see one of these in |
| 2095 | * the course of executing code then we have a problem. |
| 2096 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2097 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2098 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2099 | } |
| 2100 | break; |
| 2101 | |
| 2102 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2103 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2104 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2105 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2106 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2107 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2108 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2109 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2110 | break; |
| 2111 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2112 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2113 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2114 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2115 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2116 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2117 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2118 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2119 | break; |
| 2120 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2121 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2122 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2123 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2124 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2125 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2126 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2127 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2128 | break; |
| 2129 | |
| 2130 | /* |
| 2131 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2132 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2133 | * might want to hold the result in an actual CPU register, so the |
| 2134 | * Dalvik spec requires that these only appear immediately after an |
| 2135 | * invoke or filled-new-array. |
| 2136 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2137 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2138 | * redundant with the reset done below, but it can make the debug info |
| 2139 | * easier to read in some cases.) |
| 2140 | */ |
| 2141 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2142 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2143 | break; |
| 2144 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2145 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2148 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2149 | break; |
| 2150 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2151 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 2152 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 2153 | // where one entrypoint to the catch block is not actually an exception path. |
| 2154 | if (work_insn_idx_ == 0) { |
| 2155 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 2156 | break; |
| 2157 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2158 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 2159 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 2160 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2161 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2162 | const RegType& res_type = GetCaughtExceptionType(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2163 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2164 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2165 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2166 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2167 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2168 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2169 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2170 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2171 | } |
| 2172 | break; |
| 2173 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2174 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2175 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2176 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2177 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2178 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 2179 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2180 | } else { |
| 2181 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 2182 | // 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] | 2183 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2184 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2185 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 2186 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 2187 | return_type.IsShort() || return_type.IsChar()) && |
| 2188 | src_type.IsInteger())); |
| 2189 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2190 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2191 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2192 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2193 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2194 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2195 | } |
| 2196 | } |
| 2197 | break; |
| 2198 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2199 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2200 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2201 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2202 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2203 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2204 | } else { |
| 2205 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2206 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2207 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2208 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2209 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2210 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | break; |
| 2214 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 2215 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2216 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2217 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2218 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2219 | } else { |
| 2220 | /* return_type is the *expected* return type, not register value */ |
| 2221 | DCHECK(!return_type.IsZero()); |
| 2222 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2223 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2224 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 2225 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 2226 | // reference in vAA is an instance of the "return_type." |
| 2227 | if (reg_type.IsUndefined()) { |
| 2228 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 2229 | } else if (reg_type.IsConflict()) { |
| 2230 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 2231 | } else if (reg_type.IsUninitializedTypes()) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2232 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '" |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2233 | << reg_type << "'"; |
Andreas Gampe | a4c98f2 | 2015-11-06 16:24:49 -0800 | [diff] [blame] | 2234 | } else if (!reg_type.IsReferenceTypes()) { |
| 2235 | // We really do expect a reference here. |
| 2236 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type " |
| 2237 | << reg_type; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2238 | } else if (!return_type.IsAssignableFrom(reg_type, this)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2239 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 2240 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 2241 | << "' or '" << reg_type << "'"; |
| 2242 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2243 | bool soft_error = false; |
| 2244 | // Check whether arrays are involved. They will show a valid class status, even |
| 2245 | // if their components are erroneous. |
| 2246 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2247 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 2248 | if (soft_error) { |
| 2249 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 2250 | << reg_type << " vs " << return_type; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | if (!soft_error) { |
| 2255 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 2256 | << "', but expected from declaration '" << return_type << "'"; |
| 2257 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2258 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | break; |
| 2263 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2264 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2265 | case Instruction::CONST_4: { |
| 2266 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2267 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2268 | this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2269 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2270 | } |
| 2271 | case Instruction::CONST_16: { |
| 2272 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2273 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2274 | this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 2275 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2276 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2277 | case Instruction::CONST: { |
| 2278 | int32_t val = inst->VRegB_31i(); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2279 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2280 | this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2281 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2282 | } |
| 2283 | case Instruction::CONST_HIGH16: { |
| 2284 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2285 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2286 | this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2287 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 2288 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2289 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2290 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2291 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2292 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2293 | 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] | 2294 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2295 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2296 | } |
| 2297 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2298 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2299 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2300 | 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] | 2301 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2302 | break; |
| 2303 | } |
| 2304 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2305 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2306 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2307 | 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] | 2308 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2309 | break; |
| 2310 | } |
| 2311 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2312 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2313 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 2314 | 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] | 2315 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2316 | break; |
| 2317 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2318 | case Instruction::CONST_STRING: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2319 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2320 | this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2321 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2322 | case Instruction::CONST_STRING_JUMBO: |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2323 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2324 | this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2325 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2326 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2327 | // Get type from instruction if unresolved then we need an access check |
| 2328 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2329 | const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2330 | // 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] | 2331 | work_line_->SetRegisterType<LockOp::kClear>( |
| 2332 | this, inst->VRegA_21c(), res_type.IsConflict() ? res_type |
| 2333 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2334 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2335 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2336 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2337 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2338 | // Check whether the previous instruction is a move-object with vAA as a source, creating |
| 2339 | // untracked lock aliasing. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2340 | if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2341 | uint32_t prev_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2342 | while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) { |
Andreas Gampe | c147410 | 2015-08-18 08:57:44 -0700 | [diff] [blame] | 2343 | prev_idx--; |
| 2344 | } |
| 2345 | const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx); |
| 2346 | switch (prev_inst->Opcode()) { |
| 2347 | case Instruction::MOVE_OBJECT: |
| 2348 | case Instruction::MOVE_OBJECT_16: |
| 2349 | case Instruction::MOVE_OBJECT_FROM16: |
| 2350 | if (prev_inst->VRegB() == inst->VRegA_11x()) { |
| 2351 | // Redo the copy. This won't change the register types, but update the lock status |
| 2352 | // for the aliased register. |
| 2353 | work_line_->CopyRegister1(this, |
| 2354 | prev_inst->VRegA(), |
| 2355 | prev_inst->VRegB(), |
| 2356 | kTypeCategoryRef); |
| 2357 | } |
| 2358 | break; |
| 2359 | |
| 2360 | default: // Other instruction types ignored. |
| 2361 | break; |
| 2362 | } |
| 2363 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2364 | break; |
| 2365 | case Instruction::MONITOR_EXIT: |
| 2366 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2367 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2368 | * 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] | 2369 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2370 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 2371 | * feature that Dalvik doesn't support.) |
| 2372 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2373 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2374 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2375 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2376 | * structured locking checks are working, the former would have |
| 2377 | * failed on the -enter instruction, and the latter is impossible. |
| 2378 | * |
| 2379 | * This is fortunate, because issue 3221411 prevents us from |
| 2380 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2381 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2382 | * some catch blocks (which will show up as "dead" code when |
| 2383 | * we skip them here); if we can't, then the code path could be |
| 2384 | * "live" so we still need to check it. |
| 2385 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2386 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2387 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2388 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2389 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2390 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2391 | /* |
| 2392 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 2393 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 2394 | * |
| 2395 | * 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] | 2396 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2397 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2398 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2399 | const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2400 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2401 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2402 | // If this is a primitive type, fail HARD. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2403 | ObjPtr<mirror::Class> klass = |
| 2404 | ClassLinker::LookupResolvedType(type_idx, dex_cache_.Get(), class_loader_.Get()); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2405 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2406 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2407 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2408 | << GetDeclaringClass(); |
| 2409 | break; |
| 2410 | } |
| 2411 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2412 | DCHECK_NE(failures_.size(), 0U); |
| 2413 | if (!is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2414 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2415 | inst->VRegA_22c(), |
| 2416 | reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2417 | } |
| 2418 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2419 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2420 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2421 | 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] | 2422 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2423 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2424 | if (is_checkcast) { |
| 2425 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2426 | } else { |
| 2427 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2428 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2429 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2430 | if (is_checkcast) { |
| 2431 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2432 | } else { |
| 2433 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2434 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2435 | } else if (orig_type.IsUninitializedTypes()) { |
| 2436 | if (is_checkcast) { |
| 2437 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v" |
| 2438 | << orig_type_reg; |
| 2439 | } else { |
| 2440 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v" |
| 2441 | << orig_type_reg; |
| 2442 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2443 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2444 | if (is_checkcast) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2445 | work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2446 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2447 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2448 | inst->VRegA_22c(), |
| 2449 | reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2450 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2451 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2452 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2453 | } |
| 2454 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2455 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2456 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2457 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2458 | 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] | 2459 | } else { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2460 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 2461 | inst->VRegA_12x(), |
| 2462 | reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2463 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2464 | } else { |
| 2465 | 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] | 2466 | } |
| 2467 | break; |
| 2468 | } |
| 2469 | case Instruction::NEW_INSTANCE: { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2470 | const RegType& res_type = ResolveClassAndCheckAccess(dex::TypeIndex(inst->VRegB_21c())); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2471 | if (res_type.IsConflict()) { |
| 2472 | DCHECK_NE(failures_.size(), 0U); |
| 2473 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2474 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2475 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2476 | // can't create an instance of an interface or abstract class */ |
| 2477 | if (!res_type.IsInstantiableTypes()) { |
| 2478 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2479 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2480 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2481 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2482 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2483 | // Any registers holding previous allocations from this address that have not yet been |
| 2484 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2485 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2486 | // add the new uninitialized reference to the register state |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2487 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2488 | break; |
| 2489 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2490 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2491 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2492 | break; |
| 2493 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2494 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2495 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2496 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2497 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2498 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2499 | just_set_result = true; // Filled new array range sets result register |
| 2500 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2501 | case Instruction::CMPL_FLOAT: |
| 2502 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2503 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2504 | break; |
| 2505 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2506 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2507 | break; |
| 2508 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2509 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2510 | break; |
| 2511 | case Instruction::CMPL_DOUBLE: |
| 2512 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2513 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2514 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2515 | break; |
| 2516 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2517 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2518 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2519 | break; |
| 2520 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2521 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2522 | break; |
| 2523 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2524 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2525 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2526 | break; |
| 2527 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2528 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2529 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2530 | break; |
| 2531 | } |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2532 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2533 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2534 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2535 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2536 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 2537 | if (res_type.IsUninitializedTypes()) { |
| 2538 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized"; |
Pavel Vyssotski | 980027c | 2016-02-11 20:28:11 +0600 | [diff] [blame] | 2539 | } else if (!res_type.IsReferenceTypes()) { |
| 2540 | 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] | 2541 | } else { |
| 2542 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2543 | << "thrown class " << res_type << " not instanceof Throwable"; |
| 2544 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2545 | } |
| 2546 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2547 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2548 | case Instruction::GOTO: |
| 2549 | case Instruction::GOTO_16: |
| 2550 | case Instruction::GOTO_32: |
| 2551 | /* no effect on or use of registers */ |
| 2552 | break; |
| 2553 | |
| 2554 | case Instruction::PACKED_SWITCH: |
| 2555 | case Instruction::SPARSE_SWITCH: |
| 2556 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2557 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2558 | break; |
| 2559 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2560 | case Instruction::FILL_ARRAY_DATA: { |
| 2561 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2562 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2563 | /* array_type can be null if the reg type is Zero */ |
| 2564 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2565 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2566 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2567 | << array_type; |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 2568 | } else if (array_type.IsUnresolvedTypes()) { |
| 2569 | // If it's an unresolved array type, it must be non-primitive. |
| 2570 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type " |
| 2571 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2572 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2573 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2574 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2575 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2576 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2577 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2578 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2579 | // Now verify if the element width in the table matches the element width declared in |
| 2580 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 2581 | const uint16_t* array_data = |
| 2582 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2583 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2584 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2585 | } else { |
| 2586 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2587 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2588 | // in the table and expected from the array class. |
| 2589 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2590 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2591 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2592 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2593 | } |
| 2594 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2598 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2599 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2600 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2601 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2602 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2603 | bool mismatch = false; |
| 2604 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2605 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2606 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2607 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2608 | } else { // both integral? |
| 2609 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2610 | } |
| 2611 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2612 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2613 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2614 | } |
| 2615 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2616 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2617 | case Instruction::IF_LT: |
| 2618 | case Instruction::IF_GE: |
| 2619 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2620 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2621 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2622 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2623 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2624 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2625 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2626 | } |
| 2627 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2628 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2629 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2630 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2631 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2632 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2633 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2634 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2635 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2636 | |
| 2637 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2638 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2639 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2640 | instance_of_idx = work_insn_idx_ - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2641 | while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2642 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2643 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2644 | if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2645 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2646 | work_insn_idx_)) { |
| 2647 | break; |
| 2648 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2649 | } else { |
| 2650 | break; |
| 2651 | } |
| 2652 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2653 | 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] | 2654 | |
| 2655 | /* Check for peep-hole pattern of: |
| 2656 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2657 | * instance-of vX, vY, T; |
| 2658 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2659 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2660 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2661 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2662 | * and sharpen the type of vY to be type T. |
| 2663 | * Note, this pattern can't be if: |
| 2664 | * - if there are other branches to this branch, |
| 2665 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2666 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2667 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2668 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 2669 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 2670 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2671 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2672 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2673 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2674 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2675 | // and we don't want to disallow field and other operations on the object. If the value |
| 2676 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2677 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2678 | // type is assignable to the original then allow optimization. This check is performed to |
| 2679 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2680 | // interface from a class that would lose information relevant to field checks. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2681 | const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2682 | const RegType& cast_type = ResolveClassAndCheckAccess( |
| 2683 | dex::TypeIndex(instance_of_inst->VRegC_22c())); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2684 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2685 | if (!orig_type.Equals(cast_type) && |
| 2686 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2687 | 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] | 2688 | !cast_type.GetClass()->IsInterface() && |
| 2689 | (orig_type.IsZero() || |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 2690 | orig_type.IsStrictlyAssignableFrom( |
| 2691 | cast_type.Merge(orig_type, ®_types_, this), this))) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 2692 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2693 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2694 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2695 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2696 | branch_line.reset(update_line); |
| 2697 | } |
| 2698 | update_line->CopyFromLine(work_line_.get()); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2699 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2700 | instance_of_inst->VRegB_22c(), |
| 2701 | cast_type); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2702 | if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2703 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2704 | // register encoding space of instance-of, and propagate type information to the source |
| 2705 | // of the move-object. |
| 2706 | uint32_t move_idx = instance_of_idx - 1; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2707 | while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2708 | move_idx--; |
| 2709 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 2710 | if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2711 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2712 | work_insn_idx_)) { |
| 2713 | break; |
| 2714 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2715 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 2716 | switch (move_inst->Opcode()) { |
| 2717 | case Instruction::MOVE_OBJECT: |
| 2718 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2719 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2720 | move_inst->VRegB_12x(), |
| 2721 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2722 | } |
| 2723 | break; |
| 2724 | case Instruction::MOVE_OBJECT_FROM16: |
| 2725 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2726 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2727 | move_inst->VRegB_22x(), |
| 2728 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2729 | } |
| 2730 | break; |
| 2731 | case Instruction::MOVE_OBJECT_16: |
| 2732 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 2733 | update_line->SetRegisterType<LockOp::kKeep>(this, |
| 2734 | move_inst->VRegB_32x(), |
| 2735 | cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2736 | } |
| 2737 | break; |
| 2738 | default: |
| 2739 | break; |
| 2740 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2745 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2746 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2747 | case Instruction::IF_LTZ: |
| 2748 | case Instruction::IF_GEZ: |
| 2749 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2750 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2751 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2752 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2753 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2754 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2755 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2756 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2757 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2758 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2759 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2760 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2761 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2762 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2763 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2764 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2765 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2766 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2767 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2768 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2769 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2770 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2771 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2772 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2773 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2774 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2775 | break; |
| 2776 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2777 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2778 | break; |
| 2779 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2780 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2781 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2784 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2785 | break; |
| 2786 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2787 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2788 | break; |
| 2789 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2790 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2791 | break; |
| 2792 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2793 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2794 | break; |
| 2795 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2796 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2797 | break; |
| 2798 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2799 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2800 | break; |
| 2801 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2802 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2803 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2804 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2805 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2806 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2807 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2808 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2809 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2810 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2811 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2812 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2815 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2816 | break; |
| 2817 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2818 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2819 | break; |
| 2820 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2821 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2822 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2823 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2824 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2825 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2826 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2829 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2830 | break; |
| 2831 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2832 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2833 | break; |
| 2834 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2835 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2836 | break; |
| 2837 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2838 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2839 | break; |
| 2840 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2841 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2842 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2843 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2844 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2845 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2846 | break; |
| 2847 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2848 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2849 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2850 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2851 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2852 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2853 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2854 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2855 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2856 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2857 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2858 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2859 | break; |
| 2860 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2861 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2862 | break; |
| 2863 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2864 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2865 | break; |
| 2866 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2867 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2868 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2869 | break; |
| 2870 | |
| 2871 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2872 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2873 | break; |
| 2874 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2875 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2876 | break; |
| 2877 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2878 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2879 | break; |
| 2880 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2881 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2882 | break; |
| 2883 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2884 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2885 | break; |
| 2886 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2887 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2888 | break; |
| 2889 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2890 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2891 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2892 | break; |
| 2893 | |
| 2894 | case Instruction::INVOKE_VIRTUAL: |
| 2895 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2896 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2897 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2898 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2899 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2900 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2901 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2902 | MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL; |
| 2903 | ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2904 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2905 | if (called_method != nullptr) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 2906 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -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(called_method->GetReturnTypeDescriptor(), |
| 2909 | return_type_class, |
| 2910 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -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 | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2914 | } |
| 2915 | } |
| 2916 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2917 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2918 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2919 | dex::TypeIndex return_type_idx = |
| 2920 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2921 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2922 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2923 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2924 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2925 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2926 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2927 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2928 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2929 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2930 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2931 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2932 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2933 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2934 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 2935 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2936 | const char* return_type_descriptor; |
| 2937 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2938 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2939 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2940 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2941 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2942 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 2943 | dex::TypeIndex return_type_idx = |
| 2944 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2945 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2946 | } else { |
| 2947 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2948 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 2949 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2950 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2951 | return_type = &FromClass(return_type_descriptor, |
| 2952 | return_type_class, |
| 2953 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2954 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2955 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2956 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2957 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2958 | } |
| 2959 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2960 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2961 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2962 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2963 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2964 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2965 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2966 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 2967 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2968 | if (this_type.IsConflict()) // failure. |
| 2969 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2970 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2971 | /* no null refs allowed (?) */ |
| 2972 | if (this_type.IsZero()) { |
| 2973 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2974 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2975 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2976 | |
| 2977 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2978 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2979 | // TODO: re-enable constructor type verification |
| 2980 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2981 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2982 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2983 | // break; |
| 2984 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2985 | |
| 2986 | /* arg must be an uninitialized reference */ |
| 2987 | if (!this_type.IsUninitializedTypes()) { |
| 2988 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2989 | << this_type; |
| 2990 | break; |
| 2991 | } |
| 2992 | |
| 2993 | /* |
| 2994 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2995 | * registers that have the same object instance in them, not just the "this" register. |
| 2996 | */ |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 2997 | work_line_->MarkRefsAsInitialized(this, this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2998 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2999 | if (return_type == nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3000 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3001 | } |
| 3002 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3003 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3004 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3005 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3006 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3007 | just_set_result = true; |
| 3008 | break; |
| 3009 | } |
| 3010 | case Instruction::INVOKE_STATIC: |
| 3011 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3012 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3013 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3014 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3015 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3016 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3017 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3018 | dex::TypeIndex return_type_idx = |
| 3019 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3020 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3021 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3022 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3023 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3024 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3025 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3026 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3027 | } else { |
| 3028 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3029 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3030 | just_set_result = true; |
| 3031 | } |
| 3032 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3033 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3034 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3035 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3036 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3037 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3038 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3039 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 3040 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3041 | << abs_method->PrettyMethod() << "'"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3042 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3043 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3044 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3045 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 3046 | * interface or Object (see comments in RegType::JoinClass). |
| 3047 | */ |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3048 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3049 | if (this_type.IsZero()) { |
| 3050 | /* null pointer always passes (and always fails at runtime) */ |
| 3051 | } else { |
| 3052 | if (this_type.IsUninitializedTypes()) { |
| 3053 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 3054 | << this_type; |
| 3055 | break; |
| 3056 | } |
| 3057 | // In the past we have tried to assert that "called_interface" is assignable |
| 3058 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 3059 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 3060 | // implemented by "this_type". For example, two classes may implement the same |
| 3061 | // interfaces and have a common parent that doesn't implement the interface. The |
| 3062 | // join will set "this_type" to the parent class and a test that this implements |
| 3063 | // the interface will incorrectly fail. |
| 3064 | } |
| 3065 | /* |
| 3066 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 3067 | * the type information is in the abstract method, so we're good. |
| 3068 | */ |
| 3069 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3070 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3071 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3072 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3073 | dex::TypeIndex return_type_idx = |
| 3074 | dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3075 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3076 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3077 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3078 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3079 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3080 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3081 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3082 | } else { |
| 3083 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3084 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3085 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3086 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3087 | } |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3088 | case Instruction::INVOKE_POLYMORPHIC: |
| 3089 | case Instruction::INVOKE_POLYMORPHIC_RANGE: { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3090 | bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 3091 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range); |
| 3092 | if (called_method == nullptr) { |
| 3093 | // Convert potential soft failures in VerifyInvocationArgs() to hard errors. |
| 3094 | if (failure_messages_.size() > 0) { |
| 3095 | std::string message = failure_messages_.back()->str(); |
| 3096 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message; |
| 3097 | } else { |
| 3098 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure."; |
| 3099 | } |
| 3100 | break; |
| 3101 | } |
| 3102 | if (!CheckSignaturePolymorphicMethod(called_method) || |
| 3103 | !CheckSignaturePolymorphicReceiver(inst)) { |
| 3104 | break; |
| 3105 | } |
| 3106 | const uint32_t proto_idx = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3107 | const char* return_descriptor = |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3108 | dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx)); |
| 3109 | const RegType& return_type = |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3110 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3111 | if (!return_type.IsLowHalf()) { |
| 3112 | work_line_->SetResultRegisterType(this, return_type); |
| 3113 | } else { |
| 3114 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3115 | } |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 3116 | just_set_result = true; |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3117 | break; |
Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 3118 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3119 | case Instruction::INVOKE_CUSTOM: |
| 3120 | case Instruction::INVOKE_CUSTOM_RANGE: { |
| 3121 | // Verify registers based on method_type in the call site. |
| 3122 | bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE); |
| 3123 | |
| 3124 | // Step 1. Check the call site that produces the method handle for invocation |
| 3125 | const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3126 | if (!CheckCallSite(call_site_idx)) { |
| 3127 | DCHECK(HasFailures()); |
| 3128 | break; |
| 3129 | } |
| 3130 | |
| 3131 | // Step 2. Check the register arguments correspond to the expected arguments for the |
| 3132 | // method handle produced by step 1. The dex file verifier has checked ranges for |
| 3133 | // the first three arguments and CheckCallSite has checked the method handle type. |
| 3134 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 3135 | it.Next(); // Skip to name. |
| 3136 | it.Next(); // Skip to method type of the method handle |
| 3137 | const uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 3138 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx); |
| 3139 | DexFileParameterIterator param_it(*dex_file_, proto_id); |
| 3140 | // Treat method as static as it has yet to be determined. |
| 3141 | VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr); |
| 3142 | const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 3143 | |
| 3144 | // Step 3. Propagate return type information |
| 3145 | const RegType& return_type = |
| 3146 | reg_types_.FromDescriptor(GetClassLoader(), return_descriptor, false); |
| 3147 | if (!return_type.IsLowHalf()) { |
| 3148 | work_line_->SetResultRegisterType(this, return_type); |
| 3149 | } else { |
| 3150 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3151 | } |
| 3152 | just_set_result = true; |
| 3153 | // TODO: Add compiler support for invoke-custom (b/35337872). |
| 3154 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); |
| 3155 | break; |
| 3156 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3157 | case Instruction::NEG_INT: |
| 3158 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3159 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3160 | break; |
| 3161 | case Instruction::NEG_LONG: |
| 3162 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3163 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3164 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3165 | break; |
| 3166 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3167 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3168 | break; |
| 3169 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3170 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3171 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3172 | break; |
| 3173 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3174 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3175 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3176 | break; |
| 3177 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3178 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3179 | break; |
| 3180 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3181 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3182 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3183 | break; |
| 3184 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3185 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3186 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3187 | break; |
| 3188 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3189 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3190 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3191 | break; |
| 3192 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3193 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3194 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3195 | break; |
| 3196 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3197 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3198 | break; |
| 3199 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3200 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3201 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3202 | break; |
| 3203 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3204 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3205 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3206 | break; |
| 3207 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3208 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3209 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3210 | break; |
| 3211 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3212 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3213 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3214 | break; |
| 3215 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3216 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3217 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3218 | break; |
| 3219 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3220 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3221 | break; |
| 3222 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3223 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3224 | break; |
| 3225 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3226 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3227 | break; |
| 3228 | |
| 3229 | case Instruction::ADD_INT: |
| 3230 | case Instruction::SUB_INT: |
| 3231 | case Instruction::MUL_INT: |
| 3232 | case Instruction::REM_INT: |
| 3233 | case Instruction::DIV_INT: |
| 3234 | case Instruction::SHL_INT: |
| 3235 | case Instruction::SHR_INT: |
| 3236 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3237 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3238 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3239 | break; |
| 3240 | case Instruction::AND_INT: |
| 3241 | case Instruction::OR_INT: |
| 3242 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3243 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3244 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3245 | break; |
| 3246 | case Instruction::ADD_LONG: |
| 3247 | case Instruction::SUB_LONG: |
| 3248 | case Instruction::MUL_LONG: |
| 3249 | case Instruction::DIV_LONG: |
| 3250 | case Instruction::REM_LONG: |
| 3251 | case Instruction::AND_LONG: |
| 3252 | case Instruction::OR_LONG: |
| 3253 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3254 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3255 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3256 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3257 | break; |
| 3258 | case Instruction::SHL_LONG: |
| 3259 | case Instruction::SHR_LONG: |
| 3260 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3261 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3262 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3263 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3264 | break; |
| 3265 | case Instruction::ADD_FLOAT: |
| 3266 | case Instruction::SUB_FLOAT: |
| 3267 | case Instruction::MUL_FLOAT: |
| 3268 | case Instruction::DIV_FLOAT: |
| 3269 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3270 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3271 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3272 | break; |
| 3273 | case Instruction::ADD_DOUBLE: |
| 3274 | case Instruction::SUB_DOUBLE: |
| 3275 | case Instruction::MUL_DOUBLE: |
| 3276 | case Instruction::DIV_DOUBLE: |
| 3277 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3278 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3279 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3280 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3281 | break; |
| 3282 | case Instruction::ADD_INT_2ADDR: |
| 3283 | case Instruction::SUB_INT_2ADDR: |
| 3284 | case Instruction::MUL_INT_2ADDR: |
| 3285 | case Instruction::REM_INT_2ADDR: |
| 3286 | case Instruction::SHL_INT_2ADDR: |
| 3287 | case Instruction::SHR_INT_2ADDR: |
| 3288 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3289 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3290 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3291 | break; |
| 3292 | case Instruction::AND_INT_2ADDR: |
| 3293 | case Instruction::OR_INT_2ADDR: |
| 3294 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3295 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3296 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3297 | break; |
| 3298 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3299 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 3300 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3301 | break; |
| 3302 | case Instruction::ADD_LONG_2ADDR: |
| 3303 | case Instruction::SUB_LONG_2ADDR: |
| 3304 | case Instruction::MUL_LONG_2ADDR: |
| 3305 | case Instruction::DIV_LONG_2ADDR: |
| 3306 | case Instruction::REM_LONG_2ADDR: |
| 3307 | case Instruction::AND_LONG_2ADDR: |
| 3308 | case Instruction::OR_LONG_2ADDR: |
| 3309 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3310 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3311 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 3312 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3313 | break; |
| 3314 | case Instruction::SHL_LONG_2ADDR: |
| 3315 | case Instruction::SHR_LONG_2ADDR: |
| 3316 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3317 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3318 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3319 | break; |
| 3320 | case Instruction::ADD_FLOAT_2ADDR: |
| 3321 | case Instruction::SUB_FLOAT_2ADDR: |
| 3322 | case Instruction::MUL_FLOAT_2ADDR: |
| 3323 | case Instruction::DIV_FLOAT_2ADDR: |
| 3324 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3325 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 3326 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3327 | break; |
| 3328 | case Instruction::ADD_DOUBLE_2ADDR: |
| 3329 | case Instruction::SUB_DOUBLE_2ADDR: |
| 3330 | case Instruction::MUL_DOUBLE_2ADDR: |
| 3331 | case Instruction::DIV_DOUBLE_2ADDR: |
| 3332 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3333 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3334 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 3335 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3336 | break; |
| 3337 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 3338 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3339 | case Instruction::MUL_INT_LIT16: |
| 3340 | case Instruction::DIV_INT_LIT16: |
| 3341 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3342 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3343 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3344 | break; |
| 3345 | case Instruction::AND_INT_LIT16: |
| 3346 | case Instruction::OR_INT_LIT16: |
| 3347 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3348 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3349 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3350 | break; |
| 3351 | case Instruction::ADD_INT_LIT8: |
| 3352 | case Instruction::RSUB_INT_LIT8: |
| 3353 | case Instruction::MUL_INT_LIT8: |
| 3354 | case Instruction::DIV_INT_LIT8: |
| 3355 | case Instruction::REM_INT_LIT8: |
| 3356 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3357 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3358 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3359 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 3360 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3361 | break; |
| 3362 | case Instruction::AND_INT_LIT8: |
| 3363 | case Instruction::OR_INT_LIT8: |
| 3364 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3365 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 3366 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3367 | break; |
| 3368 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3369 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3370 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 3371 | if (IsConstructor() && !IsStatic()) { |
| 3372 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 3373 | if (declaring_class.IsUnresolvedReference()) { |
| 3374 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 3375 | // manually over the underlying dex file. |
| 3376 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 3377 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
| 3378 | if (first_index != DexFile::kDexNoIndex) { |
| 3379 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 3380 | << first_index; |
| 3381 | } |
| 3382 | break; |
| 3383 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3384 | auto* klass = declaring_class.GetClass(); |
| 3385 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 3386 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 3387 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3388 | << klass->GetInstanceField(i)->PrettyField(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3389 | break; |
| 3390 | } |
| 3391 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3392 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 3393 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 3394 | // quickened opcodes (otherwise this could be a fall-through). |
| 3395 | if (!IsConstructor()) { |
| 3396 | if (!GetMethodReturnType().IsConflict()) { |
| 3397 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 3398 | } |
| 3399 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 3400 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3401 | // Note: the following instructions encode offsets derived from class linking. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 3402 | // As such they use Class*/Field*/Executable* as these offsets only have |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3403 | // meaning if the class linking and resolution were successful. |
| 3404 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3405 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3406 | break; |
| 3407 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3408 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3409 | break; |
| 3410 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3411 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3412 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3413 | case Instruction::IGET_BOOLEAN_QUICK: |
| 3414 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 3415 | break; |
| 3416 | case Instruction::IGET_BYTE_QUICK: |
| 3417 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 3418 | break; |
| 3419 | case Instruction::IGET_CHAR_QUICK: |
| 3420 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 3421 | break; |
| 3422 | case Instruction::IGET_SHORT_QUICK: |
| 3423 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 3424 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3425 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3426 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3427 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3428 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3429 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3430 | break; |
| 3431 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3432 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3433 | break; |
| 3434 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3435 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3436 | break; |
| 3437 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3438 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3439 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3440 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3441 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3442 | break; |
| 3443 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3444 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3445 | break; |
| 3446 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 3447 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 3448 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3449 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3450 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3451 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3452 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3453 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3454 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3455 | } else { |
| 3456 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 3457 | } |
| 3458 | just_set_result = true; |
| 3459 | } |
| 3460 | break; |
| 3461 | } |
| 3462 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3463 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3464 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Narayan Kamath | 8ec3bd2 | 2016-08-03 12:46:23 +0100 | [diff] [blame] | 3465 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9: |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3466 | case Instruction::UNUSED_FE ... Instruction::UNUSED_FF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3467 | case Instruction::UNUSED_79: |
| 3468 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3469 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3470 | break; |
| 3471 | |
| 3472 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3473 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3474 | * complain if an instruction is missing (which is desirable). |
| 3475 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3476 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3477 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3478 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3479 | if (Runtime::Current()->IsAotCompiler()) { |
| 3480 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3481 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3482 | LOG(ERROR) << "Pending failures:"; |
| 3483 | for (auto& error : failures_) { |
| 3484 | LOG(ERROR) << error; |
| 3485 | } |
| 3486 | for (auto& error_msg : failure_messages_) { |
| 3487 | LOG(ERROR) << error_msg->str(); |
| 3488 | } |
| 3489 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3490 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3491 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3492 | /* immediate failure, reject class */ |
| 3493 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3494 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3495 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3496 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3497 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3498 | // Note: the flag must be reset as it is only global to decouple Fail and is semantically per |
| 3499 | // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the |
| 3500 | // very end. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3501 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3502 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3503 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3504 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3505 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3506 | */ |
| 3507 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3508 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3509 | } |
| 3510 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3511 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3512 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3513 | * |
| 3514 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3515 | * 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] | 3516 | * somebody could get a reference field, check it for zero, and if the |
| 3517 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3518 | * 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] | 3519 | * that, and will reject the code. |
| 3520 | * |
| 3521 | * TODO: avoid re-fetching the branch target |
| 3522 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3523 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3524 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3525 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3526 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3527 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3528 | return false; |
| 3529 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3530 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3531 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3532 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3533 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3534 | /* update branch target, set "changed" if appropriate */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3535 | if (nullptr != branch_line) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3536 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3537 | return false; |
| 3538 | } |
| 3539 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3540 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3541 | return false; |
| 3542 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3543 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3547 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3548 | * |
| 3549 | * We've already verified that the table is structurally sound, so we |
| 3550 | * just need to walk through and tag the targets. |
| 3551 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3552 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3553 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3554 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3555 | int switch_count = switch_insns[1]; |
| 3556 | int offset_to_targets, targ; |
| 3557 | |
| 3558 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3559 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3560 | offset_to_targets = 4; |
| 3561 | } else { |
| 3562 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3563 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3564 | offset_to_targets = 2 + 2 * switch_count; |
| 3565 | } |
| 3566 | |
| 3567 | /* verify each switch target */ |
| 3568 | for (targ = 0; targ < switch_count; targ++) { |
| 3569 | int offset; |
| 3570 | uint32_t abs_offset; |
| 3571 | |
| 3572 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3573 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 3574 | (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] | 3575 | abs_offset = work_insn_idx_ + offset; |
| 3576 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3577 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3578 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3579 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3580 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3581 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3582 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3587 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3588 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3589 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3590 | if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3591 | bool has_catch_all_handler = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3592 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3593 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3594 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3595 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3596 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3597 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3598 | dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3599 | if (!handler_type_idx.IsValid()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3600 | has_catch_all_handler = true; |
| 3601 | } else { |
| 3602 | // It is also a catch-all if it is java.lang.Throwable. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3603 | mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_, |
| 3604 | class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3605 | if (klass != nullptr) { |
| 3606 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3607 | has_catch_all_handler = true; |
| 3608 | } |
| 3609 | } else { |
| 3610 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3611 | DCHECK(self_->IsExceptionPending()); |
| 3612 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3613 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3614 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3615 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3616 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3617 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3618 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3619 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3620 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3621 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3622 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3626 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3627 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3628 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3629 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3630 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3631 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3632 | * 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] | 3633 | * it will do so before grabbing the lock). |
| 3634 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3635 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3636 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3637 | << "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] | 3638 | return false; |
| 3639 | } |
| 3640 | } |
| 3641 | } |
| 3642 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3643 | /* Handle "continue". Tag the next consecutive instruction. |
| 3644 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3645 | * because it changes work_line_ when performing peephole optimization |
| 3646 | * and this change should not be used in those cases. |
| 3647 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3648 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3649 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3650 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3651 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 3652 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3653 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3654 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3655 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3656 | // next instruction isn't one. |
| 3657 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 3658 | return false; |
| 3659 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3660 | if (nullptr != fallthrough_line) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3661 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3662 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3663 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3664 | if (GetInstructionFlags(next_insn_idx).IsReturn()) { |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3665 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3666 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3667 | AdjustReturnLine(this, ret_inst, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3668 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3669 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3670 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3671 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3672 | // needed. If the merge changes the state of the registers then the work line will be |
| 3673 | // updated. |
| 3674 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3675 | return false; |
| 3676 | } |
| 3677 | } else { |
| 3678 | /* |
| 3679 | * We're not recording register data for the next instruction, so we don't know what the |
| 3680 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3681 | */ |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3682 | GetInstructionFlags(next_insn_idx).SetChanged(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3683 | } |
| 3684 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3685 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3686 | /* 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] | 3687 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3688 | work_line_->VerifyMonitorStackEmpty(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3692 | * Update start_guess. Advance to the next instruction of that's |
| 3693 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3694 | * neither of those exists we're in a return or throw; leave start_guess |
| 3695 | * alone and let the caller sort it out. |
| 3696 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3697 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3698 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3699 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3700 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3701 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3702 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3705 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3706 | DCHECK(GetInstructionFlags(*start_guess).IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3707 | |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 3708 | if (have_pending_runtime_throw_failure_) { |
| 3709 | have_any_pending_runtime_throw_failure_ = true; |
| 3710 | // Reset the pending_runtime_throw flag now. |
| 3711 | have_pending_runtime_throw_failure_ = false; |
| 3712 | } |
| 3713 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3714 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3715 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3716 | |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3717 | void MethodVerifier::UninstantiableError(const char* descriptor) { |
| 3718 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 3719 | << "non-instantiable klass " << descriptor; |
| 3720 | } |
| 3721 | |
| 3722 | inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) { |
| 3723 | return klass->IsInstantiable() || klass->IsPrimitive(); |
| 3724 | } |
| 3725 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3726 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(dex::TypeIndex class_idx) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 3727 | mirror::Class* klass = can_load_classes_ |
| 3728 | ? Runtime::Current()->GetClassLinker()->ResolveType( |
| 3729 | *dex_file_, class_idx, dex_cache_, class_loader_) |
| 3730 | : ClassLinker::LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get()).Ptr(); |
| 3731 | if (can_load_classes_ && klass == nullptr) { |
| 3732 | DCHECK(self_->IsExceptionPending()); |
| 3733 | self_->ClearException(); |
| 3734 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3735 | const RegType* result = nullptr; |
Vladimir Marko | 9cb0c46 | 2017-04-21 13:31:41 +0100 | [diff] [blame] | 3736 | if (klass != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3737 | bool precise = klass->CannotBeAssignedFromOtherTypes(); |
| 3738 | if (precise && !IsInstantiableOrPrimitive(klass)) { |
| 3739 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3740 | UninstantiableError(descriptor); |
| 3741 | precise = false; |
| 3742 | } |
| 3743 | result = reg_types_.FindClass(klass, precise); |
| 3744 | if (result == nullptr) { |
| 3745 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3746 | result = reg_types_.InsertClass(descriptor, klass, precise); |
| 3747 | } |
| 3748 | } else { |
| 3749 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3750 | result = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3751 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3752 | DCHECK(result != nullptr); |
| 3753 | if (result->IsConflict()) { |
| 3754 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
| 3755 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3756 | << "' in " << GetDeclaringClass(); |
| 3757 | return *result; |
| 3758 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3759 | |
| 3760 | // Record result of class resolution attempt. |
| 3761 | VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass); |
| 3762 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3763 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3764 | // check at runtime if access is allowed and so pass here. If result is |
| 3765 | // primitive, skip the access check. |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3766 | if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) { |
| 3767 | const RegType& referrer = GetDeclaringClass(); |
| 3768 | if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) { |
| 3769 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 3770 | << referrer << "' -> '" << *result << "'"; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3771 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3772 | } |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 3773 | return *result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3774 | } |
| 3775 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3776 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3777 | const RegType* common_super = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3778 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3779 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3780 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3781 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3782 | CatchHandlerIterator iterator(handlers_ptr); |
| 3783 | for (; iterator.HasNext(); iterator.Next()) { |
| 3784 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 3785 | if (!iterator.GetHandlerTypeIndex().IsValid()) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3786 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3787 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3788 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3789 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 3790 | DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit. |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3791 | if (exception.IsUnresolvedTypes()) { |
| 3792 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3793 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3794 | return exception; |
| 3795 | } else { |
| 3796 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3797 | return reg_types_.Conflict(); |
| 3798 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3799 | } else if (common_super == nullptr) { |
| 3800 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3801 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3802 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3803 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3804 | common_super = &common_super->Merge(exception, ®_types_, this); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3805 | if (FailOrAbort(this, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3806 | reg_types_.JavaLangThrowable(false).IsAssignableFrom( |
| 3807 | *common_super, this), |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3808 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3809 | work_insn_idx_)) { |
| 3810 | break; |
| 3811 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3816 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3817 | } |
| 3818 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3819 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3820 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3821 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3822 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3823 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3824 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3827 | inline static MethodResolutionKind GetMethodResolutionKind( |
| 3828 | MethodType method_type, bool is_interface) { |
| 3829 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
| 3830 | return kDirectMethodResolution; |
| 3831 | } else if (method_type == METHOD_INTERFACE) { |
| 3832 | return kInterfaceMethodResolution; |
| 3833 | } else if (method_type == METHOD_SUPER && is_interface) { |
| 3834 | return kInterfaceMethodResolution; |
| 3835 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3836 | DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER |
| 3837 | || method_type == METHOD_POLYMORPHIC); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3838 | return kVirtualMethodResolution; |
| 3839 | } |
| 3840 | } |
| 3841 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3842 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3843 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3844 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3845 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3846 | if (klass_type.IsConflict()) { |
| 3847 | std::string append(" in attempt to access method "); |
| 3848 | append += dex_file_->GetMethodName(method_id); |
| 3849 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3850 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3851 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3852 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3853 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3854 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3855 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3856 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3857 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3858 | auto pointer_size = cl->GetImagePointerSize(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3859 | MethodResolutionKind res_kind = GetMethodResolutionKind(method_type, klass->IsInterface()); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3860 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3861 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3862 | bool stash_method = false; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3863 | if (res_method == nullptr) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3864 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3865 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3866 | |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3867 | if (res_kind == kDirectMethodResolution) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3868 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3869 | } else if (res_kind == kVirtualMethodResolution) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3870 | res_method = klass->FindVirtualMethod(name, signature, pointer_size); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3871 | } else { |
| 3872 | DCHECK_EQ(res_kind, kInterfaceMethodResolution); |
| 3873 | res_method = klass->FindInterfaceMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3874 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3875 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3876 | if (res_method != nullptr) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3877 | stash_method = true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3878 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3879 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3880 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3881 | // 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] | 3882 | // Note that in this case, we do not put the resolved method in the Dex cache |
| 3883 | // because it was not discovered using the expected type of method resolution. |
| 3884 | if (res_kind != kDirectMethodResolution) { |
| 3885 | // Record result of the initial resolution attempt. |
| 3886 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, nullptr); |
| 3887 | // Change resolution type to 'direct' and try to resolve again. |
| 3888 | res_kind = kDirectMethodResolution; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3889 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3890 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3891 | } |
| 3892 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3893 | |
| 3894 | // Record result of method resolution attempt. |
| 3895 | VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_kind, res_method); |
| 3896 | |
| 3897 | if (res_method == nullptr) { |
| 3898 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3899 | << klass->PrettyDescriptor() << "." |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 3900 | << dex_file_->GetMethodName(method_id) << " " |
| 3901 | << dex_file_->GetMethodSignature(method_id); |
| 3902 | return nullptr; |
| 3903 | } |
| 3904 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3905 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3906 | // enforce them here. |
| 3907 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3908 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3909 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3910 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3911 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3912 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3913 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3914 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3915 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3916 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3917 | } |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3918 | |
| 3919 | // Check that interface methods are static or match interface classes. |
| 3920 | // We only allow statics if we don't have default methods enabled. |
| 3921 | // |
| 3922 | // Note: this check must be after the initializer check, as those are required to fail a class, |
| 3923 | // while this check implies an IncompatibleClassChangeError. |
| 3924 | if (klass->IsInterface()) { |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3925 | // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if |
| 3926 | // dex file version is 37 or greater), or invoke-static. |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3927 | if (method_type != METHOD_INTERFACE && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3928 | method_type != METHOD_STATIC && |
Alex Light | b55f1ac | 2016-04-12 15:50:55 -0700 | [diff] [blame] | 3929 | ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) || |
| 3930 | method_type != METHOD_DIRECT) && |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 3931 | method_type != METHOD_SUPER) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3932 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3933 | << "non-interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3934 | << " is in an interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3935 | return nullptr; |
| 3936 | } |
| 3937 | } else { |
| 3938 | if (method_type == METHOD_INTERFACE) { |
| 3939 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3940 | << "interface method " << dex_file_->PrettyMethod(dex_method_idx) |
| 3941 | << " is in a non-interface class " << klass->PrettyClass(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 3942 | return nullptr; |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct. |
| 3947 | if (stash_method) { |
| 3948 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size); |
| 3949 | } |
| 3950 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3951 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3952 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3953 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " |
| 3954 | << res_method->PrettyMethod() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3955 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3956 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3957 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3958 | // 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] | 3959 | if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3960 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3961 | << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3962 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3963 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3964 | // See if the method type implied by the invoke instruction matches the access flags for the |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3965 | // target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two |
| 3966 | // signature polymorphic methods supported by the run-time which are native methods with variable |
| 3967 | // arguments. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3968 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3969 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 3970 | ((method_type == METHOD_SUPER || |
| 3971 | method_type == METHOD_VIRTUAL || |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3972 | method_type == METHOD_INTERFACE) && res_method->IsDirect()) || |
| 3973 | ((method_type == METHOD_POLYMORPHIC) && |
| 3974 | (!res_method->IsNative() || !res_method->IsVarargs()))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3975 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3976 | "type of " << res_method->PrettyMethod(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3977 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3978 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3979 | return res_method; |
| 3980 | } |
| 3981 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3982 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3983 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 3984 | 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] | 3985 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3986 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3987 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 3988 | const size_t expected_args = inst->VRegA(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3989 | /* caught by static verifier */ |
| 3990 | DCHECK(is_range || expected_args <= 5); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3991 | |
Orion Hodson | 7f7a074 | 2016-12-08 14:15:25 +0000 | [diff] [blame] | 3992 | // TODO(oth): Enable this path for invoke-polymorphic when b/33099829 is resolved. |
| 3993 | if (method_type != METHOD_POLYMORPHIC) { |
| 3994 | if (expected_args > code_item_->outs_size_) { |
| 3995 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3996 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3997 | return nullptr; |
| 3998 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3999 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4000 | |
| 4001 | /* |
| 4002 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4003 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4004 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4005 | */ |
| 4006 | if (method_type != METHOD_STATIC) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4007 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4008 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 4009 | CHECK(have_pending_hard_failure_); |
| 4010 | return nullptr; |
| 4011 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4012 | bool is_init = false; |
| 4013 | if (actual_arg_type.IsUninitializedTypes()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4014 | if (res_method) { |
| 4015 | if (!res_method->IsConstructor()) { |
| 4016 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4017 | return nullptr; |
| 4018 | } |
| 4019 | } else { |
| 4020 | // Check whether the name of the called method is "<init>" |
| 4021 | 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] | 4022 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4023 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 4024 | return nullptr; |
| 4025 | } |
| 4026 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4027 | is_init = true; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4028 | } |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4029 | const RegType& adjusted_type = is_init |
| 4030 | ? GetRegTypeCache()->FromUninitialized(actual_arg_type) |
| 4031 | : actual_arg_type; |
| 4032 | if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4033 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 4034 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 4035 | // class. It would be wrong to use this for the type check (interface type checks are |
| 4036 | // postponed to runtime). |
| 4037 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4038 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4039 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4040 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 4041 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4042 | } else { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4043 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4044 | const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 4045 | res_method_class = ®_types_.FromDescriptor( |
| 4046 | GetClassLoader(), |
| 4047 | dex_file_->StringByTypeIdx(class_idx), |
| 4048 | false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4049 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4050 | if (!res_method_class->IsAssignableFrom(adjusted_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4051 | Fail(adjusted_type.IsUnresolvedTypes() |
| 4052 | ? VERIFY_ERROR_NO_CLASS |
| 4053 | : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 4054 | << "'this' argument '" << actual_arg_type << "' not instance of '" |
| 4055 | << *res_method_class << "'"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4056 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4057 | // the compiler. |
| 4058 | if (have_pending_hard_failure_) { |
| 4059 | return nullptr; |
| 4060 | } |
| 4061 | } |
| 4062 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4065 | uint32_t arg[5]; |
| 4066 | if (!is_range) { |
| 4067 | inst->GetVarArgs(arg); |
| 4068 | } |
| 4069 | uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4070 | for ( ; it->HasNext(); it->Next()) { |
| 4071 | if (sig_registers >= expected_args) { |
| 4072 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4073 | " argument registers, method signature has " << sig_registers + 1 << " or more"; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4074 | return nullptr; |
| 4075 | } |
| 4076 | |
| 4077 | const char* param_descriptor = it->GetDescriptor(); |
| 4078 | |
| 4079 | if (param_descriptor == nullptr) { |
| 4080 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 4081 | "component"; |
| 4082 | return nullptr; |
| 4083 | } |
| 4084 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4085 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4086 | uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4087 | arg[sig_registers]; |
| 4088 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4089 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4090 | if (!src_type.IsIntegralTypes()) { |
| 4091 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 4092 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 4093 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4094 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 4095 | } else { |
| 4096 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 4097 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 4098 | // the compiler. |
| 4099 | if (have_pending_hard_failure_) { |
| 4100 | return nullptr; |
| 4101 | } |
| 4102 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 4103 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 4104 | // instructions not specifying register pairs by the first component, but require them |
| 4105 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 4106 | // none, this will fail below. |
| 4107 | if (!is_range && sig_registers + 1 < expected_args) { |
| 4108 | uint32_t second_reg = arg[sig_registers + 1]; |
| 4109 | if (second_reg != get_reg + 1) { |
| 4110 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 4111 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 4112 | << second_reg << "."; |
| 4113 | return nullptr; |
| 4114 | } |
| 4115 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 4119 | } |
| 4120 | if (expected_args != sig_registers) { |
| 4121 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4122 | " argument registers, method signature has " << sig_registers; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4123 | return nullptr; |
| 4124 | } |
| 4125 | return res_method; |
| 4126 | } |
| 4127 | |
| 4128 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 4129 | MethodType method_type, |
| 4130 | bool is_range) { |
| 4131 | // As the method may not have been resolved, make this static check against what we expect. |
| 4132 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 4133 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4134 | const uint32_t method_idx = inst->VRegB(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4135 | DexFileParameterIterator it(*dex_file_, |
| 4136 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4137 | VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4138 | } |
| 4139 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4140 | bool MethodVerifier::CheckCallSite(uint32_t call_site_idx) { |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4141 | if (call_site_idx >= dex_file_->NumCallSiteIds()) { |
| 4142 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx |
| 4143 | << " >= " << dex_file_->NumCallSiteIds(); |
| 4144 | return false; |
| 4145 | } |
| 4146 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4147 | CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx)); |
| 4148 | // Check essential arguments are provided. The dex file verifier has verified indicies of the |
| 4149 | // main values (method handle, name, method_type). |
| 4150 | if (it.Size() < 3) { |
| 4151 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4152 | << " has too few arguments: " |
| 4153 | << it.Size() << "< 3"; |
| 4154 | return false; |
| 4155 | } |
| 4156 | |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4157 | // Get and check the first argument: the method handle (index range |
| 4158 | // checked by the dex file verifier). |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4159 | uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i); |
| 4160 | it.Next(); |
Orion Hodson | 3a842f5 | 2017-04-21 15:24:10 +0100 | [diff] [blame] | 4161 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 4162 | const DexFile::MethodHandleItem& mh = dex_file_->GetMethodHandle(method_handle_idx); |
| 4163 | if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) { |
| 4164 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4165 | << " argument 0 method handle type is not InvokeStatic"; |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
| 4169 | // Skip the second argument, the name to resolve, as checked by the |
| 4170 | // dex file verifier. |
| 4171 | it.Next(); |
| 4172 | |
| 4173 | // Skip the third argument, the method type expected, as checked by |
| 4174 | // the dex file verifier. |
| 4175 | it.Next(); |
| 4176 | |
| 4177 | // Check the bootstrap method handle and remaining arguments. |
| 4178 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(mh.field_or_method_idx_); |
| 4179 | uint32_t length; |
| 4180 | const char* shorty = dex_file_->GetMethodShorty(method_id, &length); |
| 4181 | |
| 4182 | if (it.Size() < length - 1) { |
| 4183 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4184 | << " too few arguments for bootstrap method: " |
| 4185 | << it.Size() << " < " << (length - 1); |
| 4186 | return false; |
| 4187 | } |
| 4188 | |
| 4189 | // Check the return type and first 3 arguments are references |
| 4190 | // (CallSite, Lookup, String, MethodType). If they are not of the |
| 4191 | // expected types (or subtypes), it will trigger a |
| 4192 | // WrongMethodTypeException during execution. |
| 4193 | if (shorty[0] != 'L') { |
| 4194 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4195 | << " bootstrap return type is not a reference"; |
| 4196 | return false; |
| 4197 | } |
| 4198 | |
| 4199 | for (uint32_t i = 1; i < 4; ++i) { |
| 4200 | if (shorty[i] != 'L') { |
| 4201 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4202 | << " bootstrap method argument " << (i - 1) |
| 4203 | << " is not a reference"; |
| 4204 | return false; |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | // Check the optional arguments. |
| 4209 | for (uint32_t i = 4; i < length; ++i, it.Next()) { |
| 4210 | bool match = false; |
| 4211 | switch (it.GetValueType()) { |
| 4212 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 4213 | case EncodedArrayValueIterator::ValueType::kByte: |
| 4214 | case EncodedArrayValueIterator::ValueType::kShort: |
| 4215 | case EncodedArrayValueIterator::ValueType::kChar: |
| 4216 | case EncodedArrayValueIterator::ValueType::kInt: |
| 4217 | // These all fit within one register and encoders do not seem |
| 4218 | // too exacting on the encoding type they use (ie using |
| 4219 | // integer for all of these). |
| 4220 | match = (strchr("ZBCSI", shorty[i]) != nullptr); |
| 4221 | break; |
| 4222 | case EncodedArrayValueIterator::ValueType::kLong: |
| 4223 | match = ('J' == shorty[i]); |
| 4224 | break; |
| 4225 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 4226 | match = ('F' == shorty[i]); |
| 4227 | break; |
| 4228 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 4229 | match = ('D' == shorty[i]); |
| 4230 | break; |
| 4231 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| 4232 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| 4233 | case EncodedArrayValueIterator::ValueType::kString: |
| 4234 | case EncodedArrayValueIterator::ValueType::kType: |
| 4235 | case EncodedArrayValueIterator::ValueType::kNull: |
| 4236 | match = ('L' == shorty[i]); |
| 4237 | break; |
| 4238 | case EncodedArrayValueIterator::ValueType::kField: |
| 4239 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 4240 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 4241 | case EncodedArrayValueIterator::ValueType::kArray: |
| 4242 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 4243 | // Unreachable based on current EncodedArrayValueIterator::Next(). |
| 4244 | UNREACHABLE(); |
| 4245 | } |
| 4246 | |
| 4247 | if (!match) { |
| 4248 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx |
| 4249 | << " bootstrap method argument " << (i - 1) |
| 4250 | << " expected " << shorty[i] |
| 4251 | << " got value type: " << it.GetValueType(); |
| 4252 | return false; |
| 4253 | } |
| 4254 | } |
| 4255 | return true; |
| 4256 | } |
| 4257 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4258 | class MethodParamListDescriptorIterator { |
| 4259 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4260 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4261 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 4262 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 4263 | } |
| 4264 | |
| 4265 | bool HasNext() { |
| 4266 | return pos_ < params_size_; |
| 4267 | } |
| 4268 | |
| 4269 | void Next() { |
| 4270 | ++pos_; |
| 4271 | } |
| 4272 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4273 | const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4274 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 4275 | } |
| 4276 | |
| 4277 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4278 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4279 | size_t pos_; |
| 4280 | const DexFile::TypeList* params_; |
| 4281 | const size_t params_size_; |
| 4282 | }; |
| 4283 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4284 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4285 | const Instruction* inst, MethodType method_type, bool is_range) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4286 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 4287 | // we're making. |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4288 | const uint32_t method_idx = inst->VRegB(); |
Alex Light | 7268d47 | 2016-01-20 15:50:01 -0800 | [diff] [blame] | 4289 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4290 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 4291 | // Check what we can statically. |
| 4292 | if (!have_pending_hard_failure_) { |
| 4293 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 4294 | } |
| 4295 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4296 | } |
| 4297 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4298 | // 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] | 4299 | // 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] | 4300 | if (method_type == METHOD_SUPER) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4301 | dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4302 | const RegType& reference_type = reg_types_.FromDescriptor( |
| 4303 | GetClassLoader(), |
| 4304 | dex_file_->StringByTypeIdx(class_idx), |
| 4305 | false); |
| 4306 | if (reference_type.IsUnresolvedTypes()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4307 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super"; |
| 4308 | return nullptr; |
| 4309 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4310 | if (reference_type.GetClass()->IsInterface()) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4311 | // TODO Can we verify anything else. |
David Brazdil | 15fc729 | 2016-09-02 14:13:18 +0100 | [diff] [blame] | 4312 | if (class_idx == class_def_.class_idx_) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4313 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface"; |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4314 | return nullptr; |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4315 | } |
| 4316 | // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS |
| 4317 | // does. |
Alex Light | 55ea94d | 2016-03-15 09:50:26 -0700 | [diff] [blame] | 4318 | if (!GetDeclaringClass().HasClass()) { |
| 4319 | Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an" |
| 4320 | << "interface invoke-super"; |
| 4321 | return nullptr; |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4322 | } else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) { |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4323 | Fail(VERIFY_ERROR_CLASS_CHANGE) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4324 | << "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass()) |
| 4325 | << " in method " |
| 4326 | << dex_file_->PrettyMethod(dex_method_idx_) << " to method " |
| 4327 | << dex_file_->PrettyMethod(method_idx) << " references " |
| 4328 | << "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 4329 | return nullptr; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4330 | } |
| 4331 | } else { |
| 4332 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
| 4333 | if (super.IsUnresolvedTypes()) { |
| 4334 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4335 | << dex_file_->PrettyMethod(dex_method_idx_) |
| 4336 | << " to super " << res_method->PrettyMethod(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4337 | return nullptr; |
| 4338 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4339 | if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) || |
Aart Bik | f663e34 | 2016-04-04 17:28:59 -0700 | [diff] [blame] | 4340 | (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4341 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4342 | << dex_file_->PrettyMethod(dex_method_idx_) |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 4343 | << " to super " << super |
| 4344 | << "." << res_method->GetName() |
| 4345 | << res_method->GetSignature(); |
| 4346 | return nullptr; |
| 4347 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4348 | } |
| 4349 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4350 | |
Andreas Gampe | 74979b1 | 2017-05-16 09:28:06 -0700 | [diff] [blame] | 4351 | if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) { |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4352 | // Process the signature of the calling site that is invoking the method handle. |
| 4353 | DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(inst->VRegH())); |
| 4354 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4355 | } else { |
| 4356 | // Process the target method's signature. |
| 4357 | MethodParamListDescriptorIterator it(res_method); |
| 4358 | return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method); |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | bool MethodVerifier::CheckSignaturePolymorphicMethod(ArtMethod* method) { |
| 4363 | mirror::Class* klass = method->GetDeclaringClass(); |
| 4364 | if (klass != mirror::MethodHandle::StaticClass()) { |
| 4365 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4366 | << "Signature polymorphic method must be declared in java.lang.invoke.MethodClass"; |
| 4367 | return false; |
| 4368 | } |
| 4369 | |
| 4370 | const char* method_name = method->GetName(); |
| 4371 | if (strcmp(method_name, "invoke") != 0 && strcmp(method_name, "invokeExact") != 0) { |
| 4372 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4373 | << "Signature polymorphic method name invalid: " << method_name; |
| 4374 | return false; |
| 4375 | } |
| 4376 | |
| 4377 | const DexFile::TypeList* types = method->GetParameterTypeList(); |
| 4378 | if (types->Size() != 1) { |
| 4379 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4380 | << "Signature polymorphic method has too many arguments " << types->Size() << " != 1"; |
| 4381 | return false; |
| 4382 | } |
| 4383 | |
| 4384 | const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_; |
| 4385 | const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index); |
| 4386 | if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) { |
| 4387 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4388 | << "Signature polymorphic method has unexpected argument type: " << argument_descriptor; |
| 4389 | return false; |
| 4390 | } |
| 4391 | |
| 4392 | const char* return_descriptor = method->GetReturnTypeDescriptor(); |
| 4393 | if (strcmp(return_descriptor, "Ljava/lang/Object;") != 0) { |
| 4394 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4395 | << "Signature polymorphic method has unexpected return type: " << return_descriptor; |
| 4396 | return false; |
| 4397 | } |
| 4398 | |
| 4399 | return true; |
| 4400 | } |
| 4401 | |
| 4402 | bool MethodVerifier::CheckSignaturePolymorphicReceiver(const Instruction* inst) { |
| 4403 | const RegType& this_type = work_line_->GetInvocationThis(this, inst); |
| 4404 | if (this_type.IsZero()) { |
| 4405 | /* null pointer always passes (and always fails at run time) */ |
| 4406 | return true; |
| 4407 | } else if (!this_type.IsNonZeroReferenceTypes()) { |
| 4408 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4409 | << "invoke-polymorphic receiver is not a reference: " |
| 4410 | << this_type; |
| 4411 | return false; |
| 4412 | } else if (this_type.IsUninitializedReference()) { |
| 4413 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4414 | << "invoke-polymorphic receiver is uninitialized: " |
| 4415 | << this_type; |
| 4416 | return false; |
| 4417 | } else if (!this_type.HasClass()) { |
| 4418 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4419 | << "invoke-polymorphic receiver has no class: " |
| 4420 | << this_type; |
| 4421 | return false; |
| 4422 | } else if (!this_type.GetClass()->IsSubClass(mirror::MethodHandle::StaticClass())) { |
| 4423 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 4424 | << "invoke-polymorphic receiver is not a subclass of MethodHandle: " |
| 4425 | << this_type; |
| 4426 | return false; |
| 4427 | } |
| 4428 | return true; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4431 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 4432 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4433 | if (is_range) { |
| 4434 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 4435 | } else { |
| 4436 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 4437 | } |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4438 | const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, allow_failure); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4439 | if (!actual_arg_type.HasClass()) { |
| 4440 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 4441 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4442 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4443 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4444 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4445 | if (klass->IsInterface()) { |
| 4446 | // Derive Object.class from Class.class.getSuperclass(). |
| 4447 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4448 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4449 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4450 | return nullptr; |
| 4451 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4452 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4453 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4454 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 4455 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4456 | if (!dispatch_class->HasVTable()) { |
| 4457 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 4458 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4459 | return nullptr; |
| 4460 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4461 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4462 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 4463 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4464 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 4465 | FailOrAbort(this, allow_failure, |
| 4466 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 4467 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4468 | return nullptr; |
| 4469 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4470 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 4471 | if (self_->IsExceptionPending()) { |
| 4472 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 4473 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4474 | return nullptr; |
| 4475 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4476 | return res_method; |
| 4477 | } |
| 4478 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4479 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4480 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4481 | << dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_; |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4482 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4483 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4484 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4485 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4486 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4487 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 4488 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 4489 | work_insn_idx_)) { |
| 4490 | return nullptr; |
| 4491 | } |
| 4492 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 4493 | work_insn_idx_)) { |
| 4494 | return nullptr; |
| 4495 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4496 | |
| 4497 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 4498 | // match the call to the signature. Also, we might be calling through an abstract method |
| 4499 | // definition (which doesn't have register count values). |
Orion Hodson | cfa325e | 2016-10-13 10:25:54 +0100 | [diff] [blame] | 4500 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4501 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4502 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4503 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4504 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4505 | /* caught by static verifier */ |
| 4506 | DCHECK(is_range || expected_args <= 5); |
| 4507 | if (expected_args > code_item_->outs_size_) { |
| 4508 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 4509 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4510 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | /* |
| 4514 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 4515 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 4516 | * rigorous check here (which is okay since we have to do it at runtime). |
| 4517 | */ |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4518 | // Note: given an uninitialized type, this should always fail. Constructors aren't virtual. |
| 4519 | if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4520 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4521 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4522 | } |
| 4523 | if (!actual_arg_type.IsZero()) { |
| 4524 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4525 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4526 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4527 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4528 | if (!res_method_class.IsAssignableFrom(actual_arg_type, this)) { |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4529 | Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never |
| 4530 | ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this. |
| 4531 | : actual_arg_type.IsUnresolvedTypes() |
| 4532 | ? VERIFY_ERROR_NO_CLASS |
| 4533 | : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4534 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4535 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | /* |
| 4539 | * Process the target method's signature. This signature may or may not |
| 4540 | * have been verified, so we can't assume it's properly formed. |
| 4541 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4542 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4543 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4544 | uint32_t arg[5]; |
| 4545 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4546 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4547 | } |
| 4548 | size_t actual_args = 1; |
| 4549 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 4550 | if (actual_args >= expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4551 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" |
| 4552 | << res_method->PrettyMethod() |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4553 | << "'. Expected " << expected_args |
| 4554 | << " arguments, processing argument " << actual_args |
| 4555 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4556 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4557 | } |
| 4558 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4559 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4560 | if (descriptor == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4561 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4562 | << res_method->PrettyMethod() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4563 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4564 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4565 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4566 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4567 | 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] | 4568 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4569 | return res_method; |
| 4570 | } |
| 4571 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 4572 | } |
| 4573 | if (actual_args != expected_args) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4574 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " |
| 4575 | << res_method->PrettyMethod() << " expected " |
| 4576 | << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4577 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4578 | } else { |
| 4579 | return res_method; |
| 4580 | } |
| 4581 | } |
| 4582 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 4583 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4584 | dex::TypeIndex type_idx; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4585 | if (!is_filled) { |
| 4586 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4587 | type_idx = dex::TypeIndex(inst->VRegC_22c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4588 | } else if (!is_range) { |
| 4589 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4590 | type_idx = dex::TypeIndex(inst->VRegB_35c()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4591 | } else { |
| 4592 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4593 | type_idx = dex::TypeIndex(inst->VRegB_3rc()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4594 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4595 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4596 | if (res_type.IsConflict()) { // bad class |
| 4597 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4598 | } else { |
| 4599 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 4600 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4601 | 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] | 4602 | } else if (!is_filled) { |
| 4603 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4604 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4605 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4606 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4607 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4608 | } else { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4609 | DCHECK(!res_type.IsUnresolvedMergedReference()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4610 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 4611 | // 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] | 4612 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4613 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 4614 | uint32_t arg[5]; |
| 4615 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 4616 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4617 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4618 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4619 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4620 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 4621 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4622 | return; |
| 4623 | } |
| 4624 | } |
| 4625 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4626 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4627 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 4628 | } |
| 4629 | } |
| 4630 | } |
| 4631 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4632 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4633 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4634 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4635 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4636 | 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] | 4637 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4638 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4639 | if (array_type.IsZero()) { |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 4640 | have_pending_runtime_throw_failure_ = true; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4641 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 4642 | // instruction type. TODO: have a proper notion of bottom here. |
| 4643 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 4644 | // Reference or category 1 |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4645 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4646 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4647 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4648 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 4649 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4650 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4651 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4652 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4653 | 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] | 4654 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4655 | // Unresolved array types must be reference array types. |
| 4656 | if (is_primitive) { |
| 4657 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
| 4658 | << " source for category 1 aget"; |
| 4659 | } else { |
| 4660 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type |
| 4661 | << " because of missing class"; |
Andreas Gampe | 8d8fc48 | 2016-03-25 16:24:20 -0700 | [diff] [blame] | 4662 | // Approximate with java.lang.Object[]. |
| 4663 | work_line_->SetRegisterType<LockOp::kClear>(this, |
| 4664 | inst->VRegA_23x(), |
| 4665 | reg_types_.JavaLangObject(false)); |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4666 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4667 | } else { |
| 4668 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4669 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4670 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4671 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4672 | << " source for aget-object"; |
| 4673 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4674 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4675 | << " source for category 1 aget"; |
| 4676 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 4677 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4678 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 4679 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 4680 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4681 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4682 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 4683 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 4684 | // doubles. |
| 4685 | if (!component_type.IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 4686 | work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4687 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4688 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4689 | component_type.HighHalf(®_types_)); |
| 4690 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4691 | } |
| 4692 | } |
| 4693 | } |
| 4694 | } |
| 4695 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4696 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4697 | const uint32_t vregA) { |
| 4698 | // Primitive assignability rules are weaker than regular assignability rules. |
| 4699 | bool instruction_compatible; |
| 4700 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4701 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4702 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 4703 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4704 | value_compatible = value_type.IsIntegralTypes(); |
| 4705 | } else if (target_type.IsFloat()) { |
| 4706 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 4707 | value_compatible = value_type.IsFloatTypes(); |
| 4708 | } else if (target_type.IsLong()) { |
| 4709 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4710 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4711 | // as target_type depends on the resolved type of the field. |
| 4712 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4713 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4714 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 4715 | } else { |
| 4716 | value_compatible = false; |
| 4717 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4718 | } else if (target_type.IsDouble()) { |
| 4719 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4720 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 4721 | // as target_type depends on the resolved type of the field. |
| 4722 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4723 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 4724 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 4725 | } else { |
| 4726 | value_compatible = false; |
| 4727 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4728 | } else { |
| 4729 | instruction_compatible = false; // reference with primitive store |
| 4730 | value_compatible = false; // unused |
| 4731 | } |
| 4732 | if (!instruction_compatible) { |
| 4733 | // This is a global failure rather than a class change failure as the instructions and |
| 4734 | // the descriptors for the type should have been consistent within the same file at |
| 4735 | // compile time. |
| 4736 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4737 | << "' but expected type '" << target_type << "'"; |
| 4738 | return; |
| 4739 | } |
| 4740 | if (!value_compatible) { |
| 4741 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4742 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 4743 | return; |
| 4744 | } |
| 4745 | } |
| 4746 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4747 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4748 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4749 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4750 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4751 | 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] | 4752 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4753 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4754 | if (array_type.IsZero()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 4755 | // Null array type; this code path will fail at runtime. |
| 4756 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 4757 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 4758 | // and fits multiple register types. |
| 4759 | const RegType* modified_reg_type = &insn_type; |
| 4760 | if ((modified_reg_type == ®_types_.Integer()) || |
| 4761 | (modified_reg_type == ®_types_.LongLo())) { |
| 4762 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 4763 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 4764 | if (modified_reg_type == ®_types_.Integer()) { |
| 4765 | if (&value_type == ®_types_.Float()) { |
| 4766 | modified_reg_type = &value_type; |
| 4767 | } |
| 4768 | } else { |
| 4769 | if (&value_type == ®_types_.DoubleLo()) { |
| 4770 | modified_reg_type = &value_type; |
| 4771 | } |
| 4772 | } |
| 4773 | } |
| 4774 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 4775 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4776 | 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] | 4777 | } else if (array_type.IsUnresolvedMergedReference()) { |
Andreas Gampe | bb18a03 | 2016-03-22 20:34:25 -0700 | [diff] [blame] | 4778 | // Unresolved array types must be reference array types. |
| 4779 | if (is_primitive) { |
| 4780 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 4781 | << "' but unresolved type '" << array_type << "'"; |
| 4782 | } else { |
| 4783 | Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type |
| 4784 | << " because of missing class"; |
| 4785 | } |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 4786 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4787 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4788 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4789 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 4790 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4791 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4792 | if (!component_type.IsReferenceTypes()) { |
| 4793 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 4794 | << " source for aput-object"; |
| 4795 | } else { |
| 4796 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 4797 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 4798 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4799 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4800 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | } |
| 4805 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4806 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4807 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4808 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4809 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4810 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4811 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 4812 | field_idx, dex_file_->GetFieldName(field_id), |
| 4813 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4814 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4815 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4816 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4817 | 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] | 4818 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4819 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4820 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_); |
| 4821 | |
| 4822 | // Record result of the field resolution attempt. |
| 4823 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4824 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4825 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4826 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4827 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4828 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4829 | DCHECK(self_->IsExceptionPending()); |
| 4830 | self_->ClearException(); |
| 4831 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4832 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4833 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4834 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField() |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4835 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4836 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4837 | } else if (!field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4838 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4839 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4840 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4841 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4844 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4845 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4846 | // Check access to class. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4847 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4848 | if (klass_type.IsConflict()) { |
| 4849 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4850 | field_idx, dex_file_->GetFieldName(field_id), |
| 4851 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4852 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4853 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4854 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4855 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4856 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4857 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4858 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, class_loader_); |
| 4859 | |
| 4860 | // Record result of the field resolution attempt. |
| 4861 | VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field); |
| 4862 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4863 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4864 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4865 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4866 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4867 | DCHECK(self_->IsExceptionPending()); |
| 4868 | self_->ClearException(); |
| 4869 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4870 | } else if (obj_type.IsZero()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4871 | // Cannot infer and check type, however, access will cause null pointer exception. |
| 4872 | // Fall through into a few last soft failure checks below. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4873 | } else if (!obj_type.IsReferenceTypes()) { |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4874 | // Trying to read a field from something that isn't a reference. |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4875 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4876 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4877 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4878 | } else { |
David Brazdil | 0d638bb | 2016-07-27 15:29:25 +0100 | [diff] [blame] | 4879 | std::string temp; |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4880 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4881 | const RegType& field_klass = |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4882 | FromClass(klass->GetDescriptor(&temp), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 4883 | klass.Ptr(), |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4884 | klass->CannotBeAssignedFromOtherTypes()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4885 | if (obj_type.IsUninitializedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4886 | // Field accesses through uninitialized references are only allowable for constructors where |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4887 | // the field is declared in this class. |
| 4888 | // Note: this IsConstructor check is technically redundant, as UninitializedThis should only |
| 4889 | // appear in constructors. |
| 4890 | if (!obj_type.IsUninitializedThisReference() || |
| 4891 | !IsConstructor() || |
| 4892 | !field_klass.Equals(GetDeclaringClass())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4893 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField() |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4894 | << " of a not fully initialized object within the context" |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4895 | << " of " << dex_file_->PrettyMethod(dex_method_idx_); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4896 | return nullptr; |
| 4897 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 4898 | } else if (!field_klass.IsAssignableFrom(obj_type, this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4899 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4900 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4901 | // 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] | 4902 | VerifyError type; |
| 4903 | bool is_aot = Runtime::Current()->IsAotCompiler(); |
| 4904 | if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) { |
| 4905 | // Compiler & unresolved types involved, retry at runtime. |
| 4906 | type = VerifyError::VERIFY_ERROR_NO_CLASS; |
| 4907 | } else { |
Andreas Gampe | 8f4ade0 | 2016-04-15 10:09:16 -0700 | [diff] [blame] | 4908 | // Classes known (resolved; and thus assignability check is precise), or we are at runtime |
| 4909 | // and still missing classes. This is a hard failure. |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4910 | type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD; |
| 4911 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4912 | Fail(type) << "cannot access instance field " << field->PrettyField() |
Andreas Gampe | 6659624 | 2016-04-14 10:55:04 -0700 | [diff] [blame] | 4913 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4914 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4915 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4916 | } |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4917 | |
| 4918 | // Few last soft failure checks. |
| 4919 | if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4920 | field->GetAccessFlags())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4921 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4922 | << " from " << GetDeclaringClass(); |
| 4923 | return nullptr; |
| 4924 | } else if (field->IsStatic()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4925 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 4926 | << " to not be static"; |
| 4927 | return nullptr; |
| 4928 | } |
| 4929 | |
| 4930 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4933 | template <MethodVerifier::FieldAccessType kAccType> |
| 4934 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4935 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4936 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4937 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4938 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4939 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4940 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4941 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
David Brazdil | 68b5c0b | 2016-01-19 14:25:29 +0000 | [diff] [blame] | 4942 | |
| 4943 | // One is not allowed to access fields on uninitialized references, except to write to |
| 4944 | // fields in the constructor (before calling another constructor). |
| 4945 | // GetInstanceField does an assignability check which will fail for uninitialized types. |
| 4946 | // We thus modify the type if the uninitialized reference is a "this" reference (this also |
| 4947 | // checks at the same time that we're verifying a constructor). |
| 4948 | bool should_adjust = (kAccType == FieldAccessType::kAccPut) && |
| 4949 | object_type.IsUninitializedThisReference(); |
| 4950 | const RegType& adjusted_type = should_adjust |
| 4951 | ? GetRegTypeCache()->FromUninitialized(object_type) |
| 4952 | : object_type; |
| 4953 | field = GetInstanceField(adjusted_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4954 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 4955 | return; |
| 4956 | } |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4957 | if (should_adjust) { |
| 4958 | if (field == nullptr) { |
| 4959 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior " |
| 4960 | << "to the superclass being initialized in " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4961 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4962 | } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4963 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4964 | << field->PrettyField() << " of a not fully initialized " |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4965 | << "object within the context of " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4966 | << dex_file_->PrettyMethod(dex_method_idx_); |
Alex Light | 4a2c8fc | 2016-02-12 11:01:54 -0800 | [diff] [blame] | 4967 | return; |
| 4968 | } |
| 4969 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4970 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4971 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4972 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4973 | if (kAccType == FieldAccessType::kAccPut) { |
| 4974 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4975 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4976 | << " from other class " << GetDeclaringClass(); |
Aart Bik | c2bc265 | 2016-05-23 14:58:49 -0700 | [diff] [blame] | 4977 | // Keep hunting for possible hard fails. |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4978 | } |
| 4979 | } |
| 4980 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4981 | ObjPtr<mirror::Class> field_type_class = |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4982 | can_load_classes_ ? field->GetType<true>() : field->GetType<false>(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4983 | if (field_type_class != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 4984 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 4985 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4986 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4987 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4988 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4989 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4990 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4991 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4992 | if (field_type == nullptr) { |
| 4993 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4994 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4995 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4996 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4997 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4998 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4999 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5000 | "Unexpected third access type"); |
| 5001 | if (kAccType == FieldAccessType::kAccPut) { |
| 5002 | // sput or iput. |
| 5003 | if (is_primitive) { |
| 5004 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5005 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5006 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5007 | // If the field type is not a reference, this is a global failure rather than |
| 5008 | // a class change failure as the instructions and the descriptors for the type |
| 5009 | // should have been consistent within the same file at compile time. |
| 5010 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5011 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5012 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5013 | << " to be compatible with type '" << insn_type |
| 5014 | << "' but found type '" << *field_type |
| 5015 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5016 | return; |
| 5017 | } |
| 5018 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5019 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5020 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5021 | // sget or iget. |
| 5022 | if (is_primitive) { |
| 5023 | if (field_type->Equals(insn_type) || |
| 5024 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 5025 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 5026 | // expected that read is of the correct primitive type or that int reads are reading |
| 5027 | // floats or long reads are reading doubles |
| 5028 | } else { |
| 5029 | // This is a global failure rather than a class change failure as the instructions and |
| 5030 | // the descriptors for the type should have been consistent within the same file at |
| 5031 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5032 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5033 | << " to be of type '" << insn_type |
| 5034 | << "' but found type '" << *field_type << "' in get"; |
| 5035 | return; |
| 5036 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 5037 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5038 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5039 | // If the field type is not a reference, this is a global failure rather than |
| 5040 | // a class change failure as the instructions and the descriptors for the type |
| 5041 | // should have been consistent within the same file at compile time. |
| 5042 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 5043 | : VERIFY_ERROR_BAD_CLASS_HARD; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5044 | Fail(error) << "expected field " << ArtField::PrettyField(field) |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 5045 | << " to be compatible with type '" << insn_type |
| 5046 | << "' but found type '" << *field_type |
| 5047 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5048 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5049 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 5050 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5051 | return; |
| 5052 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5053 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5054 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5055 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5056 | } else { |
| 5057 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 5058 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5059 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5060 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5061 | } |
| 5062 | } |
| 5063 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5064 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5065 | RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5066 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5067 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 5068 | if (!object_type.HasClass()) { |
| 5069 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 5070 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5071 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5072 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5073 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5074 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5075 | if (f == nullptr) { |
| 5076 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5077 | << "' from '" << mirror::Class::PrettyDescriptor(object_type.GetClass()) << "'"; |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 5078 | } |
| 5079 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5082 | template <MethodVerifier::FieldAccessType kAccType> |
| 5083 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 5084 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 5085 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5086 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5087 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5088 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5089 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 5090 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5091 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5092 | |
| 5093 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 5094 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5095 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5096 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField() |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 5097 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5098 | return; |
| 5099 | } |
| 5100 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5101 | |
| 5102 | // Get the field type. |
| 5103 | const RegType* field_type; |
| 5104 | { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 5105 | ObjPtr<mirror::Class> field_type_class = can_load_classes_ ? field->GetType<true>() : |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5106 | field->GetType<false>(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5107 | |
| 5108 | if (field_type_class != nullptr) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5109 | field_type = &FromClass(field->GetTypeDescriptor(), |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 5110 | field_type_class.Ptr(), |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5111 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5112 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5113 | Thread* self = Thread::Current(); |
| 5114 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 5115 | self->ClearException(); |
| 5116 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5117 | field->GetTypeDescriptor(), |
| 5118 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5119 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5120 | if (field_type == nullptr) { |
| 5121 | 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] | 5122 | return; |
| 5123 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | const uint32_t vregA = inst->VRegA_22c(); |
| 5127 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 5128 | "Unexpected third access type"); |
| 5129 | if (kAccType == FieldAccessType::kAccPut) { |
| 5130 | if (is_primitive) { |
| 5131 | // Primitive field assignability rules are weaker than regular assignability rules |
| 5132 | bool instruction_compatible; |
| 5133 | bool value_compatible; |
| 5134 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 5135 | if (field_type->IsIntegralTypes()) { |
| 5136 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 5137 | value_compatible = value_type.IsIntegralTypes(); |
| 5138 | } else if (field_type->IsFloat()) { |
| 5139 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 5140 | value_compatible = value_type.IsFloatTypes(); |
| 5141 | } else if (field_type->IsLong()) { |
| 5142 | instruction_compatible = insn_type.IsLong(); |
| 5143 | value_compatible = value_type.IsLongTypes(); |
| 5144 | } else if (field_type->IsDouble()) { |
| 5145 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 5146 | value_compatible = value_type.IsDoubleTypes(); |
| 5147 | } else { |
| 5148 | instruction_compatible = false; // reference field with primitive store |
| 5149 | value_compatible = false; // unused |
| 5150 | } |
| 5151 | if (!instruction_compatible) { |
| 5152 | // This is a global failure rather than a class change failure as the instructions and |
| 5153 | // the descriptors for the type should have been consistent within the same file at |
| 5154 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5155 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5156 | << " to be of type '" << insn_type |
| 5157 | << "' but found type '" << *field_type |
| 5158 | << "' in put"; |
| 5159 | return; |
| 5160 | } |
| 5161 | if (!value_compatible) { |
| 5162 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 5163 | << " of type " << value_type |
| 5164 | << " but expected " << *field_type |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5165 | << " for store to " << ArtField::PrettyField(field) << " in put"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5166 | return; |
| 5167 | } |
| 5168 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5169 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5170 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5171 | << " to be compatible with type '" << insn_type |
| 5172 | << "' but found type '" << *field_type |
| 5173 | << "' in put-object"; |
| 5174 | return; |
| 5175 | } |
| 5176 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 5177 | } |
| 5178 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 5179 | if (is_primitive) { |
| 5180 | if (field_type->Equals(insn_type) || |
| 5181 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 5182 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 5183 | // expected that read is of the correct primitive type or that int reads are reading |
| 5184 | // floats or long reads are reading doubles |
| 5185 | } else { |
| 5186 | // This is a global failure rather than a class change failure as the instructions and |
| 5187 | // the descriptors for the type should have been consistent within the same file at |
| 5188 | // compile time |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5189 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5190 | << " to be of type '" << insn_type |
| 5191 | << "' but found type '" << *field_type << "' in Get"; |
| 5192 | return; |
| 5193 | } |
| 5194 | } else { |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 5195 | if (!insn_type.IsAssignableFrom(*field_type, this)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5196 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << ArtField::PrettyField(field) |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5197 | << " to be compatible with type '" << insn_type |
| 5198 | << "' but found type '" << *field_type |
| 5199 | << "' in get-object"; |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5200 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict()); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5201 | return; |
| 5202 | } |
| 5203 | } |
| 5204 | if (!field_type->IsLowHalf()) { |
Andreas Gampe | ad238ce | 2015-08-24 21:13:08 -0700 | [diff] [blame] | 5205 | work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5206 | } else { |
| 5207 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5208 | } |
| 5209 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 5210 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 5211 | } |
| 5212 | } |
| 5213 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5214 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5215 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 5216 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5217 | return false; |
| 5218 | } |
| 5219 | return true; |
| 5220 | } |
| 5221 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 5222 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 5223 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 5224 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 5225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 5226 | return false; |
| 5227 | } |
| 5228 | return true; |
| 5229 | } |
| 5230 | |
| 5231 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 5232 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 5233 | } |
| 5234 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5235 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 5236 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5237 | bool changed = true; |
| 5238 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5239 | if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5240 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5241 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 5242 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 5243 | * 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] | 5244 | */ |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5245 | target_line->CopyFromLine(merge_line); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5246 | if (GetInstructionFlags(next_insn).IsReturn()) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 5247 | // Verify that the monitor stack is empty on return. |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5248 | merge_line->VerifyMonitorStackEmpty(this); |
| 5249 | |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5250 | // For returns we only care about the operand to the return, all other registers are dead. |
| 5251 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 5252 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
Andreas Gampe | a727e37 | 2015-08-25 09:22:37 -0700 | [diff] [blame] | 5253 | AdjustReturnLine(this, ret_inst, target_line); |
Aart Bik | 3188364 | 2016-06-06 15:02:44 -0700 | [diff] [blame] | 5254 | // Directly bail if a hard failure was found. |
Aart Bik | b052632 | 2016-06-01 14:06:00 -0700 | [diff] [blame] | 5255 | if (have_pending_hard_failure_) { |
| 5256 | return false; |
| 5257 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 5258 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5259 | } else { |
Mathieu Chartier | 361e04a | 2016-02-16 14:06:35 -0800 | [diff] [blame] | 5260 | RegisterLineArenaUniquePtr copy; |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 5261 | if (kDebugVerify) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5262 | copy.reset(RegisterLine::Create(target_line->NumRegs(), this)); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5263 | copy->CopyFromLine(target_line); |
| 5264 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5265 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5266 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5267 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5268 | } |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 5269 | if (kDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 5270 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 5271 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5272 | << copy->Dump(this) << " MERGE\n" |
| 5273 | << merge_line->Dump(this) << " ==\n" |
| 5274 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5275 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 5276 | if (update_merge_line && changed) { |
| 5277 | merge_line->CopyFromLine(target_line); |
| 5278 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5279 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5280 | if (changed) { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5281 | GetInstructionFlags(next_insn).SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 5282 | } |
| 5283 | return true; |
| 5284 | } |
| 5285 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5286 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5287 | return &GetInstructionFlags(work_insn_idx_); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5290 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5291 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5292 | if (mirror_method_ != nullptr) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5293 | mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5294 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5295 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
| 5296 | return_type_class, |
| 5297 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5298 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5299 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 5300 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5301 | } |
| 5302 | } |
| 5303 | if (return_type_ == nullptr) { |
| 5304 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 5305 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5306 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5307 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5308 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5309 | } |
| 5310 | } |
| 5311 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5312 | } |
| 5313 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5314 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5315 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5316 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 5317 | const char* descriptor |
| 5318 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5319 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5320 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Mathieu Chartier | de40d47 | 2015-10-15 17:47:48 -0700 | [diff] [blame] | 5321 | declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5322 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 5323 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5324 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5325 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 5326 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 5327 | } |
| 5328 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5329 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 5330 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 5331 | 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] | 5332 | std::vector<int32_t> result; |
| 5333 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5334 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5335 | if (type.IsConstant()) { |
| 5336 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5337 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5338 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5339 | } else if (type.IsConstantLo()) { |
| 5340 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5341 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5342 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5343 | } else if (type.IsConstantHi()) { |
| 5344 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5345 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 5346 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5347 | } else if (type.IsIntegralTypes()) { |
| 5348 | result.push_back(kIntVReg); |
| 5349 | result.push_back(0); |
| 5350 | } else if (type.IsFloat()) { |
| 5351 | result.push_back(kFloatVReg); |
| 5352 | result.push_back(0); |
| 5353 | } else if (type.IsLong()) { |
| 5354 | result.push_back(kLongLoVReg); |
| 5355 | result.push_back(0); |
| 5356 | result.push_back(kLongHiVReg); |
| 5357 | result.push_back(0); |
| 5358 | ++i; |
| 5359 | } else if (type.IsDouble()) { |
| 5360 | result.push_back(kDoubleLoVReg); |
| 5361 | result.push_back(0); |
| 5362 | result.push_back(kDoubleHiVReg); |
| 5363 | result.push_back(0); |
| 5364 | ++i; |
| 5365 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 5366 | result.push_back(kUndefined); |
| 5367 | result.push_back(0); |
| 5368 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 5369 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 5370 | result.push_back(kReferenceVReg); |
| 5371 | result.push_back(0); |
| 5372 | } |
| 5373 | } |
| 5374 | return result; |
| 5375 | } |
| 5376 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 5377 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 5378 | if (precise) { |
| 5379 | // Precise constant type. |
| 5380 | return reg_types_.FromCat1Const(value, true); |
| 5381 | } else { |
| 5382 | // Imprecise constant type. |
| 5383 | if (value < -32768) { |
| 5384 | return reg_types_.IntConstant(); |
| 5385 | } else if (value < -128) { |
| 5386 | return reg_types_.ShortConstant(); |
| 5387 | } else if (value < 0) { |
| 5388 | return reg_types_.ByteConstant(); |
| 5389 | } else if (value == 0) { |
| 5390 | return reg_types_.Zero(); |
| 5391 | } else if (value == 1) { |
| 5392 | return reg_types_.One(); |
| 5393 | } else if (value < 128) { |
| 5394 | return reg_types_.PosByteConstant(); |
| 5395 | } else if (value < 32768) { |
| 5396 | return reg_types_.PosShortConstant(); |
| 5397 | } else if (value < 65536) { |
| 5398 | return reg_types_.CharConstant(); |
| 5399 | } else { |
| 5400 | return reg_types_.IntConstant(); |
| 5401 | } |
| 5402 | } |
| 5403 | } |
| 5404 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5405 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5406 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5407 | } |
| 5408 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 5409 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 5410 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 5411 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 5412 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5413 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 5414 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 5415 | } |
| 5416 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 5417 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 5418 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5419 | } |
| 5420 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 5421 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 5422 | mirror::Class* klass, |
| 5423 | bool precise) { |
| 5424 | DCHECK(klass != nullptr); |
| 5425 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 5426 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 5427 | << "non-instantiable klass " << descriptor; |
| 5428 | precise = false; |
| 5429 | } |
| 5430 | return reg_types_.FromClass(descriptor, klass, precise); |
| 5431 | } |
| 5432 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 5433 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5434 | } // namespace art |