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 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 56 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 57 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 58 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 59 | DCHECK_GT(insns_size, 0U); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 60 | register_lines_.reset(new RegisterLine*[insns_size]()); |
| 61 | size_ = insns_size; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 62 | for (uint32_t i = 0; i < insns_size; i++) { |
| 63 | bool interesting = false; |
| 64 | switch (mode) { |
| 65 | case kTrackRegsAll: |
| 66 | interesting = flags[i].IsOpcode(); |
| 67 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 68 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 69 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 70 | break; |
| 71 | case kTrackRegsBranches: |
| 72 | interesting = flags[i].IsBranchTarget(); |
| 73 | break; |
| 74 | default: |
| 75 | break; |
| 76 | } |
| 77 | if (interesting) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 78 | register_lines_[i] = RegisterLine::Create(registers_size, verifier); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | PcToRegisterLineTable::~PcToRegisterLineTable() { |
| 84 | for (size_t i = 0; i < size_; i++) { |
| 85 | delete register_lines_[i]; |
| 86 | if (kIsDebugBuild) { |
| 87 | register_lines_[i] = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 92 | // Note: returns true on failure. |
| 93 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 94 | const char* error_msg, uint32_t work_insn_idx) { |
| 95 | if (kIsDebugBuild) { |
| 96 | // In a debug build, abort if the error condition is wrong. |
| 97 | DCHECK(condition) << error_msg << work_insn_idx; |
| 98 | } else { |
| 99 | // In a non-debug build, just fail the class. |
| 100 | if (!condition) { |
| 101 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 109 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 110 | if (verifier->IsInstanceConstructor()) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 111 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 112 | reg_line->CheckConstructorReturn(verifier); |
| 113 | } |
| 114 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 115 | } |
| 116 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 117 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 118 | ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) { |
| 119 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 120 | mirror::Class* klass = method->GetDeclaringClass(); |
| 121 | auto h_dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 122 | auto h_class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 123 | return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache, |
| 124 | h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 125 | method->GetAccessFlags(), allow_soft_failures, false); |
| 126 | } |
| 127 | |
| 128 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 129 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 130 | mirror::Class* klass, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 131 | bool allow_soft_failures, |
| 132 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 133 | if (klass->IsVerified()) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 134 | return kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 135 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 136 | bool early_failure = false; |
| 137 | std::string failure_message; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 138 | const DexFile& dex_file = klass->GetDexFile(); |
| 139 | const DexFile::ClassDef* class_def = klass->GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 140 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 141 | std::string temp; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 142 | if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 143 | early_failure = true; |
| 144 | failure_message = " that has no super class"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 145 | } else if (super != nullptr && super->IsFinal()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 146 | early_failure = true; |
| 147 | failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 148 | } else if (class_def == nullptr) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 149 | early_failure = true; |
| 150 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 151 | } |
| 152 | if (early_failure) { |
| 153 | *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 154 | if (Runtime::Current()->IsAotCompiler()) { |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 155 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 156 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 157 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 158 | return kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 159 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 160 | StackHandleScope<2> hs(self); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 161 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 162 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 163 | return VerifyClass( |
| 164 | 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] | 165 | } |
| 166 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 167 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 168 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 169 | Handle<mirror::DexCache> dex_cache, |
| 170 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 171 | const DexFile::ClassDef* class_def, |
| 172 | bool allow_soft_failures, |
| 173 | std::string* error) { |
| 174 | DCHECK(class_def != nullptr); |
Andreas Gampe | 507cc6f | 2015-06-19 22:58:47 -0700 | [diff] [blame] | 175 | |
| 176 | // A class must not be abstract and final. |
| 177 | if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) { |
| 178 | *error = "Verifier rejected class "; |
| 179 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 180 | *error += ": class is abstract and final."; |
| 181 | return kHardFailure; |
| 182 | } |
| 183 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 184 | const uint8_t* class_data = dex_file->GetClassData(*class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 185 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 186 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 187 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 188 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 189 | ClassDataItemIterator it(*dex_file, class_data); |
| 190 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 191 | it.Next(); |
| 192 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 193 | size_t error_count = 0; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 194 | bool hard_fail = false; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 195 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 196 | int64_t previous_direct_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 197 | while (it.HasNextDirectMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 198 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 199 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 200 | if (method_idx == previous_direct_method_idx) { |
| 201 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 202 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 203 | it.Next(); |
| 204 | continue; |
| 205 | } |
| 206 | previous_direct_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 207 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 208 | ArtMethod* method = linker->ResolveMethod( |
| 209 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 210 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 211 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 212 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 213 | self->ClearException(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 214 | } else { |
| 215 | DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 216 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 217 | StackHandleScope<1> hs(self); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 218 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 219 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 220 | dex_file, |
| 221 | dex_cache, |
| 222 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 223 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 224 | it.GetMethodCodeItem(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 225 | method, it.GetMethodAccessFlags(), allow_soft_failures, false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 226 | if (result != kNoFailure) { |
| 227 | if (result == kHardFailure) { |
| 228 | hard_fail = true; |
| 229 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 230 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 231 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 232 | *error = "Verifier rejected class "; |
| 233 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 234 | *error += " due to bad method "; |
| 235 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 236 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 237 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 238 | } |
| 239 | it.Next(); |
| 240 | } |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 241 | int64_t previous_virtual_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 242 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 243 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 244 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 245 | if (method_idx == previous_virtual_method_idx) { |
| 246 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 247 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 248 | it.Next(); |
| 249 | continue; |
| 250 | } |
| 251 | previous_virtual_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 252 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 253 | ArtMethod* method = linker->ResolveMethod( |
| 254 | *dex_file, method_idx, dex_cache, class_loader, nullptr, type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 255 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 256 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 257 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 258 | self->ClearException(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 259 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 260 | StackHandleScope<1> hs(self); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 261 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 262 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 263 | dex_file, |
| 264 | dex_cache, |
| 265 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 266 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 267 | it.GetMethodCodeItem(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 268 | method, it.GetMethodAccessFlags(), allow_soft_failures, false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 269 | if (result != kNoFailure) { |
| 270 | if (result == kHardFailure) { |
| 271 | hard_fail = true; |
| 272 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 273 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 274 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 275 | *error = "Verifier rejected class "; |
| 276 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 277 | *error += " due to bad method "; |
| 278 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 279 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 280 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 281 | } |
| 282 | it.Next(); |
| 283 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 284 | if (error_count == 0) { |
| 285 | return kNoFailure; |
| 286 | } else { |
| 287 | return hard_fail ? kHardFailure : kSoftFailure; |
| 288 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 291 | static bool IsLargeMethod(const DexFile::CodeItem* const code_item) { |
Andreas Gampe | 3c651fc | 2015-05-21 14:06:46 -0700 | [diff] [blame] | 292 | if (code_item == nullptr) { |
| 293 | return false; |
| 294 | } |
| 295 | |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 296 | uint16_t registers_size = code_item->registers_size_; |
| 297 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 298 | |
| 299 | return registers_size * insns_size > 4*1024*1024; |
| 300 | } |
| 301 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 302 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 303 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 304 | Handle<mirror::DexCache> dex_cache, |
| 305 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 306 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 307 | const DexFile::CodeItem* code_item, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 308 | ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 309 | uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 310 | bool allow_soft_failures, |
| 311 | bool need_precise_constants) { |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 312 | MethodVerifier::FailureKind result = kNoFailure; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 313 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 314 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 315 | 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] | 316 | method_idx, method, method_access_flags, true, allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 317 | need_precise_constants, true); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 318 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 319 | // Verification completed, however failures may be pending that didn't cause the verification |
| 320 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 321 | CHECK(!verifier.have_pending_hard_failure_); |
| 322 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 323 | if (VLOG_IS_ON(verifier)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 324 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 325 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 326 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 327 | result = kSoftFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 328 | } |
| 329 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 330 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 331 | CHECK_NE(verifier.failures_.size(), 0U); |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 332 | |
| 333 | if (UNLIKELY(verifier.have_pending_experimental_failure_)) { |
| 334 | // Failed due to being forced into interpreter. This is ok because |
| 335 | // we just want to skip verification. |
| 336 | result = kSoftFailure; |
| 337 | } else { |
| 338 | CHECK(verifier.have_pending_hard_failure_); |
| 339 | verifier.DumpFailures(LOG(INFO) << "Verification error in " |
| 340 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 341 | result = kHardFailure; |
| 342 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 343 | if (gDebugVerify) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 344 | std::cout << "\n" << verifier.info_messages_.str(); |
| 345 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 346 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 347 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 348 | if (kTimeVerifyMethod) { |
| 349 | uint64_t duration_ns = NanoTime() - start_ns; |
| 350 | if (duration_ns > MsToNs(100)) { |
| 351 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
Andreas Gampe | a4f5bf6 | 2015-05-18 20:50:12 -0700 | [diff] [blame] | 352 | << " took " << PrettyDuration(duration_ns) |
| 353 | << (IsLargeMethod(code_item) ? " (large method)" : ""); |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 354 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 355 | } |
| 356 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 359 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, |
| 360 | VariableIndentationOutputStream* vios, |
| 361 | uint32_t dex_method_idx, |
| 362 | const DexFile* dex_file, |
| 363 | Handle<mirror::DexCache> dex_cache, |
| 364 | Handle<mirror::ClassLoader> class_loader, |
| 365 | const DexFile::ClassDef* class_def, |
| 366 | const DexFile::CodeItem* code_item, |
| 367 | ArtMethod* method, |
| 368 | uint32_t method_access_flags) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 369 | MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader, |
| 370 | class_def, code_item, dex_method_idx, method, |
| 371 | method_access_flags, true, true, true, true); |
| 372 | verifier->Verify(); |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 373 | verifier->DumpFailures(vios->Stream()); |
| 374 | vios->Stream() << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 375 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 376 | // and querying any info is dangerous/can abort. |
| 377 | if (verifier->have_pending_hard_failure_) { |
| 378 | delete verifier; |
| 379 | return nullptr; |
| 380 | } else { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 381 | verifier->Dump(vios); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 382 | return verifier; |
| 383 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 386 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 387 | const DexFile* dex_file, Handle<mirror::DexCache> dex_cache, |
| 388 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 389 | const DexFile::ClassDef* class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 390 | const DexFile::CodeItem* code_item, uint32_t dex_method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 391 | ArtMethod* method, uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 392 | bool can_load_classes, bool allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 393 | bool need_precise_constants, bool verify_to_dump, |
| 394 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 395 | : self_(self), |
| 396 | reg_types_(can_load_classes), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 397 | work_insn_idx_(DexFile::kDexNoIndex), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 398 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 399 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 400 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 401 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 402 | dex_file_(dex_file), |
| 403 | dex_cache_(dex_cache), |
| 404 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 405 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 406 | code_item_(code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 407 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 408 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 409 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 410 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 411 | have_pending_runtime_throw_failure_(false), |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 412 | have_pending_experimental_failure_(false), |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 413 | have_any_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 414 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 415 | monitor_enter_count_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 416 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 417 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 418 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 419 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 420 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 421 | verify_to_dump_(verify_to_dump), |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 422 | allow_thread_suspension_(allow_thread_suspension), |
| 423 | link_(nullptr) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 424 | self->PushVerifier(this); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 425 | DCHECK(class_def != nullptr); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 428 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 429 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 430 | STLDeleteElements(&failure_messages_); |
| 431 | } |
| 432 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 433 | void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 434 | std::vector<uint32_t>* monitor_enter_dex_pcs) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 435 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 436 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 437 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 438 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 439 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 440 | false, true, false, false); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 441 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 442 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 443 | verifier.FindLocksAtDexPc(); |
| 444 | } |
| 445 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 446 | static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) { |
| 447 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 448 | |
| 449 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 450 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 451 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | dex_pc += inst->SizeInCodeUnits(); |
| 456 | inst = inst->Next(); |
| 457 | } |
| 458 | |
| 459 | return false; |
| 460 | } |
| 461 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 462 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 463 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| 464 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 465 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 466 | // Quick check whether there are any monitor_enter instructions at all. |
| 467 | if (!HasMonitorEnterInstructions(code_item_)) { |
| 468 | return; |
| 469 | } |
| 470 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 471 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 472 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 473 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 474 | // got what we wanted. |
| 475 | Verify(); |
| 476 | } |
| 477 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 478 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 479 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 480 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 481 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 482 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 483 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
| 484 | true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 485 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 488 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 489 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 490 | |
| 491 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 492 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 493 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 494 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 495 | bool success = Verify(); |
| 496 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 497 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 498 | } |
| 499 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 500 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 501 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 502 | } |
| 503 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 504 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 507 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) { |
| 508 | StackHandleScope<2> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 509 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 510 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 511 | MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 512 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
| 513 | true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 514 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 517 | ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 518 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 519 | |
| 520 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 521 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 522 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 523 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 524 | bool success = Verify(); |
| 525 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 526 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 527 | } |
| 528 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 529 | if (register_line == nullptr) { |
| 530 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 531 | } |
| 532 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 533 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 534 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 537 | SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) { |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 538 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 539 | StackHandleScope<2> hs(self); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 540 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 541 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 542 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 543 | m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 544 | true, true, false, true); |
| 545 | return verifier.FindStringInitMap(); |
| 546 | } |
| 547 | |
| 548 | SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() { |
| 549 | Verify(); |
| 550 | return GetStringInitPcRegMap(); |
| 551 | } |
| 552 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 553 | bool MethodVerifier::Verify() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 554 | // 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] | 555 | if (code_item_ == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 556 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 557 | 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] | 558 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 559 | } else { |
| 560 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 561 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 562 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 563 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 564 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 565 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 566 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 567 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 568 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 569 | // Allocate and initialize an array to hold instruction data. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 570 | insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 571 | // Run through the instructions and see if the width checks out. |
| 572 | bool result = ComputeWidthsAndCountOps(); |
| 573 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 574 | result = result && ScanTryCatchBlocks(); |
| 575 | // Perform static instruction verification. |
| 576 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 577 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 578 | result = result && VerifyCodeFlow(); |
| 579 | // Compute information for compiler. |
| 580 | if (result && Runtime::Current()->IsCompiler()) { |
| 581 | result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this); |
| 582 | } |
| 583 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 586 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 587 | switch (error) { |
| 588 | case VERIFY_ERROR_NO_CLASS: |
| 589 | case VERIFY_ERROR_NO_FIELD: |
| 590 | case VERIFY_ERROR_NO_METHOD: |
| 591 | case VERIFY_ERROR_ACCESS_CLASS: |
| 592 | case VERIFY_ERROR_ACCESS_FIELD: |
| 593 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 594 | case VERIFY_ERROR_INSTANTIATION: |
| 595 | case VERIFY_ERROR_CLASS_CHANGE: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 596 | case VERIFY_ERROR_FORCE_INTERPRETER: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 597 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 598 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 599 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 600 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 601 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 602 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 603 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 604 | // to an interpreter. |
| 605 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 606 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 607 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 608 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 609 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 610 | |
| 611 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 612 | // try to merge garbage. |
| 613 | // 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] | 614 | // Note: this can fail before we touch any instruction, for the signature of a method. So |
| 615 | // add a check. |
| 616 | if (work_insn_idx_ < DexFile::kDexNoIndex) { |
| 617 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 618 | const Instruction* inst = Instruction::At(insns); |
| 619 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 620 | |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 621 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 622 | saved_line_->CopyFromLine(work_line_.get()); |
| 623 | } |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 624 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 625 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 626 | break; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 627 | // Indication that verification should be retried at runtime. |
| 628 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 629 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 630 | have_pending_hard_failure_ = true; |
| 631 | } |
| 632 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 633 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 634 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 635 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 636 | if (Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 637 | ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 638 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 639 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 640 | have_pending_hard_failure_ = true; |
| 641 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 644 | failures_.push_back(error); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 645 | 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] | 646 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 647 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 648 | failure_messages_.push_back(failure_message); |
| 649 | return *failure_message; |
| 650 | } |
| 651 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 652 | std::ostream& MethodVerifier::LogVerifyInfo() { |
| 653 | return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) |
| 654 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 655 | } |
| 656 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 657 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 658 | size_t failure_num = failure_messages_.size(); |
| 659 | DCHECK_NE(failure_num, 0U); |
| 660 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 661 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 662 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 663 | delete last_fail_message; |
| 664 | } |
| 665 | |
| 666 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 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 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 673 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 674 | const uint16_t* insns = code_item_->insns_; |
| 675 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 676 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 677 | size_t new_instance_count = 0; |
| 678 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 679 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 680 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 681 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 682 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 683 | switch (opcode) { |
| 684 | case Instruction::APUT_OBJECT: |
| 685 | case Instruction::CHECK_CAST: |
| 686 | has_check_casts_ = true; |
| 687 | break; |
| 688 | case Instruction::INVOKE_VIRTUAL: |
| 689 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 690 | case Instruction::INVOKE_INTERFACE: |
| 691 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 692 | has_virtual_or_interface_invokes_ = true; |
| 693 | break; |
| 694 | case Instruction::MONITOR_ENTER: |
| 695 | monitor_enter_count++; |
| 696 | break; |
| 697 | case Instruction::NEW_INSTANCE: |
| 698 | new_instance_count++; |
| 699 | break; |
| 700 | default: |
| 701 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 702 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 703 | size_t inst_size = inst->SizeInCodeUnits(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 704 | insn_flags_[dex_pc].SetIsOpcode(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 705 | dex_pc += inst_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 706 | inst = inst->RelativeAt(inst_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 709 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 710 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 711 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 715 | new_instance_count_ = new_instance_count; |
| 716 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 720 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 721 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 722 | if (tries_size == 0) { |
| 723 | return true; |
| 724 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 725 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 726 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 727 | |
| 728 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 729 | const DexFile::TryItem* try_item = &tries[idx]; |
| 730 | uint32_t start = try_item->start_addr_; |
| 731 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 732 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 733 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 734 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 737 | if (!insn_flags_[start].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 738 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 739 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 742 | uint32_t dex_pc = start; |
| 743 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 744 | while (dex_pc < end) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 745 | insn_flags_[dex_pc].SetInTry(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 746 | size_t insn_size = inst->SizeInCodeUnits(); |
| 747 | dex_pc += insn_size; |
| 748 | inst = inst->RelativeAt(insn_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 751 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 752 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 753 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 754 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 755 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 756 | CatchHandlerIterator iterator(handlers_ptr); |
| 757 | for (; iterator.HasNext(); iterator.Next()) { |
| 758 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 759 | if (!insn_flags_[dex_pc].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 760 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 761 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 762 | return false; |
| 763 | } |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 764 | if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) { |
| 765 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 766 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 767 | return false; |
| 768 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 769 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 770 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 771 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 772 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 773 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 774 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 775 | dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 776 | if (exception_type == nullptr) { |
| 777 | DCHECK(self_->IsExceptionPending()); |
| 778 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 781 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 782 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 783 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 784 | return true; |
| 785 | } |
| 786 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 787 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 788 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 789 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 790 | /* 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] | 791 | insn_flags_[0].SetBranchTarget(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 792 | insn_flags_[0].SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 793 | |
| 794 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 795 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 796 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 797 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 801 | // All invoke points are marked as "Throw" points already. |
| 802 | // 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] | 803 | if (inst->IsBranch()) { |
| 804 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
| 805 | // The compiler also needs safepoints for fall-through to loop heads. |
| 806 | // Such a loop head must be a target of a branch. |
| 807 | int32_t offset = 0; |
| 808 | bool cond, self_ok; |
| 809 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 810 | DCHECK(target_ok); |
| 811 | insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint(); |
| 812 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 813 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 814 | } else if (inst->IsReturn()) { |
| 815 | insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 816 | } |
| 817 | dex_pc += inst->SizeInCodeUnits(); |
| 818 | inst = inst->Next(); |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 823 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Igor Murashkin | 4d7b75f | 2015-07-21 17:03:36 -0700 | [diff] [blame] | 824 | if (UNLIKELY(inst->IsExperimental())) { |
| 825 | // Experimental instructions don't yet have verifier support implementation. |
| 826 | // While it is possible to use them by themselves, when we try to use stable instructions |
| 827 | // with a virtual register that was created by an experimental instruction, |
| 828 | // the data flow analysis will fail. |
| 829 | Fail(VERIFY_ERROR_FORCE_INTERPRETER) |
| 830 | << "experimental instruction is not supported by verifier; skipping verification"; |
| 831 | have_pending_experimental_failure_ = true; |
| 832 | return false; |
| 833 | } |
| 834 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 835 | bool result = true; |
| 836 | switch (inst->GetVerifyTypeArgumentA()) { |
| 837 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 838 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 839 | break; |
| 840 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 841 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | switch (inst->GetVerifyTypeArgumentB()) { |
| 845 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 846 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 847 | break; |
| 848 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 849 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 850 | break; |
| 851 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 852 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 853 | break; |
| 854 | case Instruction::kVerifyRegBNewInstance: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 855 | result = result && CheckNewInstance(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 856 | break; |
| 857 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 858 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 859 | break; |
| 860 | case Instruction::kVerifyRegBType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 861 | result = result && CheckTypeIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 862 | break; |
| 863 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 864 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | switch (inst->GetVerifyTypeArgumentC()) { |
| 868 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 869 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 870 | break; |
| 871 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 872 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 873 | break; |
| 874 | case Instruction::kVerifyRegCNewArray: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 875 | result = result && CheckNewArray(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 876 | break; |
| 877 | case Instruction::kVerifyRegCType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 878 | result = result && CheckTypeIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 879 | break; |
| 880 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 881 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 882 | break; |
| 883 | } |
| 884 | switch (inst->GetVerifyExtraFlags()) { |
| 885 | case Instruction::kVerifyArrayData: |
| 886 | result = result && CheckArrayData(code_offset); |
| 887 | break; |
| 888 | case Instruction::kVerifyBranchTarget: |
| 889 | result = result && CheckBranchTarget(code_offset); |
| 890 | break; |
| 891 | case Instruction::kVerifySwitchTargets: |
| 892 | result = result && CheckSwitchTargets(code_offset); |
| 893 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 894 | case Instruction::kVerifyVarArgNonZero: |
| 895 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 896 | case Instruction::kVerifyVarArg: { |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 897 | // Instructions that can actually return a negative value shouldn't have this flag. |
| 898 | uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA()); |
| 899 | if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) || |
| 900 | v_a > Instruction::kMaxVarArgRegs) { |
| 901 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in " |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 902 | "non-range invoke"; |
| 903 | return false; |
| 904 | } |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 905 | |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 906 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 907 | inst->GetVarArgs(args); |
Taiju Tsuiki | 29498a2 | 2015-04-13 14:21:00 +0900 | [diff] [blame] | 908 | result = result && CheckVarArgRegs(v_a, args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 909 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 910 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 911 | case Instruction::kVerifyVarArgRangeNonZero: |
| 912 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 913 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 914 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 915 | inst->VRegA() <= 0) { |
| 916 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 917 | "range invoke"; |
| 918 | return false; |
| 919 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 920 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 921 | break; |
| 922 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 923 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 924 | result = false; |
| 925 | break; |
| 926 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 927 | if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 928 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 929 | result = false; |
| 930 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 931 | return result; |
| 932 | } |
| 933 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 934 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 935 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 936 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 937 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 938 | return false; |
| 939 | } |
| 940 | return true; |
| 941 | } |
| 942 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 943 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 944 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 945 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 946 | << "+1 >= " << 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::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 953 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 954 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 955 | << dex_file_->GetHeader().field_ids_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::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 962 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 963 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 964 | << dex_file_->GetHeader().method_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::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 971 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 972 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 973 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 974 | return false; |
| 975 | } |
| 976 | // 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] | 977 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 978 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 979 | 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] | 980 | return false; |
| 981 | } |
| 982 | return true; |
| 983 | } |
| 984 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 985 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 986 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 987 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 988 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
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::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 995 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 996 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 997 | << dex_file_->GetHeader().type_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 | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1003 | bool MethodVerifier::CheckNewArray(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 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1010 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1011 | const char* cp = descriptor; |
| 1012 | while (*cp++ == '[') { |
| 1013 | bracket_count++; |
| 1014 | } |
| 1015 | if (bracket_count == 0) { |
| 1016 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1017 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1018 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1019 | return false; |
| 1020 | } else if (bracket_count > 255) { |
| 1021 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1022 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1023 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1024 | return false; |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1029 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1030 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1031 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1032 | const uint16_t* array_data; |
| 1033 | int32_t array_data_offset; |
| 1034 | |
| 1035 | DCHECK_LT(cur_offset, insn_count); |
| 1036 | /* make sure the start of the array data table is in range */ |
| 1037 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 1038 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 1039 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1040 | 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] | 1041 | << ", data offset " << array_data_offset |
| 1042 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
| 1045 | /* offset to array data table is a relative branch-style offset */ |
| 1046 | array_data = insns + array_data_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1047 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1048 | if (!IsAligned<4>(array_data)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1049 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1050 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1051 | return false; |
| 1052 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1053 | // Make sure the array-data is marked as an opcode. This ensures that it was reached when |
| 1054 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
| 1055 | if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) { |
| 1056 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset |
| 1057 | << ", data offset " << array_data_offset |
| 1058 | << " not correctly visited, probably bad padding."; |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1062 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1063 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1064 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1065 | /* make sure the end of the switch is in range */ |
| 1066 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1067 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1068 | << ", data offset " << array_data_offset << ", end " |
| 1069 | << cur_offset + array_data_offset + table_size |
| 1070 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1071 | return false; |
| 1072 | } |
| 1073 | return true; |
| 1074 | } |
| 1075 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1076 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1077 | int32_t offset; |
| 1078 | bool isConditional, selfOkay; |
| 1079 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1080 | return false; |
| 1081 | } |
| 1082 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1083 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1084 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1085 | return false; |
| 1086 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1087 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1088 | // 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] | 1089 | 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] | 1090 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1091 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
| 1094 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1095 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1096 | if (abs_offset < 0 || |
| 1097 | (uint32_t) abs_offset >= insn_count || |
| 1098 | !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1100 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1101 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1102 | return false; |
| 1103 | } |
| 1104 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1105 | return true; |
| 1106 | } |
| 1107 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1108 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1109 | bool* selfOkay) { |
| 1110 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1111 | *pConditional = false; |
| 1112 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1113 | switch (*insns & 0xff) { |
| 1114 | case Instruction::GOTO: |
| 1115 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1116 | break; |
| 1117 | case Instruction::GOTO_32: |
| 1118 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1119 | *selfOkay = true; |
| 1120 | break; |
| 1121 | case Instruction::GOTO_16: |
| 1122 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1123 | break; |
| 1124 | case Instruction::IF_EQ: |
| 1125 | case Instruction::IF_NE: |
| 1126 | case Instruction::IF_LT: |
| 1127 | case Instruction::IF_GE: |
| 1128 | case Instruction::IF_GT: |
| 1129 | case Instruction::IF_LE: |
| 1130 | case Instruction::IF_EQZ: |
| 1131 | case Instruction::IF_NEZ: |
| 1132 | case Instruction::IF_LTZ: |
| 1133 | case Instruction::IF_GEZ: |
| 1134 | case Instruction::IF_GTZ: |
| 1135 | case Instruction::IF_LEZ: |
| 1136 | *pOffset = (int16_t) insns[1]; |
| 1137 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1138 | break; |
| 1139 | default: |
| 1140 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1141 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1142 | return true; |
| 1143 | } |
| 1144 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1145 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1146 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1147 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1148 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1149 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1150 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
Jeff Hao | 9ccd151 | 2015-03-20 18:11:45 -0700 | [diff] [blame] | 1151 | if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1152 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1153 | << ", switch offset " << switch_offset |
| 1154 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1155 | return false; |
| 1156 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1157 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1158 | const uint16_t* switch_insns = insns + switch_offset; |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1159 | // Make sure the table is at an even dex pc, that is, 32-bit aligned. |
| 1160 | if (!IsAligned<4>(switch_insns)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1161 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1162 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
Andreas Gampe | 57c4758 | 2015-07-01 22:05:59 -0700 | [diff] [blame] | 1165 | // Make sure the switch data is marked as an opcode. This ensures that it was reached when |
| 1166 | // traversing the code item linearly. It is an approximation for a by-spec padding value. |
| 1167 | if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) { |
| 1168 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset |
| 1169 | << ", switch offset " << switch_offset |
| 1170 | << " not correctly visited, probably bad padding."; |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1174 | uint32_t switch_count = switch_insns[1]; |
| 1175 | int32_t keys_offset, targets_offset; |
| 1176 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1177 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1178 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1179 | targets_offset = 4; |
| 1180 | keys_offset = -1; |
| 1181 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1182 | } else { |
| 1183 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1184 | keys_offset = 2; |
| 1185 | targets_offset = 2 + 2 * switch_count; |
| 1186 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1187 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1188 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1189 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1190 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1191 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1192 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1195 | /* make sure the end of the switch is in range */ |
| 1196 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1197 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1198 | << ", switch offset " << switch_offset |
| 1199 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1200 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1201 | return false; |
| 1202 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1203 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1204 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1205 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1206 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1207 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 1208 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 1209 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1210 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 1211 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1212 | return false; |
| 1213 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1214 | last_key = key; |
| 1215 | } |
| 1216 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1217 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1218 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 1219 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 1220 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 1221 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1222 | if (abs_offset < 0 || |
| 1223 | abs_offset >= (int32_t) insn_count || |
| 1224 | !insn_flags_[abs_offset].IsOpcode()) { |
| 1225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1226 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1227 | << reinterpret_cast<void*>(cur_offset) |
| 1228 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1229 | return false; |
| 1230 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1231 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1232 | } |
| 1233 | return true; |
| 1234 | } |
| 1235 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1236 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1237 | uint16_t registers_size = code_item_->registers_size_; |
| 1238 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1239 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1240 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1241 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1242 | return false; |
| 1243 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | return true; |
| 1247 | } |
| 1248 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1249 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1250 | uint16_t registers_size = code_item_->registers_size_; |
| 1251 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1252 | // integer overflow when adding them here. |
| 1253 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1254 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1255 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1258 | return true; |
| 1259 | } |
| 1260 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1261 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1262 | uint16_t registers_size = code_item_->registers_size_; |
| 1263 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1264 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1265 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1266 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1267 | insn_flags_.get(), |
| 1268 | insns_size, |
| 1269 | registers_size, |
| 1270 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1271 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1272 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1273 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1274 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
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 | /* Initialize register types of method arguments. */ |
| 1277 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1278 | DCHECK_NE(failures_.size(), 0U); |
| 1279 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1280 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1281 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
Andreas Gampe | d5ad72f | 2015-06-26 17:33:47 -0700 | [diff] [blame] | 1284 | // We may have a runtime failure here, clear. |
| 1285 | have_pending_runtime_throw_failure_ = false; |
| 1286 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1287 | /* Perform code flow verification. */ |
| 1288 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1289 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1290 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1291 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1292 | return true; |
| 1293 | } |
| 1294 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1295 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1296 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1297 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1298 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1299 | } |
| 1300 | return os; |
| 1301 | } |
| 1302 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1303 | void MethodVerifier::Dump(std::ostream& os) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1304 | VariableIndentationOutputStream vios(&os); |
| 1305 | Dump(&vios); |
| 1306 | } |
| 1307 | |
| 1308 | void MethodVerifier::Dump(VariableIndentationOutputStream* vios) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1309 | if (code_item_ == nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1310 | vios->Stream() << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1311 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1312 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1313 | { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1314 | vios->Stream() << "Register Types:\n"; |
| 1315 | ScopedIndentation indent1(vios); |
| 1316 | reg_types_.Dump(vios->Stream()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1317 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1318 | vios->Stream() << "Dumping instructions and register lines:\n"; |
| 1319 | ScopedIndentation indent1(vios); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1320 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1321 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1322 | dex_pc += inst->SizeInCodeUnits()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1323 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1324 | if (reg_line != nullptr) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1325 | vios->Stream() << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1326 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1327 | vios->Stream() |
| 1328 | << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1329 | const bool kDumpHexOfInstruction = false; |
| 1330 | if (kDumpHexOfInstruction) { |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1331 | vios->Stream() << inst->DumpHex(5) << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1332 | } |
Vladimir Marko | 8f1e08a | 2015-06-26 12:06:30 +0100 | [diff] [blame] | 1333 | vios->Stream() << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1334 | inst = inst->Next(); |
| 1335 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1338 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1339 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1340 | case 'I': |
| 1341 | case 'C': |
| 1342 | case 'S': |
| 1343 | case 'B': |
| 1344 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1345 | case 'F': |
| 1346 | case 'D': |
| 1347 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1348 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1349 | default: |
| 1350 | return false; |
| 1351 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1354 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1355 | RegisterLine* reg_line = reg_table_.GetLine(0); |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1356 | |
| 1357 | // Should have been verified earlier. |
| 1358 | DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_); |
| 1359 | |
| 1360 | uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1361 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1362 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1363 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1364 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1365 | if (!IsStatic()) { |
Andreas Gampe | ef0b1a1 | 2015-06-19 20:37:46 -0700 | [diff] [blame] | 1366 | if (expected_args == 0) { |
| 1367 | // Expect at least a receiver. |
| 1368 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static"; |
| 1369 | return false; |
| 1370 | } |
| 1371 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1372 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1373 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1374 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1375 | const RegType& declaring_class = GetDeclaringClass(); |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1376 | if (IsConstructor()) { |
| 1377 | if (declaring_class.IsJavaLangObject()) { |
| 1378 | // "this" is implicitly initialized. |
| 1379 | reg_line->SetThisInitialized(); |
| 1380 | reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class); |
| 1381 | } else { |
| 1382 | reg_line->SetRegisterType(this, arg_start + cur_arg, |
| 1383 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1384 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1385 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1386 | reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1387 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1388 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1391 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1392 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1393 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1394 | |
| 1395 | for (; iterator.HasNext(); iterator.Next()) { |
| 1396 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1397 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1398 | LOG(FATAL) << "Null descriptor"; |
| 1399 | } |
| 1400 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1401 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1402 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
| 1405 | switch (descriptor[0]) { |
| 1406 | case 'L': |
| 1407 | case '[': |
| 1408 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1409 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1410 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1411 | // can return without doing anything or call virtual methods). |
| 1412 | { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1413 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1414 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1415 | DCHECK(HasFailures()); |
| 1416 | return false; |
| 1417 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1418 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1419 | } |
| 1420 | break; |
| 1421 | case 'Z': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1422 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | case 'C': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1425 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | case 'B': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1428 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | case 'I': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1431 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1432 | break; |
| 1433 | case 'S': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1434 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1435 | break; |
| 1436 | case 'F': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1437 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1438 | break; |
| 1439 | case 'J': |
| 1440 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1441 | if (cur_arg + 1 >= expected_args) { |
| 1442 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1443 | << " args, found more (" << descriptor << ")"; |
| 1444 | return false; |
| 1445 | } |
| 1446 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1447 | const RegType* lo_half; |
| 1448 | const RegType* hi_half; |
| 1449 | if (descriptor[0] == 'J') { |
| 1450 | lo_half = ®_types_.LongLo(); |
| 1451 | hi_half = ®_types_.LongHi(); |
| 1452 | } else { |
| 1453 | lo_half = ®_types_.DoubleLo(); |
| 1454 | hi_half = ®_types_.DoubleHi(); |
| 1455 | } |
| 1456 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1457 | cur_arg++; |
| 1458 | break; |
| 1459 | } |
| 1460 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1461 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1462 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1463 | return false; |
| 1464 | } |
| 1465 | cur_arg++; |
| 1466 | } |
| 1467 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1468 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1469 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1470 | return false; |
| 1471 | } |
| 1472 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1473 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1474 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1475 | bool result; |
| 1476 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1477 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1478 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1479 | size_t i = 0; |
| 1480 | do { |
| 1481 | i++; |
| 1482 | } while (descriptor[i] == '['); // process leading [ |
| 1483 | if (descriptor[i] == 'L') { // object array |
| 1484 | do { |
| 1485 | i++; // find closing ; |
| 1486 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1487 | result = descriptor[i] == ';'; |
| 1488 | } else { // primitive array |
| 1489 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1490 | } |
| 1491 | } else if (descriptor[0] == 'L') { |
| 1492 | // could be more thorough here, but shouldn't be required |
| 1493 | size_t i = 0; |
| 1494 | do { |
| 1495 | i++; |
| 1496 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1497 | result = descriptor[i] == ';'; |
| 1498 | } else { |
| 1499 | result = false; |
| 1500 | } |
| 1501 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1502 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1503 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1504 | } |
| 1505 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1508 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1509 | const uint16_t* insns = code_item_->insns_; |
| 1510 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1511 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1512 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1513 | insn_flags_[0].SetChanged(); |
| 1514 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1515 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1516 | /* Continue until no instructions are marked "changed". */ |
| 1517 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1518 | if (allow_thread_suspension_) { |
| 1519 | self_->AllowThreadSuspension(); |
| 1520 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1521 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1522 | uint32_t insn_idx = start_guess; |
| 1523 | for (; insn_idx < insns_size; insn_idx++) { |
| 1524 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1525 | break; |
| 1526 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1527 | if (insn_idx == insns_size) { |
| 1528 | if (start_guess != 0) { |
| 1529 | /* try again, starting from the top */ |
| 1530 | start_guess = 0; |
| 1531 | continue; |
| 1532 | } else { |
| 1533 | /* all flags are clear */ |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1537 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1538 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1539 | // "changed" flags, we need to load the set of registers from the table. |
| 1540 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1541 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1542 | // target. |
| 1543 | work_insn_idx_ = insn_idx; |
| 1544 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1545 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1546 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1547 | /* |
| 1548 | * Sanity check: retrieve the stored register line (assuming |
| 1549 | * a full table) and make sure it actually matches. |
| 1550 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1551 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1552 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1553 | if (work_line_->CompareLine(register_line) != 0) { |
| 1554 | Dump(std::cout); |
| 1555 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1556 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1557 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1558 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1559 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1560 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1561 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1562 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1563 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1564 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1565 | prepend += " failed to verify: "; |
| 1566 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1569 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1570 | insn_flags_[insn_idx].SetVisited(); |
| 1571 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1572 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1573 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1574 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1575 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1576 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1577 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1578 | * down the line, possibly in the verifier. |
| 1579 | * |
| 1580 | * If we've substituted "always throw" instructions into the stream, |
| 1581 | * we are almost certainly going to have some dead code. |
| 1582 | */ |
| 1583 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1584 | uint32_t insn_idx = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1585 | for (; insn_idx < insns_size; |
| 1586 | insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1588 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1589 | * may or may not be preceded by a padding NOP (for alignment). |
| 1590 | */ |
| 1591 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1592 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1593 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1594 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1595 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1596 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1597 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1598 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1601 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1602 | if (dead_start < 0) |
| 1603 | dead_start = insn_idx; |
| 1604 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1605 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1606 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1607 | dead_start = -1; |
| 1608 | } |
| 1609 | } |
| 1610 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1611 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1612 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1613 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1614 | // To dump the state of the verify after a method, do something like: |
| 1615 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1616 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1617 | // LOG(INFO) << info_messages_.str(); |
| 1618 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1619 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1620 | return true; |
| 1621 | } |
| 1622 | |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 1623 | // Returns the index of the first final instance field of the given class, or kDexNoIndex if there |
| 1624 | // is no such field. |
| 1625 | static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) { |
| 1626 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx); |
| 1627 | DCHECK(class_def != nullptr); |
| 1628 | const uint8_t* class_data = dex_file.GetClassData(*class_def); |
| 1629 | DCHECK(class_data != nullptr); |
| 1630 | ClassDataItemIterator it(dex_file, class_data); |
| 1631 | // Skip static fields. |
| 1632 | while (it.HasNextStaticField()) { |
| 1633 | it.Next(); |
| 1634 | } |
| 1635 | while (it.HasNextInstanceField()) { |
| 1636 | if ((it.GetFieldAccessFlags() & kAccFinal) != 0) { |
| 1637 | return it.GetMemberIndex(); |
| 1638 | } |
| 1639 | it.Next(); |
| 1640 | } |
| 1641 | return DexFile::kDexNoIndex; |
| 1642 | } |
| 1643 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1644 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1645 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1646 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1647 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1648 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1649 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1650 | 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] | 1651 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1652 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1653 | } |
| 1654 | } |
| 1655 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1656 | /* |
| 1657 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1658 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1659 | * control to another statement: |
| 1660 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1661 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1662 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1663 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1664 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1665 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1666 | * throw an exception that is handled by an encompassing "try" |
| 1667 | * block. |
| 1668 | * |
| 1669 | * We can also return, in which case there is no successor instruction |
| 1670 | * from this point. |
| 1671 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1672 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1673 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1674 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1675 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1676 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1677 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1678 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1679 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1680 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1681 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1682 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1683 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1684 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1685 | |
| 1686 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1687 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1688 | * can throw an exception, we will copy/merge this into the "catch" |
| 1689 | * address rather than work_line, because we don't want the result |
| 1690 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1691 | * a type) to be visible to the exception handler. |
| 1692 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1693 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1694 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1695 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1696 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1697 | } |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 1698 | 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] | 1699 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1700 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1701 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1702 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1703 | // branch target. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1704 | std::unique_ptr<RegisterLine> branch_line; |
| 1705 | std::unique_ptr<RegisterLine> fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1706 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1707 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1708 | case Instruction::NOP: |
| 1709 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1710 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1711 | * a signature that looks like a NOP; if we see one of these in |
| 1712 | * the course of executing code then we have a problem. |
| 1713 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1714 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1715 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1716 | } |
| 1717 | break; |
| 1718 | |
| 1719 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1720 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1721 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1722 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1723 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1724 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1725 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1726 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1727 | break; |
| 1728 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1729 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1730 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1731 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1732 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1733 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1734 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1735 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1738 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1739 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1740 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1741 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1742 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1743 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1744 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1745 | break; |
| 1746 | |
| 1747 | /* |
| 1748 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1749 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1750 | * might want to hold the result in an actual CPU register, so the |
| 1751 | * Dalvik spec requires that these only appear immediately after an |
| 1752 | * invoke or filled-new-array. |
| 1753 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1754 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1755 | * redundant with the reset done below, but it can make the debug info |
| 1756 | * easier to read in some cases.) |
| 1757 | */ |
| 1758 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1759 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1760 | break; |
| 1761 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1762 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1765 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1766 | break; |
| 1767 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1768 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1769 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 1770 | // where one entrypoint to the catch block is not actually an exception path. |
| 1771 | if (work_insn_idx_ == 0) { |
| 1772 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 1773 | break; |
| 1774 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1775 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1776 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1777 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1778 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1779 | const RegType& res_type = GetCaughtExceptionType(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1780 | work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1781 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1782 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1783 | case Instruction::RETURN_VOID: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1784 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1785 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1786 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1787 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1788 | } |
| 1789 | break; |
| 1790 | case Instruction::RETURN: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1791 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1792 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1793 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1794 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1795 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1796 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1797 | } else { |
| 1798 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1799 | // 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] | 1800 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1801 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1802 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1803 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1804 | return_type.IsShort() || return_type.IsChar()) && |
| 1805 | src_type.IsInteger())); |
| 1806 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1807 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1808 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1809 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1810 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1811 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | break; |
| 1815 | case Instruction::RETURN_WIDE: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1816 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1817 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1818 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1819 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1820 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1821 | } else { |
| 1822 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1823 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1824 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1825 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1826 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1827 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | break; |
| 1831 | case Instruction::RETURN_OBJECT: |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 1832 | if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1833 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1834 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1835 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1836 | } else { |
| 1837 | /* return_type is the *expected* return type, not register value */ |
| 1838 | DCHECK(!return_type.IsZero()); |
| 1839 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1840 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1841 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Andreas Gampe | a32210c | 2015-06-24 10:26:13 -0700 | [diff] [blame] | 1842 | // Disallow returning undefined, conflict & uninitialized values and verify that the |
| 1843 | // reference in vAA is an instance of the "return_type." |
| 1844 | if (reg_type.IsUndefined()) { |
| 1845 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register"; |
| 1846 | } else if (reg_type.IsConflict()) { |
| 1847 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict"; |
| 1848 | } else if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1849 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1850 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1851 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1852 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1853 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1854 | << "' or '" << reg_type << "'"; |
| 1855 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 1856 | bool soft_error = false; |
| 1857 | // Check whether arrays are involved. They will show a valid class status, even |
| 1858 | // if their components are erroneous. |
| 1859 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
| 1860 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error); |
| 1861 | if (soft_error) { |
| 1862 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 1863 | << reg_type << " vs " << return_type; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | if (!soft_error) { |
| 1868 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1869 | << "', but expected from declaration '" << return_type << "'"; |
| 1870 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1871 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | break; |
| 1876 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1877 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1878 | case Instruction::CONST_4: { |
| 1879 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1880 | work_line_->SetRegisterType(this, inst->VRegA_11n(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1881 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1882 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1883 | } |
| 1884 | case Instruction::CONST_16: { |
| 1885 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1886 | work_line_->SetRegisterType(this, inst->VRegA_21s(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1887 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1888 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1889 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1890 | case Instruction::CONST: { |
| 1891 | int32_t val = inst->VRegB_31i(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1892 | work_line_->SetRegisterType(this, inst->VRegA_31i(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1893 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1894 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1895 | } |
| 1896 | case Instruction::CONST_HIGH16: { |
| 1897 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1898 | work_line_->SetRegisterType(this, inst->VRegA_21h(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1899 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1900 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1901 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1902 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1903 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1904 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1905 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1906 | 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] | 1907 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1908 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1909 | } |
| 1910 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1911 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1912 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1913 | 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] | 1914 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1915 | break; |
| 1916 | } |
| 1917 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1918 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1919 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1920 | 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] | 1921 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1922 | break; |
| 1923 | } |
| 1924 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1925 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1926 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1927 | 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] | 1928 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1929 | break; |
| 1930 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1931 | case Instruction::CONST_STRING: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1932 | work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1933 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1934 | case Instruction::CONST_STRING_JUMBO: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1935 | work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1936 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1937 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1938 | // Get type from instruction if unresolved then we need an access check |
| 1939 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1940 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1941 | // 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] | 1942 | work_line_->SetRegisterType(this, inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1943 | res_type.IsConflict() ? res_type |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1944 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1945 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1946 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1947 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1948 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1949 | break; |
| 1950 | case Instruction::MONITOR_EXIT: |
| 1951 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1952 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1953 | * 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] | 1954 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1955 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1956 | * feature that Dalvik doesn't support.) |
| 1957 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1958 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1959 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1960 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1961 | * structured locking checks are working, the former would have |
| 1962 | * failed on the -enter instruction, and the latter is impossible. |
| 1963 | * |
| 1964 | * This is fortunate, because issue 3221411 prevents us from |
| 1965 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1966 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1967 | * some catch blocks (which will show up as "dead" code when |
| 1968 | * we skip them here); if we can't, then the code path could be |
| 1969 | * "live" so we still need to check it. |
| 1970 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1971 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1972 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1973 | break; |
| 1974 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1975 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1976 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1977 | /* |
| 1978 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1979 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1980 | * |
| 1981 | * 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] | 1982 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1983 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1984 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1985 | 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] | 1986 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1987 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 1988 | // If this is a primitive type, fail HARD. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1989 | mirror::Class* klass = dex_cache_->GetResolvedType(type_idx); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 1990 | if (klass != nullptr && klass->IsPrimitive()) { |
| 1991 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 1992 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 1993 | << GetDeclaringClass(); |
| 1994 | break; |
| 1995 | } |
| 1996 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1997 | DCHECK_NE(failures_.size(), 0U); |
| 1998 | if (!is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1999 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2000 | } |
| 2001 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 2002 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2003 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2004 | 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] | 2005 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2006 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2007 | if (is_checkcast) { |
| 2008 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 2009 | } else { |
| 2010 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 2011 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2012 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2013 | if (is_checkcast) { |
| 2014 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 2015 | } else { |
| 2016 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 2017 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2018 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2019 | if (is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2020 | work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2021 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2022 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2023 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2024 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2025 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2026 | } |
| 2027 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2028 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2029 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2030 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2031 | 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] | 2032 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2033 | work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2034 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 2035 | } else { |
| 2036 | 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] | 2037 | } |
| 2038 | break; |
| 2039 | } |
| 2040 | case Instruction::NEW_INSTANCE: { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2041 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2042 | if (res_type.IsConflict()) { |
| 2043 | DCHECK_NE(failures_.size(), 0U); |
| 2044 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2045 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2046 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 2047 | // can't create an instance of an interface or abstract class */ |
| 2048 | if (!res_type.IsInstantiableTypes()) { |
| 2049 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 2050 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2051 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2052 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2053 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2054 | // Any registers holding previous allocations from this address that have not yet been |
| 2055 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2056 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 2057 | // add the new uninitialized reference to the register state |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2058 | work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2059 | break; |
| 2060 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2061 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2062 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2065 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2066 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2068 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2069 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 2070 | just_set_result = true; // Filled new array range sets result register |
| 2071 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2072 | case Instruction::CMPL_FLOAT: |
| 2073 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2074 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2075 | break; |
| 2076 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2077 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2078 | break; |
| 2079 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2080 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2081 | break; |
| 2082 | case Instruction::CMPL_DOUBLE: |
| 2083 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2084 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2085 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2086 | break; |
| 2087 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2088 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2089 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2090 | break; |
| 2091 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2092 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2093 | break; |
| 2094 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2095 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2096 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2097 | break; |
| 2098 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2099 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2100 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2101 | break; |
| 2102 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2103 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2104 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2105 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2106 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2107 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2108 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2109 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2110 | } |
| 2111 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2112 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2113 | case Instruction::GOTO: |
| 2114 | case Instruction::GOTO_16: |
| 2115 | case Instruction::GOTO_32: |
| 2116 | /* no effect on or use of registers */ |
| 2117 | break; |
| 2118 | |
| 2119 | case Instruction::PACKED_SWITCH: |
| 2120 | case Instruction::SPARSE_SWITCH: |
| 2121 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2122 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2123 | break; |
| 2124 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2125 | case Instruction::FILL_ARRAY_DATA: { |
| 2126 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2127 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2128 | /* array_type can be null if the reg type is Zero */ |
| 2129 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2130 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2131 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2132 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2133 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2134 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2135 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2136 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2137 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2138 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2139 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2140 | // Now verify if the element width in the table matches the element width declared in |
| 2141 | // the array |
| 2142 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 2143 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2144 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2145 | } else { |
| 2146 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2147 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2148 | // in the table and expected from the array class. |
| 2149 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2150 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2151 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2152 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2153 | } |
| 2154 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2158 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2159 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2160 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2161 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2162 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2163 | bool mismatch = false; |
| 2164 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2165 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2166 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2167 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2168 | } else { // both integral? |
| 2169 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2170 | } |
| 2171 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2172 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2173 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2174 | } |
| 2175 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2176 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2177 | case Instruction::IF_LT: |
| 2178 | case Instruction::IF_GE: |
| 2179 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2180 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2181 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2182 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2183 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2184 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2185 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2186 | } |
| 2187 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2188 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2189 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2190 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2191 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2192 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2193 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2194 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2195 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2196 | |
| 2197 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2198 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2199 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2200 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2201 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2202 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2203 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2204 | if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(), |
| 2205 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2206 | work_insn_idx_)) { |
| 2207 | break; |
| 2208 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2209 | } else { |
| 2210 | break; |
| 2211 | } |
| 2212 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2213 | 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] | 2214 | |
| 2215 | /* Check for peep-hole pattern of: |
| 2216 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2217 | * instance-of vX, vY, T; |
| 2218 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2219 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2220 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2221 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2222 | * and sharpen the type of vY to be type T. |
| 2223 | * Note, this pattern can't be if: |
| 2224 | * - if there are other branches to this branch, |
| 2225 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2226 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2227 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2228 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 2229 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 2230 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2231 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2232 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2233 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2234 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2235 | // and we don't want to disallow field and other operations on the object. If the value |
| 2236 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2237 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2238 | // type is assignable to the original then allow optimization. This check is performed to |
| 2239 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2240 | // interface from a class that would lose information relevant to field checks. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2241 | 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] | 2242 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2243 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2244 | if (!orig_type.Equals(cast_type) && |
| 2245 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2246 | 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] | 2247 | !cast_type.GetClass()->IsInterface() && |
| 2248 | (orig_type.IsZero() || |
| 2249 | orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, ®_types_)))) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 2250 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2251 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2252 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2253 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2254 | branch_line.reset(update_line); |
| 2255 | } |
| 2256 | update_line->CopyFromLine(work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2257 | update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2258 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 2259 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2260 | // register encoding space of instance-of, and propagate type information to the source |
| 2261 | // of the move-object. |
| 2262 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2263 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2264 | move_idx--; |
| 2265 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2266 | if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(), |
| 2267 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2268 | work_insn_idx_)) { |
| 2269 | break; |
| 2270 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2271 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 2272 | switch (move_inst->Opcode()) { |
| 2273 | case Instruction::MOVE_OBJECT: |
| 2274 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2275 | update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2276 | } |
| 2277 | break; |
| 2278 | case Instruction::MOVE_OBJECT_FROM16: |
| 2279 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2280 | update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2281 | } |
| 2282 | break; |
| 2283 | case Instruction::MOVE_OBJECT_16: |
| 2284 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2285 | update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2286 | } |
| 2287 | break; |
| 2288 | default: |
| 2289 | break; |
| 2290 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2295 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2296 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2297 | case Instruction::IF_LTZ: |
| 2298 | case Instruction::IF_GEZ: |
| 2299 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2300 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2301 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2302 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2303 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2304 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 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::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2309 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2310 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2311 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2312 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2313 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2314 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2315 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2316 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2317 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2318 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2319 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2320 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2321 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2322 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2324 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2325 | break; |
| 2326 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2327 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2328 | break; |
| 2329 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2330 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2331 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2332 | break; |
| 2333 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2334 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2335 | break; |
| 2336 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2337 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2338 | break; |
| 2339 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2340 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2341 | break; |
| 2342 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2343 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2344 | break; |
| 2345 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2346 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2347 | break; |
| 2348 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2349 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2350 | break; |
| 2351 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2352 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2353 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2354 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2355 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2356 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2357 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2358 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2359 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2360 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2361 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2362 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2363 | break; |
| 2364 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2365 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2366 | break; |
| 2367 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2368 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2369 | break; |
| 2370 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2371 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2372 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2373 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2374 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2375 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2376 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2377 | break; |
| 2378 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2379 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2380 | break; |
| 2381 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2382 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2383 | break; |
| 2384 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2385 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2386 | break; |
| 2387 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2388 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2389 | break; |
| 2390 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2391 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2392 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2393 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2394 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2395 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2396 | break; |
| 2397 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2398 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2399 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2400 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2401 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2402 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
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::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2405 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2406 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2407 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2408 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2409 | break; |
| 2410 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2411 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2412 | break; |
| 2413 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2414 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2415 | break; |
| 2416 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2417 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2418 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2419 | break; |
| 2420 | |
| 2421 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2422 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2423 | break; |
| 2424 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2425 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2426 | break; |
| 2427 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2428 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2429 | break; |
| 2430 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2431 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2432 | break; |
| 2433 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2434 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2435 | break; |
| 2436 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2437 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2438 | break; |
| 2439 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2440 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2441 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2442 | break; |
| 2443 | |
| 2444 | case Instruction::INVOKE_VIRTUAL: |
| 2445 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2446 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2447 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2448 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2449 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2450 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2451 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2452 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2453 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2454 | if (called_method != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2455 | StackHandleScope<1> hs(self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2456 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2457 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2458 | return_type = &FromClass(called_method->GetReturnTypeDescriptor(), |
| 2459 | return_type_class, |
| 2460 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2461 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2462 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2463 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2467 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2468 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2469 | 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] | 2470 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2471 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2472 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2473 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2474 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2475 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2476 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2477 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2478 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2479 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2480 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2481 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2482 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2483 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2484 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2485 | const char* return_type_descriptor; |
| 2486 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2487 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2488 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2489 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2490 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2491 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2492 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2493 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2494 | } else { |
| 2495 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2496 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2497 | StackHandleScope<1> hs(self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2498 | mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2499 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 2500 | return_type = &FromClass(return_type_descriptor, |
| 2501 | return_type_class, |
| 2502 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2503 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2504 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2505 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2506 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2507 | } |
| 2508 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2509 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2510 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2511 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2512 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2513 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2514 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2515 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2516 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2517 | if (this_type.IsConflict()) // failure. |
| 2518 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2519 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2520 | /* no null refs allowed (?) */ |
| 2521 | if (this_type.IsZero()) { |
| 2522 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2523 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2524 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2525 | |
| 2526 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2527 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2528 | // TODO: re-enable constructor type verification |
| 2529 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2530 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2531 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2532 | // break; |
| 2533 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2534 | |
| 2535 | /* arg must be an uninitialized reference */ |
| 2536 | if (!this_type.IsUninitializedTypes()) { |
| 2537 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2538 | << this_type; |
| 2539 | break; |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2544 | * registers that have the same object instance in them, not just the "this" register. |
| 2545 | */ |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2546 | const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 2547 | work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2548 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2549 | if (return_type == nullptr) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2550 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, |
| 2551 | false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2552 | } |
| 2553 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2554 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2555 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2556 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2557 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2558 | just_set_result = true; |
| 2559 | break; |
| 2560 | } |
| 2561 | case Instruction::INVOKE_STATIC: |
| 2562 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2563 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2564 | ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2565 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2566 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2567 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2568 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2569 | 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] | 2570 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2571 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2572 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2573 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2574 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2575 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2576 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2577 | } else { |
| 2578 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2579 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2580 | just_set_result = true; |
| 2581 | } |
| 2582 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2583 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2584 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2585 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2586 | ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2587 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2588 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2589 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2590 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2591 | << PrettyMethod(abs_method) << "'"; |
| 2592 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2593 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2594 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2595 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2596 | * interface or Object (see comments in RegType::JoinClass). |
| 2597 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2598 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2599 | if (this_type.IsZero()) { |
| 2600 | /* null pointer always passes (and always fails at runtime) */ |
| 2601 | } else { |
| 2602 | if (this_type.IsUninitializedTypes()) { |
| 2603 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2604 | << this_type; |
| 2605 | break; |
| 2606 | } |
| 2607 | // In the past we have tried to assert that "called_interface" is assignable |
| 2608 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2609 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2610 | // implemented by "this_type". For example, two classes may implement the same |
| 2611 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2612 | // join will set "this_type" to the parent class and a test that this implements |
| 2613 | // the interface will incorrectly fail. |
| 2614 | } |
| 2615 | /* |
| 2616 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2617 | * the type information is in the abstract method, so we're good. |
| 2618 | */ |
| 2619 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2620 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2621 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2622 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2623 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2624 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2625 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2626 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2627 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2628 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2629 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2630 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2631 | } else { |
| 2632 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2633 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2634 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2635 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2636 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2637 | case Instruction::NEG_INT: |
| 2638 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2639 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2640 | break; |
| 2641 | case Instruction::NEG_LONG: |
| 2642 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2643 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2644 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2645 | break; |
| 2646 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2647 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2648 | break; |
| 2649 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2650 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2651 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2652 | break; |
| 2653 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2654 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2655 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2656 | break; |
| 2657 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2658 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2659 | break; |
| 2660 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2661 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2662 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2663 | break; |
| 2664 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2665 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2666 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2667 | break; |
| 2668 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2669 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2670 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2671 | break; |
| 2672 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2673 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2674 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2675 | break; |
| 2676 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2677 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2678 | break; |
| 2679 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2680 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2681 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2682 | break; |
| 2683 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2684 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2685 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2688 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2689 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2690 | break; |
| 2691 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2692 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2693 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2694 | break; |
| 2695 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2696 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2697 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2698 | break; |
| 2699 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2700 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2701 | break; |
| 2702 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2703 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2704 | break; |
| 2705 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2706 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2707 | break; |
| 2708 | |
| 2709 | case Instruction::ADD_INT: |
| 2710 | case Instruction::SUB_INT: |
| 2711 | case Instruction::MUL_INT: |
| 2712 | case Instruction::REM_INT: |
| 2713 | case Instruction::DIV_INT: |
| 2714 | case Instruction::SHL_INT: |
| 2715 | case Instruction::SHR_INT: |
| 2716 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2717 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2718 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2719 | break; |
| 2720 | case Instruction::AND_INT: |
| 2721 | case Instruction::OR_INT: |
| 2722 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2723 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2724 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2725 | break; |
| 2726 | case Instruction::ADD_LONG: |
| 2727 | case Instruction::SUB_LONG: |
| 2728 | case Instruction::MUL_LONG: |
| 2729 | case Instruction::DIV_LONG: |
| 2730 | case Instruction::REM_LONG: |
| 2731 | case Instruction::AND_LONG: |
| 2732 | case Instruction::OR_LONG: |
| 2733 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2734 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2735 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2736 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | case Instruction::SHL_LONG: |
| 2739 | case Instruction::SHR_LONG: |
| 2740 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2741 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2742 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2743 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | break; |
| 2745 | case Instruction::ADD_FLOAT: |
| 2746 | case Instruction::SUB_FLOAT: |
| 2747 | case Instruction::MUL_FLOAT: |
| 2748 | case Instruction::DIV_FLOAT: |
| 2749 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2750 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2751 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2752 | break; |
| 2753 | case Instruction::ADD_DOUBLE: |
| 2754 | case Instruction::SUB_DOUBLE: |
| 2755 | case Instruction::MUL_DOUBLE: |
| 2756 | case Instruction::DIV_DOUBLE: |
| 2757 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2758 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2759 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2760 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2761 | break; |
| 2762 | case Instruction::ADD_INT_2ADDR: |
| 2763 | case Instruction::SUB_INT_2ADDR: |
| 2764 | case Instruction::MUL_INT_2ADDR: |
| 2765 | case Instruction::REM_INT_2ADDR: |
| 2766 | case Instruction::SHL_INT_2ADDR: |
| 2767 | case Instruction::SHR_INT_2ADDR: |
| 2768 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2769 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2770 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2771 | break; |
| 2772 | case Instruction::AND_INT_2ADDR: |
| 2773 | case Instruction::OR_INT_2ADDR: |
| 2774 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2775 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2776 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2777 | break; |
| 2778 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2779 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2780 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2781 | break; |
| 2782 | case Instruction::ADD_LONG_2ADDR: |
| 2783 | case Instruction::SUB_LONG_2ADDR: |
| 2784 | case Instruction::MUL_LONG_2ADDR: |
| 2785 | case Instruction::DIV_LONG_2ADDR: |
| 2786 | case Instruction::REM_LONG_2ADDR: |
| 2787 | case Instruction::AND_LONG_2ADDR: |
| 2788 | case Instruction::OR_LONG_2ADDR: |
| 2789 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2790 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2791 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2792 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2793 | break; |
| 2794 | case Instruction::SHL_LONG_2ADDR: |
| 2795 | case Instruction::SHR_LONG_2ADDR: |
| 2796 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2797 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2798 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2799 | break; |
| 2800 | case Instruction::ADD_FLOAT_2ADDR: |
| 2801 | case Instruction::SUB_FLOAT_2ADDR: |
| 2802 | case Instruction::MUL_FLOAT_2ADDR: |
| 2803 | case Instruction::DIV_FLOAT_2ADDR: |
| 2804 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2805 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2806 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2807 | break; |
| 2808 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2809 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2810 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2811 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2812 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2813 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2814 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2815 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2816 | break; |
| 2817 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 2818 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2819 | case Instruction::MUL_INT_LIT16: |
| 2820 | case Instruction::DIV_INT_LIT16: |
| 2821 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2822 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2823 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | case Instruction::AND_INT_LIT16: |
| 2826 | case Instruction::OR_INT_LIT16: |
| 2827 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2828 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2829 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2830 | break; |
| 2831 | case Instruction::ADD_INT_LIT8: |
| 2832 | case Instruction::RSUB_INT_LIT8: |
| 2833 | case Instruction::MUL_INT_LIT8: |
| 2834 | case Instruction::DIV_INT_LIT8: |
| 2835 | case Instruction::REM_INT_LIT8: |
| 2836 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2837 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2838 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2839 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2840 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2841 | break; |
| 2842 | case Instruction::AND_INT_LIT8: |
| 2843 | case Instruction::OR_INT_LIT8: |
| 2844 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2845 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2846 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2847 | break; |
| 2848 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2849 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2850 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2851 | if (IsConstructor() && !IsStatic()) { |
| 2852 | auto& declaring_class = GetDeclaringClass(); |
Andreas Gampe | 68df320 | 2015-06-22 11:35:46 -0700 | [diff] [blame] | 2853 | if (declaring_class.IsUnresolvedReference()) { |
| 2854 | // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it |
| 2855 | // manually over the underlying dex file. |
| 2856 | uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_, |
| 2857 | dex_file_->GetMethodId(dex_method_idx_).class_idx_); |
| 2858 | if (first_index != DexFile::kDexNoIndex) { |
| 2859 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field " |
| 2860 | << first_index; |
| 2861 | } |
| 2862 | break; |
| 2863 | } |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2864 | auto* klass = declaring_class.GetClass(); |
| 2865 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 2866 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 2867 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
| 2868 | << PrettyField(klass->GetInstanceField(i)); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2869 | break; |
| 2870 | } |
| 2871 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2872 | } |
Andreas Gampe | b291796 | 2015-07-31 13:36:10 -0700 | [diff] [blame] | 2873 | // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from |
| 2874 | // quickened opcodes (otherwise this could be a fall-through). |
| 2875 | if (!IsConstructor()) { |
| 2876 | if (!GetMethodReturnType().IsConflict()) { |
| 2877 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
| 2878 | } |
| 2879 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2880 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2881 | // Note: the following instructions encode offsets derived from class linking. |
| 2882 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2883 | // meaning if the class linking and resolution were successful. |
| 2884 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2885 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2886 | break; |
| 2887 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2888 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2889 | break; |
| 2890 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2891 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2892 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 2893 | case Instruction::IGET_BOOLEAN_QUICK: |
| 2894 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 2895 | break; |
| 2896 | case Instruction::IGET_BYTE_QUICK: |
| 2897 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 2898 | break; |
| 2899 | case Instruction::IGET_CHAR_QUICK: |
| 2900 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 2901 | break; |
| 2902 | case Instruction::IGET_SHORT_QUICK: |
| 2903 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 2904 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2905 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2906 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2907 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2908 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2909 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2910 | break; |
| 2911 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2912 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2913 | break; |
| 2914 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2915 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2916 | break; |
| 2917 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2918 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2919 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2920 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2921 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2922 | break; |
| 2923 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2924 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2925 | break; |
| 2926 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2927 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2928 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2929 | ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2930 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2931 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2932 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2933 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2934 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2935 | } else { |
| 2936 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2937 | } |
| 2938 | just_set_result = true; |
| 2939 | } |
| 2940 | break; |
| 2941 | } |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2942 | case Instruction::INVOKE_LAMBDA: { |
| 2943 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2944 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2945 | // appropriate verification errors at runtime. |
| 2946 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification |
| 2947 | break; |
| 2948 | } |
| 2949 | case Instruction::CREATE_LAMBDA: { |
| 2950 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2951 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2952 | // appropriate verification errors at runtime. |
| 2953 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification |
| 2954 | break; |
| 2955 | } |
| 2956 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2957 | case Instruction::UNUSED_F4: |
| 2958 | case Instruction::UNUSED_F5: |
| 2959 | case Instruction::UNUSED_F7: { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2960 | DCHECK(false); // TODO(iam): Implement opcodes for lambdas |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2961 | // Conservatively fail verification on release builds. |
| 2962 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
| 2963 | break; |
| 2964 | } |
| 2965 | |
| 2966 | case Instruction::BOX_LAMBDA: { |
| 2967 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2968 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2969 | // appropriate verification errors at runtime. |
| 2970 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 2971 | |
| 2972 | // Partial verification. Sets the resulting type to always be an object, which |
| 2973 | // is good enough for some other verification to occur without hard-failing. |
| 2974 | const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB |
| 2975 | const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_); |
| 2976 | work_line_->SetRegisterType(this, vreg_target_object, reg_type); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 2977 | break; |
| 2978 | } |
| 2979 | |
| 2980 | case Instruction::UNBOX_LAMBDA: { |
| 2981 | // Don't bother verifying, instead the interpreter will take the slow path with access checks. |
| 2982 | // If the code would've normally hard-failed, then the interpreter will throw the |
| 2983 | // appropriate verification errors at runtime. |
| 2984 | Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification |
| 2985 | break; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2986 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2987 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2988 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 2989 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 2990 | case Instruction::UNUSED_FA ... Instruction::UNUSED_FF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2991 | case Instruction::UNUSED_79: |
| 2992 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2993 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2994 | break; |
| 2995 | |
| 2996 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2997 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2998 | * complain if an instruction is missing (which is desirable). |
| 2999 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3000 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3001 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3002 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3003 | if (Runtime::Current()->IsAotCompiler()) { |
| 3004 | /* When AOT compiling, check that the last failure is a hard failure */ |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3005 | if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 3006 | LOG(ERROR) << "Pending failures:"; |
| 3007 | for (auto& error : failures_) { |
| 3008 | LOG(ERROR) << error; |
| 3009 | } |
| 3010 | for (auto& error_msg : failure_messages_) { |
| 3011 | LOG(ERROR) << error_msg->str(); |
| 3012 | } |
| 3013 | LOG(FATAL) << "Pending hard failure, but last failure not hard."; |
| 3014 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3015 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3016 | /* immediate failure, reject class */ |
| 3017 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 3018 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3019 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3020 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 3021 | opcode_flags = Instruction::kThrow; |
Andreas Gampe | d12e782 | 2015-06-25 10:26:40 -0700 | [diff] [blame] | 3022 | have_any_pending_runtime_throw_failure_ = true; |
| 3023 | // Reset the pending_runtime_throw flag. The flag is a global to decouple Fail and is per |
| 3024 | // instruction. |
| 3025 | have_pending_runtime_throw_failure_ = false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3026 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3027 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3028 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 3029 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 3030 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3031 | */ |
| 3032 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3033 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3034 | } |
| 3035 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3036 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3037 | |
| 3038 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3039 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3040 | * |
| 3041 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3042 | * 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] | 3043 | * somebody could get a reference field, check it for zero, and if the |
| 3044 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3045 | * 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] | 3046 | * that, and will reject the code. |
| 3047 | * |
| 3048 | * TODO: avoid re-fetching the branch target |
| 3049 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3050 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3051 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3052 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3053 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3054 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3055 | return false; |
| 3056 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3057 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3058 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3059 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3060 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3061 | /* update branch target, set "changed" if appropriate */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3062 | if (nullptr != branch_line.get()) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3063 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3064 | return false; |
| 3065 | } |
| 3066 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3067 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3068 | return false; |
| 3069 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3070 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3074 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3075 | * |
| 3076 | * We've already verified that the table is structurally sound, so we |
| 3077 | * just need to walk through and tag the targets. |
| 3078 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3079 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3080 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 3081 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 3082 | int switch_count = switch_insns[1]; |
| 3083 | int offset_to_targets, targ; |
| 3084 | |
| 3085 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 3086 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 3087 | offset_to_targets = 4; |
| 3088 | } else { |
| 3089 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3090 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3091 | offset_to_targets = 2 + 2 * switch_count; |
| 3092 | } |
| 3093 | |
| 3094 | /* verify each switch target */ |
| 3095 | for (targ = 0; targ < switch_count; targ++) { |
| 3096 | int offset; |
| 3097 | uint32_t abs_offset; |
| 3098 | |
| 3099 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 3100 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 3101 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3102 | abs_offset = work_insn_idx_ + offset; |
| 3103 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 3104 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3105 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3106 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3107 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3108 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3109 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3114 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 3115 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3116 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3117 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3118 | bool has_catch_all_handler = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3119 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3120 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3121 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 3122 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3123 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3124 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3125 | uint16_t handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 3126 | if (handler_type_idx == DexFile::kDexNoIndex16) { |
| 3127 | has_catch_all_handler = true; |
| 3128 | } else { |
| 3129 | // It is also a catch-all if it is java.lang.Throwable. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3130 | mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_, |
| 3131 | class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3132 | if (klass != nullptr) { |
| 3133 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 3134 | has_catch_all_handler = true; |
| 3135 | } |
| 3136 | } else { |
| 3137 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3138 | DCHECK(self_->IsExceptionPending()); |
| 3139 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3140 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3141 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3142 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3143 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3144 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3145 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3146 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3147 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3148 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3149 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3153 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3154 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3155 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3156 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3157 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3158 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3159 | * 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] | 3160 | * it will do so before grabbing the lock). |
| 3161 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3162 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3163 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3164 | << "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] | 3165 | return false; |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3170 | /* Handle "continue". Tag the next consecutive instruction. |
| 3171 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3172 | * because it changes work_line_ when performing peephole optimization |
| 3173 | * and this change should not be used in those cases. |
| 3174 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3175 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3176 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3177 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3178 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 3179 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3180 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3181 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3182 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3183 | // next instruction isn't one. |
| 3184 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 3185 | return false; |
| 3186 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3187 | if (nullptr != fallthrough_line.get()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3188 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3189 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3190 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3191 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 3192 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3193 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 3194 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3195 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 3196 | SafelyMarkAllRegistersAsConflicts(this, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3197 | } else { |
| 3198 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3199 | work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3200 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3201 | work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3205 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3206 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3207 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3208 | // needed. If the merge changes the state of the registers then the work line will be |
| 3209 | // updated. |
| 3210 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3211 | return false; |
| 3212 | } |
| 3213 | } else { |
| 3214 | /* |
| 3215 | * We're not recording register data for the next instruction, so we don't know what the |
| 3216 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3217 | */ |
| 3218 | insn_flags_[next_insn_idx].SetChanged(); |
| 3219 | } |
| 3220 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3221 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3222 | /* 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] | 3223 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3224 | if (!work_line_->VerifyMonitorStackEmpty(this)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3225 | return false; |
| 3226 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3230 | * Update start_guess. Advance to the next instruction of that's |
| 3231 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3232 | * neither of those exists we're in a return or throw; leave start_guess |
| 3233 | * alone and let the caller sort it out. |
| 3234 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3235 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3236 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3237 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3238 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3239 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3240 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3243 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 3244 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3245 | |
| 3246 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3247 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3248 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3249 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3250 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3251 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3252 | mirror::Class* klass = dex_cache_->GetResolvedType(class_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3253 | const RegType& result = klass != nullptr ? |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3254 | FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) : |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3255 | reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3256 | if (result.IsConflict()) { |
| 3257 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3258 | << "' in " << referrer; |
| 3259 | return result; |
| 3260 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3261 | if (klass == nullptr && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3262 | dex_cache_->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3263 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3264 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3265 | // check at runtime if access is allowed and so pass here. If result is |
| 3266 | // primitive, skip the access check. |
| 3267 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 3268 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3269 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3270 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3271 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3272 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3275 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3276 | const RegType* common_super = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3277 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3278 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3279 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3280 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3281 | CatchHandlerIterator iterator(handlers_ptr); |
| 3282 | for (; iterator.HasNext(); iterator.Next()) { |
| 3283 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3284 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3285 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3286 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3287 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3288 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3289 | if (exception.IsUnresolvedTypes()) { |
| 3290 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3291 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3292 | return exception; |
| 3293 | } else { |
| 3294 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3295 | return reg_types_.Conflict(); |
| 3296 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3297 | } else if (common_super == nullptr) { |
| 3298 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3299 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3300 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3301 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3302 | common_super = &common_super->Merge(exception, ®_types_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3303 | if (FailOrAbort(this, |
| 3304 | reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super), |
| 3305 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3306 | work_insn_idx_)) { |
| 3307 | break; |
| 3308 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | } |
| 3312 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3313 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3314 | } |
| 3315 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3316 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3317 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3318 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3319 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3320 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3321 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3324 | ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess( |
| 3325 | uint32_t dex_method_idx, MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3326 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3327 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3328 | if (klass_type.IsConflict()) { |
| 3329 | std::string append(" in attempt to access method "); |
| 3330 | append += dex_file_->GetMethodName(method_id); |
| 3331 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3332 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3333 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3334 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3335 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3336 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3337 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3338 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3339 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3340 | auto pointer_size = cl->GetImagePointerSize(); |
| 3341 | ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3342 | if (res_method == nullptr) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3343 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3344 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3345 | |
| 3346 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3347 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3348 | } else if (method_type == METHOD_INTERFACE) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3349 | res_method = klass->FindInterfaceMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3350 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3351 | res_method = klass->FindVirtualMethod(name, signature, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3352 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3353 | if (res_method != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3354 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3355 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3356 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3357 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3358 | // a class has changed, and will be flagged as an error in later checks. |
| 3359 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3360 | res_method = klass->FindDirectMethod(name, signature, pointer_size); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3361 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3362 | if (res_method == nullptr) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3363 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 3364 | << PrettyDescriptor(klass) << "." << name |
| 3365 | << " " << signature; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3366 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3367 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3368 | } |
| 3369 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3370 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3371 | // enforce them here. |
| 3372 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3373 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 3374 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3375 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3376 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3377 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3378 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3379 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 3380 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3381 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3382 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3383 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3384 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3385 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3386 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3387 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3388 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3389 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 3390 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3391 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3392 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3393 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3394 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3395 | // Check that interface methods match interface classes. |
| 3396 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 3397 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 3398 | << " is in an interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3399 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3400 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 3401 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 3402 | << " is in a non-interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3403 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3404 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3405 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3406 | // target method. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3407 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3408 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3409 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3410 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3411 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3412 | " type of " << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3413 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3414 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3415 | return res_method; |
| 3416 | } |
| 3417 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3418 | template <class T> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3419 | ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator( |
| 3420 | 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] | 3421 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3422 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3423 | // definition (which doesn't have register count values). |
| 3424 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3425 | /* caught by static verifier */ |
| 3426 | DCHECK(is_range || expected_args <= 5); |
| 3427 | if (expected_args > code_item_->outs_size_) { |
| 3428 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3429 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3430 | return nullptr; |
| 3431 | } |
| 3432 | |
| 3433 | uint32_t arg[5]; |
| 3434 | if (!is_range) { |
| 3435 | inst->GetVarArgs(arg); |
| 3436 | } |
| 3437 | uint32_t sig_registers = 0; |
| 3438 | |
| 3439 | /* |
| 3440 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3441 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3442 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3443 | */ |
| 3444 | if (method_type != METHOD_STATIC) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3445 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3446 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3447 | CHECK(have_pending_hard_failure_); |
| 3448 | return nullptr; |
| 3449 | } |
| 3450 | if (actual_arg_type.IsUninitializedReference()) { |
| 3451 | if (res_method) { |
| 3452 | if (!res_method->IsConstructor()) { |
| 3453 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3454 | return nullptr; |
| 3455 | } |
| 3456 | } else { |
| 3457 | // Check whether the name of the called method is "<init>" |
| 3458 | 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] | 3459 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3460 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3461 | return nullptr; |
| 3462 | } |
| 3463 | } |
| 3464 | } |
| 3465 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3466 | const RegType* res_method_class; |
Andreas Gampe | d9e2301 | 2015-06-04 22:19:58 -0700 | [diff] [blame] | 3467 | // Miranda methods have the declaring interface as their declaring class, not the abstract |
| 3468 | // class. It would be wrong to use this for the type check (interface type checks are |
| 3469 | // postponed to runtime). |
| 3470 | if (res_method != nullptr && !res_method->IsMiranda()) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3471 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3472 | std::string temp; |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3473 | res_method_class = &FromClass(klass->GetDescriptor(&temp), klass, |
| 3474 | klass->CannotBeAssignedFromOtherTypes()); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3475 | } else { |
| 3476 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3477 | const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3478 | res_method_class = ®_types_.FromDescriptor(GetClassLoader(), |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3479 | dex_file_->StringByTypeIdx(class_idx), |
| 3480 | false); |
| 3481 | } |
| 3482 | if (!res_method_class->IsAssignableFrom(actual_arg_type)) { |
| 3483 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3484 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
| 3485 | << "' not instance of '" << *res_method_class << "'"; |
| 3486 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3487 | // the compiler. |
| 3488 | if (have_pending_hard_failure_) { |
| 3489 | return nullptr; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | sig_registers = 1; |
| 3494 | } |
| 3495 | |
| 3496 | for ( ; it->HasNext(); it->Next()) { |
| 3497 | if (sig_registers >= expected_args) { |
| 3498 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
| 3499 | " arguments, found " << sig_registers << " or more."; |
| 3500 | return nullptr; |
| 3501 | } |
| 3502 | |
| 3503 | const char* param_descriptor = it->GetDescriptor(); |
| 3504 | |
| 3505 | if (param_descriptor == nullptr) { |
| 3506 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 3507 | "component"; |
| 3508 | return nullptr; |
| 3509 | } |
| 3510 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3511 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3512 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) : |
| 3513 | arg[sig_registers]; |
| 3514 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3515 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3516 | if (!src_type.IsIntegralTypes()) { |
| 3517 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3518 | << " but expected " << reg_type; |
Andreas Gampe | b588f4c | 2015-05-26 13:35:39 -0700 | [diff] [blame] | 3519 | return nullptr; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3520 | } |
Andreas Gampe | da9badb | 2015-06-05 20:22:12 -0700 | [diff] [blame] | 3521 | } else { |
| 3522 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
| 3523 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3524 | // the compiler. |
| 3525 | if (have_pending_hard_failure_) { |
| 3526 | return nullptr; |
| 3527 | } |
| 3528 | } else if (reg_type.IsLongOrDoubleTypes()) { |
| 3529 | // Check that registers are consecutive (for non-range invokes). Invokes are the only |
| 3530 | // instructions not specifying register pairs by the first component, but require them |
| 3531 | // nonetheless. Only check when there's an actual register in the parameters. If there's |
| 3532 | // none, this will fail below. |
| 3533 | if (!is_range && sig_registers + 1 < expected_args) { |
| 3534 | uint32_t second_reg = arg[sig_registers + 1]; |
| 3535 | if (second_reg != get_reg + 1) { |
| 3536 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter " |
| 3537 | "at index " << sig_registers << " is not a pair: " << get_reg << " + " |
| 3538 | << second_reg << "."; |
| 3539 | return nullptr; |
| 3540 | } |
| 3541 | } |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 3545 | } |
| 3546 | if (expected_args != sig_registers) { |
| 3547 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
| 3548 | " arguments, found " << sig_registers; |
| 3549 | return nullptr; |
| 3550 | } |
| 3551 | return res_method; |
| 3552 | } |
| 3553 | |
| 3554 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 3555 | MethodType method_type, |
| 3556 | bool is_range) { |
| 3557 | // As the method may not have been resolved, make this static check against what we expect. |
| 3558 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 3559 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
| 3560 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3561 | DexFileParameterIterator it(*dex_file_, |
| 3562 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
| 3563 | VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range, |
| 3564 | nullptr); |
| 3565 | } |
| 3566 | |
| 3567 | class MethodParamListDescriptorIterator { |
| 3568 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3569 | explicit MethodParamListDescriptorIterator(ArtMethod* res_method) : |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3570 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 3571 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 3572 | } |
| 3573 | |
| 3574 | bool HasNext() { |
| 3575 | return pos_ < params_size_; |
| 3576 | } |
| 3577 | |
| 3578 | void Next() { |
| 3579 | ++pos_; |
| 3580 | } |
| 3581 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 3582 | const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3583 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 3584 | } |
| 3585 | |
| 3586 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3587 | ArtMethod* res_method_; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3588 | size_t pos_; |
| 3589 | const DexFile::TypeList* params_; |
| 3590 | const size_t params_size_; |
| 3591 | }; |
| 3592 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3593 | ArtMethod* MethodVerifier::VerifyInvocationArgs( |
| 3594 | const Instruction* inst, MethodType method_type, bool is_range, bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3595 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3596 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3597 | 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] | 3598 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3599 | ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3600 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3601 | // Check what we can statically. |
| 3602 | if (!have_pending_hard_failure_) { |
| 3603 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 3604 | } |
| 3605 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3608 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3609 | // has a vtable entry for the target method. |
| 3610 | if (is_super) { |
| 3611 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3612 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3613 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3614 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3615 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3616 | << " to super " << PrettyMethod(res_method); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3617 | return nullptr; |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3618 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3619 | mirror::Class* super_klass = super.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3620 | if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3621 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3622 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3623 | << " to super " << super |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3624 | << "." << res_method->GetName() |
| 3625 | << res_method->GetSignature(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3626 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3627 | } |
| 3628 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3629 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3630 | // Process the target method's signature. This signature may or may not |
| 3631 | MethodParamListDescriptorIterator it(res_method); |
| 3632 | return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type, |
| 3633 | is_range, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3634 | } |
| 3635 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3636 | ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line, |
| 3637 | bool is_range, bool allow_failure) { |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3638 | if (is_range) { |
| 3639 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3640 | } else { |
| 3641 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 3642 | } |
| 3643 | 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] | 3644 | if (!actual_arg_type.HasClass()) { |
| 3645 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 3646 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3647 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3648 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3649 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3650 | if (klass->IsInterface()) { |
| 3651 | // Derive Object.class from Class.class.getSuperclass(). |
| 3652 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3653 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3654 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3655 | return nullptr; |
| 3656 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3657 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3658 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3659 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3660 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3661 | if (!dispatch_class->HasVTable()) { |
| 3662 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 3663 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3664 | return nullptr; |
| 3665 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3666 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3667 | auto* cl = Runtime::Current()->GetClassLinker(); |
| 3668 | auto pointer_size = cl->GetImagePointerSize(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3669 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 3670 | FailOrAbort(this, allow_failure, |
| 3671 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 3672 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3673 | return nullptr; |
| 3674 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3675 | ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3676 | if (self_->IsExceptionPending()) { |
| 3677 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 3678 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3679 | return nullptr; |
| 3680 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3681 | return res_method; |
| 3682 | } |
| 3683 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3684 | ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3685 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 3686 | << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_; |
| 3687 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3688 | ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3689 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3690 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3691 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3692 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3693 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 3694 | work_insn_idx_)) { |
| 3695 | return nullptr; |
| 3696 | } |
| 3697 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 3698 | work_insn_idx_)) { |
| 3699 | return nullptr; |
| 3700 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3701 | |
| 3702 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3703 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3704 | // definition (which doesn't have register count values). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3705 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3706 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3707 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3708 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3709 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3710 | /* caught by static verifier */ |
| 3711 | DCHECK(is_range || expected_args <= 5); |
| 3712 | if (expected_args > code_item_->outs_size_) { |
| 3713 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3714 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3715 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | /* |
| 3719 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3720 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3721 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3722 | */ |
| 3723 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3724 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3725 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3726 | } |
| 3727 | if (!actual_arg_type.IsZero()) { |
| 3728 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3729 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3730 | const RegType& res_method_class = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 3731 | FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3732 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3733 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3734 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3735 | << "' not instance of '" << res_method_class << "'"; |
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 | } |
| 3739 | /* |
| 3740 | * Process the target method's signature. This signature may or may not |
| 3741 | * have been verified, so we can't assume it's properly formed. |
| 3742 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3743 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3744 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3745 | uint32_t arg[5]; |
| 3746 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3747 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3748 | } |
| 3749 | size_t actual_args = 1; |
| 3750 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3751 | if (actual_args >= expected_args) { |
| 3752 | 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] | 3753 | << "'. Expected " << expected_args |
| 3754 | << " arguments, processing argument " << actual_args |
| 3755 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3756 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3757 | } |
| 3758 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3759 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3760 | if (descriptor == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3761 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3762 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3763 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3764 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3765 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3766 | 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] | 3767 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3768 | return res_method; |
| 3769 | } |
| 3770 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3771 | } |
| 3772 | if (actual_args != expected_args) { |
| 3773 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3774 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3775 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3776 | } else { |
| 3777 | return res_method; |
| 3778 | } |
| 3779 | } |
| 3780 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3781 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3782 | uint32_t type_idx; |
| 3783 | if (!is_filled) { |
| 3784 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3785 | type_idx = inst->VRegC_22c(); |
| 3786 | } else if (!is_range) { |
| 3787 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3788 | type_idx = inst->VRegB_35c(); |
| 3789 | } else { |
| 3790 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3791 | type_idx = inst->VRegB_3rc(); |
| 3792 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3793 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3794 | if (res_type.IsConflict()) { // bad class |
| 3795 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3796 | } else { |
| 3797 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3798 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3799 | 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] | 3800 | } else if (!is_filled) { |
| 3801 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3802 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3803 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3804 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3805 | work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3806 | } else { |
| 3807 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3808 | // 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] | 3809 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3810 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3811 | uint32_t arg[5]; |
| 3812 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3813 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3814 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3815 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3816 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3817 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 3818 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3819 | return; |
| 3820 | } |
| 3821 | } |
| 3822 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3823 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3824 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3825 | } |
| 3826 | } |
| 3827 | } |
| 3828 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3829 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3830 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3831 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3832 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3833 | 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] | 3834 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3835 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3836 | if (array_type.IsZero()) { |
Nicolas Geoffray | 4824c27 | 2015-06-24 15:53:03 +0100 | [diff] [blame] | 3837 | have_pending_runtime_throw_failure_ = true; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3838 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3839 | // instruction type. TODO: have a proper notion of bottom here. |
| 3840 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3841 | // Reference or category 1 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3842 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3843 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3844 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3845 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 3846 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3847 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3848 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3849 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3850 | 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] | 3851 | } else { |
| 3852 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3853 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3854 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3855 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3856 | << " source for aget-object"; |
| 3857 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3858 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3859 | << " source for category 1 aget"; |
| 3860 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3861 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3862 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3863 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3864 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3865 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3866 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3867 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3868 | // doubles. |
| 3869 | if (!component_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3870 | work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3871 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3872 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3873 | component_type.HighHalf(®_types_)); |
| 3874 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | } |
| 3878 | } |
| 3879 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3880 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3881 | const uint32_t vregA) { |
| 3882 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3883 | bool instruction_compatible; |
| 3884 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3885 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3886 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3887 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3888 | value_compatible = value_type.IsIntegralTypes(); |
| 3889 | } else if (target_type.IsFloat()) { |
| 3890 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3891 | value_compatible = value_type.IsFloatTypes(); |
| 3892 | } else if (target_type.IsLong()) { |
| 3893 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3894 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3895 | // as target_type depends on the resolved type of the field. |
| 3896 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3897 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3898 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 3899 | } else { |
| 3900 | value_compatible = false; |
| 3901 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3902 | } else if (target_type.IsDouble()) { |
| 3903 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3904 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3905 | // as target_type depends on the resolved type of the field. |
| 3906 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3907 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3908 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 3909 | } else { |
| 3910 | value_compatible = false; |
| 3911 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3912 | } else { |
| 3913 | instruction_compatible = false; // reference with primitive store |
| 3914 | value_compatible = false; // unused |
| 3915 | } |
| 3916 | if (!instruction_compatible) { |
| 3917 | // This is a global failure rather than a class change failure as the instructions and |
| 3918 | // the descriptors for the type should have been consistent within the same file at |
| 3919 | // compile time. |
| 3920 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3921 | << "' but expected type '" << target_type << "'"; |
| 3922 | return; |
| 3923 | } |
| 3924 | if (!value_compatible) { |
| 3925 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3926 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3927 | return; |
| 3928 | } |
| 3929 | } |
| 3930 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3931 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3932 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3933 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3934 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3935 | 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] | 3936 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3937 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3938 | if (array_type.IsZero()) { |
Nicolas Geoffray | 66389fb | 2015-06-19 10:35:42 +0100 | [diff] [blame] | 3939 | // Null array type; this code path will fail at runtime. |
| 3940 | // Still check that the given value matches the instruction's type. |
Andreas Gampe | 4bf4c78 | 2015-08-14 14:07:43 -0700 | [diff] [blame^] | 3941 | // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed |
| 3942 | // and fits multiple register types. |
| 3943 | const RegType* modified_reg_type = &insn_type; |
| 3944 | if ((modified_reg_type == ®_types_.Integer()) || |
| 3945 | (modified_reg_type == ®_types_.LongLo())) { |
| 3946 | // May be integer or float | long or double. Overwrite insn_type accordingly. |
| 3947 | const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x()); |
| 3948 | if (modified_reg_type == ®_types_.Integer()) { |
| 3949 | if (&value_type == ®_types_.Float()) { |
| 3950 | modified_reg_type = &value_type; |
| 3951 | } |
| 3952 | } else { |
| 3953 | if (&value_type == ®_types_.DoubleLo()) { |
| 3954 | modified_reg_type = &value_type; |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3959 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3960 | 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] | 3961 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3962 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3963 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3964 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3965 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3966 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3967 | if (!component_type.IsReferenceTypes()) { |
| 3968 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3969 | << " source for aput-object"; |
| 3970 | } else { |
| 3971 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3972 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3973 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3974 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3975 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | } |
| 3979 | } |
| 3980 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3981 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3982 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3983 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3984 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3985 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3986 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3987 | field_idx, dex_file_->GetFieldName(field_id), |
| 3988 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3989 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3990 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3991 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3992 | 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] | 3993 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3994 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3995 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 3996 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3997 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3998 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3999 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4000 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4001 | DCHECK(self_->IsExceptionPending()); |
| 4002 | self_->ClearException(); |
| 4003 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4004 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4005 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4006 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4007 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4008 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4009 | } else if (!field->IsStatic()) { |
| 4010 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4011 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4012 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4013 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4014 | } |
| 4015 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4016 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4017 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4018 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4019 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4020 | if (klass_type.IsConflict()) { |
| 4021 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 4022 | field_idx, dex_file_->GetFieldName(field_id), |
| 4023 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4024 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4025 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4026 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4027 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 4028 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4029 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4030 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 4031 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4032 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 4033 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4034 | << dex_file_->GetFieldName(field_id) << ") in " |
| 4035 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4036 | DCHECK(self_->IsExceptionPending()); |
| 4037 | self_->ClearException(); |
| 4038 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4039 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 4040 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4041 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4042 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4043 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4044 | } else if (field->IsStatic()) { |
| 4045 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 4046 | << " to not be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4047 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4048 | } else if (obj_type.IsZero()) { |
| 4049 | // Cannot infer and check type, however, access will cause null pointer exception |
| 4050 | return field; |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 4051 | } else if (!obj_type.IsReferenceTypes()) { |
| 4052 | // Trying to read a field from something that isn't a reference |
| 4053 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 4054 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4055 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 4056 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4057 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4058 | const RegType& field_klass = |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4059 | FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
| 4060 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4061 | if (obj_type.IsUninitializedTypes() && |
| 4062 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 4063 | !field_klass.Equals(GetDeclaringClass()))) { |
| 4064 | // Field accesses through uninitialized references are only allowable for constructors where |
| 4065 | // the field is declared in this class |
| 4066 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4067 | << " of a not fully initialized object within the context" |
| 4068 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4069 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4070 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 4071 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 4072 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 4073 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 4074 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 4075 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4076 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4077 | } else { |
| 4078 | return field; |
| 4079 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4083 | template <MethodVerifier::FieldAccessType kAccType> |
| 4084 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4085 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4086 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4087 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4088 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4089 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4090 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4091 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 4092 | field = GetInstanceField(object_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4093 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 4094 | return; |
| 4095 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 4096 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4097 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4098 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4099 | if (kAccType == FieldAccessType::kAccPut) { |
| 4100 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4101 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 4102 | << " from other class " << GetDeclaringClass(); |
| 4103 | return; |
| 4104 | } |
| 4105 | } |
| 4106 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4107 | mirror::Class* field_type_class = |
| 4108 | can_load_classes_ ? field->GetType<true>() : field->GetType<false>(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4109 | if (field_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4110 | field_type = &FromClass(field->GetTypeDescriptor(), field_type_class, |
| 4111 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4112 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4113 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4114 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4115 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4116 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4117 | if (field_type == nullptr) { |
| 4118 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 4119 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4120 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4121 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 4122 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 4123 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4124 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4125 | "Unexpected third access type"); |
| 4126 | if (kAccType == FieldAccessType::kAccPut) { |
| 4127 | // sput or iput. |
| 4128 | if (is_primitive) { |
| 4129 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4130 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4131 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4132 | // If the field type is not a reference, this is a global failure rather than |
| 4133 | // a class change failure as the instructions and the descriptors for the type |
| 4134 | // should have been consistent within the same file at compile time. |
| 4135 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4136 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4137 | Fail(error) << "expected field " << PrettyField(field) |
| 4138 | << " to be compatible with type '" << insn_type |
| 4139 | << "' but found type '" << *field_type |
| 4140 | << "' in put-object"; |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4141 | return; |
| 4142 | } |
| 4143 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4144 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4145 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4146 | // sget or iget. |
| 4147 | if (is_primitive) { |
| 4148 | if (field_type->Equals(insn_type) || |
| 4149 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 4150 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 4151 | // expected that read is of the correct primitive type or that int reads are reading |
| 4152 | // floats or long reads are reading doubles |
| 4153 | } else { |
| 4154 | // This is a global failure rather than a class change failure as the instructions and |
| 4155 | // the descriptors for the type should have been consistent within the same file at |
| 4156 | // compile time |
| 4157 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4158 | << " to be of type '" << insn_type |
| 4159 | << "' but found type '" << *field_type << "' in get"; |
| 4160 | return; |
| 4161 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 4162 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4163 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Vladimir Marko | 414000e | 2015-06-23 17:45:21 +0100 | [diff] [blame] | 4164 | // If the field type is not a reference, this is a global failure rather than |
| 4165 | // a class change failure as the instructions and the descriptors for the type |
| 4166 | // should have been consistent within the same file at compile time. |
| 4167 | VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT |
| 4168 | : VERIFY_ERROR_BAD_CLASS_HARD; |
| 4169 | Fail(error) << "expected field " << PrettyField(field) |
| 4170 | << " to be compatible with type '" << insn_type |
| 4171 | << "' but found type '" << *field_type |
| 4172 | << "' in get-object"; |
Andreas Gampe | 890da29 | 2015-07-06 17:20:18 -0700 | [diff] [blame] | 4173 | if (error != VERIFY_ERROR_BAD_CLASS_HARD) { |
| 4174 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 4175 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4176 | return; |
| 4177 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4178 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4179 | if (!field_type->IsLowHalf()) { |
| 4180 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 4181 | } else { |
| 4182 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 4183 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4184 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4185 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4186 | } |
| 4187 | } |
| 4188 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4189 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4190 | RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4191 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4192 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4193 | if (!object_type.HasClass()) { |
| 4194 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 4195 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4196 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4197 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4198 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4199 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 4200 | if (f == nullptr) { |
| 4201 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
| 4202 | << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; |
| 4203 | } |
| 4204 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4205 | } |
| 4206 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4207 | template <MethodVerifier::FieldAccessType kAccType> |
| 4208 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4209 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 4210 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4211 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4212 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4213 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4214 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 4215 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4216 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4217 | |
| 4218 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 4219 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4220 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4221 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4222 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4223 | return; |
| 4224 | } |
| 4225 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4226 | |
| 4227 | // Get the field type. |
| 4228 | const RegType* field_type; |
| 4229 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4230 | mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() : |
| 4231 | field->GetType<false>(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4232 | |
| 4233 | if (field_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4234 | field_type = &FromClass(field->GetTypeDescriptor(), field_type_class, |
| 4235 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4236 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4237 | Thread* self = Thread::Current(); |
| 4238 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 4239 | self->ClearException(); |
| 4240 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 4241 | field->GetTypeDescriptor(), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4242 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4243 | if (field_type == nullptr) { |
| 4244 | 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] | 4245 | return; |
| 4246 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | const uint32_t vregA = inst->VRegA_22c(); |
| 4250 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4251 | "Unexpected third access type"); |
| 4252 | if (kAccType == FieldAccessType::kAccPut) { |
| 4253 | if (is_primitive) { |
| 4254 | // Primitive field assignability rules are weaker than regular assignability rules |
| 4255 | bool instruction_compatible; |
| 4256 | bool value_compatible; |
| 4257 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 4258 | if (field_type->IsIntegralTypes()) { |
| 4259 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 4260 | value_compatible = value_type.IsIntegralTypes(); |
| 4261 | } else if (field_type->IsFloat()) { |
| 4262 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 4263 | value_compatible = value_type.IsFloatTypes(); |
| 4264 | } else if (field_type->IsLong()) { |
| 4265 | instruction_compatible = insn_type.IsLong(); |
| 4266 | value_compatible = value_type.IsLongTypes(); |
| 4267 | } else if (field_type->IsDouble()) { |
| 4268 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 4269 | value_compatible = value_type.IsDoubleTypes(); |
| 4270 | } else { |
| 4271 | instruction_compatible = false; // reference field with primitive store |
| 4272 | value_compatible = false; // unused |
| 4273 | } |
| 4274 | if (!instruction_compatible) { |
| 4275 | // This is a global failure rather than a class change failure as the instructions and |
| 4276 | // the descriptors for the type should have been consistent within the same file at |
| 4277 | // compile time |
| 4278 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4279 | << " to be of type '" << insn_type |
| 4280 | << "' but found type '" << *field_type |
| 4281 | << "' in put"; |
| 4282 | return; |
| 4283 | } |
| 4284 | if (!value_compatible) { |
| 4285 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4286 | << " of type " << value_type |
| 4287 | << " but expected " << *field_type |
| 4288 | << " for store to " << PrettyField(field) << " in put"; |
| 4289 | return; |
| 4290 | } |
| 4291 | } else { |
| 4292 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4293 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4294 | << " to be compatible with type '" << insn_type |
| 4295 | << "' but found type '" << *field_type |
| 4296 | << "' in put-object"; |
| 4297 | return; |
| 4298 | } |
| 4299 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 4300 | } |
| 4301 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4302 | if (is_primitive) { |
| 4303 | if (field_type->Equals(insn_type) || |
| 4304 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 4305 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 4306 | // expected that read is of the correct primitive type or that int reads are reading |
| 4307 | // floats or long reads are reading doubles |
| 4308 | } else { |
| 4309 | // This is a global failure rather than a class change failure as the instructions and |
| 4310 | // the descriptors for the type should have been consistent within the same file at |
| 4311 | // compile time |
| 4312 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4313 | << " to be of type '" << insn_type |
| 4314 | << "' but found type '" << *field_type << "' in Get"; |
| 4315 | return; |
| 4316 | } |
| 4317 | } else { |
| 4318 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4319 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4320 | << " to be compatible with type '" << insn_type |
| 4321 | << "' but found type '" << *field_type |
| 4322 | << "' in get-object"; |
| 4323 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 4324 | return; |
| 4325 | } |
| 4326 | } |
| 4327 | if (!field_type->IsLowHalf()) { |
| 4328 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 4329 | } else { |
| 4330 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4331 | } |
| 4332 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4333 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4334 | } |
| 4335 | } |
| 4336 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4337 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4338 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4339 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4340 | return false; |
| 4341 | } |
| 4342 | return true; |
| 4343 | } |
| 4344 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 4345 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 4346 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 4347 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 4348 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 4349 | return false; |
| 4350 | } |
| 4351 | return true; |
| 4352 | } |
| 4353 | |
| 4354 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 4355 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 4356 | } |
| 4357 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4358 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 4359 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4360 | bool changed = true; |
| 4361 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 4362 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4363 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4364 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4365 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4366 | * 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] | 4367 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4368 | if (!insn_flags_[next_insn].IsReturn()) { |
| 4369 | target_line->CopyFromLine(merge_line); |
| 4370 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4371 | // Verify that the monitor stack is empty on return. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4372 | if (!merge_line->VerifyMonitorStackEmpty(this)) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4373 | return false; |
| 4374 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4375 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4376 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 4377 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 4378 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 4379 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Andreas Gampe | f10b6e1 | 2015-08-12 10:48:12 -0700 | [diff] [blame] | 4380 | // Explicitly copy the this-initialized flag from the merge-line, as we didn't copy its |
| 4381 | // state. Must be done before SafelyMarkAllRegistersAsConflicts as that will do the |
| 4382 | // super-constructor-call checking. |
| 4383 | target_line->CopyThisInitialized(*merge_line); |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 4384 | SafelyMarkAllRegistersAsConflicts(this, target_line); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4385 | } else { |
| 4386 | target_line->CopyFromLine(merge_line); |
| 4387 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4388 | target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4389 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4390 | target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4394 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4395 | std::unique_ptr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 4396 | RegisterLine::Create(target_line->NumRegs(), this) : |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4397 | nullptr); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4398 | if (gDebugVerify) { |
| 4399 | copy->CopyFromLine(target_line); |
| 4400 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4401 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4402 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4403 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4404 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 4405 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4406 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4407 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4408 | << copy->Dump(this) << " MERGE\n" |
| 4409 | << merge_line->Dump(this) << " ==\n" |
| 4410 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4411 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4412 | if (update_merge_line && changed) { |
| 4413 | merge_line->CopyFromLine(target_line); |
| 4414 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4415 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4416 | if (changed) { |
| 4417 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4418 | } |
| 4419 | return true; |
| 4420 | } |
| 4421 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4422 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4423 | return &insn_flags_[work_insn_idx_]; |
| 4424 | } |
| 4425 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4426 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4427 | if (return_type_ == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4428 | if (mirror_method_ != nullptr) { |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4429 | mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4430 | if (return_type_class != nullptr) { |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4431 | return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(), |
| 4432 | return_type_class, |
| 4433 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4434 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4435 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4436 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4437 | } |
| 4438 | } |
| 4439 | if (return_type_ == nullptr) { |
| 4440 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4441 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 4442 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 4443 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4444 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4445 | } |
| 4446 | } |
| 4447 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4450 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4451 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4452 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4453 | const char* descriptor |
| 4454 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4455 | if (mirror_method_ != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4456 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4457 | declaring_class_ = &FromClass(descriptor, klass, |
| 4458 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4459 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4460 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4461 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4462 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4463 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4466 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4467 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 4468 | 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] | 4469 | std::vector<int32_t> result; |
| 4470 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4471 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4472 | if (type.IsConstant()) { |
| 4473 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4474 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4475 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4476 | } else if (type.IsConstantLo()) { |
| 4477 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4478 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4479 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4480 | } else if (type.IsConstantHi()) { |
| 4481 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4482 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4483 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4484 | } else if (type.IsIntegralTypes()) { |
| 4485 | result.push_back(kIntVReg); |
| 4486 | result.push_back(0); |
| 4487 | } else if (type.IsFloat()) { |
| 4488 | result.push_back(kFloatVReg); |
| 4489 | result.push_back(0); |
| 4490 | } else if (type.IsLong()) { |
| 4491 | result.push_back(kLongLoVReg); |
| 4492 | result.push_back(0); |
| 4493 | result.push_back(kLongHiVReg); |
| 4494 | result.push_back(0); |
| 4495 | ++i; |
| 4496 | } else if (type.IsDouble()) { |
| 4497 | result.push_back(kDoubleLoVReg); |
| 4498 | result.push_back(0); |
| 4499 | result.push_back(kDoubleHiVReg); |
| 4500 | result.push_back(0); |
| 4501 | ++i; |
| 4502 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 4503 | result.push_back(kUndefined); |
| 4504 | result.push_back(0); |
| 4505 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4506 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4507 | result.push_back(kReferenceVReg); |
| 4508 | result.push_back(0); |
| 4509 | } |
| 4510 | } |
| 4511 | return result; |
| 4512 | } |
| 4513 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4514 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 4515 | if (precise) { |
| 4516 | // Precise constant type. |
| 4517 | return reg_types_.FromCat1Const(value, true); |
| 4518 | } else { |
| 4519 | // Imprecise constant type. |
| 4520 | if (value < -32768) { |
| 4521 | return reg_types_.IntConstant(); |
| 4522 | } else if (value < -128) { |
| 4523 | return reg_types_.ShortConstant(); |
| 4524 | } else if (value < 0) { |
| 4525 | return reg_types_.ByteConstant(); |
| 4526 | } else if (value == 0) { |
| 4527 | return reg_types_.Zero(); |
| 4528 | } else if (value == 1) { |
| 4529 | return reg_types_.One(); |
| 4530 | } else if (value < 128) { |
| 4531 | return reg_types_.PosByteConstant(); |
| 4532 | } else if (value < 32768) { |
| 4533 | return reg_types_.PosShortConstant(); |
| 4534 | } else if (value < 65536) { |
| 4535 | return reg_types_.CharConstant(); |
| 4536 | } else { |
| 4537 | return reg_types_.IntConstant(); |
| 4538 | } |
| 4539 | } |
| 4540 | } |
| 4541 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4542 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4543 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4544 | } |
| 4545 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4546 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4547 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4548 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4549 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4550 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 4551 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4554 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 4555 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4556 | } |
| 4557 | |
Andreas Gampe | f23f33d | 2015-06-23 14:18:17 -0700 | [diff] [blame] | 4558 | const RegType& MethodVerifier::FromClass(const char* descriptor, |
| 4559 | mirror::Class* klass, |
| 4560 | bool precise) { |
| 4561 | DCHECK(klass != nullptr); |
| 4562 | if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) { |
| 4563 | Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for " |
| 4564 | << "non-instantiable klass " << descriptor; |
| 4565 | precise = false; |
| 4566 | } |
| 4567 | return reg_types_.FromClass(descriptor, klass, precise); |
| 4568 | } |
| 4569 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4570 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4571 | } // namespace art |