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" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 23 | #include "base/mutex-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 25 | #include "compiler_callbacks.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 27 | #include "dex_instruction-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 28 | #include "dex_instruction_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 29 | #include "dex_instruction_visitor.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 30 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 31 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 32 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 33 | #include "leb128.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 34 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class.h" |
| 36 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 37 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "mirror/object-inl.h" |
| 39 | #include "mirror/object_array-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 40 | #include "reg_type-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 41 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 42 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 43 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 44 | #include "handle_scope-inl.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 45 | #include "verifier/dex_gc_map.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 46 | |
| 47 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 48 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 49 | |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 50 | static constexpr bool kTimeVerifyMethod = !kIsDebugBuild; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 51 | static constexpr bool gDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 52 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 53 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 54 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 55 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 56 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 57 | DCHECK_GT(insns_size, 0U); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 58 | register_lines_.reset(new RegisterLine*[insns_size]()); |
| 59 | size_ = insns_size; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 60 | for (uint32_t i = 0; i < insns_size; i++) { |
| 61 | bool interesting = false; |
| 62 | switch (mode) { |
| 63 | case kTrackRegsAll: |
| 64 | interesting = flags[i].IsOpcode(); |
| 65 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 66 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 67 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 68 | break; |
| 69 | case kTrackRegsBranches: |
| 70 | interesting = flags[i].IsBranchTarget(); |
| 71 | break; |
| 72 | default: |
| 73 | break; |
| 74 | } |
| 75 | if (interesting) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 76 | register_lines_[i] = RegisterLine::Create(registers_size, verifier); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | PcToRegisterLineTable::~PcToRegisterLineTable() { |
| 82 | for (size_t i = 0; i < size_; i++) { |
| 83 | delete register_lines_[i]; |
| 84 | if (kIsDebugBuild) { |
| 85 | register_lines_[i] = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 90 | // Note: returns true on failure. |
| 91 | ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition, |
| 92 | const char* error_msg, uint32_t work_insn_idx) { |
| 93 | if (kIsDebugBuild) { |
| 94 | // In a debug build, abort if the error condition is wrong. |
| 95 | DCHECK(condition) << error_msg << work_insn_idx; |
| 96 | } else { |
| 97 | // In a non-debug build, just fail the class. |
| 98 | if (!condition) { |
| 99 | verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx; |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return false; |
| 105 | } |
| 106 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 107 | static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) { |
| 108 | if (verifier->IsConstructor()) { |
| 109 | // Before we mark all regs as conflicts, check that we don't have an uninitialized this. |
| 110 | reg_line->CheckConstructorReturn(verifier); |
| 111 | } |
| 112 | reg_line->MarkAllRegistersAsConflicts(verifier); |
| 113 | } |
| 114 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod( |
| 116 | mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) { |
| 117 | Thread* self = Thread::Current(); |
| 118 | StackHandleScope<3> hs(self); |
| 119 | mirror::Class* klass = method->GetDeclaringClass(); |
| 120 | auto h_dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 121 | auto h_class_loader(hs.NewHandle(klass->GetClassLoader())); |
| 122 | auto h_method = hs.NewHandle(method); |
| 123 | return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache, |
| 124 | h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method, |
| 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())); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 163 | return VerifyClass(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] | 164 | } |
| 165 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 166 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self, |
| 167 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 168 | Handle<mirror::DexCache> dex_cache, |
| 169 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 170 | const DexFile::ClassDef* class_def, |
| 171 | bool allow_soft_failures, |
| 172 | std::string* error) { |
| 173 | DCHECK(class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 174 | const uint8_t* class_data = dex_file->GetClassData(*class_def); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 175 | if (class_data == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 176 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 177 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 178 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 179 | ClassDataItemIterator it(*dex_file, class_data); |
| 180 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 181 | it.Next(); |
| 182 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 183 | size_t error_count = 0; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 184 | bool hard_fail = false; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 185 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 186 | int64_t previous_direct_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 187 | while (it.HasNextDirectMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 188 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 189 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 190 | if (method_idx == previous_direct_method_idx) { |
| 191 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 192 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 193 | it.Next(); |
| 194 | continue; |
| 195 | } |
| 196 | previous_direct_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 197 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 198 | mirror::ArtMethod* method = |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 199 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, |
| 200 | NullHandle<mirror::ArtMethod>(), type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 201 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 202 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 203 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 204 | self->ClearException(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 205 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 206 | StackHandleScope<1> hs(self); |
| 207 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(method)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 208 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 209 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 210 | dex_file, |
| 211 | dex_cache, |
| 212 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 213 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 214 | it.GetMethodCodeItem(), |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 215 | h_method, |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 216 | it.GetMethodAccessFlags(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 217 | allow_soft_failures, |
| 218 | false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 219 | if (result != kNoFailure) { |
| 220 | if (result == kHardFailure) { |
| 221 | hard_fail = true; |
| 222 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 223 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 224 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 225 | *error = "Verifier rejected class "; |
| 226 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 227 | *error += " due to bad method "; |
| 228 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 229 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 230 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 231 | } |
| 232 | it.Next(); |
| 233 | } |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 234 | int64_t previous_virtual_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 235 | while (it.HasNextVirtualMethod()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 236 | self->AllowThreadSuspension(); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 237 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 238 | if (method_idx == previous_virtual_method_idx) { |
| 239 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 240 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 241 | it.Next(); |
| 242 | continue; |
| 243 | } |
| 244 | previous_virtual_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 245 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 246 | mirror::ArtMethod* method = |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 247 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, |
| 248 | NullHandle<mirror::ArtMethod>(), type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 249 | if (method == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 250 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 251 | // We couldn't resolve the method, but continue regardless. |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 252 | self->ClearException(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 253 | } |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 254 | StackHandleScope<1> hs(self); |
| 255 | Handle<mirror::ArtMethod> h_method(hs.NewHandle(method)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 256 | MethodVerifier::FailureKind result = VerifyMethod(self, |
| 257 | method_idx, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 258 | dex_file, |
| 259 | dex_cache, |
| 260 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 261 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 262 | it.GetMethodCodeItem(), |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 263 | h_method, |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 264 | it.GetMethodAccessFlags(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 265 | allow_soft_failures, |
| 266 | false); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 267 | if (result != kNoFailure) { |
| 268 | if (result == kHardFailure) { |
| 269 | hard_fail = true; |
| 270 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 271 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 272 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 273 | *error = "Verifier rejected class "; |
| 274 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 275 | *error += " due to bad method "; |
| 276 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 277 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 278 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 279 | } |
| 280 | it.Next(); |
| 281 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 282 | if (error_count == 0) { |
| 283 | return kNoFailure; |
| 284 | } else { |
| 285 | return hard_fail ? kHardFailure : kSoftFailure; |
| 286 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 289 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 290 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 291 | Handle<mirror::DexCache> dex_cache, |
| 292 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 293 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 294 | const DexFile::CodeItem* code_item, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 295 | Handle<mirror::ArtMethod> method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 296 | uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 297 | bool allow_soft_failures, |
| 298 | bool need_precise_constants) { |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 299 | MethodVerifier::FailureKind result = kNoFailure; |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 300 | uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 301 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 302 | 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] | 303 | method_idx, method, method_access_flags, true, allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 304 | need_precise_constants, true); |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 305 | if (verifier.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 306 | // Verification completed, however failures may be pending that didn't cause the verification |
| 307 | // to hard fail. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 308 | CHECK(!verifier.have_pending_hard_failure_); |
| 309 | if (verifier.failures_.size() != 0) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 310 | if (VLOG_IS_ON(verifier)) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 311 | verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 312 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 313 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 314 | result = kSoftFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 315 | } |
| 316 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 317 | // Bad method data. |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 318 | CHECK_NE(verifier.failures_.size(), 0U); |
| 319 | CHECK(verifier.have_pending_hard_failure_); |
| 320 | verifier.DumpFailures(LOG(INFO) << "Verification error in " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 321 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 322 | if (gDebugVerify) { |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 323 | std::cout << "\n" << verifier.info_messages_.str(); |
| 324 | verifier.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 325 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 326 | result = kHardFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 327 | } |
Mathieu Chartier | 8e219ae | 2014-08-19 14:29:46 -0700 | [diff] [blame] | 328 | if (kTimeVerifyMethod) { |
| 329 | uint64_t duration_ns = NanoTime() - start_ns; |
| 330 | if (duration_ns > MsToNs(100)) { |
| 331 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
| 332 | << " took " << PrettyDuration(duration_ns); |
| 333 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 334 | } |
| 335 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 338 | MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 339 | const DexFile* dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 340 | Handle<mirror::DexCache> dex_cache, |
| 341 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 342 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 343 | const DexFile::CodeItem* code_item, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 344 | Handle<mirror::ArtMethod> method, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 345 | uint32_t method_access_flags) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 346 | MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader, |
| 347 | class_def, code_item, dex_method_idx, method, |
| 348 | method_access_flags, true, true, true, true); |
| 349 | verifier->Verify(); |
| 350 | verifier->DumpFailures(os); |
| 351 | os << verifier->info_messages_.str(); |
Andreas Gampe | 5cbcde2 | 2014-09-16 14:59:49 -0700 | [diff] [blame] | 352 | // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized |
| 353 | // and querying any info is dangerous/can abort. |
| 354 | if (verifier->have_pending_hard_failure_) { |
| 355 | delete verifier; |
| 356 | return nullptr; |
| 357 | } else { |
| 358 | verifier->Dump(os); |
| 359 | return verifier; |
| 360 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 363 | MethodVerifier::MethodVerifier(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 364 | const DexFile* dex_file, Handle<mirror::DexCache> dex_cache, |
| 365 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 366 | const DexFile::ClassDef* class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 367 | const DexFile::CodeItem* code_item, uint32_t dex_method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 368 | Handle<mirror::ArtMethod> method, uint32_t method_access_flags, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 369 | bool can_load_classes, bool allow_soft_failures, |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 370 | bool need_precise_constants, bool verify_to_dump, |
| 371 | bool allow_thread_suspension) |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 372 | : self_(self), |
| 373 | reg_types_(can_load_classes), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 374 | work_insn_idx_(-1), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 375 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 376 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 377 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 378 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 379 | dex_file_(dex_file), |
| 380 | dex_cache_(dex_cache), |
| 381 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 382 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 383 | code_item_(code_item), |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 384 | declaring_class_(nullptr), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 385 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 386 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 387 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 388 | have_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 389 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 390 | monitor_enter_count_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 391 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 392 | allow_soft_failures_(allow_soft_failures), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 393 | need_precise_constants_(need_precise_constants), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 394 | has_check_casts_(false), |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 395 | has_virtual_or_interface_invokes_(false), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 396 | verify_to_dump_(verify_to_dump), |
| 397 | allow_thread_suspension_(allow_thread_suspension) { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 398 | self->PushVerifier(this); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 399 | DCHECK(class_def != nullptr); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 402 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | d0ad2ee | 2015-03-31 14:59:59 -0700 | [diff] [blame] | 403 | Thread::Current()->PopVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 404 | STLDeleteElements(&failure_messages_); |
| 405 | } |
| 406 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 407 | void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 408 | std::vector<uint32_t>* monitor_enter_dex_pcs) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 409 | Thread* self = Thread::Current(); |
| 410 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 411 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 412 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 413 | Handle<mirror::ArtMethod> method(hs.NewHandle(m)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 414 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 415 | m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 416 | false, true, false, false); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 417 | verifier.interesting_dex_pc_ = dex_pc; |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 418 | verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 419 | verifier.FindLocksAtDexPc(); |
| 420 | } |
| 421 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 422 | static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) { |
| 423 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 424 | |
| 425 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 426 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
| 427 | if (inst->Opcode() == Instruction::MONITOR_ENTER) { |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | dex_pc += inst->SizeInCodeUnits(); |
| 432 | inst = inst->Next(); |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 438 | void MethodVerifier::FindLocksAtDexPc() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 439 | CHECK(monitor_enter_dex_pcs_ != nullptr); |
| 440 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 441 | |
Andreas Gampe | cb3c08f | 2014-09-18 13:16:38 -0700 | [diff] [blame] | 442 | // Quick check whether there are any monitor_enter instructions at all. |
| 443 | if (!HasMonitorEnterInstructions(code_item_)) { |
| 444 | return; |
| 445 | } |
| 446 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 447 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 448 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 449 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 450 | // got what we wanted. |
| 451 | Verify(); |
| 452 | } |
| 453 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 454 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m, |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 455 | uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 456 | Thread* self = Thread::Current(); |
| 457 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 458 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 459 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 460 | Handle<mirror::ArtMethod> method(hs.NewHandle(m)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 461 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 462 | m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 463 | true, true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 464 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 467 | ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 468 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 469 | |
| 470 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 471 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 472 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 473 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 474 | bool success = Verify(); |
| 475 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 476 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 477 | } |
| 478 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 479 | if (register_line == nullptr) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 480 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 481 | } |
| 482 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 483 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 486 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 487 | uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 488 | Thread* self = Thread::Current(); |
| 489 | StackHandleScope<3> hs(self); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 490 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 491 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 492 | Handle<mirror::ArtMethod> method(hs.NewHandle(m)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 493 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 494 | m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 495 | true, true, false, true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 496 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 499 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 500 | CHECK(code_item_ != nullptr); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 501 | |
| 502 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 503 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 504 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 505 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 506 | bool success = Verify(); |
| 507 | if (!success) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 508 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 509 | } |
| 510 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 511 | if (register_line == nullptr) { |
| 512 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 513 | } |
| 514 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 515 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 516 | return GetQuickInvokedMethod(inst, register_line, is_range, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 519 | SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(mirror::ArtMethod* m) { |
| 520 | Thread* self = Thread::Current(); |
| 521 | StackHandleScope<3> hs(self); |
| 522 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache())); |
| 523 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader())); |
| 524 | Handle<mirror::ArtMethod> method(hs.NewHandle(m)); |
| 525 | MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(), |
| 526 | m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(), |
| 527 | true, true, false, true); |
| 528 | return verifier.FindStringInitMap(); |
| 529 | } |
| 530 | |
| 531 | SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() { |
| 532 | Verify(); |
| 533 | return GetStringInitPcRegMap(); |
| 534 | } |
| 535 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 536 | bool MethodVerifier::Verify() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 537 | // 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] | 538 | if (code_item_ == nullptr) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 539 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 540 | 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] | 541 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 542 | } else { |
| 543 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 544 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 545 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 546 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 547 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 548 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 549 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 550 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 551 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 552 | // Allocate and initialize an array to hold instruction data. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 553 | insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 554 | // Run through the instructions and see if the width checks out. |
| 555 | bool result = ComputeWidthsAndCountOps(); |
| 556 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 557 | result = result && ScanTryCatchBlocks(); |
| 558 | // Perform static instruction verification. |
| 559 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 560 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 561 | result = result && VerifyCodeFlow(); |
| 562 | // Compute information for compiler. |
| 563 | if (result && Runtime::Current()->IsCompiler()) { |
| 564 | result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this); |
| 565 | } |
| 566 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 569 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 570 | switch (error) { |
| 571 | case VERIFY_ERROR_NO_CLASS: |
| 572 | case VERIFY_ERROR_NO_FIELD: |
| 573 | case VERIFY_ERROR_NO_METHOD: |
| 574 | case VERIFY_ERROR_ACCESS_CLASS: |
| 575 | case VERIFY_ERROR_ACCESS_FIELD: |
| 576 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 577 | case VERIFY_ERROR_INSTANTIATION: |
| 578 | case VERIFY_ERROR_CLASS_CHANGE: |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 579 | if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 580 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 581 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 582 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 583 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 584 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 585 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 586 | // to an interpreter. |
| 587 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 588 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 589 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 590 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 591 | have_pending_runtime_throw_failure_ = true; |
Andreas Gampe | d7f8d05 | 2015-03-12 11:05:47 -0700 | [diff] [blame] | 592 | |
| 593 | // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll |
| 594 | // try to merge garbage. |
| 595 | // Note: this assumes that Fail is called before we do any work_line modifications. |
| 596 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 597 | const Instruction* inst = Instruction::At(insns); |
| 598 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
| 599 | |
| 600 | if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) { |
| 601 | saved_line_->CopyFromLine(work_line_.get()); |
| 602 | } |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 603 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 604 | break; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 605 | // Indication that verification should be retried at runtime. |
| 606 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 607 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 608 | have_pending_hard_failure_ = true; |
| 609 | } |
| 610 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 611 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 612 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 613 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 614 | if (Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 615 | ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 616 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 617 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 618 | have_pending_hard_failure_ = true; |
| 619 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 622 | failures_.push_back(error); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 623 | 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] | 624 | work_insn_idx_)); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 625 | std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 626 | failure_messages_.push_back(failure_message); |
| 627 | return *failure_message; |
| 628 | } |
| 629 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 630 | std::ostream& MethodVerifier::LogVerifyInfo() { |
| 631 | return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_) |
| 632 | << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : "; |
| 633 | } |
| 634 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 635 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 636 | size_t failure_num = failure_messages_.size(); |
| 637 | DCHECK_NE(failure_num, 0U); |
| 638 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 639 | prepend += last_fail_message->str(); |
Elena Sayapina | 78480ec | 2014-08-15 15:52:42 +0700 | [diff] [blame] | 640 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 641 | delete last_fail_message; |
| 642 | } |
| 643 | |
| 644 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 645 | size_t failure_num = failure_messages_.size(); |
| 646 | DCHECK_NE(failure_num, 0U); |
| 647 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 648 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 651 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 652 | const uint16_t* insns = code_item_->insns_; |
| 653 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 654 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 655 | size_t new_instance_count = 0; |
| 656 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 657 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 658 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 659 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 660 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 661 | switch (opcode) { |
| 662 | case Instruction::APUT_OBJECT: |
| 663 | case Instruction::CHECK_CAST: |
| 664 | has_check_casts_ = true; |
| 665 | break; |
| 666 | case Instruction::INVOKE_VIRTUAL: |
| 667 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 668 | case Instruction::INVOKE_INTERFACE: |
| 669 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 670 | has_virtual_or_interface_invokes_ = true; |
| 671 | break; |
| 672 | case Instruction::MONITOR_ENTER: |
| 673 | monitor_enter_count++; |
| 674 | break; |
| 675 | case Instruction::NEW_INSTANCE: |
| 676 | new_instance_count++; |
| 677 | break; |
| 678 | default: |
| 679 | break; |
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 | size_t inst_size = inst->SizeInCodeUnits(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 682 | insn_flags_[dex_pc].SetIsOpcode(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 683 | dex_pc += inst_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 684 | inst = inst->RelativeAt(inst_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 687 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 688 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 689 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 690 | return false; |
| 691 | } |
| 692 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 693 | new_instance_count_ = new_instance_count; |
| 694 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 695 | return true; |
| 696 | } |
| 697 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 698 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 699 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 700 | if (tries_size == 0) { |
| 701 | return true; |
| 702 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 703 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 704 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 705 | |
| 706 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 707 | const DexFile::TryItem* try_item = &tries[idx]; |
| 708 | uint32_t start = try_item->start_addr_; |
| 709 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 710 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 711 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 712 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 713 | return false; |
| 714 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 715 | if (!insn_flags_[start].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 716 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 717 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 718 | return false; |
| 719 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 720 | uint32_t dex_pc = start; |
| 721 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 722 | while (dex_pc < end) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 723 | insn_flags_[dex_pc].SetInTry(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 724 | size_t insn_size = inst->SizeInCodeUnits(); |
| 725 | dex_pc += insn_size; |
| 726 | inst = inst->RelativeAt(insn_size); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 729 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 730 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 731 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 732 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 733 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 734 | CatchHandlerIterator iterator(handlers_ptr); |
| 735 | for (; iterator.HasNext(); iterator.Next()) { |
| 736 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 737 | if (!insn_flags_[dex_pc].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 738 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 739 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 742 | if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) { |
| 743 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 744 | << "exception handler begins with move-result* (" << dex_pc << ")"; |
| 745 | return false; |
| 746 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 747 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 748 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 749 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 750 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 751 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 752 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 753 | dex_cache_, class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 754 | if (exception_type == nullptr) { |
| 755 | DCHECK(self_->IsExceptionPending()); |
| 756 | self_->ClearException(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 759 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 760 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 761 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 762 | return true; |
| 763 | } |
| 764 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 765 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 766 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 767 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 768 | /* 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] | 769 | insn_flags_[0].SetBranchTarget(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 770 | insn_flags_[0].SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 771 | |
| 772 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 773 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 774 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 775 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 776 | return false; |
| 777 | } |
| 778 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 779 | // All invoke points are marked as "Throw" points already. |
| 780 | // 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] | 781 | if (inst->IsBranch()) { |
| 782 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
| 783 | // The compiler also needs safepoints for fall-through to loop heads. |
| 784 | // Such a loop head must be a target of a branch. |
| 785 | int32_t offset = 0; |
| 786 | bool cond, self_ok; |
| 787 | bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok); |
| 788 | DCHECK(target_ok); |
| 789 | insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint(); |
| 790 | } else if (inst->IsSwitch() || inst->IsThrow()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 791 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 792 | } else if (inst->IsReturn()) { |
| 793 | insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 794 | } |
| 795 | dex_pc += inst->SizeInCodeUnits(); |
| 796 | inst = inst->Next(); |
| 797 | } |
| 798 | return true; |
| 799 | } |
| 800 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 801 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 802 | bool result = true; |
| 803 | switch (inst->GetVerifyTypeArgumentA()) { |
| 804 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 805 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 806 | break; |
| 807 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 808 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 809 | break; |
| 810 | } |
| 811 | switch (inst->GetVerifyTypeArgumentB()) { |
| 812 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 813 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 814 | break; |
| 815 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 816 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 817 | break; |
| 818 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 819 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 820 | break; |
| 821 | case Instruction::kVerifyRegBNewInstance: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 822 | result = result && CheckNewInstance(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 823 | break; |
| 824 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 825 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 826 | break; |
| 827 | case Instruction::kVerifyRegBType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 828 | result = result && CheckTypeIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 829 | break; |
| 830 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 831 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 832 | break; |
| 833 | } |
| 834 | switch (inst->GetVerifyTypeArgumentC()) { |
| 835 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 836 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 837 | break; |
| 838 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 839 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 840 | break; |
| 841 | case Instruction::kVerifyRegCNewArray: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 842 | result = result && CheckNewArray(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 843 | break; |
| 844 | case Instruction::kVerifyRegCType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 845 | result = result && CheckTypeIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 846 | break; |
| 847 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 848 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 849 | break; |
| 850 | } |
| 851 | switch (inst->GetVerifyExtraFlags()) { |
| 852 | case Instruction::kVerifyArrayData: |
| 853 | result = result && CheckArrayData(code_offset); |
| 854 | break; |
| 855 | case Instruction::kVerifyBranchTarget: |
| 856 | result = result && CheckBranchTarget(code_offset); |
| 857 | break; |
| 858 | case Instruction::kVerifySwitchTargets: |
| 859 | result = result && CheckSwitchTargets(code_offset); |
| 860 | break; |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 861 | case Instruction::kVerifyVarArgNonZero: |
| 862 | // Fall-through. |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 863 | case Instruction::kVerifyVarArg: { |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 864 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) { |
| 865 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 866 | "non-range invoke"; |
| 867 | return false; |
| 868 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 869 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 870 | inst->GetVarArgs(args); |
| 871 | result = result && CheckVarArgRegs(inst->VRegA(), args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 872 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 873 | } |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 874 | case Instruction::kVerifyVarArgRangeNonZero: |
| 875 | // Fall-through. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 876 | case Instruction::kVerifyVarArgRange: |
Andreas Gampe | c331431 | 2014-06-19 18:13:29 -0700 | [diff] [blame] | 877 | if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero && |
| 878 | inst->VRegA() <= 0) { |
| 879 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in " |
| 880 | "range invoke"; |
| 881 | return false; |
| 882 | } |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 883 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 884 | break; |
| 885 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 886 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 887 | result = false; |
| 888 | break; |
| 889 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 890 | if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) { |
Ian Rogers | 5fb22a9 | 2014-06-13 10:31:28 -0700 | [diff] [blame] | 891 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name(); |
| 892 | result = false; |
| 893 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 894 | return result; |
| 895 | } |
| 896 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 897 | inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 898 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 899 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 900 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 901 | return false; |
| 902 | } |
| 903 | return true; |
| 904 | } |
| 905 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 906 | inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 907 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 908 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 909 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | return true; |
| 913 | } |
| 914 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 915 | inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 916 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 917 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 918 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | return true; |
| 922 | } |
| 923 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 924 | inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 925 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 926 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 927 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 928 | return false; |
| 929 | } |
| 930 | return true; |
| 931 | } |
| 932 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 933 | inline bool MethodVerifier::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 934 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 935 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 936 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | // 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] | 940 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 941 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 942 | 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] | 943 | return false; |
| 944 | } |
| 945 | return true; |
| 946 | } |
| 947 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 948 | inline bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 949 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 950 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 951 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 952 | return false; |
| 953 | } |
| 954 | return true; |
| 955 | } |
| 956 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 957 | inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 958 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 959 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 960 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 961 | return false; |
| 962 | } |
| 963 | return true; |
| 964 | } |
| 965 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 966 | bool MethodVerifier::CheckNewArray(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 967 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 968 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 969 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 970 | return false; |
| 971 | } |
| 972 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 973 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 974 | const char* cp = descriptor; |
| 975 | while (*cp++ == '[') { |
| 976 | bracket_count++; |
| 977 | } |
| 978 | if (bracket_count == 0) { |
| 979 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 980 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 981 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 982 | return false; |
| 983 | } else if (bracket_count > 255) { |
| 984 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 985 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 986 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 987 | return false; |
| 988 | } |
| 989 | return true; |
| 990 | } |
| 991 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 992 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 993 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 994 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 995 | const uint16_t* array_data; |
| 996 | int32_t array_data_offset; |
| 997 | |
| 998 | DCHECK_LT(cur_offset, insn_count); |
| 999 | /* make sure the start of the array data table is in range */ |
| 1000 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 1001 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 1002 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1003 | 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] | 1004 | << ", data offset " << array_data_offset |
| 1005 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1006 | return false; |
| 1007 | } |
| 1008 | /* offset to array data table is a relative branch-style offset */ |
| 1009 | array_data = insns + array_data_offset; |
| 1010 | /* make sure the table is 32-bit aligned */ |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1011 | if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1012 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 1013 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1014 | return false; |
| 1015 | } |
| 1016 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1017 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1018 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 1019 | /* make sure the end of the switch is in range */ |
| 1020 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1021 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 1022 | << ", data offset " << array_data_offset << ", end " |
| 1023 | << cur_offset + array_data_offset + table_size |
| 1024 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1025 | return false; |
| 1026 | } |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1030 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1031 | int32_t offset; |
| 1032 | bool isConditional, selfOkay; |
| 1033 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1037 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 1038 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 1041 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 1042 | // 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] | 1043 | 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] | 1044 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 1045 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1046 | return false; |
| 1047 | } |
| 1048 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 1049 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1050 | if (abs_offset < 0 || |
| 1051 | (uint32_t) abs_offset >= insn_count || |
| 1052 | !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1053 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1054 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1055 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1056 | return false; |
| 1057 | } |
| 1058 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1062 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1063 | bool* selfOkay) { |
| 1064 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 1065 | *pConditional = false; |
| 1066 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1067 | switch (*insns & 0xff) { |
| 1068 | case Instruction::GOTO: |
| 1069 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1070 | break; |
| 1071 | case Instruction::GOTO_32: |
| 1072 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1073 | *selfOkay = true; |
| 1074 | break; |
| 1075 | case Instruction::GOTO_16: |
| 1076 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | case Instruction::IF_EQ: |
| 1079 | case Instruction::IF_NE: |
| 1080 | case Instruction::IF_LT: |
| 1081 | case Instruction::IF_GE: |
| 1082 | case Instruction::IF_GT: |
| 1083 | case Instruction::IF_LE: |
| 1084 | case Instruction::IF_EQZ: |
| 1085 | case Instruction::IF_NEZ: |
| 1086 | case Instruction::IF_LTZ: |
| 1087 | case Instruction::IF_GEZ: |
| 1088 | case Instruction::IF_GTZ: |
| 1089 | case Instruction::IF_LEZ: |
| 1090 | *pOffset = (int16_t) insns[1]; |
| 1091 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1092 | break; |
| 1093 | default: |
| 1094 | return false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1095 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
| 1098 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1099 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1100 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1101 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1102 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1103 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1104 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
Jeff Hao | 9ccd151 | 2015-03-20 18:11:45 -0700 | [diff] [blame] | 1105 | 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] | 1106 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1107 | << ", switch offset " << switch_offset |
| 1108 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1109 | return false; |
| 1110 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1111 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1112 | const uint16_t* switch_insns = insns + switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1113 | /* make sure the table is 32-bit aligned */ |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1114 | if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1115 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 1116 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1117 | return false; |
| 1118 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1119 | uint32_t switch_count = switch_insns[1]; |
| 1120 | int32_t keys_offset, targets_offset; |
| 1121 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1122 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1123 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1124 | targets_offset = 4; |
| 1125 | keys_offset = -1; |
| 1126 | expected_signature = Instruction::kPackedSwitchSignature; |
| 1127 | } else { |
| 1128 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1129 | keys_offset = 2; |
| 1130 | targets_offset = 2 + 2 * switch_count; |
| 1131 | expected_signature = Instruction::kSparseSwitchSignature; |
| 1132 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1133 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1134 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1135 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 1136 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 1137 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1138 | return false; |
| 1139 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1140 | /* make sure the end of the switch is in range */ |
| 1141 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1142 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 1143 | << ", switch offset " << switch_offset |
| 1144 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1145 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1146 | return false; |
| 1147 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1148 | /* for a sparse switch, verify the keys are in ascending order */ |
| 1149 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1150 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 1151 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1152 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 1153 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 1154 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1155 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 1156 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1157 | return false; |
| 1158 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1159 | last_key = key; |
| 1160 | } |
| 1161 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1162 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1163 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 1164 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 1165 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 1166 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1167 | if (abs_offset < 0 || |
| 1168 | abs_offset >= (int32_t) insn_count || |
| 1169 | !insn_flags_[abs_offset].IsOpcode()) { |
| 1170 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1171 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1172 | << reinterpret_cast<void*>(cur_offset) |
| 1173 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1174 | return false; |
| 1175 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1176 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1177 | } |
| 1178 | return true; |
| 1179 | } |
| 1180 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1181 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1182 | if (vA > Instruction::kMaxVarArgRegs) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1183 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | return false; |
| 1185 | } |
| 1186 | uint16_t registers_size = code_item_->registers_size_; |
| 1187 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1188 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1189 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1190 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1198 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1199 | uint16_t registers_size = code_item_->registers_size_; |
| 1200 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1201 | // integer overflow when adding them here. |
| 1202 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1203 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1204 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1205 | return false; |
| 1206 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1207 | return true; |
| 1208 | } |
| 1209 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1210 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1211 | uint16_t registers_size = code_item_->registers_size_; |
| 1212 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1213 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1214 | if (registers_size * insns_size > 4*1024*1024) { |
buzbee | 4922ef9 | 2012-02-24 14:32:20 -0800 | [diff] [blame] | 1215 | LOG(WARNING) << "warning: method is huge (regs=" << registers_size |
| 1216 | << " insns_size=" << insns_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1217 | } |
| 1218 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1219 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1220 | insn_flags_.get(), |
| 1221 | insns_size, |
| 1222 | registers_size, |
| 1223 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1224 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1225 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1226 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1227 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1228 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1229 | /* Initialize register types of method arguments. */ |
| 1230 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1231 | DCHECK_NE(failures_.size(), 0U); |
| 1232 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1233 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1234 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | /* Perform code flow verification. */ |
| 1238 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1239 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1240 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1241 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1242 | return true; |
| 1243 | } |
| 1244 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1245 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1246 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1247 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1248 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1249 | } |
| 1250 | return os; |
| 1251 | } |
| 1252 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1253 | void MethodVerifier::Dump(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1254 | if (code_item_ == nullptr) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1255 | os << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1256 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1257 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1258 | { |
| 1259 | os << "Register Types:\n"; |
| 1260 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1261 | std::ostream indent_os(&indent_filter); |
| 1262 | reg_types_.Dump(indent_os); |
| 1263 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1264 | os << "Dumping instructions and register lines:\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1265 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1266 | std::ostream indent_os(&indent_filter); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1267 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1268 | 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] | 1269 | dex_pc += inst->SizeInCodeUnits()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1270 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1271 | if (reg_line != nullptr) { |
| 1272 | indent_os << reg_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1273 | } |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1274 | indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1275 | const bool kDumpHexOfInstruction = false; |
| 1276 | if (kDumpHexOfInstruction) { |
| 1277 | indent_os << inst->DumpHex(5) << " "; |
| 1278 | } |
| 1279 | indent_os << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1280 | inst = inst->Next(); |
| 1281 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1284 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1285 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1286 | case 'I': |
| 1287 | case 'C': |
| 1288 | case 'S': |
| 1289 | case 'B': |
| 1290 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1291 | case 'F': |
| 1292 | case 'D': |
| 1293 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1294 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1295 | default: |
| 1296 | return false; |
| 1297 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1300 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1302 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1303 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1304 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1305 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1306 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1307 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1308 | if (!IsStatic()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1309 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1310 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1311 | // called. |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1312 | const RegType& declaring_class = GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1313 | if (IsConstructor() && !declaring_class.IsJavaLangObject()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1314 | reg_line->SetRegisterType(this, arg_start + cur_arg, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1315 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1316 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1317 | reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1318 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1319 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1322 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1323 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1324 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1325 | |
| 1326 | for (; iterator.HasNext(); iterator.Next()) { |
| 1327 | const char* descriptor = iterator.GetDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1328 | if (descriptor == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1329 | LOG(FATAL) << "Null descriptor"; |
| 1330 | } |
| 1331 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1332 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1333 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1334 | return false; |
| 1335 | } |
| 1336 | switch (descriptor[0]) { |
| 1337 | case 'L': |
| 1338 | case '[': |
| 1339 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1340 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1341 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1342 | // can return without doing anything or call virtual methods). |
| 1343 | { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1344 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1345 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1346 | DCHECK(HasFailures()); |
| 1347 | return false; |
| 1348 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1349 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1350 | } |
| 1351 | break; |
| 1352 | case 'Z': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1353 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1354 | break; |
| 1355 | case 'C': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1356 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1357 | break; |
| 1358 | case 'B': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1359 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1360 | break; |
| 1361 | case 'I': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1362 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1363 | break; |
| 1364 | case 'S': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1365 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1366 | break; |
| 1367 | case 'F': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1368 | reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1369 | break; |
| 1370 | case 'J': |
| 1371 | case 'D': { |
Andreas Gampe | 77cd4d6 | 2014-06-19 17:29:48 -0700 | [diff] [blame] | 1372 | if (cur_arg + 1 >= expected_args) { |
| 1373 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1374 | << " args, found more (" << descriptor << ")"; |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1378 | const RegType* lo_half; |
| 1379 | const RegType* hi_half; |
| 1380 | if (descriptor[0] == 'J') { |
| 1381 | lo_half = ®_types_.LongLo(); |
| 1382 | hi_half = ®_types_.LongHi(); |
| 1383 | } else { |
| 1384 | lo_half = ®_types_.DoubleLo(); |
| 1385 | hi_half = ®_types_.DoubleHi(); |
| 1386 | } |
| 1387 | reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1388 | cur_arg++; |
| 1389 | break; |
| 1390 | } |
| 1391 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1392 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1393 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1394 | return false; |
| 1395 | } |
| 1396 | cur_arg++; |
| 1397 | } |
| 1398 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1399 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1400 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1401 | return false; |
| 1402 | } |
| 1403 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1404 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1405 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1406 | bool result; |
| 1407 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1408 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1409 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1410 | size_t i = 0; |
| 1411 | do { |
| 1412 | i++; |
| 1413 | } while (descriptor[i] == '['); // process leading [ |
| 1414 | if (descriptor[i] == 'L') { // object array |
| 1415 | do { |
| 1416 | i++; // find closing ; |
| 1417 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1418 | result = descriptor[i] == ';'; |
| 1419 | } else { // primitive array |
| 1420 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1421 | } |
| 1422 | } else if (descriptor[0] == 'L') { |
| 1423 | // could be more thorough here, but shouldn't be required |
| 1424 | size_t i = 0; |
| 1425 | do { |
| 1426 | i++; |
| 1427 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1428 | result = descriptor[i] == ';'; |
| 1429 | } else { |
| 1430 | result = false; |
| 1431 | } |
| 1432 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1433 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1434 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1435 | } |
| 1436 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1439 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1440 | const uint16_t* insns = code_item_->insns_; |
| 1441 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1442 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1443 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1444 | insn_flags_[0].SetChanged(); |
| 1445 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1446 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1447 | /* Continue until no instructions are marked "changed". */ |
| 1448 | while (true) { |
Mathieu Chartier | 4306ef8 | 2014-12-19 18:41:47 -0800 | [diff] [blame] | 1449 | if (allow_thread_suspension_) { |
| 1450 | self_->AllowThreadSuspension(); |
| 1451 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1452 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1453 | uint32_t insn_idx = start_guess; |
| 1454 | for (; insn_idx < insns_size; insn_idx++) { |
| 1455 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1456 | break; |
| 1457 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1458 | if (insn_idx == insns_size) { |
| 1459 | if (start_guess != 0) { |
| 1460 | /* try again, starting from the top */ |
| 1461 | start_guess = 0; |
| 1462 | continue; |
| 1463 | } else { |
| 1464 | /* all flags are clear */ |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1468 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1469 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1470 | // "changed" flags, we need to load the set of registers from the table. |
| 1471 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1472 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1473 | // target. |
| 1474 | work_insn_idx_ = insn_idx; |
| 1475 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1476 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 1477 | } else if (kIsDebugBuild) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1478 | /* |
| 1479 | * Sanity check: retrieve the stored register line (assuming |
| 1480 | * a full table) and make sure it actually matches. |
| 1481 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1482 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1483 | if (register_line != nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1484 | if (work_line_->CompareLine(register_line) != 0) { |
| 1485 | Dump(std::cout); |
| 1486 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1487 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1488 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1489 | << " work_line=" << work_line_->Dump(this) << "\n" |
| 1490 | << " expected=" << register_line->Dump(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1491 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1492 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1493 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1494 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1495 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1496 | prepend += " failed to verify: "; |
| 1497 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1498 | return false; |
| 1499 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1500 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1501 | insn_flags_[insn_idx].SetVisited(); |
| 1502 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1503 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1504 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1505 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1506 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1507 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1508 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1509 | * down the line, possibly in the verifier. |
| 1510 | * |
| 1511 | * If we've substituted "always throw" instructions into the stream, |
| 1512 | * we are almost certainly going to have some dead code. |
| 1513 | */ |
| 1514 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1515 | uint32_t insn_idx = 0; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1516 | for (; insn_idx < insns_size; |
| 1517 | insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1518 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1519 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1520 | * may or may not be preceded by a padding NOP (for alignment). |
| 1521 | */ |
| 1522 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1523 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1524 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1525 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1526 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1527 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1528 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1529 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1532 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1533 | if (dead_start < 0) |
| 1534 | dead_start = insn_idx; |
| 1535 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1536 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1537 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1538 | dead_start = -1; |
| 1539 | } |
| 1540 | } |
| 1541 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1542 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1543 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1544 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1545 | // To dump the state of the verify after a method, do something like: |
| 1546 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1547 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1548 | // LOG(INFO) << info_messages_.str(); |
| 1549 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1550 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1551 | return true; |
| 1552 | } |
| 1553 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1554 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1555 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1556 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1557 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1558 | // for a thread to be suspended). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1559 | if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1560 | 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] | 1561 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1562 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1563 | } |
| 1564 | } |
| 1565 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1566 | /* |
| 1567 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1568 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1569 | * control to another statement: |
| 1570 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1571 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1572 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1573 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1574 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1575 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1576 | * throw an exception that is handled by an encompassing "try" |
| 1577 | * block. |
| 1578 | * |
| 1579 | * We can also return, in which case there is no successor instruction |
| 1580 | * from this point. |
| 1581 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1582 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1583 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1584 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1585 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1586 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1587 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1588 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1589 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1590 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1591 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1592 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1593 | << work_line_->Dump(this) << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1594 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1595 | |
| 1596 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1597 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1598 | * can throw an exception, we will copy/merge this into the "catch" |
| 1599 | * address rather than work_line, because we don't want the result |
| 1600 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1601 | * a type) to be visible to the exception handler. |
| 1602 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1603 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1604 | saved_line_->CopyFromLine(work_line_.get()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1605 | } else if (kIsDebugBuild) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1606 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1609 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1610 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1611 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1612 | // branch target. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1613 | std::unique_ptr<RegisterLine> branch_line; |
| 1614 | std::unique_ptr<RegisterLine> fallthrough_line; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1615 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 1616 | /* |
| 1617 | * If we are in a constructor, and we currently have an UninitializedThis type |
| 1618 | * in a register somewhere, we need to make sure it isn't overwritten. |
| 1619 | */ |
| 1620 | bool track_uninitialized_this = false; |
| 1621 | size_t uninitialized_this_loc = 0; |
| 1622 | if (IsConstructor()) { |
| 1623 | track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc); |
| 1624 | } |
| 1625 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1626 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1627 | case Instruction::NOP: |
| 1628 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1629 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1630 | * a signature that looks like a NOP; if we see one of these in |
| 1631 | * the course of executing code then we have a problem. |
| 1632 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1633 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1634 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1635 | } |
| 1636 | break; |
| 1637 | |
| 1638 | case Instruction::MOVE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1639 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1640 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1641 | case Instruction::MOVE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1642 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1643 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1644 | case Instruction::MOVE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1645 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1646 | break; |
| 1647 | case Instruction::MOVE_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1648 | work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1649 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1650 | case Instruction::MOVE_WIDE_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1651 | work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1652 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1653 | case Instruction::MOVE_WIDE_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1654 | work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1655 | break; |
| 1656 | case Instruction::MOVE_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1657 | work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1658 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1659 | case Instruction::MOVE_OBJECT_FROM16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1660 | work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1661 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1662 | case Instruction::MOVE_OBJECT_16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1663 | work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1664 | break; |
| 1665 | |
| 1666 | /* |
| 1667 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1668 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1669 | * might want to hold the result in an actual CPU register, so the |
| 1670 | * Dalvik spec requires that these only appear immediately after an |
| 1671 | * invoke or filled-new-array. |
| 1672 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1673 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1674 | * redundant with the reset done below, but it can make the debug info |
| 1675 | * easier to read in some cases.) |
| 1676 | */ |
| 1677 | case Instruction::MOVE_RESULT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1678 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1679 | break; |
| 1680 | case Instruction::MOVE_RESULT_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1681 | work_line_->CopyResultRegister2(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1682 | break; |
| 1683 | case Instruction::MOVE_RESULT_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1684 | work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1685 | break; |
| 1686 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1687 | case Instruction::MOVE_EXCEPTION: { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 1688 | // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case |
| 1689 | // where one entrypoint to the catch block is not actually an exception path. |
| 1690 | if (work_insn_idx_ == 0) { |
| 1691 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0"; |
| 1692 | break; |
| 1693 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1694 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1695 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1696 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1697 | */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1698 | const RegType& res_type = GetCaughtExceptionType(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1699 | work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1700 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1701 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1702 | case Instruction::RETURN_VOID: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1703 | if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1704 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1705 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1706 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1707 | } |
| 1708 | break; |
| 1709 | case Instruction::RETURN: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1710 | if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1711 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1712 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1713 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1714 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1715 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1716 | } else { |
| 1717 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1718 | // 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] | 1719 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1720 | const RegType& src_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1721 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1722 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1723 | return_type.IsShort() || return_type.IsChar()) && |
| 1724 | src_type.IsInteger())); |
| 1725 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1726 | bool success = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1727 | work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1728 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1729 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1730 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1731 | } |
| 1732 | } |
| 1733 | break; |
| 1734 | case Instruction::RETURN_WIDE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1735 | if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1736 | /* check the method signature */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1737 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1738 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1739 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1740 | } else { |
| 1741 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1742 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1743 | bool success = work_line_->VerifyRegisterType(this, vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1744 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1745 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1746 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | break; |
| 1750 | case Instruction::RETURN_OBJECT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1751 | if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1752 | const RegType& return_type = GetMethodReturnType(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1753 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1754 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1755 | } else { |
| 1756 | /* return_type is the *expected* return type, not register value */ |
| 1757 | DCHECK(!return_type.IsZero()); |
| 1758 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1759 | const uint32_t vregA = inst->VRegA_11x(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1760 | const RegType& reg_type = work_line_->GetRegisterType(this, vregA); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1761 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 1762 | // instance of the "return_type" |
| 1763 | if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1764 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1765 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1766 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1767 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1768 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1769 | << "' or '" << reg_type << "'"; |
| 1770 | } else { |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 1771 | bool soft_error = false; |
| 1772 | // Check whether arrays are involved. They will show a valid class status, even |
| 1773 | // if their components are erroneous. |
| 1774 | if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) { |
| 1775 | return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error); |
| 1776 | if (soft_error) { |
| 1777 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: " |
| 1778 | << reg_type << " vs " << return_type; |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | if (!soft_error) { |
| 1783 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1784 | << "', but expected from declaration '" << return_type << "'"; |
| 1785 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1786 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | break; |
| 1791 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1792 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1793 | case Instruction::CONST_4: { |
| 1794 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1795 | work_line_->SetRegisterType(this, inst->VRegA_11n(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1796 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1797 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1798 | } |
| 1799 | case Instruction::CONST_16: { |
| 1800 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1801 | work_line_->SetRegisterType(this, inst->VRegA_21s(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1802 | DetermineCat1Constant(val, need_precise_constants_)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1803 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1804 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1805 | case Instruction::CONST: { |
| 1806 | int32_t val = inst->VRegB_31i(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1807 | work_line_->SetRegisterType(this, inst->VRegA_31i(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1808 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1809 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1810 | } |
| 1811 | case Instruction::CONST_HIGH16: { |
| 1812 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1813 | work_line_->SetRegisterType(this, inst->VRegA_21h(), |
Ian Rogers | 46960fe | 2014-05-23 10:43:43 -0700 | [diff] [blame] | 1814 | DetermineCat1Constant(val, need_precise_constants_)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1815 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1816 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1817 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1818 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1819 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1820 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1821 | 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] | 1822 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1823 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1824 | } |
| 1825 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1826 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1827 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1828 | 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] | 1829 | work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1830 | break; |
| 1831 | } |
| 1832 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1833 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1834 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1835 | 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] | 1836 | work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1840 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1841 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1842 | 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] | 1843 | work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1844 | break; |
| 1845 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1846 | case Instruction::CONST_STRING: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1847 | work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1848 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1849 | case Instruction::CONST_STRING_JUMBO: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1850 | work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1851 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1852 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1853 | // Get type from instruction if unresolved then we need an access check |
| 1854 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1855 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1856 | // 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] | 1857 | work_line_->SetRegisterType(this, inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1858 | res_type.IsConflict() ? res_type |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1859 | : reg_types_.JavaLangClass()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1860 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1861 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1862 | case Instruction::MONITOR_ENTER: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1863 | work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1864 | break; |
| 1865 | case Instruction::MONITOR_EXIT: |
| 1866 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1867 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1868 | * 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] | 1869 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1870 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1871 | * feature that Dalvik doesn't support.) |
| 1872 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1873 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1874 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1875 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1876 | * structured locking checks are working, the former would have |
| 1877 | * failed on the -enter instruction, and the latter is impossible. |
| 1878 | * |
| 1879 | * This is fortunate, because issue 3221411 prevents us from |
| 1880 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1881 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1882 | * some catch blocks (which will show up as "dead" code when |
| 1883 | * we skip them here); if we can't, then the code path could be |
| 1884 | * "live" so we still need to check it. |
| 1885 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1886 | opcode_flags &= ~Instruction::kThrow; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1887 | work_line_->PopMonitor(this, inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1888 | break; |
| 1889 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1890 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1891 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1892 | /* |
| 1893 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1894 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1895 | * |
| 1896 | * 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] | 1897 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1898 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1899 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1900 | 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] | 1901 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1902 | if (res_type.IsConflict()) { |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 1903 | // If this is a primitive type, fail HARD. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 1904 | mirror::Class* klass = dex_cache_->GetResolvedType(type_idx); |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 1905 | if (klass != nullptr && klass->IsPrimitive()) { |
| 1906 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type " |
| 1907 | << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in " |
| 1908 | << GetDeclaringClass(); |
| 1909 | break; |
| 1910 | } |
| 1911 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1912 | DCHECK_NE(failures_.size(), 0U); |
| 1913 | if (!is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1914 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1915 | } |
| 1916 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 1917 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1918 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1919 | 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] | 1920 | const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1921 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1922 | if (is_checkcast) { |
| 1923 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 1924 | } else { |
| 1925 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 1926 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1927 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1928 | if (is_checkcast) { |
| 1929 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 1930 | } else { |
| 1931 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 1932 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1933 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1934 | if (is_checkcast) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1935 | work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1936 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1937 | work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1938 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1939 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1940 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1941 | } |
| 1942 | case Instruction::ARRAY_LENGTH: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1943 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1944 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1945 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1946 | 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] | 1947 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1948 | work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1949 | } |
Andreas Gampe | 65c9db8 | 2014-07-28 13:14:34 -0700 | [diff] [blame] | 1950 | } else { |
| 1951 | 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] | 1952 | } |
| 1953 | break; |
| 1954 | } |
| 1955 | case Instruction::NEW_INSTANCE: { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1956 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1957 | if (res_type.IsConflict()) { |
| 1958 | DCHECK_NE(failures_.size(), 0U); |
| 1959 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1960 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1961 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 1962 | // can't create an instance of an interface or abstract class */ |
| 1963 | if (!res_type.IsInstantiableTypes()) { |
| 1964 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 1965 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1966 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1967 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 1968 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1969 | // Any registers holding previous allocations from this address that have not yet been |
| 1970 | // initialized must be marked invalid. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1971 | work_line_->MarkUninitRefsAsInvalid(this, uninit_type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1972 | // add the new uninitialized reference to the register state |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1973 | work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1974 | break; |
| 1975 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1976 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1977 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1980 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1981 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1982 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1983 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1984 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1985 | just_set_result = true; // Filled new array range sets result register |
| 1986 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1987 | case Instruction::CMPL_FLOAT: |
| 1988 | case Instruction::CMPG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1989 | if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1990 | break; |
| 1991 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1992 | if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1993 | break; |
| 1994 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1995 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1996 | break; |
| 1997 | case Instruction::CMPL_DOUBLE: |
| 1998 | case Instruction::CMPG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1999 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2000 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2001 | break; |
| 2002 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2003 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2004 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2005 | break; |
| 2006 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2007 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2008 | break; |
| 2009 | case Instruction::CMP_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2010 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2011 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2012 | break; |
| 2013 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2014 | if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2015 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2016 | break; |
| 2017 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2018 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2019 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2020 | case Instruction::THROW: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2021 | const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2022 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2023 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 2024 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2025 | } |
| 2026 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2027 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | case Instruction::GOTO: |
| 2029 | case Instruction::GOTO_16: |
| 2030 | case Instruction::GOTO_32: |
| 2031 | /* no effect on or use of registers */ |
| 2032 | break; |
| 2033 | |
| 2034 | case Instruction::PACKED_SWITCH: |
| 2035 | case Instruction::SPARSE_SWITCH: |
| 2036 | /* verify that vAA is an integer, or can be converted to one */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2037 | work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2040 | case Instruction::FILL_ARRAY_DATA: { |
| 2041 | /* Similar to the verification done for APUT */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2042 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2043 | /* array_type can be null if the reg type is Zero */ |
| 2044 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2045 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2046 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 2047 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 2048 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2049 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2050 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2051 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2052 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 2053 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2054 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2055 | // Now verify if the element width in the table matches the element width declared in |
| 2056 | // the array |
| 2057 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 2058 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2059 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2060 | } else { |
| 2061 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 2062 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 2063 | // in the table and expected from the array class. |
| 2064 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2065 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 2066 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 2067 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2068 | } |
| 2069 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2073 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2074 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2075 | case Instruction::IF_NE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2076 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2077 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2078 | bool mismatch = false; |
| 2079 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 2080 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 2081 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 2082 | mismatch = !reg_type2.IsReferenceTypes(); |
| 2083 | } else { // both integral? |
| 2084 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 2085 | } |
| 2086 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2087 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 2088 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2089 | } |
| 2090 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2091 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2092 | case Instruction::IF_LT: |
| 2093 | case Instruction::IF_GE: |
| 2094 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2095 | case Instruction::IF_LE: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2096 | const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t()); |
| 2097 | const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2098 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2099 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 2100 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2101 | } |
| 2102 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2103 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2104 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2105 | case Instruction::IF_NEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2106 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2107 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2108 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2109 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2110 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2111 | |
| 2112 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2113 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2114 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2115 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2116 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2117 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2118 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2119 | if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(), |
| 2120 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2121 | work_insn_idx_)) { |
| 2122 | break; |
| 2123 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2124 | } else { |
| 2125 | break; |
| 2126 | } |
| 2127 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2128 | 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] | 2129 | |
| 2130 | /* Check for peep-hole pattern of: |
| 2131 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2132 | * instance-of vX, vY, T; |
| 2133 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2134 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2135 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2136 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2137 | * and sharpen the type of vY to be type T. |
| 2138 | * Note, this pattern can't be if: |
| 2139 | * - if there are other branches to this branch, |
| 2140 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2141 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2142 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2143 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 2144 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 2145 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2146 | // Check the type of the instance-of is different than that of registers type, as if they |
| 2147 | // are the same there is no work to be done here. Check that the conversion is not to or |
| 2148 | // from an unresolved type as type information is imprecise. If the instance-of is to an |
| 2149 | // interface then ignore the type information as interfaces can only be treated as Objects |
| 2150 | // and we don't want to disallow field and other operations on the object. If the value |
| 2151 | // being instance-of checked against is known null (zero) then allow the optimization as |
| 2152 | // we didn't have type information. If the merge of the instance-of type with the original |
| 2153 | // type is assignable to the original then allow optimization. This check is performed to |
| 2154 | // ensure that subsequent merges don't lose type information - such as becoming an |
| 2155 | // interface from a class that would lose information relevant to field checks. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2156 | 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] | 2157 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2158 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2159 | if (!orig_type.Equals(cast_type) && |
| 2160 | !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
Andreas Gampe | 00633eb | 2014-07-17 16:13:35 -0700 | [diff] [blame] | 2161 | 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] | 2162 | !cast_type.GetClass()->IsInterface() && |
| 2163 | (orig_type.IsZero() || |
| 2164 | orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, ®_types_)))) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 2165 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2166 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2167 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 2168 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2169 | branch_line.reset(update_line); |
| 2170 | } |
| 2171 | update_line->CopyFromLine(work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2172 | update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2173 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 2174 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 2175 | // register encoding space of instance-of, and propagate type information to the source |
| 2176 | // of the move-object. |
| 2177 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 2178 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2179 | move_idx--; |
| 2180 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 2181 | if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(), |
| 2182 | "Unable to get previous instruction of if-eqz/if-nez for work index ", |
| 2183 | work_insn_idx_)) { |
| 2184 | break; |
| 2185 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2186 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 2187 | switch (move_inst->Opcode()) { |
| 2188 | case Instruction::MOVE_OBJECT: |
| 2189 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2190 | update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2191 | } |
| 2192 | break; |
| 2193 | case Instruction::MOVE_OBJECT_FROM16: |
| 2194 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2195 | update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2196 | } |
| 2197 | break; |
| 2198 | case Instruction::MOVE_OBJECT_16: |
| 2199 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2200 | update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 2201 | } |
| 2202 | break; |
| 2203 | default: |
| 2204 | break; |
| 2205 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2210 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2211 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2212 | case Instruction::IF_LTZ: |
| 2213 | case Instruction::IF_GEZ: |
| 2214 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2215 | case Instruction::IF_LEZ: { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2216 | const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2217 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2218 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2219 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2220 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2221 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2222 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2223 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2224 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2225 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2226 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2227 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2228 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2229 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2230 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2231 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2232 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2233 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2234 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2235 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2236 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2237 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2238 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2239 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2240 | break; |
| 2241 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2242 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2243 | break; |
| 2244 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2245 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2246 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2247 | break; |
| 2248 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2249 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2250 | break; |
| 2251 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2252 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2253 | break; |
| 2254 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2255 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2256 | break; |
| 2257 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2258 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2259 | break; |
| 2260 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2261 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2262 | break; |
| 2263 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2264 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2265 | break; |
| 2266 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2267 | case Instruction::IGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2268 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2269 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2270 | case Instruction::IGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2271 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2272 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2273 | case Instruction::IGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2274 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2275 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2276 | case Instruction::IGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2277 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2278 | break; |
| 2279 | case Instruction::IGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2280 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2281 | break; |
| 2282 | case Instruction::IGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2283 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2284 | break; |
| 2285 | case Instruction::IGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2286 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2287 | false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2288 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2289 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2290 | case Instruction::IPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2291 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2292 | break; |
| 2293 | case Instruction::IPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2294 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2295 | break; |
| 2296 | case Instruction::IPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2297 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2298 | break; |
| 2299 | case Instruction::IPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2300 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2301 | break; |
| 2302 | case Instruction::IPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2303 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2304 | break; |
| 2305 | case Instruction::IPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2306 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2307 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2308 | case Instruction::IPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2309 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2310 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2311 | break; |
| 2312 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2313 | case Instruction::SGET_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2314 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2315 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2316 | case Instruction::SGET_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2317 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2318 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2319 | case Instruction::SGET_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2320 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2321 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2322 | case Instruction::SGET_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2323 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2324 | break; |
| 2325 | case Instruction::SGET: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2326 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2327 | break; |
| 2328 | case Instruction::SGET_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2329 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2330 | break; |
| 2331 | case Instruction::SGET_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2332 | VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false, |
| 2333 | true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2334 | break; |
| 2335 | |
| 2336 | case Instruction::SPUT_BOOLEAN: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2337 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2338 | break; |
| 2339 | case Instruction::SPUT_BYTE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2340 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2341 | break; |
| 2342 | case Instruction::SPUT_CHAR: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2343 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2344 | break; |
| 2345 | case Instruction::SPUT_SHORT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2346 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2347 | break; |
| 2348 | case Instruction::SPUT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2349 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2350 | break; |
| 2351 | case Instruction::SPUT_WIDE: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2352 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2353 | break; |
| 2354 | case Instruction::SPUT_OBJECT: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2355 | VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false, |
| 2356 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2357 | break; |
| 2358 | |
| 2359 | case Instruction::INVOKE_VIRTUAL: |
| 2360 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2361 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2362 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2363 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2364 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2365 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2366 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Andreas Gampe | acc4d2f | 2014-06-12 19:35:05 -0700 | [diff] [blame] | 2367 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, |
| 2368 | is_super); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2369 | const RegType* return_type = nullptr; |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2370 | if (called_method != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2371 | StackHandleScope<1> hs(self_); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2372 | Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method)); |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 2373 | mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2374 | if (return_type_class != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2375 | return_type = ®_types_.FromClass(h_called_method->GetReturnTypeDescriptor(), |
| 2376 | return_type_class, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2377 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| 2378 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2379 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2380 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2384 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2385 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2386 | 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] | 2387 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2388 | return_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2389 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2390 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2391 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2392 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2393 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2394 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2395 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2396 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2397 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2398 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2399 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2400 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2401 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2402 | is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2403 | const char* return_type_descriptor; |
| 2404 | bool is_constructor; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2405 | const RegType* return_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2406 | if (called_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2407 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2408 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2409 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2410 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2411 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2412 | } else { |
| 2413 | is_constructor = called_method->IsConstructor(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2414 | return_type_descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2415 | StackHandleScope<1> hs(self_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2416 | Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method)); |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 2417 | mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2418 | if (return_type_class != nullptr) { |
| 2419 | return_type = ®_types_.FromClass(return_type_descriptor, |
| 2420 | return_type_class, |
| 2421 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| 2422 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2423 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 2424 | self_->ClearException(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2425 | } |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2426 | } |
| 2427 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2428 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2429 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2430 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2431 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2432 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2433 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2434 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2435 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2436 | if (this_type.IsConflict()) // failure. |
| 2437 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2438 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2439 | /* no null refs allowed (?) */ |
| 2440 | if (this_type.IsZero()) { |
| 2441 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2442 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2443 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2444 | |
| 2445 | /* must be in same class or in superclass */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2446 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2447 | // TODO: re-enable constructor type verification |
| 2448 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2449 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2450 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2451 | // break; |
| 2452 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2453 | |
| 2454 | /* arg must be an uninitialized reference */ |
| 2455 | if (!this_type.IsUninitializedTypes()) { |
| 2456 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2457 | << this_type; |
| 2458 | break; |
| 2459 | } |
| 2460 | |
| 2461 | /* |
| 2462 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2463 | * registers that have the same object instance in them, not just the "this" register. |
| 2464 | */ |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame^] | 2465 | const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 2466 | work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2467 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2468 | if (return_type == nullptr) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2469 | return_type = ®_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, |
| 2470 | false); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2471 | } |
| 2472 | if (!return_type->IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2473 | work_line_->SetResultRegisterType(this, *return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2474 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2475 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2476 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2477 | just_set_result = true; |
| 2478 | break; |
| 2479 | } |
| 2480 | case Instruction::INVOKE_STATIC: |
| 2481 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2482 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2483 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2484 | METHOD_STATIC, |
| 2485 | is_range, |
| 2486 | false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2487 | const char* descriptor; |
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 | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2490 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2491 | 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] | 2492 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2493 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2494 | descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2495 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2496 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2497 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2498 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2499 | } else { |
| 2500 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2501 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2502 | just_set_result = true; |
| 2503 | } |
| 2504 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2505 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2506 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2507 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2508 | mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2509 | METHOD_INTERFACE, |
| 2510 | is_range, |
| 2511 | false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2512 | if (abs_method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2513 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2514 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2515 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2516 | << PrettyMethod(abs_method) << "'"; |
| 2517 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2518 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2519 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2520 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2521 | * interface or Object (see comments in RegType::JoinClass). |
| 2522 | */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2523 | const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2524 | if (this_type.IsZero()) { |
| 2525 | /* null pointer always passes (and always fails at runtime) */ |
| 2526 | } else { |
| 2527 | if (this_type.IsUninitializedTypes()) { |
| 2528 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2529 | << this_type; |
| 2530 | break; |
| 2531 | } |
| 2532 | // In the past we have tried to assert that "called_interface" is assignable |
| 2533 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2534 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2535 | // implemented by "this_type". For example, two classes may implement the same |
| 2536 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2537 | // join will set "this_type" to the parent class and a test that this implements |
| 2538 | // the interface will incorrectly fail. |
| 2539 | } |
| 2540 | /* |
| 2541 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2542 | * the type information is in the abstract method, so we're good. |
| 2543 | */ |
| 2544 | const char* descriptor; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2545 | if (abs_method == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2546 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2547 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2548 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2549 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2550 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2551 | descriptor = abs_method->GetReturnTypeDescriptor(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2552 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2553 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2554 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2555 | work_line_->SetResultRegisterType(this, return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2556 | } else { |
| 2557 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2558 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2559 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2560 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2561 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2562 | case Instruction::NEG_INT: |
| 2563 | case Instruction::NOT_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2564 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2565 | break; |
| 2566 | case Instruction::NEG_LONG: |
| 2567 | case Instruction::NOT_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2568 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2569 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2570 | break; |
| 2571 | case Instruction::NEG_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2572 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2573 | break; |
| 2574 | case Instruction::NEG_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2575 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2576 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2577 | break; |
| 2578 | case Instruction::INT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2579 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2580 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2581 | break; |
| 2582 | case Instruction::INT_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2583 | work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2584 | break; |
| 2585 | case Instruction::INT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2586 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2587 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2588 | break; |
| 2589 | case Instruction::LONG_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2590 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2591 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2592 | break; |
| 2593 | case Instruction::LONG_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2594 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2595 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2596 | break; |
| 2597 | case Instruction::LONG_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2598 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2599 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2600 | break; |
| 2601 | case Instruction::FLOAT_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2602 | work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2603 | break; |
| 2604 | case Instruction::FLOAT_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2605 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2606 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2607 | break; |
| 2608 | case Instruction::FLOAT_TO_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2609 | work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2610 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2611 | break; |
| 2612 | case Instruction::DOUBLE_TO_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2613 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2614 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2615 | break; |
| 2616 | case Instruction::DOUBLE_TO_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2617 | work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2618 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2619 | break; |
| 2620 | case Instruction::DOUBLE_TO_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2621 | work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2622 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2623 | break; |
| 2624 | case Instruction::INT_TO_BYTE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2625 | work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2626 | break; |
| 2627 | case Instruction::INT_TO_CHAR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2628 | work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2629 | break; |
| 2630 | case Instruction::INT_TO_SHORT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2631 | work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2632 | break; |
| 2633 | |
| 2634 | case Instruction::ADD_INT: |
| 2635 | case Instruction::SUB_INT: |
| 2636 | case Instruction::MUL_INT: |
| 2637 | case Instruction::REM_INT: |
| 2638 | case Instruction::DIV_INT: |
| 2639 | case Instruction::SHL_INT: |
| 2640 | case Instruction::SHR_INT: |
| 2641 | case Instruction::USHR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2642 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2643 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2644 | break; |
| 2645 | case Instruction::AND_INT: |
| 2646 | case Instruction::OR_INT: |
| 2647 | case Instruction::XOR_INT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2648 | work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2649 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2650 | break; |
| 2651 | case Instruction::ADD_LONG: |
| 2652 | case Instruction::SUB_LONG: |
| 2653 | case Instruction::MUL_LONG: |
| 2654 | case Instruction::DIV_LONG: |
| 2655 | case Instruction::REM_LONG: |
| 2656 | case Instruction::AND_LONG: |
| 2657 | case Instruction::OR_LONG: |
| 2658 | case Instruction::XOR_LONG: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2659 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2660 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2661 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2662 | break; |
| 2663 | case Instruction::SHL_LONG: |
| 2664 | case Instruction::SHR_LONG: |
| 2665 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2666 | /* shift distance is Int, making these different from other binary operations */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2667 | work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2668 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2669 | break; |
| 2670 | case Instruction::ADD_FLOAT: |
| 2671 | case Instruction::SUB_FLOAT: |
| 2672 | case Instruction::MUL_FLOAT: |
| 2673 | case Instruction::DIV_FLOAT: |
| 2674 | case Instruction::REM_FLOAT: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2675 | work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2676 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2677 | break; |
| 2678 | case Instruction::ADD_DOUBLE: |
| 2679 | case Instruction::SUB_DOUBLE: |
| 2680 | case Instruction::MUL_DOUBLE: |
| 2681 | case Instruction::DIV_DOUBLE: |
| 2682 | case Instruction::REM_DOUBLE: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2683 | work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2684 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2685 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | case Instruction::ADD_INT_2ADDR: |
| 2688 | case Instruction::SUB_INT_2ADDR: |
| 2689 | case Instruction::MUL_INT_2ADDR: |
| 2690 | case Instruction::REM_INT_2ADDR: |
| 2691 | case Instruction::SHL_INT_2ADDR: |
| 2692 | case Instruction::SHR_INT_2ADDR: |
| 2693 | case Instruction::USHR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2694 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2695 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2696 | break; |
| 2697 | case Instruction::AND_INT_2ADDR: |
| 2698 | case Instruction::OR_INT_2ADDR: |
| 2699 | case Instruction::XOR_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2700 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2701 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2702 | break; |
| 2703 | case Instruction::DIV_INT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2704 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(), |
| 2705 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2706 | break; |
| 2707 | case Instruction::ADD_LONG_2ADDR: |
| 2708 | case Instruction::SUB_LONG_2ADDR: |
| 2709 | case Instruction::MUL_LONG_2ADDR: |
| 2710 | case Instruction::DIV_LONG_2ADDR: |
| 2711 | case Instruction::REM_LONG_2ADDR: |
| 2712 | case Instruction::AND_LONG_2ADDR: |
| 2713 | case Instruction::OR_LONG_2ADDR: |
| 2714 | case Instruction::XOR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2715 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2716 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2717 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2718 | break; |
| 2719 | case Instruction::SHL_LONG_2ADDR: |
| 2720 | case Instruction::SHR_LONG_2ADDR: |
| 2721 | case Instruction::USHR_LONG_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2722 | work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2723 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | break; |
| 2725 | case Instruction::ADD_FLOAT_2ADDR: |
| 2726 | case Instruction::SUB_FLOAT_2ADDR: |
| 2727 | case Instruction::MUL_FLOAT_2ADDR: |
| 2728 | case Instruction::DIV_FLOAT_2ADDR: |
| 2729 | case Instruction::REM_FLOAT_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2730 | work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(), |
| 2731 | reg_types_.Float(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2732 | break; |
| 2733 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2734 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2735 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2736 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2737 | case Instruction::REM_DOUBLE_2ADDR: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2738 | work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2739 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2740 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2741 | break; |
| 2742 | case Instruction::ADD_INT_LIT16: |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 2743 | case Instruction::RSUB_INT_LIT16: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | case Instruction::MUL_INT_LIT16: |
| 2745 | case Instruction::DIV_INT_LIT16: |
| 2746 | case Instruction::REM_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2747 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2748 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2749 | break; |
| 2750 | case Instruction::AND_INT_LIT16: |
| 2751 | case Instruction::OR_INT_LIT16: |
| 2752 | case Instruction::XOR_INT_LIT16: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2753 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2754 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2755 | break; |
| 2756 | case Instruction::ADD_INT_LIT8: |
| 2757 | case Instruction::RSUB_INT_LIT8: |
| 2758 | case Instruction::MUL_INT_LIT8: |
| 2759 | case Instruction::DIV_INT_LIT8: |
| 2760 | case Instruction::REM_INT_LIT8: |
| 2761 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2762 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2763 | case Instruction::USHR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2764 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false, |
| 2765 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2766 | break; |
| 2767 | case Instruction::AND_INT_LIT8: |
| 2768 | case Instruction::OR_INT_LIT8: |
| 2769 | case Instruction::XOR_INT_LIT8: |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2770 | work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true, |
| 2771 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2772 | break; |
| 2773 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2774 | // Special instructions. |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2775 | case Instruction::RETURN_VOID_NO_BARRIER: |
| 2776 | if (IsConstructor() && !IsStatic()) { |
| 2777 | auto& declaring_class = GetDeclaringClass(); |
| 2778 | auto* klass = declaring_class.GetClass(); |
| 2779 | for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) { |
| 2780 | if (klass->GetInstanceField(i)->IsFinal()) { |
Mathieu Chartier | e86deef | 2015-03-19 13:43:37 -0700 | [diff] [blame] | 2781 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for " |
| 2782 | << PrettyField(klass->GetInstanceField(i)); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 2783 | break; |
| 2784 | } |
| 2785 | } |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2786 | } |
| 2787 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2788 | // Note: the following instructions encode offsets derived from class linking. |
| 2789 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2790 | // meaning if the class linking and resolution were successful. |
| 2791 | case Instruction::IGET_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2792 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2793 | break; |
| 2794 | case Instruction::IGET_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2795 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2796 | break; |
| 2797 | case Instruction::IGET_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2798 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2799 | break; |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 2800 | case Instruction::IGET_BOOLEAN_QUICK: |
| 2801 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true); |
| 2802 | break; |
| 2803 | case Instruction::IGET_BYTE_QUICK: |
| 2804 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true); |
| 2805 | break; |
| 2806 | case Instruction::IGET_CHAR_QUICK: |
| 2807 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true); |
| 2808 | break; |
| 2809 | case Instruction::IGET_SHORT_QUICK: |
| 2810 | VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true); |
| 2811 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2812 | case Instruction::IPUT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2813 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2814 | break; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2815 | case Instruction::IPUT_BOOLEAN_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2816 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | case Instruction::IPUT_BYTE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2819 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2820 | break; |
| 2821 | case Instruction::IPUT_CHAR_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2822 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | case Instruction::IPUT_SHORT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2825 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2826 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2827 | case Instruction::IPUT_WIDE_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2828 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2829 | break; |
| 2830 | case Instruction::IPUT_OBJECT_QUICK: |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 2831 | VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2832 | break; |
| 2833 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2834 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2835 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2836 | mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2837 | if (called_method != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 2838 | const char* descriptor = called_method->GetReturnTypeDescriptor(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 2839 | const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2840 | if (!return_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2841 | work_line_->SetResultRegisterType(this, return_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2842 | } else { |
| 2843 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2844 | } |
| 2845 | just_set_result = true; |
| 2846 | } |
| 2847 | break; |
| 2848 | } |
| 2849 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2850 | /* These should never appear during verification. */ |
Mathieu Chartier | ffc605c | 2014-12-10 10:35:44 -0800 | [diff] [blame] | 2851 | case Instruction::UNUSED_3E ... Instruction::UNUSED_43: |
| 2852 | case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2853 | case Instruction::UNUSED_79: |
| 2854 | case Instruction::UNUSED_7A: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2855 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2856 | break; |
| 2857 | |
| 2858 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2859 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2860 | * complain if an instruction is missing (which is desirable). |
| 2861 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2862 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2863 | |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 2864 | /* |
| 2865 | * If we are in a constructor, and we had an UninitializedThis type |
| 2866 | * in a register somewhere, we need to make sure it wasn't overwritten. |
| 2867 | */ |
| 2868 | if (track_uninitialized_this) { |
| 2869 | bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT || |
| 2870 | inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
| 2871 | if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc, |
| 2872 | was_invoke_direct)) { |
| 2873 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 2874 | << "Constructor failed to initialize this object"; |
| 2875 | } |
| 2876 | } |
| 2877 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2878 | if (have_pending_hard_failure_) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2879 | if (Runtime::Current()->IsAotCompiler()) { |
| 2880 | /* When AOT compiling, check that the last failure is a hard failure */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2881 | CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2882 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2883 | /* immediate failure, reject class */ |
| 2884 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 2885 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2886 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2887 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2888 | opcode_flags = Instruction::kThrow; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2889 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2890 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2891 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2892 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2893 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2894 | */ |
| 2895 | if (!just_set_result) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2896 | work_line_->SetResultTypeToUnknown(this); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2897 | } |
| 2898 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2899 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2900 | |
| 2901 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2902 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2903 | * |
| 2904 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2905 | * 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] | 2906 | * somebody could get a reference field, check it for zero, and if the |
| 2907 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2908 | * 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] | 2909 | * that, and will reject the code. |
| 2910 | * |
| 2911 | * TODO: avoid re-fetching the branch target |
| 2912 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2913 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2914 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2915 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2916 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2917 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2918 | return false; |
| 2919 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2920 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 2921 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2922 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2923 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2924 | /* update branch target, set "changed" if appropriate */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2925 | if (nullptr != branch_line.get()) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2926 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2927 | return false; |
| 2928 | } |
| 2929 | } else { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2930 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) { |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2931 | return false; |
| 2932 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2933 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2937 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2938 | * |
| 2939 | * We've already verified that the table is structurally sound, so we |
| 2940 | * just need to walk through and tag the targets. |
| 2941 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2942 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2943 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 2944 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 2945 | int switch_count = switch_insns[1]; |
| 2946 | int offset_to_targets, targ; |
| 2947 | |
| 2948 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 2949 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 2950 | offset_to_targets = 4; |
| 2951 | } else { |
| 2952 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2953 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2954 | offset_to_targets = 2 + 2 * switch_count; |
| 2955 | } |
| 2956 | |
| 2957 | /* verify each switch target */ |
| 2958 | for (targ = 0; targ < switch_count; targ++) { |
| 2959 | int offset; |
| 2960 | uint32_t abs_offset; |
| 2961 | |
| 2962 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 2963 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 2964 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2965 | abs_offset = work_insn_idx_ + offset; |
| 2966 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 2967 | if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2968 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2969 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2970 | if (!UpdateRegisters(abs_offset, work_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2971 | return false; |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 2972 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2977 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 2978 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2979 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2980 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 2981 | bool has_catch_all_handler = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2982 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2983 | |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 2984 | // Need the linker to try and resolve the handled class to check if it's Throwable. |
| 2985 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 2986 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2987 | for (; iterator.HasNext(); iterator.Next()) { |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 2988 | uint16_t handler_type_idx = iterator.GetHandlerTypeIndex(); |
| 2989 | if (handler_type_idx == DexFile::kDexNoIndex16) { |
| 2990 | has_catch_all_handler = true; |
| 2991 | } else { |
| 2992 | // It is also a catch-all if it is java.lang.Throwable. |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2993 | mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_, |
| 2994 | class_loader_); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 2995 | if (klass != nullptr) { |
| 2996 | if (klass == mirror::Throwable::GetJavaLangThrowable()) { |
| 2997 | has_catch_all_handler = true; |
| 2998 | } |
| 2999 | } else { |
| 3000 | // Clear exception. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3001 | DCHECK(self_->IsExceptionPending()); |
| 3002 | self_->ClearException(); |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3003 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3004 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3005 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3006 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 3007 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 3008 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3009 | */ |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3010 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3011 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3012 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3013 | } |
| 3014 | |
| 3015 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3016 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 3017 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3018 | */ |
Andreas Gampe | f91baf1 | 2014-07-18 15:41:00 -0700 | [diff] [blame] | 3019 | if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3020 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3021 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 3022 | * 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] | 3023 | * it will do so before grabbing the lock). |
| 3024 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3025 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3026 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3027 | << "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] | 3028 | return false; |
| 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3033 | /* Handle "continue". Tag the next consecutive instruction. |
| 3034 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 3035 | * because it changes work_line_ when performing peephole optimization |
| 3036 | * and this change should not be used in those cases. |
| 3037 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3038 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3039 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3040 | uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits(); |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3041 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 3042 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 3043 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3044 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3045 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 3046 | // next instruction isn't one. |
| 3047 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 3048 | return false; |
| 3049 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3050 | if (nullptr != fallthrough_line.get()) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3051 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 3052 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 3053 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3054 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 3055 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3056 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 3057 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 3058 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 3059 | SafelyMarkAllRegistersAsConflicts(this, work_line_.get()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3060 | } else { |
| 3061 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3062 | work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3063 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3064 | work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3068 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3069 | if (next_line != nullptr) { |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 3070 | // Merge registers into what we have for the next instruction, and set the "changed" flag if |
| 3071 | // needed. If the merge changes the state of the registers then the work line will be |
| 3072 | // updated. |
| 3073 | if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) { |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3074 | return false; |
| 3075 | } |
| 3076 | } else { |
| 3077 | /* |
| 3078 | * We're not recording register data for the next instruction, so we don't know what the |
| 3079 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 3080 | */ |
| 3081 | insn_flags_[next_insn_idx].SetChanged(); |
| 3082 | } |
| 3083 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 3084 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3085 | /* 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] | 3086 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3087 | if (!work_line_->VerifyMonitorStackEmpty(this)) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3088 | return false; |
| 3089 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 3093 | * Update start_guess. Advance to the next instruction of that's |
| 3094 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3095 | * neither of those exists we're in a return or throw; leave start_guess |
| 3096 | * alone and let the caller sort it out. |
| 3097 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3098 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3099 | DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst); |
| 3100 | *start_guess = work_insn_idx_ + inst->SizeInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3101 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3102 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3103 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3104 | } |
| 3105 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3106 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 3107 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3108 | |
| 3109 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3110 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3111 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3112 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3113 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3114 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3115 | mirror::Class* klass = dex_cache_->GetResolvedType(class_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3116 | const RegType& result = klass != nullptr ? |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3117 | reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) : |
| 3118 | reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3119 | if (result.IsConflict()) { |
| 3120 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 3121 | << "' in " << referrer; |
| 3122 | return result; |
| 3123 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3124 | if (klass == nullptr && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3125 | dex_cache_->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3126 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3127 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3128 | // check at runtime if access is allowed and so pass here. If result is |
| 3129 | // primitive, skip the access check. |
| 3130 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 3131 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3132 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3133 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3134 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3135 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3136 | } |
| 3137 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3138 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3139 | const RegType* common_super = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3140 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3141 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3142 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3143 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3144 | CatchHandlerIterator iterator(handlers_ptr); |
| 3145 | for (; iterator.HasNext(); iterator.Next()) { |
| 3146 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 3147 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 3148 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3149 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3150 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3151 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 3152 | if (exception.IsUnresolvedTypes()) { |
| 3153 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 3154 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 3155 | return exception; |
| 3156 | } else { |
| 3157 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 3158 | return reg_types_.Conflict(); |
| 3159 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 3160 | } else if (common_super == nullptr) { |
| 3161 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3162 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3163 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3164 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3165 | common_super = &common_super->Merge(exception, ®_types_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3166 | if (FailOrAbort(this, |
| 3167 | reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super), |
| 3168 | "java.lang.Throwable is not assignable-from common_super at ", |
| 3169 | work_insn_idx_)) { |
| 3170 | break; |
| 3171 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3176 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3177 | } |
| 3178 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3179 | if (common_super == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3180 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3181 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3182 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3183 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 3184 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3187 | mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3188 | MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3189 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3190 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3191 | if (klass_type.IsConflict()) { |
| 3192 | std::string append(" in attempt to access method "); |
| 3193 | append += dex_file_->GetMethodName(method_id); |
| 3194 | AppendToLastFailMessage(append); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3195 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3196 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3197 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3198 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3199 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3200 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3201 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3202 | mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3203 | if (res_method == nullptr) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3204 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3205 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3206 | |
| 3207 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3208 | res_method = klass->FindDirectMethod(name, signature); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3209 | } else if (method_type == METHOD_INTERFACE) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3210 | res_method = klass->FindInterfaceMethod(name, signature); |
| 3211 | } else { |
| 3212 | res_method = klass->FindVirtualMethod(name, signature); |
| 3213 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3214 | if (res_method != nullptr) { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3215 | dex_cache_->SetResolvedMethod(dex_method_idx, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3216 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3217 | // If a virtual or interface method wasn't found with the expected type, look in |
| 3218 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 3219 | // a class has changed, and will be flagged as an error in later checks. |
| 3220 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
| 3221 | res_method = klass->FindDirectMethod(name, signature); |
| 3222 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3223 | if (res_method == nullptr) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3224 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 3225 | << PrettyDescriptor(klass) << "." << name |
| 3226 | << " " << signature; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3227 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3228 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3229 | } |
| 3230 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3231 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 3232 | // enforce them here. |
| 3233 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3234 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 3235 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3236 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3237 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3238 | // Disallow any calls to class initializers. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3239 | if (res_method->IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3240 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 3241 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3242 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3243 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3244 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3245 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3246 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3247 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3248 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3249 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3250 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 3251 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3252 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3253 | << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3254 | return nullptr; |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3255 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3256 | // Check that interface methods match interface classes. |
| 3257 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 3258 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 3259 | << " is in an interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3260 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3261 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 3262 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 3263 | << " is in a non-interface class " << PrettyClass(klass); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3264 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3265 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3266 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3267 | // target method. |
Brian Carlstrom | be6fa5e | 2014-12-09 20:15:42 -0800 | [diff] [blame] | 3268 | if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) || |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3269 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3270 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3271 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3272 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3273 | " type of " << PrettyMethod(res_method); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3274 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3275 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3276 | return res_method; |
| 3277 | } |
| 3278 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3279 | template <class T> |
| 3280 | mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst, |
| 3281 | MethodType method_type, |
| 3282 | bool is_range, |
| 3283 | mirror::ArtMethod* res_method) { |
| 3284 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3285 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3286 | // definition (which doesn't have register count values). |
| 3287 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3288 | /* caught by static verifier */ |
| 3289 | DCHECK(is_range || expected_args <= 5); |
| 3290 | if (expected_args > code_item_->outs_size_) { |
| 3291 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3292 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3293 | return nullptr; |
| 3294 | } |
| 3295 | |
| 3296 | uint32_t arg[5]; |
| 3297 | if (!is_range) { |
| 3298 | inst->GetVarArgs(arg); |
| 3299 | } |
| 3300 | uint32_t sig_registers = 0; |
| 3301 | |
| 3302 | /* |
| 3303 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3304 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3305 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3306 | */ |
| 3307 | if (method_type != METHOD_STATIC) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3308 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3309 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3310 | CHECK(have_pending_hard_failure_); |
| 3311 | return nullptr; |
| 3312 | } |
| 3313 | if (actual_arg_type.IsUninitializedReference()) { |
| 3314 | if (res_method) { |
| 3315 | if (!res_method->IsConstructor()) { |
| 3316 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3317 | return nullptr; |
| 3318 | } |
| 3319 | } else { |
| 3320 | // Check whether the name of the called method is "<init>" |
| 3321 | 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] | 3322 | if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3323 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3324 | return nullptr; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3329 | const RegType* res_method_class; |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3330 | if (res_method != nullptr) { |
| 3331 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3332 | std::string temp; |
| 3333 | res_method_class = ®_types_.FromClass(klass->GetDescriptor(&temp), klass, |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3334 | klass->CannotBeAssignedFromOtherTypes()); |
| 3335 | } else { |
| 3336 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3337 | const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_; |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3338 | res_method_class = ®_types_.FromDescriptor(GetClassLoader(), |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3339 | dex_file_->StringByTypeIdx(class_idx), |
| 3340 | false); |
| 3341 | } |
| 3342 | if (!res_method_class->IsAssignableFrom(actual_arg_type)) { |
| 3343 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3344 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
| 3345 | << "' not instance of '" << *res_method_class << "'"; |
| 3346 | // Continue on soft failures. We need to find possible hard failures to avoid problems in |
| 3347 | // the compiler. |
| 3348 | if (have_pending_hard_failure_) { |
| 3349 | return nullptr; |
| 3350 | } |
| 3351 | } |
| 3352 | } |
| 3353 | sig_registers = 1; |
| 3354 | } |
| 3355 | |
| 3356 | for ( ; it->HasNext(); it->Next()) { |
| 3357 | if (sig_registers >= expected_args) { |
| 3358 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() << |
| 3359 | " arguments, found " << sig_registers << " or more."; |
| 3360 | return nullptr; |
| 3361 | } |
| 3362 | |
| 3363 | const char* param_descriptor = it->GetDescriptor(); |
| 3364 | |
| 3365 | if (param_descriptor == nullptr) { |
| 3366 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature " |
| 3367 | "component"; |
| 3368 | return nullptr; |
| 3369 | } |
| 3370 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3371 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3372 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) : |
| 3373 | arg[sig_registers]; |
| 3374 | if (reg_type.IsIntegralTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3375 | const RegType& src_type = work_line_->GetRegisterType(this, get_reg); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3376 | if (!src_type.IsIntegralTypes()) { |
| 3377 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3378 | << " but expected " << reg_type; |
| 3379 | return res_method; |
| 3380 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3381 | } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3382 | // Continue on soft failures. We need to find possible hard failures to avoid problems in the |
| 3383 | // compiler. |
| 3384 | if (have_pending_hard_failure_) { |
| 3385 | return res_method; |
| 3386 | } |
| 3387 | } |
| 3388 | sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1; |
| 3389 | } |
| 3390 | if (expected_args != sig_registers) { |
| 3391 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args << |
| 3392 | " arguments, found " << sig_registers; |
| 3393 | return nullptr; |
| 3394 | } |
| 3395 | return res_method; |
| 3396 | } |
| 3397 | |
| 3398 | void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, |
| 3399 | MethodType method_type, |
| 3400 | bool is_range) { |
| 3401 | // As the method may not have been resolved, make this static check against what we expect. |
| 3402 | // The main reason for this code block is to fail hard when we find an illegal use, e.g., |
| 3403 | // wrong number of arguments or wrong primitive types, even if the method could not be resolved. |
| 3404 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3405 | DexFileParameterIterator it(*dex_file_, |
| 3406 | dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_)); |
| 3407 | VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range, |
| 3408 | nullptr); |
| 3409 | } |
| 3410 | |
| 3411 | class MethodParamListDescriptorIterator { |
| 3412 | public: |
| 3413 | explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) : |
| 3414 | res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()), |
| 3415 | params_size_(params_ == nullptr ? 0 : params_->Size()) { |
| 3416 | } |
| 3417 | |
| 3418 | bool HasNext() { |
| 3419 | return pos_ < params_size_; |
| 3420 | } |
| 3421 | |
| 3422 | void Next() { |
| 3423 | ++pos_; |
| 3424 | } |
| 3425 | |
| 3426 | const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3427 | return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_); |
| 3428 | } |
| 3429 | |
| 3430 | private: |
| 3431 | mirror::ArtMethod* res_method_; |
| 3432 | size_t pos_; |
| 3433 | const DexFile::TypeList* params_; |
| 3434 | const size_t params_size_; |
| 3435 | }; |
| 3436 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3437 | mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3438 | MethodType method_type, |
| 3439 | bool is_range, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3440 | bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3441 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3442 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3443 | 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] | 3444 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3445 | mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3446 | if (res_method == nullptr) { // error or class is unresolved |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3447 | // Check what we can statically. |
| 3448 | if (!have_pending_hard_failure_) { |
| 3449 | VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range); |
| 3450 | } |
| 3451 | return nullptr; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3454 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3455 | // has a vtable entry for the target method. |
| 3456 | if (is_super) { |
| 3457 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3458 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3459 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3460 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3461 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3462 | << " to super " << PrettyMethod(res_method); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3463 | return nullptr; |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3464 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3465 | mirror::Class* super_klass = super.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3466 | if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3467 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3468 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3469 | << " to super " << super |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3470 | << "." << res_method->GetName() |
| 3471 | << res_method->GetSignature(); |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3472 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3473 | } |
| 3474 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3475 | |
Andreas Gampe | 95c0bf8 | 2014-06-16 14:06:52 -0700 | [diff] [blame] | 3476 | // Process the target method's signature. This signature may or may not |
| 3477 | MethodParamListDescriptorIterator it(res_method); |
| 3478 | return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type, |
| 3479 | is_range, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3480 | } |
| 3481 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3482 | mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3483 | RegisterLine* reg_line, bool is_range, |
| 3484 | bool allow_failure) { |
| 3485 | if (is_range) { |
| 3486 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3487 | } else { |
| 3488 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK); |
| 3489 | } |
| 3490 | 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] | 3491 | if (!actual_arg_type.HasClass()) { |
| 3492 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 3493 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3494 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3495 | mirror::Class* klass = actual_arg_type.GetClass(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3496 | mirror::Class* dispatch_class; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3497 | if (klass->IsInterface()) { |
| 3498 | // Derive Object.class from Class.class.getSuperclass(). |
| 3499 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3500 | if (FailOrAbort(this, object_klass->IsObjectClass(), |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3501 | "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) { |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3502 | return nullptr; |
| 3503 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3504 | dispatch_class = object_klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3505 | } else { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3506 | dispatch_class = klass; |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3507 | } |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3508 | if (!dispatch_class->HasVTable()) { |
| 3509 | FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ", |
| 3510 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3511 | return nullptr; |
| 3512 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3513 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3514 | if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) { |
| 3515 | FailOrAbort(this, allow_failure, |
| 3516 | "Receiver class has not enough vtable slots for quickened invoke at ", |
| 3517 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3518 | return nullptr; |
| 3519 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3520 | mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index); |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3521 | if (self_->IsExceptionPending()) { |
| 3522 | FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ", |
| 3523 | work_insn_idx_); |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3524 | return nullptr; |
| 3525 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3526 | return res_method; |
| 3527 | } |
| 3528 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3529 | mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 3530 | bool is_range) { |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 3531 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_) |
| 3532 | << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_; |
| 3533 | |
Mathieu Chartier | 091d238 | 2015-03-06 10:59:06 -0800 | [diff] [blame] | 3534 | mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3535 | if (res_method == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3536 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3537 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3538 | } |
Andreas Gampe | 7c03810 | 2014-10-27 20:08:46 -0700 | [diff] [blame] | 3539 | if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ", |
| 3540 | work_insn_idx_)) { |
| 3541 | return nullptr; |
| 3542 | } |
| 3543 | if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ", |
| 3544 | work_insn_idx_)) { |
| 3545 | return nullptr; |
| 3546 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3547 | |
| 3548 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3549 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3550 | // definition (which doesn't have register count values). |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3551 | const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3552 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3553 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3554 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3555 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3556 | /* caught by static verifier */ |
| 3557 | DCHECK(is_range || expected_args <= 5); |
| 3558 | if (expected_args > code_item_->outs_size_) { |
| 3559 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3560 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3561 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3566 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3567 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3568 | */ |
| 3569 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3570 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3571 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3572 | } |
| 3573 | if (!actual_arg_type.IsZero()) { |
| 3574 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3575 | std::string temp; |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3576 | const RegType& res_method_class = |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3577 | reg_types_.FromClass(klass->GetDescriptor(&temp), klass, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3578 | klass->CannotBeAssignedFromOtherTypes()); |
| 3579 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3580 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3581 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3582 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3583 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | /* |
| 3587 | * Process the target method's signature. This signature may or may not |
| 3588 | * have been verified, so we can't assume it's properly formed. |
| 3589 | */ |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3590 | const DexFile::TypeList* params = res_method->GetParameterTypeList(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3591 | size_t params_size = params == nullptr ? 0 : params->Size(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3592 | uint32_t arg[5]; |
| 3593 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3594 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3595 | } |
| 3596 | size_t actual_args = 1; |
| 3597 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3598 | if (actual_args >= expected_args) { |
| 3599 | 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] | 3600 | << "'. Expected " << expected_args |
| 3601 | << " arguments, processing argument " << actual_args |
| 3602 | << " (where longs/doubles count twice)."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3603 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3604 | } |
| 3605 | const char* descriptor = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3606 | res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3607 | if (descriptor == nullptr) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3608 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3609 | << " missing signature component"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3610 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3611 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3612 | const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3613 | 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] | 3614 | if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3615 | return res_method; |
| 3616 | } |
| 3617 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3618 | } |
| 3619 | if (actual_args != expected_args) { |
| 3620 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3621 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3622 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3623 | } else { |
| 3624 | return res_method; |
| 3625 | } |
| 3626 | } |
| 3627 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3628 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3629 | uint32_t type_idx; |
| 3630 | if (!is_filled) { |
| 3631 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3632 | type_idx = inst->VRegC_22c(); |
| 3633 | } else if (!is_range) { |
| 3634 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3635 | type_idx = inst->VRegB_35c(); |
| 3636 | } else { |
| 3637 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3638 | type_idx = inst->VRegB_3rc(); |
| 3639 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3640 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3641 | if (res_type.IsConflict()) { // bad class |
| 3642 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3643 | } else { |
| 3644 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3645 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3646 | 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] | 3647 | } else if (!is_filled) { |
| 3648 | /* make sure "size" register is valid type */ |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3649 | work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3650 | /* set register type to array class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3651 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3652 | work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3653 | } else { |
| 3654 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3655 | // 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] | 3656 | const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3657 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3658 | uint32_t arg[5]; |
| 3659 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3660 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3661 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3662 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3663 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3664 | if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) { |
| 3665 | work_line_->SetResultRegisterType(this, reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3666 | return; |
| 3667 | } |
| 3668 | } |
| 3669 | // filled-array result goes into "result" register |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3670 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3671 | work_line_->SetResultRegisterType(this, precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3672 | } |
| 3673 | } |
| 3674 | } |
| 3675 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3676 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3677 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3678 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3679 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3680 | 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] | 3681 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3682 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3683 | if (array_type.IsZero()) { |
| 3684 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3685 | // instruction type. TODO: have a proper notion of bottom here. |
| 3686 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3687 | // Reference or category 1 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3688 | work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3689 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3690 | // Category 2 |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3691 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), |
| 3692 | reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3693 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3694 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3695 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3696 | 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] | 3697 | } else { |
| 3698 | /* verify the class */ |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3699 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3700 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3701 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3702 | << " source for aget-object"; |
| 3703 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3704 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3705 | << " source for category 1 aget"; |
| 3706 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3707 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3708 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3709 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3710 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3711 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3712 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3713 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3714 | // doubles. |
| 3715 | if (!component_type.IsLowHalf()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3716 | work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3717 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3718 | work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3719 | component_type.HighHalf(®_types_)); |
| 3720 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3721 | } |
| 3722 | } |
| 3723 | } |
| 3724 | } |
| 3725 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3726 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3727 | const uint32_t vregA) { |
| 3728 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3729 | bool instruction_compatible; |
| 3730 | bool value_compatible; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3731 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3732 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3733 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3734 | value_compatible = value_type.IsIntegralTypes(); |
| 3735 | } else if (target_type.IsFloat()) { |
| 3736 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3737 | value_compatible = value_type.IsFloatTypes(); |
| 3738 | } else if (target_type.IsLong()) { |
| 3739 | instruction_compatible = insn_type.IsLong(); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3740 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3741 | // as target_type depends on the resolved type of the field. |
| 3742 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3743 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3744 | value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi); |
| 3745 | } else { |
| 3746 | value_compatible = false; |
| 3747 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3748 | } else if (target_type.IsDouble()) { |
| 3749 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3750 | // Additional register check: this is not checked statically (as part of VerifyInstructions), |
| 3751 | // as target_type depends on the resolved type of the field. |
| 3752 | if (instruction_compatible && work_line_->NumRegs() > vregA + 1) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3753 | const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1); |
Andreas Gampe | 376fa68 | 2014-09-07 13:06:12 -0700 | [diff] [blame] | 3754 | value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi); |
| 3755 | } else { |
| 3756 | value_compatible = false; |
| 3757 | } |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3758 | } else { |
| 3759 | instruction_compatible = false; // reference with primitive store |
| 3760 | value_compatible = false; // unused |
| 3761 | } |
| 3762 | if (!instruction_compatible) { |
| 3763 | // This is a global failure rather than a class change failure as the instructions and |
| 3764 | // the descriptors for the type should have been consistent within the same file at |
| 3765 | // compile time. |
| 3766 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3767 | << "' but expected type '" << target_type << "'"; |
| 3768 | return; |
| 3769 | } |
| 3770 | if (!value_compatible) { |
| 3771 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3772 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3773 | return; |
| 3774 | } |
| 3775 | } |
| 3776 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3777 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3778 | const RegType& insn_type, bool is_primitive) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3779 | const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3780 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3781 | 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] | 3782 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3783 | const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3784 | if (array_type.IsZero()) { |
| 3785 | // Null array type; this code path will fail at runtime. Infer a merge-able type from the |
| 3786 | // instruction type. |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3787 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3788 | 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] | 3789 | } else { |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3790 | const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3791 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3792 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3793 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3794 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3795 | if (!component_type.IsReferenceTypes()) { |
| 3796 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3797 | << " source for aput-object"; |
| 3798 | } else { |
| 3799 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3800 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3801 | // incompatible classes will be caught at runtime as an array store exception |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3802 | work_line_->VerifyRegisterType(this, vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3803 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3804 | } |
| 3805 | } |
| 3806 | } |
| 3807 | } |
| 3808 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3809 | ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3810 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3811 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3812 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3813 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3814 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3815 | field_idx, dex_file_->GetFieldName(field_id), |
| 3816 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3817 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3818 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3819 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3820 | 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] | 3821 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3822 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3823 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 3824 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3825 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3826 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3827 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3828 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3829 | DCHECK(self_->IsExceptionPending()); |
| 3830 | self_->ClearException(); |
| 3831 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3832 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3833 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3834 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3835 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3836 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3837 | } else if (!field->IsStatic()) { |
| 3838 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3839 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3840 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3841 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3842 | } |
| 3843 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3844 | ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3845 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3846 | // Check access to class |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3847 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3848 | if (klass_type.IsConflict()) { |
| 3849 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 3850 | field_idx, dex_file_->GetFieldName(field_id), |
| 3851 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3852 | return nullptr; |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3853 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3854 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3855 | return nullptr; // Can't resolve Class so no more to do here |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3856 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3857 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3858 | ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_, |
| 3859 | class_loader_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3860 | if (field == nullptr) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3861 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3862 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3863 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3864 | DCHECK(self_->IsExceptionPending()); |
| 3865 | self_->ClearException(); |
| 3866 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3867 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3868 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3869 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3870 | << " from " << GetDeclaringClass(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3871 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3872 | } else if (field->IsStatic()) { |
| 3873 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3874 | << " to not be static"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3875 | return nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3876 | } else if (obj_type.IsZero()) { |
| 3877 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3878 | return field; |
Stephen Kyle | 695c598 | 2014-08-22 15:03:07 +0100 | [diff] [blame] | 3879 | } else if (!obj_type.IsReferenceTypes()) { |
| 3880 | // Trying to read a field from something that isn't a reference |
| 3881 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has " |
| 3882 | << "non-reference type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3883 | return nullptr; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3884 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3885 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3886 | const RegType& field_klass = |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3887 | reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3888 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3889 | if (obj_type.IsUninitializedTypes() && |
| 3890 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 3891 | !field_klass.Equals(GetDeclaringClass()))) { |
| 3892 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3893 | // the field is declared in this class |
| 3894 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3895 | << " of a not fully initialized object within the context" |
| 3896 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3897 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3898 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 3899 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3900 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3901 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3902 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3903 | << " from object of type " << obj_type; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3904 | return nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3905 | } else { |
| 3906 | return field; |
| 3907 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3908 | } |
| 3909 | } |
| 3910 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3911 | template <MethodVerifier::FieldAccessType kAccType> |
| 3912 | void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 3913 | bool is_primitive, bool is_static) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3914 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3915 | ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3916 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3917 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3918 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3919 | const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3920 | field = GetInstanceField(object_type, field_idx); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3921 | if (UNLIKELY(have_pending_hard_failure_)) { |
| 3922 | return; |
| 3923 | } |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3924 | } |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 3925 | const RegType* field_type = nullptr; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3926 | if (field != nullptr) { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3927 | if (kAccType == FieldAccessType::kAccPut) { |
| 3928 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3929 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3930 | << " from other class " << GetDeclaringClass(); |
| 3931 | return; |
| 3932 | } |
| 3933 | } |
| 3934 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3935 | mirror::Class* field_type_class = |
| 3936 | can_load_classes_ ? field->GetType<true>() : field->GetType<false>(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3937 | if (field_type_class != nullptr) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 3938 | field_type = ®_types_.FromClass(field->GetTypeDescriptor(), field_type_class, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3939 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3940 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3941 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 3942 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3943 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3944 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3945 | if (field_type == nullptr) { |
| 3946 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3947 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 3948 | field_type = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3949 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3950 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3951 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3952 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 3953 | "Unexpected third access type"); |
| 3954 | if (kAccType == FieldAccessType::kAccPut) { |
| 3955 | // sput or iput. |
| 3956 | if (is_primitive) { |
| 3957 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3958 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3959 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 3960 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3961 | << " to be compatible with type '" << insn_type |
| 3962 | << "' but found type '" << *field_type |
| 3963 | << "' in put-object"; |
| 3964 | return; |
| 3965 | } |
| 3966 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3967 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3968 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 3969 | // sget or iget. |
| 3970 | if (is_primitive) { |
| 3971 | if (field_type->Equals(insn_type) || |
| 3972 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 3973 | (field_type->IsDouble() && insn_type.IsLong())) { |
| 3974 | // expected that read is of the correct primitive type or that int reads are reading |
| 3975 | // floats or long reads are reading doubles |
| 3976 | } else { |
| 3977 | // This is a global failure rather than a class change failure as the instructions and |
| 3978 | // the descriptors for the type should have been consistent within the same file at |
| 3979 | // compile time |
| 3980 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3981 | << " to be of type '" << insn_type |
| 3982 | << "' but found type '" << *field_type << "' in get"; |
| 3983 | return; |
| 3984 | } |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3985 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3986 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 3987 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3988 | << " to be compatible with type '" << insn_type |
| 3989 | << "' but found type '" << *field_type |
| 3990 | << "' in get-object"; |
| 3991 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 3992 | return; |
| 3993 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3994 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 3995 | if (!field_type->IsLowHalf()) { |
| 3996 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 3997 | } else { |
| 3998 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
| 3999 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4000 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4001 | LOG(FATAL) << "Unexpected case."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4002 | } |
| 4003 | } |
| 4004 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4005 | ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4006 | RegisterLine* reg_line) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4007 | DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4008 | const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 4009 | if (!object_type.HasClass()) { |
| 4010 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 4011 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4012 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4013 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4014 | ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4015 | DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 4016 | if (f == nullptr) { |
| 4017 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
| 4018 | << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; |
| 4019 | } |
| 4020 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4021 | } |
| 4022 | |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4023 | template <MethodVerifier::FieldAccessType kAccType> |
| 4024 | void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type, |
| 4025 | bool is_primitive) { |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 4026 | DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4027 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4028 | ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4029 | if (field == nullptr) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4030 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 4031 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4032 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4033 | |
| 4034 | // For an IPUT_QUICK, we now test for final flag of the field. |
| 4035 | if (kAccType == FieldAccessType::kAccPut) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4036 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 4037 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 4038 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4039 | return; |
| 4040 | } |
| 4041 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4042 | |
| 4043 | // Get the field type. |
| 4044 | const RegType* field_type; |
| 4045 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4046 | mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() : |
| 4047 | field->GetType<false>(); |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4048 | |
| 4049 | if (field_type_class != nullptr) { |
| 4050 | field_type = ®_types_.FromClass(field->GetTypeDescriptor(), field_type_class, |
| 4051 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4052 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4053 | Thread* self = Thread::Current(); |
| 4054 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 4055 | self->ClearException(); |
| 4056 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 4057 | field->GetTypeDescriptor(), false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4058 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4059 | if (field_type == nullptr) { |
| 4060 | 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] | 4061 | return; |
| 4062 | } |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | const uint32_t vregA = inst->VRegA_22c(); |
| 4066 | static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet, |
| 4067 | "Unexpected third access type"); |
| 4068 | if (kAccType == FieldAccessType::kAccPut) { |
| 4069 | if (is_primitive) { |
| 4070 | // Primitive field assignability rules are weaker than regular assignability rules |
| 4071 | bool instruction_compatible; |
| 4072 | bool value_compatible; |
| 4073 | const RegType& value_type = work_line_->GetRegisterType(this, vregA); |
| 4074 | if (field_type->IsIntegralTypes()) { |
| 4075 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 4076 | value_compatible = value_type.IsIntegralTypes(); |
| 4077 | } else if (field_type->IsFloat()) { |
| 4078 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 4079 | value_compatible = value_type.IsFloatTypes(); |
| 4080 | } else if (field_type->IsLong()) { |
| 4081 | instruction_compatible = insn_type.IsLong(); |
| 4082 | value_compatible = value_type.IsLongTypes(); |
| 4083 | } else if (field_type->IsDouble()) { |
| 4084 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 4085 | value_compatible = value_type.IsDoubleTypes(); |
| 4086 | } else { |
| 4087 | instruction_compatible = false; // reference field with primitive store |
| 4088 | value_compatible = false; // unused |
| 4089 | } |
| 4090 | if (!instruction_compatible) { |
| 4091 | // This is a global failure rather than a class change failure as the instructions and |
| 4092 | // the descriptors for the type should have been consistent within the same file at |
| 4093 | // compile time |
| 4094 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4095 | << " to be of type '" << insn_type |
| 4096 | << "' but found type '" << *field_type |
| 4097 | << "' in put"; |
| 4098 | return; |
| 4099 | } |
| 4100 | if (!value_compatible) { |
| 4101 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 4102 | << " of type " << value_type |
| 4103 | << " but expected " << *field_type |
| 4104 | << " for store to " << PrettyField(field) << " in put"; |
| 4105 | return; |
| 4106 | } |
| 4107 | } else { |
| 4108 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4109 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4110 | << " to be compatible with type '" << insn_type |
| 4111 | << "' but found type '" << *field_type |
| 4112 | << "' in put-object"; |
| 4113 | return; |
| 4114 | } |
| 4115 | work_line_->VerifyRegisterType(this, vregA, *field_type); |
| 4116 | } |
| 4117 | } else if (kAccType == FieldAccessType::kAccGet) { |
| 4118 | if (is_primitive) { |
| 4119 | if (field_type->Equals(insn_type) || |
| 4120 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 4121 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
| 4122 | // expected that read is of the correct primitive type or that int reads are reading |
| 4123 | // floats or long reads are reading doubles |
| 4124 | } else { |
| 4125 | // This is a global failure rather than a class change failure as the instructions and |
| 4126 | // the descriptors for the type should have been consistent within the same file at |
| 4127 | // compile time |
| 4128 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 4129 | << " to be of type '" << insn_type |
| 4130 | << "' but found type '" << *field_type << "' in Get"; |
| 4131 | return; |
| 4132 | } |
| 4133 | } else { |
| 4134 | if (!insn_type.IsAssignableFrom(*field_type)) { |
| 4135 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 4136 | << " to be compatible with type '" << insn_type |
| 4137 | << "' but found type '" << *field_type |
| 4138 | << "' in get-object"; |
| 4139 | work_line_->SetRegisterType(this, vregA, reg_types_.Conflict()); |
| 4140 | return; |
| 4141 | } |
| 4142 | } |
| 4143 | if (!field_type->IsLowHalf()) { |
| 4144 | work_line_->SetRegisterType(this, vregA, *field_type); |
| 4145 | } else { |
| 4146 | work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4147 | } |
| 4148 | } else { |
Andreas Gampe | 896df40 | 2014-10-20 22:25:29 -0700 | [diff] [blame] | 4149 | LOG(FATAL) << "Unexpected case."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 4150 | } |
| 4151 | } |
| 4152 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4153 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4154 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4155 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4156 | return false; |
| 4157 | } |
| 4158 | return true; |
| 4159 | } |
| 4160 | |
Stephen Kyle | 9bc6199 | 2014-09-22 13:53:15 +0100 | [diff] [blame] | 4161 | bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) { |
| 4162 | if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) && |
| 4163 | ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) { |
| 4164 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*"; |
| 4165 | return false; |
| 4166 | } |
| 4167 | return true; |
| 4168 | } |
| 4169 | |
| 4170 | bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) { |
| 4171 | return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx)); |
| 4172 | } |
| 4173 | |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4174 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, |
| 4175 | bool update_merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4176 | bool changed = true; |
| 4177 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 4178 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4179 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4180 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 4181 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 4182 | * 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] | 4183 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4184 | if (!insn_flags_[next_insn].IsReturn()) { |
| 4185 | target_line->CopyFromLine(merge_line); |
| 4186 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4187 | // Verify that the monitor stack is empty on return. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4188 | if (!merge_line->VerifyMonitorStackEmpty(this)) { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 4189 | return false; |
| 4190 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4191 | // For returns we only care about the operand to the return, all other registers are dead. |
| 4192 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 4193 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 4194 | Instruction::Code opcode = ret_inst->Opcode(); |
Mathieu Chartier | d7cbf8a | 2015-03-19 12:43:20 -0700 | [diff] [blame] | 4195 | if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) { |
Stephen Kyle | 7e541c9 | 2014-12-17 17:10:02 +0000 | [diff] [blame] | 4196 | SafelyMarkAllRegistersAsConflicts(this, target_line); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4197 | } else { |
| 4198 | target_line->CopyFromLine(merge_line); |
| 4199 | if (opcode == Instruction::RETURN_WIDE) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4200 | target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4201 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4202 | target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x()); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4206 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4207 | std::unique_ptr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 4208 | RegisterLine::Create(target_line->NumRegs(), this) : |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4209 | nullptr); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4210 | if (gDebugVerify) { |
| 4211 | copy->CopyFromLine(target_line); |
| 4212 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4213 | changed = target_line->MergeRegisters(this, merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4214 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4215 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4216 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 4217 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 4218 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 4219 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4220 | << copy->Dump(this) << " MERGE\n" |
| 4221 | << merge_line->Dump(this) << " ==\n" |
| 4222 | << target_line->Dump(this) << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4223 | } |
Ian Rogers | ebbdd87 | 2014-07-07 23:53:08 -0700 | [diff] [blame] | 4224 | if (update_merge_line && changed) { |
| 4225 | merge_line->CopyFromLine(target_line); |
| 4226 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4227 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4228 | if (changed) { |
| 4229 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4230 | } |
| 4231 | return true; |
| 4232 | } |
| 4233 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4234 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4235 | return &insn_flags_[work_insn_idx_]; |
| 4236 | } |
| 4237 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4238 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4239 | if (return_type_ == nullptr) { |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 4240 | if (mirror_method_.Get() != nullptr) { |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4241 | mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4242 | if (return_type_class != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4243 | return_type_ = ®_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(), |
| 4244 | return_type_class, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4245 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4246 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4247 | DCHECK(!can_load_classes_ || self_->IsExceptionPending()); |
| 4248 | self_->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | if (return_type_ == nullptr) { |
| 4252 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 4253 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 4254 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 4255 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4256 | return_type_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4257 | } |
| 4258 | } |
| 4259 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4262 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4263 | if (declaring_class_ == nullptr) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4264 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4265 | const char* descriptor |
| 4266 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Hiroshi Yamauchi | dc37617 | 2014-08-22 11:13:12 -0700 | [diff] [blame] | 4267 | if (mirror_method_.Get() != nullptr) { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4268 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 4269 | declaring_class_ = ®_types_.FromClass(descriptor, klass, |
| 4270 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4271 | } else { |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4272 | declaring_class_ = ®_types_.FromDescriptor(GetClassLoader(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4273 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4274 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4275 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4278 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4279 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 4280 | 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] | 4281 | std::vector<int32_t> result; |
| 4282 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4283 | const RegType& type = line->GetRegisterType(this, i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4284 | if (type.IsConstant()) { |
| 4285 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4286 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4287 | result.push_back(const_val->ConstantValue()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4288 | } else if (type.IsConstantLo()) { |
| 4289 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4290 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4291 | result.push_back(const_val->ConstantValueLo()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4292 | } else if (type.IsConstantHi()) { |
| 4293 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4294 | const ConstantType* const_val = down_cast<const ConstantType*>(&type); |
| 4295 | result.push_back(const_val->ConstantValueHi()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4296 | } else if (type.IsIntegralTypes()) { |
| 4297 | result.push_back(kIntVReg); |
| 4298 | result.push_back(0); |
| 4299 | } else if (type.IsFloat()) { |
| 4300 | result.push_back(kFloatVReg); |
| 4301 | result.push_back(0); |
| 4302 | } else if (type.IsLong()) { |
| 4303 | result.push_back(kLongLoVReg); |
| 4304 | result.push_back(0); |
| 4305 | result.push_back(kLongHiVReg); |
| 4306 | result.push_back(0); |
| 4307 | ++i; |
| 4308 | } else if (type.IsDouble()) { |
| 4309 | result.push_back(kDoubleLoVReg); |
| 4310 | result.push_back(0); |
| 4311 | result.push_back(kDoubleHiVReg); |
| 4312 | result.push_back(0); |
| 4313 | ++i; |
| 4314 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 4315 | result.push_back(kUndefined); |
| 4316 | result.push_back(0); |
| 4317 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4318 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4319 | result.push_back(kReferenceVReg); |
| 4320 | result.push_back(0); |
| 4321 | } |
| 4322 | } |
| 4323 | return result; |
| 4324 | } |
| 4325 | |
Ian Rogers | d8f69b0 | 2014-09-10 21:43:52 +0000 | [diff] [blame] | 4326 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 4327 | if (precise) { |
| 4328 | // Precise constant type. |
| 4329 | return reg_types_.FromCat1Const(value, true); |
| 4330 | } else { |
| 4331 | // Imprecise constant type. |
| 4332 | if (value < -32768) { |
| 4333 | return reg_types_.IntConstant(); |
| 4334 | } else if (value < -128) { |
| 4335 | return reg_types_.ShortConstant(); |
| 4336 | } else if (value < 0) { |
| 4337 | return reg_types_.ByteConstant(); |
| 4338 | } else if (value == 0) { |
| 4339 | return reg_types_.Zero(); |
| 4340 | } else if (value == 1) { |
| 4341 | return reg_types_.One(); |
| 4342 | } else if (value < 128) { |
| 4343 | return reg_types_.PosByteConstant(); |
| 4344 | } else if (value < 32768) { |
| 4345 | return reg_types_.PosShortConstant(); |
| 4346 | } else if (value < 65536) { |
| 4347 | return reg_types_.CharConstant(); |
| 4348 | } else { |
| 4349 | return reg_types_.IntConstant(); |
| 4350 | } |
| 4351 | } |
| 4352 | } |
| 4353 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4354 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4355 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4358 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4359 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4360 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4361 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4362 | void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) { |
| 4363 | RegTypeCache::VisitStaticRoots(visitor); |
Mathieu Chartier | 7c438b1 | 2014-09-12 17:01:24 -0700 | [diff] [blame] | 4364 | } |
| 4365 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 4366 | void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) { |
| 4367 | reg_types_.VisitRoots(visitor, root_info); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4368 | } |
| 4369 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4370 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4371 | } // namespace art |