Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 17 | #include "method_verifier-inl.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 21 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_method-inl.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 23 | #include "base/logging.h" |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 24 | #include "base/mutex-inl.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 26 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 27 | #include "compiler_callbacks.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "dex_file-inl.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 29 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | #include "dex_instruction_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 31 | #include "dex_instruction_visitor.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 33 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 34 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 35 | #include "leb128.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "mirror/class.h" |
| 37 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 38 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "mirror/object-inl.h" |
| 40 | #include "mirror/object_array-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 41 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 42 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 43 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 44 | #include "scoped_thread_state_change.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 45 | #include "utils.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | #include "handle_scope-inl.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 47 | #include "verifier/dex_gc_map.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 48 | |
| 49 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 50 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 51 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 52 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 53 | static constexpr bool gDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 54 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 55 | |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 56 | // On VLOG(verifier), should we dump the whole state when we run into a hard failure? |
| 57 | static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true; |
| 58 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 59 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 60 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 61 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 62 | DCHECK_GT(insns_size, 0U); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 63 | register_lines_.reset(new RegisterLine*[insns_size]()); |
| 64 | size_ = insns_size; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 65 | for (uint32_t i = 0; i < insns_size; i++) { |
| 66 | bool interesting = false; |
| 67 | switch (mode) { |
| 68 | case kTrackRegsAll: |
| 69 | interesting = flags[i].IsOpcode(); |
| 70 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 71 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 72 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 73 | break; |
| 74 | case kTrackRegsBranches: |
| 75 | interesting = flags[i].IsBranchTarget(); |
| 76 | break; |
| 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | if (interesting) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 81 | register_lines_[i] = RegisterLine::Create(registers_size, verifier); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | PcToRegisterLineTable::~PcToRegisterLineTable() { |
| 87 | for (size_t i = 0; i < size_; i++) { |
| 88 | delete register_lines_[i]; |
| 89 | if (kIsDebugBuild) { |
| 90 | register_lines_[i] = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 95 | // Note: returns true on failure. |
| 96 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 97 | const char* error_msg, uint32_t work_insn_idx) { |
| 98 | if (kIsDebugBuild) { |
| 99 | // In a debug build, abort if the error condition is wrong. |
| 100 | DCHECK(condition) << error_msg << work_insn_idx; |
| 101 | } else { |
| 102 | // In a non-debug build, just fail the class. |
| 103 | if (!condition) { |
| 104 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 112 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 113 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 114 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 115 | reg_line->CheckConstructorReturn(verifier); |
| 116 | } |
| 117 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 118 | } |
| 119 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 121 | ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) { |
| 122 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 123 | mirror::Class* klass = method->GetDeclaringClass(); |
| 124 | auto h_dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 125 | auto h_class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 126 | return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache, |
| 127 | h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | method->GetAccessFlags(), allow_soft_failures, false); |
| 129 | } |
| 130 | |
| 131 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 132 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 133 | mirror::Class* klass, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 134 | bool allow_soft_failures, |
| 135 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 136 | if (klass->IsVerified()) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 137 | return kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 138 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 139 | bool early_failure = false; |
| 140 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 141 | const DexFile& dex_file = klass->GetDexFile(); |
| 142 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 143 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 144 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 145 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 146 | early_failure = true; |
| 147 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 148 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 149 | early_failure = true; |
| 150 | failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 151 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 152 | early_failure = true; |
| 153 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 154 | } |
| 155 | if (early_failure) { |
| 156 | *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 157 | if (Runtime::Current()->IsAotCompiler()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 158 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 159 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 160 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 161 | return kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 162 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 163 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 164 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 165 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 166 | return VerifyClass( |
| 167 | self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error); |
Shih-wei Liao | 371814f | 2011-10-27 16:52:10 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 170 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 171 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 172 | Handle<mirror::DexCache> dex_cache, |
| 173 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 174 | const DexFile::ClassDef* class_def, |
| 175 | bool allow_soft_failures, |
| 176 | std::string* error) { |
| 177 | DCHECK(class_def != nullptr); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 178 | |
| 179 | // A class must not be abstract and final. |
| 180 | if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
| 181 | *error = "Verifier rejected class "; |
| 182 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 183 | *error += ": class is abstract and final."; |
| 184 | return kHardFailure; |
| 185 | } |
| 186 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 187 | const uint8_t* class_data = dex_file->GetClassData(*class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 188 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 189 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 190 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 191 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 192 | ClassDataItemIterator it(*dex_file, class_data); |
| 193 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 194 | it.Next(); |
| 195 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 196 | size_t error_count = 0; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 197 | bool hard_fail = false; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 198 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 199 | int64_t previous_direct_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 200 | while (it.HasNextDirectMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 201 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 202 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 203 | if (method_idx == previous_direct_method_idx) { |
| 204 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 205 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 206 | it.Next(); |
| 207 | continue; |
| 208 | } |
| 209 | previous_direct_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 210 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 211 | ArtMethod* method = linker->ResolveMethod( |
| 212 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 213 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 214 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 215 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 216 | self->ClearException(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 217 | } else { |
| 218 | DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 219 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 220 | StackHandleScope<1> hs(self); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 221 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 222 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 223 | dex_file, |
| 224 | dex_cache, |
| 225 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 226 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 227 | it.GetMethodCodeItem(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 228 | method, it.GetMethodAccessFlags(), allow_soft_failures, false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 229 | if (result != kNoFailure) { |
| 230 | if (result == kHardFailure) { |
| 231 | hard_fail = true; |
| 232 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 233 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 234 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 235 | *error = "Verifier rejected class "; |
| 236 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 237 | *error += " due to bad method "; |
| 238 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 239 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 240 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 241 | } |
| 242 | it.Next(); |
| 243 | } |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 244 | int64_t previous_virtual_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 245 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 246 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 247 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 248 | if (method_idx == previous_virtual_method_idx) { |
| 249 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 250 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 251 | it.Next(); |
| 252 | continue; |
| 253 | } |
| 254 | previous_virtual_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 255 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 256 | ArtMethod* method = linker->ResolveMethod( |
| 257 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 258 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 259 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 260 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 261 | self->ClearException(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 262 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 263 | StackHandleScope<1> hs(self); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 264 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 265 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 266 | dex_file, |
| 267 | dex_cache, |
| 268 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 269 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 270 | it.GetMethodCodeItem(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 271 | method, it.GetMethodAccessFlags(), allow_soft_failures, false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 272 | if (result != kNoFailure) { |
| 273 | if (result == kHardFailure) { |
| 274 | hard_fail = true; |
| 275 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 276 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 277 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 278 | *error = "Verifier rejected class "; |
| 279 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 280 | *error += " due to bad method "; |
| 281 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 282 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 283 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 284 | } |
| 285 | it.Next(); |
| 286 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 287 | if (error_count == 0) { |
| 288 | return kNoFailure; |
| 289 | } else { |
| 290 | return hard_fail ? kHardFailure : kSoftFailure; |
| 291 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 294 | static bool IsLargeMethod(const DexFile::CodeItem* const code_item) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 295 | if (code_item == nullptr) { |
| 296 | return false; |
| 297 | } |
| 298 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 299 | uint16_t registers_size = code_item->registers_size_; |
| 300 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 301 | |
| 302 | return registers_size * insns_size > 4*1024*1024; |
| 303 | } |
| 304 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 305 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 306 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 307 | Handle<mirror::DexCache> dex_cache, |
| 308 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 309 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 310 | const DexFile::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 311 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 312 | uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 313 | bool allow_soft_failures, |
| 314 | bool need_precise_constants) { |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 315 | MethodVerifier::FailureKind result = kNoFailure; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 316 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 317 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 318 | MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item, |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 319 | method_idx, method, method_access_flags, true, allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 320 | need_precise_constants, true); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 321 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 322 | // Verification completed, however failures may be pending that didn't cause the verification |
| 323 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 324 | CHECK(!verifier.have_pending_hard_failure_); |
| 325 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 326 | if (VLOG_IS_ON(verifier)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 327 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 328 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 329 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 330 | result = kSoftFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 331 | } |
| 332 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 333 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 334 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 335 | |
| 336 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 337 | // Failed due to being forced into interpreter. This is ok because |
| 338 | // we just want to skip verification. |
| 339 | result = kSoftFailure; |
| 340 | } else { |
| 341 | CHECK(verifier.have_pending_hard_failure_); |
| 342 | verifier.DumpFailures(LOG(INFO) << "Verification error in " |
| 343 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 344 | result = kHardFailure; |
| 345 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 346 | if (gDebugVerify) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 347 | std::cout << "\n" << verifier.info_messages_.str(); |
| 348 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 349 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 350 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 351 | if (kTimeVerifyMethod) { |
| 352 | uint64_t duration_ns = NanoTime() - start_ns; |
| 353 | if (duration_ns > MsToNs(100)) { |
| 354 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 355 | << " took " << PrettyDuration(duration_ns) |
| 356 | << (IsLargeMethod(code_item) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 357 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 358 | } |
| 359 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 362 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 363 | VariableIndentationOutputStream* vios, |
| 364 | uint32_t dex_method_idx, |
| 365 | const DexFile* dex_file, |
| 366 | Handle<mirror::DexCache> dex_cache, |
| 367 | Handle<mirror::ClassLoader> class_loader, |
| 368 | const DexFile::ClassDef* class_def, |
| 369 | const DexFile::CodeItem* code_item, |
| 370 | ArtMethod* method, |
| 371 | uint32_t method_access_flags) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 372 | MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader, |
| 373 | class_def, code_item, dex_method_idx, method, |
| 374 | method_access_flags, true, true, true, true); |
| 375 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 376 | verifier->DumpFailures(vios->Stream()); |
| 377 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 378 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 379 | // and querying any info is dangerous/can abort. |
| 380 | if (verifier->have_pending_hard_failure_) { |
| 381 | delete verifier; |
| 382 | return nullptr; |
| 383 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 384 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 385 | return verifier; |
| 386 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 389 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 390 | const DexFile* dex_file, Handle<mirror::DexCache> dex_cache, |
| 391 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 392 | const DexFile::ClassDef* class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 393 | const DexFile::CodeItem* code_item, uint32_t dex_method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 394 | ArtMethod* method, uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 395 | bool can_load_classes, bool allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 396 | bool need_precise_constants, bool verify_to_dump, |
| 397 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 398 | : self_(self), |
| 399 | reg_types_(can_load_classes), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 400 | work_insn_idx_(DexFile::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 401 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 402 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 403 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 404 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 405 | dex_file_(dex_file), |
| 406 | dex_cache_(dex_cache), |
| 407 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 408 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 409 | code_item_(code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 410 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 411 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 412 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 413 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 414 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 415 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 416 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 417 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 418 | monitor_enter_count_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 419 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 420 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 421 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 422 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 423 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 424 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 425 | allow_thread_suspension_(allow_thread_suspension), |
| 426 | link_(nullptr) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 427 | self->PushVerifier(this); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 428 | DCHECK(class_def != nullptr); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 431 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 432 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 433 | STLDeleteElements(&failure_messages_); |
| 434 | } |
| 435 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 436 | void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 437 | std::vector<uint32_t>* monitor_enter_dex_pcs) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 438 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 439 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 440 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 441 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 442 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 443 | false, true, false, false); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 444 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 445 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 446 | verifier.FindLocksAtDexPc(); |
| 447 | } |
| 448 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 449 | static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) { |
| 450 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 451 | |
| 452 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 453 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 454 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | dex_pc += inst->SizeInCodeUnits(); |
| 459 | inst = inst->Next(); |
| 460 | } |
| 461 | |
| 462 | return false; |
| 463 | } |
| 464 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 465 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 466 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| 467 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 468 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 469 | // Quick check whether there are any monitor_enter instructions at all. |
| 470 | if (!HasMonitorEnterInstructions(code_item_)) { |
| 471 | return; |
| 472 | } |
| 473 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 474 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 475 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 476 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 477 | // got what we wanted. |
| 478 | Verify(); |
| 479 | } |
| 480 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 481 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 482 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 483 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 484 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 485 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 486 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
| 487 | true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 488 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 491 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 492 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 493 | |
| 494 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 495 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 496 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 497 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 498 | bool success = Verify(); |
| 499 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 500 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 501 | } |
| 502 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 503 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 504 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 505 | } |
| 506 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 507 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 510 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 511 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 512 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 513 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 514 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 515 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
| 516 | true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 517 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 520 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 521 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 522 | |
| 523 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 524 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 525 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 526 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 527 | bool success = Verify(); |
| 528 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 529 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 530 | } |
| 531 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 532 | if (register_line == nullptr) { |
| 533 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 534 | } |
| 535 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 536 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 537 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 540 | SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 541 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 542 | StackHandleScope<2> hs(self); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 543 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 544 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 545 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 546 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 547 | true, true, false, true); |
| 548 | return verifier.FindStringInitMap(); |
| 549 | } |
| 550 | |
| 551 | SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() { |
| 552 | Verify(); |
| 553 | return GetStringInitPcRegMap(); |
| 554 | } |
| 555 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 556 | bool MethodVerifier::Verify() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 557 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 558 | if (code_item_ == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 559 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 560 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 561 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 562 | } else { |
| 563 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 564 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 565 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 566 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 567 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 568 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 569 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 570 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 571 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 572 | // Allocate and initialize an array to hold instruction data. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 573 | insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 574 | // Run through the instructions and see if the width checks out. |
| 575 | bool result = ComputeWidthsAndCountOps(); |
| 576 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 577 | result = result && ScanTryCatchBlocks(); |
| 578 | // Perform static instruction verification. |
| 579 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 580 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 581 | result = result && VerifyCodeFlow(); |
| 582 | // Compute information for compiler. |
| 583 | if (result && Runtime::Current()->IsCompiler()) { |
| 584 | result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this); |
| 585 | } |
| 586 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 589 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 590 | switch (error) { |
| 591 | case VERIFY_ERROR_NO_CLASS: |
| 592 | case VERIFY_ERROR_NO_FIELD: |
| 593 | case VERIFY_ERROR_NO_METHOD: |
| 594 | case VERIFY_ERROR_ACCESS_CLASS: |
| 595 | case VERIFY_ERROR_ACCESS_FIELD: |
| 596 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 597 | case VERIFY_ERROR_INSTANTIATION: |
| 598 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 599 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 600 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 601 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 602 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 603 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 604 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 605 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 606 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 607 | // to an interpreter. |
| 608 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 609 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 610 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 611 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 612 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 613 | |
| 614 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 615 | // try to merge garbage. |
| 616 | // 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] | 617 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 618 | // add a check. |
| 619 | if (work_insn_idx_ < DexFile::kDexNoIndex) { |
| 620 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 621 | const Instruction* inst = Instruction::At(insns); |
| 622 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 623 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 624 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 625 | saved_line_->CopyFromLine(work_line_.get()); |
| 626 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 627 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 628 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 629 | break; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 630 | // Indication that verification should be retried at runtime. |
| 631 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 632 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 633 | have_pending_hard_failure_ = true; |
| 634 | } |
| 635 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 636 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 637 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 638 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 639 | if (Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 640 | ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 641 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 642 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 643 | have_pending_hard_failure_ = true; |
Andreas Gampe | ebf850c | 2015-08-14 15:37:35 -0700 | [diff] [blame] | 644 | if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) { |
| 645 | ScopedObjectAccess soa(Thread::Current()); |
| 646 | std::ostringstream oss; |
| 647 | Dump(oss); |
| 648 | LOG(ERROR) << oss.str(); |
| 649 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 650 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 653 | failures_.push_back(error); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 654 | std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 655 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 656 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 657 | failure_messages_.push_back(failure_message); |
| 658 | return *failure_message; |
| 659 | } |
| 660 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 661 | std::ostream& MethodVerifier::LogVerifyInfo() { |
| 662 | return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) |
| 663 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 664 | } |
| 665 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 666 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 667 | size_t failure_num = failure_messages_.size(); |
| 668 | DCHECK_NE(failure_num, 0U); |
| 669 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 670 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 671 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 672 | delete last_fail_message; |
| 673 | } |
| 674 | |
| 675 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 676 | size_t failure_num = failure_messages_.size(); |
| 677 | DCHECK_NE(failure_num, 0U); |
| 678 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 679 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 682 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 683 | const uint16_t* insns = code_item_->insns_; |
| 684 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 685 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 686 | size_t new_instance_count = 0; |
| 687 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 688 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 689 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 690 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 691 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 692 | switch (opcode) { |
| 693 | case Instruction::APUT_OBJECT: |
| 694 | case Instruction::CHECK_CAST: |
| 695 | has_check_casts_ = true; |
| 696 | break; |
| 697 | case Instruction::INVOKE_VIRTUAL: |
| 698 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 699 | case Instruction::INVOKE_INTERFACE: |
| 700 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 701 | has_virtual_or_interface_invokes_ = true; |
| 702 | break; |
| 703 | case Instruction::MONITOR_ENTER: |
| 704 | monitor_enter_count++; |
| 705 | break; |
| 706 | case Instruction::NEW_INSTANCE: |
| 707 | new_instance_count++; |
| 708 | break; |
| 709 | default: |
| 710 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 711 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 712 | size_t inst_size = inst->SizeInCodeUnits(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 713 | insn_flags_[dex_pc].SetIsOpcode(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 714 | dex_pc += inst_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 715 | inst = inst->RelativeAt(inst_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 718 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 719 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 720 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 721 | return false; |
| 722 | } |
| 723 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 724 | new_instance_count_ = new_instance_count; |
| 725 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 729 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 730 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 731 | if (tries_size == 0) { |
| 732 | return true; |
| 733 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 734 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 735 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 736 | |
| 737 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 738 | const DexFile::TryItem* try_item = &tries[idx]; |
| 739 | uint32_t start = try_item->start_addr_; |
| 740 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 741 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 742 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 743 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 744 | return false; |
| 745 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 746 | if (!insn_flags_[start].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 747 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 748 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 749 | return false; |
| 750 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 751 | uint32_t dex_pc = start; |
| 752 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 753 | while (dex_pc < end) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 754 | insn_flags_[dex_pc].SetInTry(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 755 | size_t insn_size = inst->SizeInCodeUnits(); |
| 756 | dex_pc += insn_size; |
| 757 | inst = inst->RelativeAt(insn_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 758 | } |
| 759 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 760 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 761 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 762 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 763 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 764 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 765 | CatchHandlerIterator iterator(handlers_ptr); |
| 766 | for (; iterator.HasNext(); iterator.Next()) { |
| 767 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 768 | if (!insn_flags_[dex_pc].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 769 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 770 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 771 | return false; |
| 772 | } |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 773 | if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) { |
| 774 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 775 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 776 | return false; |
| 777 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 778 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 779 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 780 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 781 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 782 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 783 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 784 | dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 785 | if (exception_type == nullptr) { |
| 786 | DCHECK(self_->IsExceptionPending()); |
| 787 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 790 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 791 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 792 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 796 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 797 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 798 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 799 | /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 800 | insn_flags_[0].SetBranchTarget(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 801 | insn_flags_[0].SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 802 | |
| 803 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 804 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 805 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 806 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 810 | // All invoke points are marked as "Throw" points already. |
| 811 | // 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] | 812 | if (inst->IsBranch()) { |
| 813 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
| 814 | // The compiler also needs safepoints for fall-through to loop heads. |
| 815 | // Such a loop head must be a target of a branch. |
| 816 | int32_t offset = 0; |
| 817 | bool cond, self_ok; |
| 818 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 819 | DCHECK(target_ok); |
| 820 | insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint(); |
| 821 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 822 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 823 | } else if (inst->IsReturn()) { |
| 824 | insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 825 | } |
| 826 | dex_pc += inst->SizeInCodeUnits(); |
| 827 | inst = inst->Next(); |
| 828 | } |
| 829 | return true; |
| 830 | } |
| 831 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 832 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 833 | if (UNLIKELY(inst->IsExperimental())) { |
| 834 | // Experimental instructions don't yet have verifier support implementation. |
| 835 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 836 | // with a virtual register that was created by an experimental instruction, |
| 837 | // the data flow analysis will fail. |
| 838 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 839 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 840 | have_pending_experimental_failure_ = true; |
| 841 | return false; |
| 842 | } |
| 843 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 844 | bool result = true; |
| 845 | switch (inst->GetVerifyTypeArgumentA()) { |
| 846 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 847 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 848 | break; |
| 849 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 850 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | switch (inst->GetVerifyTypeArgumentB()) { |
| 854 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 855 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 856 | break; |
| 857 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 858 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 859 | break; |
| 860 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 861 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 862 | break; |
| 863 | case Instruction::kVerifyRegBNewInstance: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 864 | result = result && CheckNewInstance(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 865 | break; |
| 866 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 867 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 868 | break; |
| 869 | case Instruction::kVerifyRegBType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 870 | result = result && CheckTypeIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 871 | break; |
| 872 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 873 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | switch (inst->GetVerifyTypeArgumentC()) { |
| 877 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 878 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 879 | break; |
| 880 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 881 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 882 | break; |
| 883 | case Instruction::kVerifyRegCNewArray: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 884 | result = result && CheckNewArray(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 885 | break; |
| 886 | case Instruction::kVerifyRegCType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 887 | result = result && CheckTypeIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 888 | break; |
| 889 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 890 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | switch (inst->GetVerifyExtraFlags()) { |
| 894 | case Instruction::kVerifyArrayData: |
| 895 | result = result && CheckArrayData(code_offset); |
| 896 | break; |
| 897 | case Instruction::kVerifyBranchTarget: |
| 898 | result = result && CheckBranchTarget(code_offset); |
| 899 | break; |
| 900 | case Instruction::kVerifySwitchTargets: |
| 901 | result = result && CheckSwitchTargets(code_offset); |
| 902 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 903 | case Instruction::kVerifyVarArgNonZero: |
| 904 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 905 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 906 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 907 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 908 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 909 | v_a > Instruction::kMaxVarArgRegs) { |
| 910 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 911 | "non-range invoke"; |
| 912 | return false; |
| 913 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 914 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 915 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 916 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 917 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 918 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 919 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 920 | case Instruction::kVerifyVarArgRangeNonZero: |
| 921 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 922 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 923 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 924 | inst->VRegA() <= 0) { |
| 925 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 926 | "range invoke"; |
| 927 | return false; |
| 928 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 929 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 930 | break; |
| 931 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 932 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 933 | result = false; |
| 934 | break; |
| 935 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 936 | if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 937 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 938 | result = false; |
| 939 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 940 | return result; |
| 941 | } |
| 942 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 943 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 944 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 945 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 946 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 947 | return false; |
| 948 | } |
| 949 | return true; |
| 950 | } |
| 951 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 952 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 953 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 954 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 955 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | return true; |
| 959 | } |
| 960 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 961 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 962 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 963 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 964 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 965 | return false; |
| 966 | } |
| 967 | return true; |
| 968 | } |
| 969 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 970 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 971 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 972 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 973 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | return true; |
| 977 | } |
| 978 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 979 | inline bool MethodVerifier::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 980 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 981 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 982 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 983 | return false; |
| 984 | } |
| 985 | // 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] | 986 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 987 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 988 | 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] | 989 | return false; |
| 990 | } |
| 991 | return true; |
| 992 | } |
| 993 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 994 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 995 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 996 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 997 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 998 | return false; |
| 999 | } |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1003 | inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1004 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1005 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1006 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1007 | return false; |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1012 | bool MethodVerifier::CheckNewArray(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1013 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1014 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 1015 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1019 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1020 | const char* cp = descriptor; |
| 1021 | while (*cp++ == '[') { |
| 1022 | bracket_count++; |
| 1023 | } |
| 1024 | if (bracket_count == 0) { |
| 1025 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1027 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1028 | return false; |
| 1029 | } else if (bracket_count > 255) { |
| 1030 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1031 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1032 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
| 1035 | return true; |
| 1036 | } |
| 1037 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1038 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1040 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1041 | const uint16_t* array_data; |
| 1042 | int32_t array_data_offset; |
| 1043 | |
| 1044 | DCHECK_LT(cur_offset, insn_count); |
| 1045 | /* make sure the start of the array data table is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 1046 | array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| 1047 | if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1048 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1049 | 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] | 1050 | << ", data offset " << array_data_offset |
| 1051 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1052 | return false; |
| 1053 | } |
| 1054 | /* offset to array data table is a relative branch-style offset */ |
| 1055 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1056 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1057 | if (!IsAligned<4>(array_data)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1058 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1059 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1060 | return false; |
| 1061 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1062 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1063 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
| 1064 | if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) { |
| 1065 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1066 | << ", data offset " << array_data_offset |
| 1067 | << " not correctly visited, probably bad padding."; |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1071 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1072 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1073 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1074 | /* make sure the end of the switch is in range */ |
| 1075 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1076 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1077 | << ", data offset " << array_data_offset << ", end " |
| 1078 | << cur_offset + array_data_offset + table_size |
| 1079 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1080 | return false; |
| 1081 | } |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1085 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1086 | int32_t offset; |
| 1087 | bool isConditional, selfOkay; |
| 1088 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1092 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1093 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1094 | return false; |
| 1095 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1096 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1097 | // to have identical "wrap-around" behavior, but it's unwise to depend on that. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1098 | if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1100 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1101 | return false; |
| 1102 | } |
| 1103 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1104 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1105 | if (abs_offset < 0 || |
| 1106 | (uint32_t) abs_offset >= insn_count || |
| 1107 | !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1108 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1109 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1110 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1111 | return false; |
| 1112 | } |
| 1113 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1117 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1118 | bool* selfOkay) { |
| 1119 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1120 | *pConditional = false; |
| 1121 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1122 | switch (*insns & 0xff) { |
| 1123 | case Instruction::GOTO: |
| 1124 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1125 | break; |
| 1126 | case Instruction::GOTO_32: |
| 1127 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1128 | *selfOkay = true; |
| 1129 | break; |
| 1130 | case Instruction::GOTO_16: |
| 1131 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1132 | break; |
| 1133 | case Instruction::IF_EQ: |
| 1134 | case Instruction::IF_NE: |
| 1135 | case Instruction::IF_LT: |
| 1136 | case Instruction::IF_GE: |
| 1137 | case Instruction::IF_GT: |
| 1138 | case Instruction::IF_LE: |
| 1139 | case Instruction::IF_EQZ: |
| 1140 | case Instruction::IF_NEZ: |
| 1141 | case Instruction::IF_LTZ: |
| 1142 | case Instruction::IF_GEZ: |
| 1143 | case Instruction::IF_GTZ: |
| 1144 | case Instruction::IF_LEZ: |
| 1145 | *pOffset = (int16_t) insns[1]; |
| 1146 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1147 | break; |
| 1148 | default: |
| 1149 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1150 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1151 | return true; |
| 1152 | } |
| 1153 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1154 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1155 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1156 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1157 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1158 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 1159 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
| 1160 | if (static_cast<int32_t>(cur_offset) + switch_offset < 0 || |
| 1161 | cur_offset + switch_offset + 2 > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1162 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1163 | << ", switch offset " << switch_offset |
| 1164 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1167 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1168 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1169 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1170 | if (!IsAligned<4>(switch_insns)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1171 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1172 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1175 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1176 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
| 1177 | if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) { |
| 1178 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1179 | << ", switch offset " << switch_offset |
| 1180 | << " not correctly visited, probably bad padding."; |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | uint32_t switch_count = switch_insns[1]; |
| 1185 | int32_t keys_offset, targets_offset; |
| 1186 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1187 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1188 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1189 | targets_offset = 4; |
| 1190 | keys_offset = -1; |
| 1191 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1192 | } else { |
| 1193 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1194 | keys_offset = 2; |
| 1195 | targets_offset = 2 + 2 * switch_count; |
| 1196 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1197 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1198 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1199 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1200 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1201 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1202 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1203 | return false; |
| 1204 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1205 | /* make sure the end of the switch is in range */ |
| 1206 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1207 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1208 | << ", switch offset " << switch_offset |
| 1209 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1210 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1211 | return false; |
| 1212 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1213 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1214 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1215 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1216 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 1217 | int32_t key = |
| 1218 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) | |
| 1219 | static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1220 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1221 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 1222 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1223 | return false; |
| 1224 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1225 | last_key = key; |
| 1226 | } |
| 1227 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1228 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1229 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 1230 | int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1231 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1232 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1233 | if (abs_offset < 0 || |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 1234 | abs_offset >= static_cast<int32_t>(insn_count) || |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1235 | !insn_flags_[abs_offset].IsOpcode()) { |
| 1236 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1237 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1238 | << reinterpret_cast<void*>(cur_offset) |
| 1239 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1242 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1243 | } |
| 1244 | return true; |
| 1245 | } |
| 1246 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1247 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1248 | uint16_t registers_size = code_item_->registers_size_; |
| 1249 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1250 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1251 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1252 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1253 | return false; |
| 1254 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | return true; |
| 1258 | } |
| 1259 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1260 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1261 | uint16_t registers_size = code_item_->registers_size_; |
| 1262 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1263 | // integer overflow when adding them here. |
| 1264 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1265 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1266 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1269 | return true; |
| 1270 | } |
| 1271 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1272 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1273 | uint16_t registers_size = code_item_->registers_size_; |
| 1274 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1275 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1276 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1277 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1278 | insn_flags_.get(), |
| 1279 | insns_size, |
| 1280 | registers_size, |
| 1281 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1282 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1283 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1284 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1285 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1286 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1287 | /* Initialize register types of method arguments. */ |
| 1288 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1289 | DCHECK_NE(failures_.size(), 0U); |
| 1290 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1291 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1292 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1293 | return false; |
| 1294 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1295 | // We may have a runtime failure here, clear. |
| 1296 | have_pending_runtime_throw_failure_ = false; |
| 1297 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1298 | /* Perform code flow verification. */ |
| 1299 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1300 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1302 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1303 | return true; |
| 1304 | } |
| 1305 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1306 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1307 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1308 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1309 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1310 | } |
| 1311 | return os; |
| 1312 | } |
| 1313 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1314 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1315 | VariableIndentationOutputStream vios(&os); |
| 1316 | Dump(&vios); |
| 1317 | } |
| 1318 | |
| 1319 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1320 | if (code_item_ == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1321 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1322 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1323 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1324 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1325 | vios->Stream() << "Register Types:\n"; |
| 1326 | ScopedIndentation indent1(vios); |
| 1327 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1328 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1329 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1330 | ScopedIndentation indent1(vios); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1331 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1332 | 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] | 1333 | dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1334 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1335 | if (reg_line != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1336 | vios->Stream() << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1337 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1338 | vios->Stream() |
| 1339 | << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1340 | const bool kDumpHexOfInstruction = false; |
| 1341 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1342 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1343 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1344 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1345 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1348 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1349 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1350 | case 'I': |
| 1351 | case 'C': |
| 1352 | case 'S': |
| 1353 | case 'B': |
| 1354 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1355 | case 'F': |
| 1356 | case 'D': |
| 1357 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1358 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1359 | default: |
| 1360 | return false; |
| 1361 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1364 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1365 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1366 | |
| 1367 | // Should have been verified earlier. |
| 1368 | DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_); |
| 1369 | |
| 1370 | uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1371 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1372 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1373 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1374 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1375 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1376 | if (expected_args == 0) { |
| 1377 | // Expect at least a receiver. |
| 1378 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1382 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1383 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1384 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1385 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1386 | if (IsConstructor()) { |
| 1387 | if (declaring_class.IsJavaLangObject()) { |
| 1388 | // "this" is implicitly initialized. |
| 1389 | reg_line->SetThisInitialized(); |
| 1390 | reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class); |
| 1391 | } else { |
| 1392 | reg_line->SetRegisterType(this, arg_start + cur_arg, |
| 1393 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1394 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1395 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1396 | reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1397 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1398 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1401 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1402 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1403 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1404 | |
| 1405 | for (; iterator.HasNext(); iterator.Next()) { |
| 1406 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1407 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1408 | LOG(FATAL) << "Null descriptor"; |
| 1409 | } |
| 1410 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1411 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1412 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1413 | return false; |
| 1414 | } |
| 1415 | switch (descriptor[0]) { |
| 1416 | case 'L': |
| 1417 | case '[': |
| 1418 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1419 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1420 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1421 | // can return without doing anything or call virtual methods). |
| 1422 | { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1423 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1424 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1425 | DCHECK(HasFailures()); |
| 1426 | return false; |
| 1427 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1428 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1429 | } |
| 1430 | break; |
| 1431 | case 'Z': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1432 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1433 | break; |
| 1434 | case 'C': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1435 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1436 | break; |
| 1437 | case 'B': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1438 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1439 | break; |
| 1440 | case 'I': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1441 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1442 | break; |
| 1443 | case 'S': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1444 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1445 | break; |
| 1446 | case 'F': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1447 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1448 | break; |
| 1449 | case 'J': |
| 1450 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1451 | if (cur_arg + 1 >= expected_args) { |
| 1452 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1453 | << " args, found more (" << descriptor << ")"; |
| 1454 | return false; |
| 1455 | } |
| 1456 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1457 | const RegType* lo_half; |
| 1458 | const RegType* hi_half; |
| 1459 | if (descriptor[0] == 'J') { |
| 1460 | lo_half = ®_types_.LongLo(); |
| 1461 | hi_half = ®_types_.LongHi(); |
| 1462 | } else { |
| 1463 | lo_half = ®_types_.DoubleLo(); |
| 1464 | hi_half = ®_types_.DoubleHi(); |
| 1465 | } |
| 1466 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1467 | cur_arg++; |
| 1468 | break; |
| 1469 | } |
| 1470 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1471 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1472 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1473 | return false; |
| 1474 | } |
| 1475 | cur_arg++; |
| 1476 | } |
| 1477 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1478 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1479 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1480 | return false; |
| 1481 | } |
| 1482 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1483 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1484 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1485 | bool result; |
| 1486 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1487 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1488 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1489 | size_t i = 0; |
| 1490 | do { |
| 1491 | i++; |
| 1492 | } while (descriptor[i] == '['); // process leading [ |
| 1493 | if (descriptor[i] == 'L') { // object array |
| 1494 | do { |
| 1495 | i++; // find closing ; |
| 1496 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1497 | result = descriptor[i] == ';'; |
| 1498 | } else { // primitive array |
| 1499 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1500 | } |
| 1501 | } else if (descriptor[0] == 'L') { |
| 1502 | // could be more thorough here, but shouldn't be required |
| 1503 | size_t i = 0; |
| 1504 | do { |
| 1505 | i++; |
| 1506 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1507 | result = descriptor[i] == ';'; |
| 1508 | } else { |
| 1509 | result = false; |
| 1510 | } |
| 1511 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1512 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1513 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1514 | } |
| 1515 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1518 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1519 | const uint16_t* insns = code_item_->insns_; |
| 1520 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1521 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1522 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1523 | insn_flags_[0].SetChanged(); |
| 1524 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1525 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1526 | /* Continue until no instructions are marked "changed". */ |
| 1527 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1528 | if (allow_thread_suspension_) { |
| 1529 | self_->AllowThreadSuspension(); |
| 1530 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1531 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1532 | uint32_t insn_idx = start_guess; |
| 1533 | for (; insn_idx < insns_size; insn_idx++) { |
| 1534 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1535 | break; |
| 1536 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1537 | if (insn_idx == insns_size) { |
| 1538 | if (start_guess != 0) { |
| 1539 | /* try again, starting from the top */ |
| 1540 | start_guess = 0; |
| 1541 | continue; |
| 1542 | } else { |
| 1543 | /* all flags are clear */ |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1547 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1548 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1549 | // "changed" flags, we need to load the set of registers from the table. |
| 1550 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1551 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1552 | // target. |
| 1553 | work_insn_idx_ = insn_idx; |
| 1554 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1555 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1556 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1557 | /* |
| 1558 | * Sanity check: retrieve the stored register line (assuming |
| 1559 | * a full table) and make sure it actually matches. |
| 1560 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1561 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1562 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1563 | if (work_line_->CompareLine(register_line) != 0) { |
| 1564 | Dump(std::cout); |
| 1565 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1566 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1567 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1568 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1569 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1570 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1571 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1572 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1573 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1574 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1575 | prepend += " failed to verify: "; |
| 1576 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1577 | return false; |
| 1578 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1579 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1580 | insn_flags_[insn_idx].SetVisited(); |
| 1581 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1582 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1583 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1584 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1585 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1586 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1588 | * down the line, possibly in the verifier. |
| 1589 | * |
| 1590 | * If we've substituted "always throw" instructions into the stream, |
| 1591 | * we are almost certainly going to have some dead code. |
| 1592 | */ |
| 1593 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1594 | uint32_t insn_idx = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1595 | for (; insn_idx < insns_size; |
| 1596 | insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1597 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1598 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1599 | * may or may not be preceded by a padding NOP (for alignment). |
| 1600 | */ |
| 1601 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1602 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1603 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1604 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1605 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1606 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1607 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1608 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1611 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1612 | if (dead_start < 0) |
| 1613 | dead_start = insn_idx; |
| 1614 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1615 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1616 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1617 | dead_start = -1; |
| 1618 | } |
| 1619 | } |
| 1620 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1621 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1622 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1623 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1624 | // To dump the state of the verify after a method, do something like: |
| 1625 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1626 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1627 | // LOG(INFO) << info_messages_.str(); |
| 1628 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1629 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1630 | return true; |
| 1631 | } |
| 1632 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1633 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1634 | // is no such field. |
| 1635 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) { |
| 1636 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 1637 | DCHECK(class_def != nullptr); |
| 1638 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 1639 | DCHECK(class_data != nullptr); |
| 1640 | ClassDataItemIterator it(dex_file, class_data); |
| 1641 | // Skip static fields. |
| 1642 | while (it.HasNextStaticField()) { |
| 1643 | it.Next(); |
| 1644 | } |
| 1645 | while (it.HasNextInstanceField()) { |
| 1646 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 1647 | return it.GetMemberIndex(); |
| 1648 | } |
| 1649 | it.Next(); |
| 1650 | } |
| 1651 | return DexFile::kDexNoIndex; |
| 1652 | } |
| 1653 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1654 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1655 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1656 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1657 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1658 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1659 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1660 | 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] | 1661 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1662 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1663 | } |
| 1664 | } |
| 1665 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1666 | /* |
| 1667 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1668 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1669 | * control to another statement: |
| 1670 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1671 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1672 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1673 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1674 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1675 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1676 | * throw an exception that is handled by an encompassing "try" |
| 1677 | * block. |
| 1678 | * |
| 1679 | * We can also return, in which case there is no successor instruction |
| 1680 | * from this point. |
| 1681 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1682 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1683 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1684 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1685 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1686 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1687 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1688 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1689 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1690 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1691 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1692 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1693 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1694 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1695 | |
| 1696 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1697 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1698 | * can throw an exception, we will copy/merge this into the "catch" |
| 1699 | * address rather than work_line, because we don't want the result |
| 1700 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1701 | * a type) to be visible to the exception handler. |
| 1702 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1703 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1704 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1705 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1706 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1707 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 1708 | 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] | 1709 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1710 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1711 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1712 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1713 | // branch target. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1714 | std::unique_ptr<RegisterLine> branch_line; |
| 1715 | std::unique_ptr<RegisterLine> fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1716 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1717 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1718 | case Instruction::NOP: |
| 1719 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1720 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1721 | * a signature that looks like a NOP; if we see one of these in |
| 1722 | * the course of executing code then we have a problem. |
| 1723 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1724 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1725 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1726 | } |
| 1727 | break; |
| 1728 | |
| 1729 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1730 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1731 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1732 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1733 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1734 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1735 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1736 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1737 | break; |
| 1738 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1739 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1740 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1741 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1742 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1743 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1744 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1745 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1746 | break; |
| 1747 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1748 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1749 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1750 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1751 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1752 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1753 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1754 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1755 | break; |
| 1756 | |
| 1757 | /* |
| 1758 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1759 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1760 | * might want to hold the result in an actual CPU register, so the |
| 1761 | * Dalvik spec requires that these only appear immediately after an |
| 1762 | * invoke or filled-new-array. |
| 1763 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1764 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1765 | * redundant with the reset done below, but it can make the debug info |
| 1766 | * easier to read in some cases.) |
| 1767 | */ |
| 1768 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1769 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1770 | break; |
| 1771 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1772 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1773 | break; |
| 1774 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1775 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1776 | break; |
| 1777 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1778 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1779 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 1780 | // where one entrypoint to the catch block is not actually an exception path. |
| 1781 | if (work_insn_idx_ == 0) { |
| 1782 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 1783 | break; |
| 1784 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1785 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1786 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1787 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1788 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1789 | const RegType& res_type = GetCaughtExceptionType(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1790 | work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1791 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1792 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1793 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1794 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1795 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1796 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1797 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1798 | } |
| 1799 | break; |
| 1800 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1801 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1802 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1803 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1804 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1805 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1806 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1807 | } else { |
| 1808 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1809 | // 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] | 1810 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1811 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1812 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1813 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1814 | return_type.IsShort() || return_type.IsChar()) && |
| 1815 | src_type.IsInteger())); |
| 1816 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1817 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1818 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1819 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1820 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1821 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | break; |
| 1825 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1826 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1827 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1828 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1829 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1830 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1831 | } else { |
| 1832 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1833 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1834 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1835 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1836 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1837 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | break; |
| 1841 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1842 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1843 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1844 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1845 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1846 | } else { |
| 1847 | /* return_type is the *expected* return type, not register value */ |
| 1848 | DCHECK(!return_type.IsZero()); |
| 1849 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1850 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1851 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 1852 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 1853 | // reference in vAA is an instance of the "return_type." |
| 1854 | if (reg_type.IsUndefined()) { |
| 1855 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 1856 | } else if (reg_type.IsConflict()) { |
| 1857 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 1858 | } else if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1859 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1860 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1861 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1862 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1863 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1864 | << "' or '" << reg_type << "'"; |
| 1865 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 1866 | bool soft_error = false; |
| 1867 | // Check whether arrays are involved. They will show a valid class status, even |
| 1868 | // if their components are erroneous. |
| 1869 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
| 1870 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error); |
| 1871 | if (soft_error) { |
| 1872 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 1873 | << reg_type << " vs " << return_type; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | if (!soft_error) { |
| 1878 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1879 | << "', but expected from declaration '" << return_type << "'"; |
| 1880 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1881 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | break; |
| 1886 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1887 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1888 | case Instruction::CONST_4: { |
| 1889 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1890 | work_line_->SetRegisterType(this, inst->VRegA_11n(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1891 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1892 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1893 | } |
| 1894 | case Instruction::CONST_16: { |
| 1895 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1896 | work_line_->SetRegisterType(this, inst->VRegA_21s(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1897 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1898 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1899 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1900 | case Instruction::CONST: { |
| 1901 | int32_t val = inst->VRegB_31i(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1902 | work_line_->SetRegisterType(this, inst->VRegA_31i(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1903 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1904 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1905 | } |
| 1906 | case Instruction::CONST_HIGH16: { |
| 1907 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1908 | work_line_->SetRegisterType(this, inst->VRegA_21h(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1909 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1910 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1911 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1912 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1913 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1914 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1915 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1916 | 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] | 1917 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1918 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1919 | } |
| 1920 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1921 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1922 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1923 | 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] | 1924 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1925 | break; |
| 1926 | } |
| 1927 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1928 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1929 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1930 | 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] | 1931 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1932 | break; |
| 1933 | } |
| 1934 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1935 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1936 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1937 | 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] | 1938 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1939 | break; |
| 1940 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1941 | case Instruction::CONST_STRING: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1942 | work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1943 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1944 | case Instruction::CONST_STRING_JUMBO: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1945 | work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1946 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1947 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1948 | // Get type from instruction if unresolved then we need an access check |
| 1949 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1950 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1951 | // Register holds class, ie its type is class, on error it will hold Conflict. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1952 | work_line_->SetRegisterType(this, inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1953 | res_type.IsConflict() ? res_type |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1954 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1955 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1956 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1957 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1958 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1959 | break; |
| 1960 | case Instruction::MONITOR_EXIT: |
| 1961 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1962 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1963 | * 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] | 1964 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1965 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1966 | * feature that Dalvik doesn't support.) |
| 1967 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1968 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1969 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1970 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1971 | * structured locking checks are working, the former would have |
| 1972 | * failed on the -enter instruction, and the latter is impossible. |
| 1973 | * |
| 1974 | * This is fortunate, because issue 3221411 prevents us from |
| 1975 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1976 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1977 | * some catch blocks (which will show up as "dead" code when |
| 1978 | * we skip them here); if we can't, then the code path could be |
| 1979 | * "live" so we still need to check it. |
| 1980 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1981 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1982 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1983 | break; |
| 1984 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1985 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1986 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1987 | /* |
| 1988 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1989 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1990 | * |
| 1991 | * 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] | 1992 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1993 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1994 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1995 | const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1996 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1997 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 1998 | // If this is a primitive type, fail HARD. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1999 | mirror::Class* klass = dex_cache_->GetResolvedType(type_idx); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2000 | if (klass != nullptr && klass->IsPrimitive()) { |
| 2001 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 2002 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 2003 | << GetDeclaringClass(); |
| 2004 | break; |
| 2005 | } |
| 2006 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2007 | DCHECK_NE(failures_.size(), 0U); |
| 2008 | if (!is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2009 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2010 | } |
| 2011 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2012 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2013 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2014 | 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] | 2015 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2016 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2017 | if (is_checkcast) { |
| 2018 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2019 | } else { |
| 2020 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2021 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2022 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2023 | if (is_checkcast) { |
| 2024 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2025 | } else { |
| 2026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2027 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2028 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2029 | if (is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2030 | work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2031 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2032 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2033 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2034 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2035 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2036 | } |
| 2037 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2038 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2039 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2040 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2041 | 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] | 2042 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2043 | work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2044 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2045 | } else { |
| 2046 | 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] | 2047 | } |
| 2048 | break; |
| 2049 | } |
| 2050 | case Instruction::NEW_INSTANCE: { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2051 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2052 | if (res_type.IsConflict()) { |
| 2053 | DCHECK_NE(failures_.size(), 0U); |
| 2054 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2055 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2056 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2057 | // can't create an instance of an interface or abstract class */ |
| 2058 | if (!res_type.IsInstantiableTypes()) { |
| 2059 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2060 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2061 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2062 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2063 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2064 | // Any registers holding previous allocations from this address that have not yet been |
| 2065 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2066 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2067 | // add the new uninitialized reference to the register state |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2068 | work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2069 | break; |
| 2070 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2071 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2072 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2073 | break; |
| 2074 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2075 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2076 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2077 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2078 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2079 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2080 | just_set_result = true; // Filled new array range sets result register |
| 2081 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2082 | case Instruction::CMPL_FLOAT: |
| 2083 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2084 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2085 | break; |
| 2086 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2087 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2088 | break; |
| 2089 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2090 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2091 | break; |
| 2092 | case Instruction::CMPL_DOUBLE: |
| 2093 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2094 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2095 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2096 | break; |
| 2097 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2098 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2099 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2100 | break; |
| 2101 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2102 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2103 | break; |
| 2104 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2105 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2106 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2107 | break; |
| 2108 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2109 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2110 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2111 | break; |
| 2112 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2113 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2114 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2115 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2116 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2117 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2118 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2119 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2120 | } |
| 2121 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2122 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2123 | case Instruction::GOTO: |
| 2124 | case Instruction::GOTO_16: |
| 2125 | case Instruction::GOTO_32: |
| 2126 | /* no effect on or use of registers */ |
| 2127 | break; |
| 2128 | |
| 2129 | case Instruction::PACKED_SWITCH: |
| 2130 | case Instruction::SPARSE_SWITCH: |
| 2131 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2132 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2133 | break; |
| 2134 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2135 | case Instruction::FILL_ARRAY_DATA: { |
| 2136 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2137 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2138 | /* array_type can be null if the reg type is Zero */ |
| 2139 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2140 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2141 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2142 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2143 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2144 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2145 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2146 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2147 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2148 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2149 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2150 | // Now verify if the element width in the table matches the element width declared in |
| 2151 | // the array |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 2152 | const uint16_t* array_data = |
| 2153 | insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16)); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2154 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2155 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2156 | } else { |
| 2157 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2158 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2159 | // in the table and expected from the array class. |
| 2160 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2161 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2162 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2163 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2169 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2170 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2171 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2172 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2173 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2174 | bool mismatch = false; |
| 2175 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2176 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2177 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2178 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2179 | } else { // both integral? |
| 2180 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2181 | } |
| 2182 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2183 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2184 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2185 | } |
| 2186 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2187 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2188 | case Instruction::IF_LT: |
| 2189 | case Instruction::IF_GE: |
| 2190 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2191 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2192 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2193 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2194 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2195 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2196 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2197 | } |
| 2198 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2199 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2200 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2201 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2202 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2203 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2204 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2205 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2206 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2207 | |
| 2208 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2209 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2210 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2211 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2212 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2213 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2214 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2215 | if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(), |
| 2216 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2217 | work_insn_idx_)) { |
| 2218 | break; |
| 2219 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2220 | } else { |
| 2221 | break; |
| 2222 | } |
| 2223 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2224 | 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] | 2225 | |
| 2226 | /* Check for peep-hole pattern of: |
| 2227 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2228 | * instance-of vX, vY, T; |
| 2229 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2230 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2231 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2232 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2233 | * and sharpen the type of vY to be type T. |
| 2234 | * Note, this pattern can't be if: |
| 2235 | * - if there are other branches to this branch, |
| 2236 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2237 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2238 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2239 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 2240 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 2241 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2242 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2243 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2244 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2245 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2246 | // and we don't want to disallow field and other operations on the object. If the value |
| 2247 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2248 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2249 | // type is assignable to the original then allow optimization. This check is performed to |
| 2250 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2251 | // interface from a class that would lose information relevant to field checks. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2252 | const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2253 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2254 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2255 | if (!orig_type.Equals(cast_type) && |
| 2256 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2257 | 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] | 2258 | !cast_type.GetClass()->IsInterface() && |
| 2259 | (orig_type.IsZero() || |
| 2260 | orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, ®_types_)))) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 2261 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2262 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2263 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2264 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2265 | branch_line.reset(update_line); |
| 2266 | } |
| 2267 | update_line->CopyFromLine(work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2268 | update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2269 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 2270 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2271 | // register encoding space of instance-of, and propagate type information to the source |
| 2272 | // of the move-object. |
| 2273 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2274 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2275 | move_idx--; |
| 2276 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2277 | if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(), |
| 2278 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2279 | work_insn_idx_)) { |
| 2280 | break; |
| 2281 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2282 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 2283 | switch (move_inst->Opcode()) { |
| 2284 | case Instruction::MOVE_OBJECT: |
| 2285 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2286 | update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2287 | } |
| 2288 | break; |
| 2289 | case Instruction::MOVE_OBJECT_FROM16: |
| 2290 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2291 | update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2292 | } |
| 2293 | break; |
| 2294 | case Instruction::MOVE_OBJECT_16: |
| 2295 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2296 | update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2297 | } |
| 2298 | break; |
| 2299 | default: |
| 2300 | break; |
| 2301 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2306 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2307 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2308 | case Instruction::IF_LTZ: |
| 2309 | case Instruction::IF_GEZ: |
| 2310 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2311 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2312 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2313 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2314 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2315 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2316 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2317 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2318 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2319 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2320 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2321 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2322 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2323 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2324 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2325 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2326 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2327 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2328 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2329 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2330 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2331 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2332 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2333 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2334 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2335 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2336 | break; |
| 2337 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2338 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2339 | break; |
| 2340 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2341 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2342 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2345 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2346 | break; |
| 2347 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2348 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2349 | break; |
| 2350 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2351 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2352 | break; |
| 2353 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2354 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2355 | break; |
| 2356 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2357 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2360 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2361 | break; |
| 2362 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2363 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2364 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2365 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2366 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2367 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2368 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2369 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2370 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2371 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2372 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2373 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2374 | break; |
| 2375 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2376 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2377 | break; |
| 2378 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2379 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2380 | break; |
| 2381 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2382 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2383 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2384 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2385 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2386 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2387 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2388 | break; |
| 2389 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2390 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2391 | break; |
| 2392 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2393 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2394 | break; |
| 2395 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2396 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2397 | break; |
| 2398 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2399 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2400 | break; |
| 2401 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2402 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2403 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2404 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2405 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2406 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2407 | break; |
| 2408 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2409 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2410 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2411 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2412 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2413 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2414 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2415 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2416 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2417 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2418 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2419 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2420 | break; |
| 2421 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2422 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2423 | break; |
| 2424 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2425 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2426 | break; |
| 2427 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2428 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2429 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2430 | break; |
| 2431 | |
| 2432 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2433 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2434 | break; |
| 2435 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2436 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2437 | break; |
| 2438 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2439 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2440 | break; |
| 2441 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2442 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2443 | break; |
| 2444 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2445 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2446 | break; |
| 2447 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2448 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2449 | break; |
| 2450 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2451 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2452 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2453 | break; |
| 2454 | |
| 2455 | case Instruction::INVOKE_VIRTUAL: |
| 2456 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2457 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2458 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2459 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2460 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2461 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2462 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2463 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2464 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2465 | if (called_method != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2466 | StackHandleScope<1> hs(self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2467 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2468 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2469 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
| 2470 | return_type_class, |
| 2471 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2472 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2473 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2474 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2478 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2479 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2480 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2481 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2482 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2483 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2484 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2485 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2486 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2487 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2488 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2489 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2490 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2491 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2492 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2493 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2494 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2495 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2496 | const char* return_type_descriptor; |
| 2497 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2498 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2499 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2500 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2501 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2502 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2503 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2504 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2505 | } else { |
| 2506 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2507 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2508 | StackHandleScope<1> hs(self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2509 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2510 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2511 | return_type = &FromClass(return_type_descriptor, |
| 2512 | return_type_class, |
| 2513 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2514 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2515 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2516 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2517 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2518 | } |
| 2519 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2520 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2521 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2522 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2523 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2524 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2525 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2526 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2527 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2528 | if (this_type.IsConflict()) // failure. |
| 2529 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2530 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2531 | /* no null refs allowed (?) */ |
| 2532 | if (this_type.IsZero()) { |
| 2533 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2534 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2535 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2536 | |
| 2537 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2538 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2539 | // TODO: re-enable constructor type verification |
| 2540 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2541 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2542 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2543 | // break; |
| 2544 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2545 | |
| 2546 | /* arg must be an uninitialized reference */ |
| 2547 | if (!this_type.IsUninitializedTypes()) { |
| 2548 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2549 | << this_type; |
| 2550 | break; |
| 2551 | } |
| 2552 | |
| 2553 | /* |
| 2554 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2555 | * registers that have the same object instance in them, not just the "this" register. |
| 2556 | */ |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2557 | const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 2558 | work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2559 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2560 | if (return_type == nullptr) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2561 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, |
| 2562 | false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2563 | } |
| 2564 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2565 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2566 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2567 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2568 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2569 | just_set_result = true; |
| 2570 | break; |
| 2571 | } |
| 2572 | case Instruction::INVOKE_STATIC: |
| 2573 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2574 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2575 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2576 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2577 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2578 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2579 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2580 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2581 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2582 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2583 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2584 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2585 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2586 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2587 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2588 | } else { |
| 2589 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2590 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2591 | just_set_result = true; |
| 2592 | } |
| 2593 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2594 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2595 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2596 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2597 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2598 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2599 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2600 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2601 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2602 | << PrettyMethod(abs_method) << "'"; |
| 2603 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2604 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2605 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2606 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2607 | * interface or Object (see comments in RegType::JoinClass). |
| 2608 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2609 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2610 | if (this_type.IsZero()) { |
| 2611 | /* null pointer always passes (and always fails at runtime) */ |
| 2612 | } else { |
| 2613 | if (this_type.IsUninitializedTypes()) { |
| 2614 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2615 | << this_type; |
| 2616 | break; |
| 2617 | } |
| 2618 | // In the past we have tried to assert that "called_interface" is assignable |
| 2619 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2620 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2621 | // implemented by "this_type". For example, two classes may implement the same |
| 2622 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2623 | // join will set "this_type" to the parent class and a test that this implements |
| 2624 | // the interface will incorrectly fail. |
| 2625 | } |
| 2626 | /* |
| 2627 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2628 | * the type information is in the abstract method, so we're good. |
| 2629 | */ |
| 2630 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2631 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2632 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2633 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2634 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2635 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2636 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2637 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2638 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2639 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2640 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2641 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2642 | } else { |
| 2643 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2644 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2645 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2646 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2647 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2648 | case Instruction::NEG_INT: |
| 2649 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2650 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2651 | break; |
| 2652 | case Instruction::NEG_LONG: |
| 2653 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2654 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2655 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2656 | break; |
| 2657 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2658 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2659 | break; |
| 2660 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2661 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2662 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2663 | break; |
| 2664 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2665 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2666 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2667 | break; |
| 2668 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2669 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2670 | break; |
| 2671 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2672 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2673 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2674 | break; |
| 2675 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2676 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2677 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2678 | break; |
| 2679 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2680 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2681 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2682 | break; |
| 2683 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2684 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2685 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2688 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2689 | break; |
| 2690 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2691 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2692 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2693 | break; |
| 2694 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2695 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2696 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2697 | break; |
| 2698 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2699 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2700 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2701 | break; |
| 2702 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2703 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2704 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2705 | break; |
| 2706 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2707 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2708 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2709 | break; |
| 2710 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2711 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2712 | break; |
| 2713 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2714 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2715 | break; |
| 2716 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2717 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2718 | break; |
| 2719 | |
| 2720 | case Instruction::ADD_INT: |
| 2721 | case Instruction::SUB_INT: |
| 2722 | case Instruction::MUL_INT: |
| 2723 | case Instruction::REM_INT: |
| 2724 | case Instruction::DIV_INT: |
| 2725 | case Instruction::SHL_INT: |
| 2726 | case Instruction::SHR_INT: |
| 2727 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2728 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2729 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2730 | break; |
| 2731 | case Instruction::AND_INT: |
| 2732 | case Instruction::OR_INT: |
| 2733 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2734 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2735 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2736 | break; |
| 2737 | case Instruction::ADD_LONG: |
| 2738 | case Instruction::SUB_LONG: |
| 2739 | case Instruction::MUL_LONG: |
| 2740 | case Instruction::DIV_LONG: |
| 2741 | case Instruction::REM_LONG: |
| 2742 | case Instruction::AND_LONG: |
| 2743 | case Instruction::OR_LONG: |
| 2744 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2745 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2746 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2747 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2748 | break; |
| 2749 | case Instruction::SHL_LONG: |
| 2750 | case Instruction::SHR_LONG: |
| 2751 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2752 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2753 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2754 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2755 | break; |
| 2756 | case Instruction::ADD_FLOAT: |
| 2757 | case Instruction::SUB_FLOAT: |
| 2758 | case Instruction::MUL_FLOAT: |
| 2759 | case Instruction::DIV_FLOAT: |
| 2760 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2761 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2762 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2763 | break; |
| 2764 | case Instruction::ADD_DOUBLE: |
| 2765 | case Instruction::SUB_DOUBLE: |
| 2766 | case Instruction::MUL_DOUBLE: |
| 2767 | case Instruction::DIV_DOUBLE: |
| 2768 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2769 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2770 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2771 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2772 | break; |
| 2773 | case Instruction::ADD_INT_2ADDR: |
| 2774 | case Instruction::SUB_INT_2ADDR: |
| 2775 | case Instruction::MUL_INT_2ADDR: |
| 2776 | case Instruction::REM_INT_2ADDR: |
| 2777 | case Instruction::SHL_INT_2ADDR: |
| 2778 | case Instruction::SHR_INT_2ADDR: |
| 2779 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2780 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2781 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | case Instruction::AND_INT_2ADDR: |
| 2784 | case Instruction::OR_INT_2ADDR: |
| 2785 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2786 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2787 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2788 | break; |
| 2789 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2790 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2791 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2792 | break; |
| 2793 | case Instruction::ADD_LONG_2ADDR: |
| 2794 | case Instruction::SUB_LONG_2ADDR: |
| 2795 | case Instruction::MUL_LONG_2ADDR: |
| 2796 | case Instruction::DIV_LONG_2ADDR: |
| 2797 | case Instruction::REM_LONG_2ADDR: |
| 2798 | case Instruction::AND_LONG_2ADDR: |
| 2799 | case Instruction::OR_LONG_2ADDR: |
| 2800 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2801 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2802 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2803 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2804 | break; |
| 2805 | case Instruction::SHL_LONG_2ADDR: |
| 2806 | case Instruction::SHR_LONG_2ADDR: |
| 2807 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2808 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2809 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2810 | break; |
| 2811 | case Instruction::ADD_FLOAT_2ADDR: |
| 2812 | case Instruction::SUB_FLOAT_2ADDR: |
| 2813 | case Instruction::MUL_FLOAT_2ADDR: |
| 2814 | case Instruction::DIV_FLOAT_2ADDR: |
| 2815 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2816 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2817 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2818 | break; |
| 2819 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2820 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2821 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2822 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2823 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2824 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2825 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2826 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 2829 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2830 | case Instruction::MUL_INT_LIT16: |
| 2831 | case Instruction::DIV_INT_LIT16: |
| 2832 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2833 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2834 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | case Instruction::AND_INT_LIT16: |
| 2837 | case Instruction::OR_INT_LIT16: |
| 2838 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2839 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2840 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2841 | break; |
| 2842 | case Instruction::ADD_INT_LIT8: |
| 2843 | case Instruction::RSUB_INT_LIT8: |
| 2844 | case Instruction::MUL_INT_LIT8: |
| 2845 | case Instruction::DIV_INT_LIT8: |
| 2846 | case Instruction::REM_INT_LIT8: |
| 2847 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2848 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2849 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2850 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2851 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2852 | break; |
| 2853 | case Instruction::AND_INT_LIT8: |
| 2854 | case Instruction::OR_INT_LIT8: |
| 2855 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2856 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2857 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2858 | break; |
| 2859 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2860 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2861 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2862 | if (IsConstructor() && !IsStatic()) { |
| 2863 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2864 | if (declaring_class.IsUnresolvedReference()) { |
| 2865 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 2866 | // manually over the underlying dex file. |
| 2867 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 2868 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
| 2869 | if (first_index != DexFile::kDexNoIndex) { |
| 2870 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 2871 | << first_index; |
| 2872 | } |
| 2873 | break; |
| 2874 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2875 | auto* klass = declaring_class.GetClass(); |
| 2876 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 2877 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 2878 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
| 2879 | << PrettyField(klass->GetInstanceField(i)); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2880 | break; |
| 2881 | } |
| 2882 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2883 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 2884 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 2885 | // quickened opcodes (otherwise this could be a fall-through). |
| 2886 | if (!IsConstructor()) { |
| 2887 | if (!GetMethodReturnType().IsConflict()) { |
| 2888 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 2889 | } |
| 2890 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2891 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2892 | // Note: the following instructions encode offsets derived from class linking. |
| 2893 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2894 | // meaning if the class linking and resolution were successful. |
| 2895 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2896 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2897 | break; |
| 2898 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2899 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2900 | break; |
| 2901 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2902 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2903 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 2904 | case Instruction::IGET_BOOLEAN_QUICK: |
| 2905 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 2906 | break; |
| 2907 | case Instruction::IGET_BYTE_QUICK: |
| 2908 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 2909 | break; |
| 2910 | case Instruction::IGET_CHAR_QUICK: |
| 2911 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 2912 | break; |
| 2913 | case Instruction::IGET_SHORT_QUICK: |
| 2914 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 2915 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2916 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2917 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2918 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2919 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2920 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2921 | break; |
| 2922 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2923 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2924 | break; |
| 2925 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2926 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2927 | break; |
| 2928 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2929 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2930 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2931 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2932 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2933 | break; |
| 2934 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2935 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2936 | break; |
| 2937 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2938 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2939 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2940 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2941 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2942 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2943 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2944 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2945 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2946 | } else { |
| 2947 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2948 | } |
| 2949 | just_set_result = true; |
| 2950 | } |
| 2951 | break; |
| 2952 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2953 | case Instruction::INVOKE_LAMBDA: { |
| 2954 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2955 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2956 | // appropriate verification errors at runtime. |
| 2957 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification |
| 2958 | break; |
| 2959 | } |
| 2960 | case Instruction::CREATE_LAMBDA: { |
| 2961 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2962 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2963 | // appropriate verification errors at runtime. |
| 2964 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification |
| 2965 | break; |
| 2966 | } |
| 2967 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2968 | case Instruction::UNUSED_F4: |
| 2969 | case Instruction::UNUSED_F5: |
| 2970 | case Instruction::UNUSED_F7: { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2971 | DCHECK(false); // TODO(iam): Implement opcodes for lambdas |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2972 | // Conservatively fail verification on release builds. |
| 2973 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
| 2974 | break; |
| 2975 | } |
| 2976 | |
| 2977 | case Instruction::BOX_LAMBDA: { |
| 2978 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2979 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2980 | // appropriate verification errors at runtime. |
| 2981 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 2982 | |
| 2983 | // Partial verification. Sets the resulting type to always be an object, which |
| 2984 | // is good enough for some other verification to occur without hard-failing. |
| 2985 | const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB |
| 2986 | const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_); |
| 2987 | work_line_->SetRegisterType(this, vreg_target_object, reg_type); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2988 | break; |
| 2989 | } |
| 2990 | |
| 2991 | case Instruction::UNBOX_LAMBDA: { |
| 2992 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2993 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2994 | // appropriate verification errors at runtime. |
| 2995 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification |
| 2996 | break; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2997 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2998 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2999 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 3000 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 3001 | case Instruction::UNUSED_FA ... Instruction::UNUSED_FF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3002 | case Instruction::UNUSED_79: |
| 3003 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3004 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3005 | break; |
| 3006 | |
| 3007 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3008 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3009 | * complain if an instruction is missing (which is desirable). |
| 3010 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3011 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3012 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3013 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3014 | if (Runtime::Current()->IsAotCompiler()) { |
| 3015 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3016 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3017 | LOG(ERROR) << "Pending failures:"; |
| 3018 | for (auto& error : failures_) { |
| 3019 | LOG(ERROR) << error; |
| 3020 | } |
| 3021 | for (auto& error_msg : failure_messages_) { |
| 3022 | LOG(ERROR) << error_msg->str(); |
| 3023 | } |
| 3024 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3025 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3026 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3027 | /* immediate failure, reject class */ |
| 3028 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3029 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3030 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3031 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3032 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 3033 | have_any_pending_runtime_throw_failure_ = true; |
| 3034 | // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per |
| 3035 | // instruction. |
| 3036 | have_pending_runtime_throw_failure_ = false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3037 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3038 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3039 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3040 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3041 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3042 | */ |
| 3043 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3044 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3045 | } |
| 3046 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3047 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3048 | |
| 3049 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3050 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3051 | * |
| 3052 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3053 | * 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] | 3054 | * somebody could get a reference field, check it for zero, and if the |
| 3055 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3056 | * 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] | 3057 | * that, and will reject the code. |
| 3058 | * |
| 3059 | * TODO: avoid re-fetching the branch target |
| 3060 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3061 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3062 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3063 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3064 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3065 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3066 | return false; |
| 3067 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3068 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3069 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3070 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3071 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3072 | /* update branch target, set "changed" if appropriate */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3073 | if (nullptr != branch_line.get()) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3074 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3075 | return false; |
| 3076 | } |
| 3077 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3078 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3079 | return false; |
| 3080 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3081 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3085 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3086 | * |
| 3087 | * We've already verified that the table is structurally sound, so we |
| 3088 | * just need to walk through and tag the targets. |
| 3089 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3090 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 3091 | int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3092 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3093 | int switch_count = switch_insns[1]; |
| 3094 | int offset_to_targets, targ; |
| 3095 | |
| 3096 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3097 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3098 | offset_to_targets = 4; |
| 3099 | } else { |
| 3100 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3101 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3102 | offset_to_targets = 2 + 2 * switch_count; |
| 3103 | } |
| 3104 | |
| 3105 | /* verify each switch target */ |
| 3106 | for (targ = 0; targ < switch_count; targ++) { |
| 3107 | int offset; |
| 3108 | uint32_t abs_offset; |
| 3109 | |
| 3110 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3111 | offset = switch_insns[offset_to_targets + targ * 2] | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame^] | 3112 | (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] | 3113 | abs_offset = work_insn_idx_ + offset; |
| 3114 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3115 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3116 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3117 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3118 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3119 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3120 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3125 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3126 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3127 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3128 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3129 | bool has_catch_all_handler = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3130 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3131 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3132 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3133 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3134 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3135 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3136 | uint16_t handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3137 | if (handler_type_idx == DexFile::kDexNoIndex16) { |
| 3138 | has_catch_all_handler = true; |
| 3139 | } else { |
| 3140 | // It is also a catch-all if it is java.lang.Throwable. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3141 | mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_, |
| 3142 | class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3143 | if (klass != nullptr) { |
| 3144 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3145 | has_catch_all_handler = true; |
| 3146 | } |
| 3147 | } else { |
| 3148 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3149 | DCHECK(self_->IsExceptionPending()); |
| 3150 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3151 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3152 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3153 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3154 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3155 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3156 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3157 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3158 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3159 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3160 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3164 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3165 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3166 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3167 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3168 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3169 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3170 | * 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] | 3171 | * it will do so before grabbing the lock). |
| 3172 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3173 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3174 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3175 | << "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] | 3176 | return false; |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3181 | /* Handle "continue". Tag the next consecutive instruction. |
| 3182 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3183 | * because it changes work_line_ when performing peephole optimization |
| 3184 | * and this change should not be used in those cases. |
| 3185 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3186 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3187 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3188 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3189 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 3190 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3191 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3192 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3193 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3194 | // next instruction isn't one. |
| 3195 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 3196 | return false; |
| 3197 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3198 | if (nullptr != fallthrough_line.get()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3199 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3200 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3201 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3202 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 3203 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3204 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 3205 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3206 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 3207 | SafelyMarkAllRegistersAsConflicts(this, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3208 | } else { |
| 3209 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3210 | work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3211 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3212 | work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3216 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3217 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3218 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3219 | // needed. If the merge changes the state of the registers then the work line will be |
| 3220 | // updated. |
| 3221 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3222 | return false; |
| 3223 | } |
| 3224 | } else { |
| 3225 | /* |
| 3226 | * We're not recording register data for the next instruction, so we don't know what the |
| 3227 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3228 | */ |
| 3229 | insn_flags_[next_insn_idx].SetChanged(); |
| 3230 | } |
| 3231 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3232 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3233 | /* 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] | 3234 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3235 | if (!work_line_->VerifyMonitorStackEmpty(this)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3236 | return false; |
| 3237 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3241 | * Update start_guess. Advance to the next instruction of that's |
| 3242 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3243 | * neither of those exists we're in a return or throw; leave start_guess |
| 3244 | * alone and let the caller sort it out. |
| 3245 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3246 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3247 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3248 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3249 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3250 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3251 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3254 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 3255 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3256 | |
| 3257 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3258 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3259 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3260 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3261 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3262 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3263 | mirror::Class* klass = dex_cache_->GetResolvedType(class_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3264 | const RegType& result = klass != nullptr ? |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3265 | FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) : |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3266 | reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3267 | if (result.IsConflict()) { |
| 3268 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3269 | << "' in " << referrer; |
| 3270 | return result; |
| 3271 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3272 | if (klass == nullptr && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3273 | dex_cache_->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3274 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3275 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3276 | // check at runtime if access is allowed and so pass here. If result is |
| 3277 | // primitive, skip the access check. |
| 3278 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 3279 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3280 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3281 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3282 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3283 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3286 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3287 | const RegType* common_super = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3288 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3289 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3290 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3291 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3292 | CatchHandlerIterator iterator(handlers_ptr); |
| 3293 | for (; iterator.HasNext(); iterator.Next()) { |
| 3294 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3295 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3296 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3297 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3298 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3299 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3300 | if (exception.IsUnresolvedTypes()) { |
| 3301 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3302 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3303 | return exception; |
| 3304 | } else { |
| 3305 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3306 | return reg_types_.Conflict(); |
| 3307 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3308 | } else if (common_super == nullptr) { |
| 3309 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3310 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3311 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3312 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3313 | common_super = &common_super->Merge(exception, ®_types_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3314 | if (FailOrAbort(this, |
| 3315 | reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super), |
| 3316 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3317 | work_insn_idx_)) { |
| 3318 | break; |
| 3319 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3320 | } |
| 3321 | } |
| 3322 | } |
| 3323 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3324 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3325 | } |
| 3326 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3327 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3328 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3329 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3330 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3331 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3332 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3333 | } |
| 3334 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3335 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
| 3336 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3337 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3338 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3339 | if (klass_type.IsConflict()) { |
| 3340 | std::string append(" in attempt to access method "); |
| 3341 | append += dex_file_->GetMethodName(method_id); |
| 3342 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3343 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3344 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3345 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3346 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3347 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3348 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3349 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3350 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3351 | auto pointer_size = cl->GetImagePointerSize(); |
| 3352 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3353 | if (res_method == nullptr) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3354 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3355 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3356 | |
| 3357 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3358 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3359 | } else if (method_type == METHOD_INTERFACE) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3360 | res_method = klass->FindInterfaceMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3361 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3362 | res_method = klass->FindVirtualMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3363 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3364 | if (res_method != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3365 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3366 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3367 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3368 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3369 | // a class has changed, and will be flagged as an error in later checks. |
| 3370 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3371 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3372 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3373 | if (res_method == nullptr) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3374 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 3375 | << PrettyDescriptor(klass) << "." << name |
| 3376 | << " " << signature; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3377 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3378 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3379 | } |
| 3380 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3381 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3382 | // enforce them here. |
| 3383 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3384 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 3385 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3386 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3387 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3388 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3389 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3390 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 3391 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3392 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3393 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3394 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3395 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3396 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3397 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3398 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3399 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3400 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 3401 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3402 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3403 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3404 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3405 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3406 | // Check that interface methods match interface classes. |
| 3407 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 3408 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 3409 | << " is in an interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3410 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3411 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 3412 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 3413 | << " is in a non-interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3414 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3415 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3416 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3417 | // target method. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3418 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3419 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3420 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3421 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3422 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3423 | " type of " << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3424 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3425 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3426 | return res_method; |
| 3427 | } |
| 3428 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3429 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3430 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 3431 | 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] | 3432 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3433 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3434 | // definition (which doesn't have register count values). |
| 3435 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3436 | /* caught by static verifier */ |
| 3437 | DCHECK(is_range || expected_args <= 5); |
| 3438 | if (expected_args > code_item_->outs_size_) { |
| 3439 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3440 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3441 | return nullptr; |
| 3442 | } |
| 3443 | |
| 3444 | uint32_t arg[5]; |
| 3445 | if (!is_range) { |
| 3446 | inst->GetVarArgs(arg); |
| 3447 | } |
| 3448 | uint32_t sig_registers = 0; |
| 3449 | |
| 3450 | /* |
| 3451 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3452 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3453 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3454 | */ |
| 3455 | if (method_type != METHOD_STATIC) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3456 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3457 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3458 | CHECK(have_pending_hard_failure_); |
| 3459 | return nullptr; |
| 3460 | } |
| 3461 | if (actual_arg_type.IsUninitializedReference()) { |
| 3462 | if (res_method) { |
| 3463 | if (!res_method->IsConstructor()) { |
| 3464 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3465 | return nullptr; |
| 3466 | } |
| 3467 | } else { |
| 3468 | // Check whether the name of the called method is "<init>" |
| 3469 | 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] | 3470 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3471 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3472 | return nullptr; |
| 3473 | } |
| 3474 | } |
| 3475 | } |
| 3476 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3477 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 3478 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 3479 | // class. It would be wrong to use this for the type check (interface type checks are |
| 3480 | // postponed to runtime). |
| 3481 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3482 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3483 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3484 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 3485 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3486 | } else { |
| 3487 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3488 | const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3489 | res_method_class = ®_types_.FromDescriptor(GetClassLoader(), |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3490 | dex_file_->StringByTypeIdx(class_idx), |
| 3491 | false); |
| 3492 | } |
| 3493 | if (!res_method_class->IsAssignableFrom(actual_arg_type)) { |
| 3494 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3495 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
| 3496 | << "' not instance of '" << *res_method_class << "'"; |
| 3497 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3498 | // the compiler. |
| 3499 | if (have_pending_hard_failure_) { |
| 3500 | return nullptr; |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | sig_registers = 1; |
| 3505 | } |
| 3506 | |
| 3507 | for ( ; it->HasNext(); it->Next()) { |
| 3508 | if (sig_registers >= expected_args) { |
| 3509 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
| 3510 | " arguments, found " << sig_registers << " or more."; |
| 3511 | return nullptr; |
| 3512 | } |
| 3513 | |
| 3514 | const char* param_descriptor = it->GetDescriptor(); |
| 3515 | |
| 3516 | if (param_descriptor == nullptr) { |
| 3517 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 3518 | "component"; |
| 3519 | return nullptr; |
| 3520 | } |
| 3521 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3522 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3523 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) : |
| 3524 | arg[sig_registers]; |
| 3525 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3526 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3527 | if (!src_type.IsIntegralTypes()) { |
| 3528 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3529 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3530 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3531 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 3532 | } else { |
| 3533 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 3534 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3535 | // the compiler. |
| 3536 | if (have_pending_hard_failure_) { |
| 3537 | return nullptr; |
| 3538 | } |
| 3539 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 3540 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 3541 | // instructions not specifying register pairs by the first component, but require them |
| 3542 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 3543 | // none, this will fail below. |
| 3544 | if (!is_range && sig_registers + 1 < expected_args) { |
| 3545 | uint32_t second_reg = arg[sig_registers + 1]; |
| 3546 | if (second_reg != get_reg + 1) { |
| 3547 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 3548 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 3549 | << second_reg << "."; |
| 3550 | return nullptr; |
| 3551 | } |
| 3552 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 3556 | } |
| 3557 | if (expected_args != sig_registers) { |
| 3558 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
| 3559 | " arguments, found " << sig_registers; |
| 3560 | return nullptr; |
| 3561 | } |
| 3562 | return res_method; |
| 3563 | } |
| 3564 | |
| 3565 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 3566 | MethodType method_type, |
| 3567 | bool is_range) { |
| 3568 | // As the method may not have been resolved, make this static check against what we expect. |
| 3569 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 3570 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
| 3571 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3572 | DexFileParameterIterator it(*dex_file_, |
| 3573 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
| 3574 | VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range, |
| 3575 | nullptr); |
| 3576 | } |
| 3577 | |
| 3578 | class MethodParamListDescriptorIterator { |
| 3579 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3580 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3581 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 3582 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 3583 | } |
| 3584 | |
| 3585 | bool HasNext() { |
| 3586 | return pos_ < params_size_; |
| 3587 | } |
| 3588 | |
| 3589 | void Next() { |
| 3590 | ++pos_; |
| 3591 | } |
| 3592 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3593 | const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3594 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 3595 | } |
| 3596 | |
| 3597 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3598 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3599 | size_t pos_; |
| 3600 | const DexFile::TypeList* params_; |
| 3601 | const size_t params_size_; |
| 3602 | }; |
| 3603 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3604 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
| 3605 | const Instruction* inst, MethodType method_type, bool is_range, bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3606 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3607 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3608 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Andreas Gampe | acc4d2f | 2014-06-12 19:35:05 -0700 | [diff] [blame] | 3609 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3610 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3611 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3612 | // Check what we can statically. |
| 3613 | if (!have_pending_hard_failure_) { |
| 3614 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 3615 | } |
| 3616 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3619 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3620 | // has a vtable entry for the target method. |
| 3621 | if (is_super) { |
| 3622 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3623 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3624 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3625 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3626 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3627 | << " to super " << PrettyMethod(res_method); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3628 | return nullptr; |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3629 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3630 | mirror::Class* super_klass = super.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3631 | if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3632 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3633 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3634 | << " to super " << super |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3635 | << "." << res_method->GetName() |
| 3636 | << res_method->GetSignature(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3637 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3638 | } |
| 3639 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3640 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3641 | // Process the target method's signature. This signature may or may not |
| 3642 | MethodParamListDescriptorIterator it(res_method); |
| 3643 | return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type, |
| 3644 | is_range, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3647 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 3648 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3649 | if (is_range) { |
| 3650 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3651 | } else { |
| 3652 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 3653 | } |
| 3654 | const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 3655 | if (!actual_arg_type.HasClass()) { |
| 3656 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 3657 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3658 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3659 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3660 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3661 | if (klass->IsInterface()) { |
| 3662 | // Derive Object.class from Class.class.getSuperclass(). |
| 3663 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3664 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3665 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3666 | return nullptr; |
| 3667 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3668 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3669 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3670 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3671 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3672 | if (!dispatch_class->HasVTable()) { |
| 3673 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 3674 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3675 | return nullptr; |
| 3676 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3677 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3678 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3679 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3680 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 3681 | FailOrAbort(this, allow_failure, |
| 3682 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 3683 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3684 | return nullptr; |
| 3685 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3686 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3687 | if (self_->IsExceptionPending()) { |
| 3688 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 3689 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3690 | return nullptr; |
| 3691 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3692 | return res_method; |
| 3693 | } |
| 3694 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3695 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3696 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 3697 | << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_; |
| 3698 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3699 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3700 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3701 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3702 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3703 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3704 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 3705 | work_insn_idx_)) { |
| 3706 | return nullptr; |
| 3707 | } |
| 3708 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 3709 | work_insn_idx_)) { |
| 3710 | return nullptr; |
| 3711 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3712 | |
| 3713 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3714 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3715 | // definition (which doesn't have register count values). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3716 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3717 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3718 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3719 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3720 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3721 | /* caught by static verifier */ |
| 3722 | DCHECK(is_range || expected_args <= 5); |
| 3723 | if (expected_args > code_item_->outs_size_) { |
| 3724 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3725 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3726 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | /* |
| 3730 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3731 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3732 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3733 | */ |
| 3734 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3735 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3736 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3737 | } |
| 3738 | if (!actual_arg_type.IsZero()) { |
| 3739 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3740 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3741 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3742 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3743 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3744 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3745 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3746 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3747 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | /* |
| 3751 | * Process the target method's signature. This signature may or may not |
| 3752 | * have been verified, so we can't assume it's properly formed. |
| 3753 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3754 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3755 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3756 | uint32_t arg[5]; |
| 3757 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3758 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3759 | } |
| 3760 | size_t actual_args = 1; |
| 3761 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3762 | if (actual_args >= expected_args) { |
| 3763 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3764 | << "'. Expected " << expected_args |
| 3765 | << " arguments, processing argument " << actual_args |
| 3766 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3767 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3768 | } |
| 3769 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3770 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3771 | if (descriptor == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3772 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3773 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3774 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3775 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3776 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3777 | 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] | 3778 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3779 | return res_method; |
| 3780 | } |
| 3781 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3782 | } |
| 3783 | if (actual_args != expected_args) { |
| 3784 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3785 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3786 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3787 | } else { |
| 3788 | return res_method; |
| 3789 | } |
| 3790 | } |
| 3791 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3792 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3793 | uint32_t type_idx; |
| 3794 | if (!is_filled) { |
| 3795 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3796 | type_idx = inst->VRegC_22c(); |
| 3797 | } else if (!is_range) { |
| 3798 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3799 | type_idx = inst->VRegB_35c(); |
| 3800 | } else { |
| 3801 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3802 | type_idx = inst->VRegB_3rc(); |
| 3803 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3804 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3805 | if (res_type.IsConflict()) { // bad class |
| 3806 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3807 | } else { |
| 3808 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3809 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3810 | 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] | 3811 | } else if (!is_filled) { |
| 3812 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3813 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3814 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3815 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3816 | work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3817 | } else { |
| 3818 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3819 | // 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] | 3820 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3821 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3822 | uint32_t arg[5]; |
| 3823 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3824 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3825 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3826 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3827 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3828 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 3829 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3830 | return; |
| 3831 | } |
| 3832 | } |
| 3833 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3834 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3835 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3840 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3841 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3842 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3843 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3844 | 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] | 3845 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3846 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3847 | if (array_type.IsZero()) { |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 3848 | have_pending_runtime_throw_failure_ = true; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3849 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3850 | // instruction type. TODO: have a proper notion of bottom here. |
| 3851 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3852 | // Reference or category 1 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3853 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3854 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3855 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3856 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 3857 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3858 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3859 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3860 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3861 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget"; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3862 | } else { |
| 3863 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3864 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3865 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3866 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3867 | << " source for aget-object"; |
| 3868 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3869 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3870 | << " source for category 1 aget"; |
| 3871 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3872 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3873 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3874 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3875 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3876 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3877 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3878 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3879 | // doubles. |
| 3880 | if (!component_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3881 | work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3882 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3883 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3884 | component_type.HighHalf(®_types_)); |
| 3885 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | } |
| 3890 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3891 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3892 | const uint32_t vregA) { |
| 3893 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3894 | bool instruction_compatible; |
| 3895 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3896 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3897 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3898 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3899 | value_compatible = value_type.IsIntegralTypes(); |
| 3900 | } else if (target_type.IsFloat()) { |
| 3901 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3902 | value_compatible = value_type.IsFloatTypes(); |
| 3903 | } else if (target_type.IsLong()) { |
| 3904 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3905 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3906 | // as target_type depends on the resolved type of the field. |
| 3907 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3908 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3909 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 3910 | } else { |
| 3911 | value_compatible = false; |
| 3912 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3913 | } else if (target_type.IsDouble()) { |
| 3914 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3915 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3916 | // as target_type depends on the resolved type of the field. |
| 3917 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3918 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3919 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 3920 | } else { |
| 3921 | value_compatible = false; |
| 3922 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3923 | } else { |
| 3924 | instruction_compatible = false; // reference with primitive store |
| 3925 | value_compatible = false; // unused |
| 3926 | } |
| 3927 | if (!instruction_compatible) { |
| 3928 | // This is a global failure rather than a class change failure as the instructions and |
| 3929 | // the descriptors for the type should have been consistent within the same file at |
| 3930 | // compile time. |
| 3931 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3932 | << "' but expected type '" << target_type << "'"; |
| 3933 | return; |
| 3934 | } |
| 3935 | if (!value_compatible) { |
| 3936 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3937 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3938 | return; |
| 3939 | } |
| 3940 | } |
| 3941 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3942 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3943 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3944 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3945 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3946 | 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] | 3947 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3948 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3949 | if (array_type.IsZero()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 3950 | // Null array type; this code path will fail at runtime. |
| 3951 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame] | 3952 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 3953 | // and fits multiple register types. |
| 3954 | const RegType* modified_reg_type = &insn_type; |
| 3955 | if ((modified_reg_type == ®_types_.Integer()) || |
| 3956 | (modified_reg_type == ®_types_.LongLo())) { |
| 3957 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 3958 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 3959 | if (modified_reg_type == ®_types_.Integer()) { |
| 3960 | if (&value_type == ®_types_.Float()) { |
| 3961 | modified_reg_type = &value_type; |
| 3962 | } |
| 3963 | } else { |
| 3964 | if (&value_type == ®_types_.DoubleLo()) { |
| 3965 | modified_reg_type = &value_type; |
| 3966 | } |
| 3967 | } |
| 3968 | } |
| 3969 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3970 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3971 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput"; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3972 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3973 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3974 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3975 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3976 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3977 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3978 | if (!component_type.IsReferenceTypes()) { |
| 3979 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3980 | << " source for aput-object"; |
| 3981 | } else { |
| 3982 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3983 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3984 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3985 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3986 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3987 | } |
| 3988 | } |
| 3989 | } |
| 3990 | } |
| 3991 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3992 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3993 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3994 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3995 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3996 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3997 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3998 | field_idx, dex_file_->GetFieldName(field_id), |
| 3999 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4000 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4001 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4002 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4003 | 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] | 4004 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4005 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4006 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 4007 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4008 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4009 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4010 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4011 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4012 | DCHECK(self_->IsExceptionPending()); |
| 4013 | self_->ClearException(); |
| 4014 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4015 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4016 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4017 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4018 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4019 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4020 | } else if (!field->IsStatic()) { |
| 4021 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4022 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4023 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4024 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4025 | } |
| 4026 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4027 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4028 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4029 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4030 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4031 | if (klass_type.IsConflict()) { |
| 4032 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4033 | field_idx, dex_file_->GetFieldName(field_id), |
| 4034 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4035 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4036 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4037 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4038 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4039 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4040 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4041 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 4042 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4043 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4044 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4045 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4046 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4047 | DCHECK(self_->IsExceptionPending()); |
| 4048 | self_->ClearException(); |
| 4049 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4050 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4051 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4052 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4053 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4054 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4055 | } else if (field->IsStatic()) { |
| 4056 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 4057 | << " to not be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4058 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4059 | } else if (obj_type.IsZero()) { |
| 4060 | // Cannot infer and check type, however, access will cause null pointer exception |
| 4061 | return field; |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4062 | } else if (!obj_type.IsReferenceTypes()) { |
| 4063 | // Trying to read a field from something that isn't a reference |
| 4064 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4065 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4066 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4067 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4068 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4069 | const RegType& field_klass = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4070 | FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
| 4071 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4072 | if (obj_type.IsUninitializedTypes() && |
| 4073 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 4074 | !field_klass.Equals(GetDeclaringClass()))) { |
| 4075 | // Field accesses through uninitialized references are only allowable for constructors where |
| 4076 | // the field is declared in this class |
| 4077 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4078 | << " of a not fully initialized object within the context" |
| 4079 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4080 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4081 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 4082 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4083 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4084 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 4085 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 4086 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4087 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4088 | } else { |
| 4089 | return field; |
| 4090 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4091 | } |
| 4092 | } |
| 4093 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4094 | template <MethodVerifier::FieldAccessType kAccType> |
| 4095 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4096 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4097 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4098 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4099 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4100 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4101 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4102 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4103 | field = GetInstanceField(object_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4104 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 4105 | return; |
| 4106 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4107 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4108 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4109 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4110 | if (kAccType == FieldAccessType::kAccPut) { |
| 4111 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4112 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 4113 | << " from other class " << GetDeclaringClass(); |
| 4114 | return; |
| 4115 | } |
| 4116 | } |
| 4117 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4118 | mirror::Class* field_type_class = |
| 4119 | can_load_classes_ ? field->GetType<true>() : field->GetType<false>(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4120 | if (field_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4121 | field_type = &FromClass(field->GetTypeDescriptor(), field_type_class, |
| 4122 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4123 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4124 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4125 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4126 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4127 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4128 | if (field_type == nullptr) { |
| 4129 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4130 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4131 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4132 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4133 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4134 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4135 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4136 | "Unexpected third access type"); |
| 4137 | if (kAccType == FieldAccessType::kAccPut) { |
| 4138 | // sput or iput. |
| 4139 | if (is_primitive) { |
| 4140 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4141 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4142 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4143 | // If the field type is not a reference, this is a global failure rather than |
| 4144 | // a class change failure as the instructions and the descriptors for the type |
| 4145 | // should have been consistent within the same file at compile time. |
| 4146 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4147 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4148 | Fail(error) << "expected field " << PrettyField(field) |
| 4149 | << " to be compatible with type '" << insn_type |
| 4150 | << "' but found type '" << *field_type |
| 4151 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4152 | return; |
| 4153 | } |
| 4154 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4155 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4156 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4157 | // sget or iget. |
| 4158 | if (is_primitive) { |
| 4159 | if (field_type->Equals(insn_type) || |
| 4160 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4161 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4162 | // expected that read is of the correct primitive type or that int reads are reading |
| 4163 | // floats or long reads are reading doubles |
| 4164 | } else { |
| 4165 | // This is a global failure rather than a class change failure as the instructions and |
| 4166 | // the descriptors for the type should have been consistent within the same file at |
| 4167 | // compile time |
| 4168 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4169 | << " to be of type '" << insn_type |
| 4170 | << "' but found type '" << *field_type << "' in get"; |
| 4171 | return; |
| 4172 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4173 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4174 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4175 | // If the field type is not a reference, this is a global failure rather than |
| 4176 | // a class change failure as the instructions and the descriptors for the type |
| 4177 | // should have been consistent within the same file at compile time. |
| 4178 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4179 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4180 | Fail(error) << "expected field " << PrettyField(field) |
| 4181 | << " to be compatible with type '" << insn_type |
| 4182 | << "' but found type '" << *field_type |
| 4183 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4184 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 4185 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 4186 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4187 | return; |
| 4188 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4189 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4190 | if (!field_type->IsLowHalf()) { |
| 4191 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 4192 | } else { |
| 4193 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4194 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4195 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4196 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4197 | } |
| 4198 | } |
| 4199 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4200 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4201 | RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4202 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4203 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4204 | if (!object_type.HasClass()) { |
| 4205 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 4206 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4207 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4208 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4209 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4210 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 4211 | if (f == nullptr) { |
| 4212 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
| 4213 | << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; |
| 4214 | } |
| 4215 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4216 | } |
| 4217 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4218 | template <MethodVerifier::FieldAccessType kAccType> |
| 4219 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4220 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 4221 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4222 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4223 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4224 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 4226 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4227 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4228 | |
| 4229 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 4230 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4231 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4232 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4233 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4234 | return; |
| 4235 | } |
| 4236 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4237 | |
| 4238 | // Get the field type. |
| 4239 | const RegType* field_type; |
| 4240 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4241 | mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() : |
| 4242 | field->GetType<false>(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4243 | |
| 4244 | if (field_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4245 | field_type = &FromClass(field->GetTypeDescriptor(), field_type_class, |
| 4246 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4247 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4248 | Thread* self = Thread::Current(); |
| 4249 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 4250 | self->ClearException(); |
| 4251 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 4252 | field->GetTypeDescriptor(), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4253 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4254 | if (field_type == nullptr) { |
| 4255 | 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] | 4256 | return; |
| 4257 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | const uint32_t vregA = inst->VRegA_22c(); |
| 4261 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4262 | "Unexpected third access type"); |
| 4263 | if (kAccType == FieldAccessType::kAccPut) { |
| 4264 | if (is_primitive) { |
| 4265 | // Primitive field assignability rules are weaker than regular assignability rules |
| 4266 | bool instruction_compatible; |
| 4267 | bool value_compatible; |
| 4268 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 4269 | if (field_type->IsIntegralTypes()) { |
| 4270 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 4271 | value_compatible = value_type.IsIntegralTypes(); |
| 4272 | } else if (field_type->IsFloat()) { |
| 4273 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 4274 | value_compatible = value_type.IsFloatTypes(); |
| 4275 | } else if (field_type->IsLong()) { |
| 4276 | instruction_compatible = insn_type.IsLong(); |
| 4277 | value_compatible = value_type.IsLongTypes(); |
| 4278 | } else if (field_type->IsDouble()) { |
| 4279 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 4280 | value_compatible = value_type.IsDoubleTypes(); |
| 4281 | } else { |
| 4282 | instruction_compatible = false; // reference field with primitive store |
| 4283 | value_compatible = false; // unused |
| 4284 | } |
| 4285 | if (!instruction_compatible) { |
| 4286 | // This is a global failure rather than a class change failure as the instructions and |
| 4287 | // the descriptors for the type should have been consistent within the same file at |
| 4288 | // compile time |
| 4289 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4290 | << " to be of type '" << insn_type |
| 4291 | << "' but found type '" << *field_type |
| 4292 | << "' in put"; |
| 4293 | return; |
| 4294 | } |
| 4295 | if (!value_compatible) { |
| 4296 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4297 | << " of type " << value_type |
| 4298 | << " but expected " << *field_type |
| 4299 | << " for store to " << PrettyField(field) << " in put"; |
| 4300 | return; |
| 4301 | } |
| 4302 | } else { |
| 4303 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4304 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4305 | << " to be compatible with type '" << insn_type |
| 4306 | << "' but found type '" << *field_type |
| 4307 | << "' in put-object"; |
| 4308 | return; |
| 4309 | } |
| 4310 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 4311 | } |
| 4312 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4313 | if (is_primitive) { |
| 4314 | if (field_type->Equals(insn_type) || |
| 4315 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 4316 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 4317 | // expected that read is of the correct primitive type or that int reads are reading |
| 4318 | // floats or long reads are reading doubles |
| 4319 | } else { |
| 4320 | // This is a global failure rather than a class change failure as the instructions and |
| 4321 | // the descriptors for the type should have been consistent within the same file at |
| 4322 | // compile time |
| 4323 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4324 | << " to be of type '" << insn_type |
| 4325 | << "' but found type '" << *field_type << "' in Get"; |
| 4326 | return; |
| 4327 | } |
| 4328 | } else { |
| 4329 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4330 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4331 | << " to be compatible with type '" << insn_type |
| 4332 | << "' but found type '" << *field_type |
| 4333 | << "' in get-object"; |
| 4334 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 4335 | return; |
| 4336 | } |
| 4337 | } |
| 4338 | if (!field_type->IsLowHalf()) { |
| 4339 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 4340 | } else { |
| 4341 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4342 | } |
| 4343 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4344 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4348 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4349 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4350 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4351 | return false; |
| 4352 | } |
| 4353 | return true; |
| 4354 | } |
| 4355 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 4356 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 4357 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 4358 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 4359 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 4360 | return false; |
| 4361 | } |
| 4362 | return true; |
| 4363 | } |
| 4364 | |
| 4365 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 4366 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 4367 | } |
| 4368 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4369 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 4370 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4371 | bool changed = true; |
| 4372 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 4373 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4374 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4375 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4376 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4377 | * 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] | 4378 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4379 | if (!insn_flags_[next_insn].IsReturn()) { |
| 4380 | target_line->CopyFromLine(merge_line); |
| 4381 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4382 | // Verify that the monitor stack is empty on return. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4383 | if (!merge_line->VerifyMonitorStackEmpty(this)) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4384 | return false; |
| 4385 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4386 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4387 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 4388 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 4389 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 4390 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 4391 | // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its |
| 4392 | // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the |
| 4393 | // super-constructor-call checking. |
| 4394 | target_line->CopyThisInitialized(*merge_line); |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 4395 | SafelyMarkAllRegistersAsConflicts(this, target_line); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4396 | } else { |
| 4397 | target_line->CopyFromLine(merge_line); |
| 4398 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4399 | target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4400 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4401 | target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4405 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4406 | std::unique_ptr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 4407 | RegisterLine::Create(target_line->NumRegs(), this) : |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4408 | nullptr); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4409 | if (gDebugVerify) { |
| 4410 | copy->CopyFromLine(target_line); |
| 4411 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4412 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4413 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4414 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4415 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 4416 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4417 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4418 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4419 | << copy->Dump(this) << " MERGE\n" |
| 4420 | << merge_line->Dump(this) << " ==\n" |
| 4421 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4422 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4423 | if (update_merge_line && changed) { |
| 4424 | merge_line->CopyFromLine(target_line); |
| 4425 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4426 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4427 | if (changed) { |
| 4428 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4429 | } |
| 4430 | return true; |
| 4431 | } |
| 4432 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4433 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4434 | return &insn_flags_[work_insn_idx_]; |
| 4435 | } |
| 4436 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4437 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4438 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4439 | if (mirror_method_ != nullptr) { |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4440 | mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4441 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4442 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
| 4443 | return_type_class, |
| 4444 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4445 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4446 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4447 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | if (return_type_ == nullptr) { |
| 4451 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4452 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 4453 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 4454 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4455 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4456 | } |
| 4457 | } |
| 4458 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4461 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4462 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4463 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4464 | const char* descriptor |
| 4465 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4466 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4467 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4468 | declaring_class_ = &FromClass(descriptor, klass, |
| 4469 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4470 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4471 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4472 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4473 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4474 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4477 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4478 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 4479 | 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] | 4480 | std::vector<int32_t> result; |
| 4481 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4482 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4483 | if (type.IsConstant()) { |
| 4484 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4485 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4486 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4487 | } else if (type.IsConstantLo()) { |
| 4488 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4489 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4490 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4491 | } else if (type.IsConstantHi()) { |
| 4492 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4493 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4494 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4495 | } else if (type.IsIntegralTypes()) { |
| 4496 | result.push_back(kIntVReg); |
| 4497 | result.push_back(0); |
| 4498 | } else if (type.IsFloat()) { |
| 4499 | result.push_back(kFloatVReg); |
| 4500 | result.push_back(0); |
| 4501 | } else if (type.IsLong()) { |
| 4502 | result.push_back(kLongLoVReg); |
| 4503 | result.push_back(0); |
| 4504 | result.push_back(kLongHiVReg); |
| 4505 | result.push_back(0); |
| 4506 | ++i; |
| 4507 | } else if (type.IsDouble()) { |
| 4508 | result.push_back(kDoubleLoVReg); |
| 4509 | result.push_back(0); |
| 4510 | result.push_back(kDoubleHiVReg); |
| 4511 | result.push_back(0); |
| 4512 | ++i; |
| 4513 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 4514 | result.push_back(kUndefined); |
| 4515 | result.push_back(0); |
| 4516 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4517 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4518 | result.push_back(kReferenceVReg); |
| 4519 | result.push_back(0); |
| 4520 | } |
| 4521 | } |
| 4522 | return result; |
| 4523 | } |
| 4524 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4525 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 4526 | if (precise) { |
| 4527 | // Precise constant type. |
| 4528 | return reg_types_.FromCat1Const(value, true); |
| 4529 | } else { |
| 4530 | // Imprecise constant type. |
| 4531 | if (value < -32768) { |
| 4532 | return reg_types_.IntConstant(); |
| 4533 | } else if (value < -128) { |
| 4534 | return reg_types_.ShortConstant(); |
| 4535 | } else if (value < 0) { |
| 4536 | return reg_types_.ByteConstant(); |
| 4537 | } else if (value == 0) { |
| 4538 | return reg_types_.Zero(); |
| 4539 | } else if (value == 1) { |
| 4540 | return reg_types_.One(); |
| 4541 | } else if (value < 128) { |
| 4542 | return reg_types_.PosByteConstant(); |
| 4543 | } else if (value < 32768) { |
| 4544 | return reg_types_.PosShortConstant(); |
| 4545 | } else if (value < 65536) { |
| 4546 | return reg_types_.CharConstant(); |
| 4547 | } else { |
| 4548 | return reg_types_.IntConstant(); |
| 4549 | } |
| 4550 | } |
| 4551 | } |
| 4552 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4553 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4554 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4557 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4558 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4559 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4560 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4561 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 4562 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4565 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 4566 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4567 | } |
| 4568 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4569 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 4570 | mirror::Class* klass, |
| 4571 | bool precise) { |
| 4572 | DCHECK(klass != nullptr); |
| 4573 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 4574 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 4575 | << "non-instantiable klass " << descriptor; |
| 4576 | precise = false; |
| 4577 | } |
| 4578 | return reg_types_.FromClass(descriptor, klass, precise); |
| 4579 | } |
| 4580 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4581 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4582 | } // namespace art |