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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 22 | #include "base/mutex-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 23 | #include "class_linker.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 24 | #include "compiler_callbacks.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 26 | #include "dex_instruction-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | #include "dex_instruction_visitor.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 29 | #include "indenter.h" |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 30 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 31 | #include "leb128.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 32 | #include "mirror/art_field-inl.h" |
| 33 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/class.h" |
| 35 | #include "mirror/class-inl.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 36 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 37 | #include "mirror/object-inl.h" |
| 38 | #include "mirror/object_array-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 39 | #include "object_utils.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 40 | #include "register_line-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 41 | #include "runtime.h" |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | #include "handle_scope-inl.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 44 | #include "verifier/dex_gc_map.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 45 | |
| 46 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 47 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 49 | static const bool gDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 50 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 51 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 52 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 53 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 54 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 55 | DCHECK_GT(insns_size, 0U); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 56 | register_lines_.reset(new RegisterLine*[insns_size]()); |
| 57 | size_ = insns_size; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 58 | for (uint32_t i = 0; i < insns_size; i++) { |
| 59 | bool interesting = false; |
| 60 | switch (mode) { |
| 61 | case kTrackRegsAll: |
| 62 | interesting = flags[i].IsOpcode(); |
| 63 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 64 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 65 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 66 | break; |
| 67 | case kTrackRegsBranches: |
| 68 | interesting = flags[i].IsBranchTarget(); |
| 69 | break; |
| 70 | default: |
| 71 | break; |
| 72 | } |
| 73 | if (interesting) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 74 | register_lines_[i] = RegisterLine::Create(registers_size, verifier); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | PcToRegisterLineTable::~PcToRegisterLineTable() { |
| 80 | for (size_t i = 0; i < size_; i++) { |
| 81 | delete register_lines_[i]; |
| 82 | if (kIsDebugBuild) { |
| 83 | register_lines_[i] = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 88 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 89 | bool allow_soft_failures, |
| 90 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 91 | if (klass->IsVerified()) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 92 | return kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 93 | } |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 94 | bool early_failure = false; |
| 95 | std::string failure_message; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 96 | ClassHelper kh(klass); |
| 97 | const DexFile& dex_file = kh.GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 98 | const DexFile::ClassDef* class_def = kh.GetClassDef(); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 99 | mirror::Class* super = klass->GetSuperClass(); |
| 100 | if (super == NULL && strcmp("Ljava/lang/Object;", kh.GetDescriptor()) != 0) { |
| 101 | early_failure = true; |
| 102 | failure_message = " that has no super class"; |
| 103 | } else if (super != NULL && super->IsFinal()) { |
| 104 | early_failure = true; |
| 105 | failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super); |
| 106 | } else if (class_def == NULL) { |
| 107 | early_failure = true; |
| 108 | failure_message = " that isn't present in dex file " + dex_file.GetLocation(); |
| 109 | } |
| 110 | if (early_failure) { |
| 111 | *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message; |
| 112 | if (Runtime::Current()->IsCompiler()) { |
| 113 | ClassReference ref(&dex_file, klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 114 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
Jeff Hao | 2d7e5aa | 2013-12-13 17:39:59 -0800 | [diff] [blame] | 115 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 116 | return kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 117 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 118 | StackHandleScope<2> hs(Thread::Current()); |
| 119 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(kh.GetDexCache())); |
| 120 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 121 | return VerifyClass(&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] | 122 | } |
| 123 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 124 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 125 | Handle<mirror::DexCache>& dex_cache, |
| 126 | Handle<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 127 | const DexFile::ClassDef* class_def, |
| 128 | bool allow_soft_failures, |
| 129 | std::string* error) { |
| 130 | DCHECK(class_def != nullptr); |
| 131 | const byte* class_data = dex_file->GetClassData(*class_def); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 132 | if (class_data == NULL) { |
| 133 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 134 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 135 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 136 | ClassDataItemIterator it(*dex_file, class_data); |
| 137 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 138 | it.Next(); |
| 139 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 140 | size_t error_count = 0; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 141 | bool hard_fail = false; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 142 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 143 | int64_t previous_direct_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 144 | while (it.HasNextDirectMethod()) { |
| 145 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 146 | if (method_idx == previous_direct_method_idx) { |
| 147 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 148 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 149 | it.Next(); |
| 150 | continue; |
| 151 | } |
| 152 | previous_direct_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 153 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 154 | mirror::ArtMethod* method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 155 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 156 | if (method == NULL) { |
| 157 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 158 | // We couldn't resolve the method, but continue regardless. |
| 159 | Thread::Current()->ClearException(); |
| 160 | } |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 161 | MethodVerifier::FailureKind result = VerifyMethod(method_idx, |
| 162 | dex_file, |
| 163 | dex_cache, |
| 164 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 165 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 166 | it.GetMethodCodeItem(), |
| 167 | method, |
| 168 | it.GetMemberAccessFlags(), |
| 169 | allow_soft_failures); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 170 | if (result != kNoFailure) { |
| 171 | if (result == kHardFailure) { |
| 172 | hard_fail = true; |
| 173 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 174 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 175 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 176 | *error = "Verifier rejected class "; |
| 177 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 178 | *error += " due to bad method "; |
| 179 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 180 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 181 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 182 | } |
| 183 | it.Next(); |
| 184 | } |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 185 | int64_t previous_virtual_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 186 | while (it.HasNextVirtualMethod()) { |
| 187 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 188 | if (method_idx == previous_virtual_method_idx) { |
| 189 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 190 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 191 | it.Next(); |
| 192 | continue; |
| 193 | } |
| 194 | previous_virtual_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 195 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 196 | mirror::ArtMethod* method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 198 | if (method == NULL) { |
| 199 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 200 | // We couldn't resolve the method, but continue regardless. |
| 201 | Thread::Current()->ClearException(); |
| 202 | } |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 203 | MethodVerifier::FailureKind result = VerifyMethod(method_idx, |
| 204 | dex_file, |
| 205 | dex_cache, |
| 206 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 207 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 208 | it.GetMethodCodeItem(), |
| 209 | method, |
| 210 | it.GetMemberAccessFlags(), |
| 211 | allow_soft_failures); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 212 | if (result != kNoFailure) { |
| 213 | if (result == kHardFailure) { |
| 214 | hard_fail = true; |
| 215 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 216 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 217 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 218 | *error = "Verifier rejected class "; |
| 219 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 220 | *error += " due to bad method "; |
| 221 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 222 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 223 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 224 | } |
| 225 | it.Next(); |
| 226 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 227 | if (error_count == 0) { |
| 228 | return kNoFailure; |
| 229 | } else { |
| 230 | return hard_fail ? kHardFailure : kSoftFailure; |
| 231 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 234 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, |
| 235 | const DexFile* dex_file, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 236 | Handle<mirror::DexCache>& dex_cache, |
| 237 | Handle<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 238 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 239 | const DexFile::CodeItem* code_item, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 240 | mirror::ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 241 | uint32_t method_access_flags, |
| 242 | bool allow_soft_failures) { |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 243 | MethodVerifier::FailureKind result = kNoFailure; |
| 244 | uint64_t start_ns = NanoTime(); |
| 245 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 246 | MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item, |
| 247 | method_idx, method, method_access_flags, true, allow_soft_failures); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 248 | if (verifier_.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 249 | // Verification completed, however failures may be pending that didn't cause the verification |
| 250 | // to hard fail. |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 251 | CHECK(!verifier_.have_pending_hard_failure_); |
| 252 | if (verifier_.failures_.size() != 0) { |
| 253 | if (VLOG_IS_ON(verifier)) { |
| 254 | verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
| 255 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 256 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 257 | result = kSoftFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 258 | } |
| 259 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 260 | // Bad method data. |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 261 | CHECK_NE(verifier_.failures_.size(), 0U); |
| 262 | CHECK(verifier_.have_pending_hard_failure_); |
| 263 | verifier_.DumpFailures(LOG(INFO) << "Verification error in " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 264 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 265 | if (gDebugVerify) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 266 | std::cout << "\n" << verifier_.info_messages_.str(); |
| 267 | verifier_.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 268 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 269 | result = kHardFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 270 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 271 | uint64_t duration_ns = NanoTime() - start_ns; |
| 272 | if (duration_ns > MsToNs(100)) { |
| 273 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
| 274 | << " took " << PrettyDuration(duration_ns); |
| 275 | } |
| 276 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 279 | void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 280 | const DexFile* dex_file, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 281 | Handle<mirror::DexCache>& dex_cache, |
| 282 | Handle<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 283 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 284 | const DexFile::CodeItem* code_item, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 285 | mirror::ArtMethod* method, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 286 | uint32_t method_access_flags) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 287 | MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 288 | dex_method_idx, method, method_access_flags, true, true); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 289 | verifier.Verify(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 290 | verifier.DumpFailures(os); |
| 291 | os << verifier.info_messages_.str(); |
| 292 | verifier.Dump(os); |
| 293 | } |
| 294 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 295 | MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache, |
| 296 | Handle<mirror::ClassLoader>* class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 297 | const DexFile::ClassDef* class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 298 | const DexFile::CodeItem* code_item, uint32_t dex_method_idx, |
| 299 | mirror::ArtMethod* method, uint32_t method_access_flags, |
| 300 | bool can_load_classes, bool allow_soft_failures) |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 301 | : reg_types_(can_load_classes), |
| 302 | work_insn_idx_(-1), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 303 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 304 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 305 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 306 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 307 | dex_file_(dex_file), |
| 308 | dex_cache_(dex_cache), |
| 309 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 310 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 311 | code_item_(code_item), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 312 | declaring_class_(NULL), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 313 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 314 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 315 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 316 | have_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 317 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 318 | monitor_enter_count_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 319 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 320 | allow_soft_failures_(allow_soft_failures), |
| 321 | has_check_casts_(false), |
| 322 | has_virtual_or_interface_invokes_(false) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 323 | Runtime::Current()->AddMethodVerifier(this); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 324 | DCHECK(class_def != nullptr); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 327 | MethodVerifier::~MethodVerifier() { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 328 | Runtime::Current()->RemoveMethodVerifier(this); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 329 | STLDeleteElements(&failure_messages_); |
| 330 | } |
| 331 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 332 | void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 333 | std::vector<uint32_t>& monitor_enter_dex_pcs) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 334 | MethodHelper mh(m); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 335 | StackHandleScope<2> hs(Thread::Current()); |
| 336 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache())); |
| 337 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 338 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
| 339 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false, |
| 340 | true); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 341 | verifier.interesting_dex_pc_ = dex_pc; |
| 342 | verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs; |
| 343 | verifier.FindLocksAtDexPc(); |
| 344 | } |
| 345 | |
| 346 | void MethodVerifier::FindLocksAtDexPc() { |
| 347 | CHECK(monitor_enter_dex_pcs_ != NULL); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 348 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 349 | |
| 350 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 351 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 352 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 353 | // got what we wanted. |
| 354 | Verify(); |
| 355 | } |
| 356 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 357 | mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 358 | uint32_t dex_pc) { |
| 359 | MethodHelper mh(m); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 360 | StackHandleScope<2> hs(Thread::Current()); |
| 361 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache())); |
| 362 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 363 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 364 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 365 | true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 366 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 369 | mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 370 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 371 | |
| 372 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 373 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 374 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 375 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 376 | bool success = Verify(); |
| 377 | if (!success) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 378 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 379 | } |
| 380 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
| 381 | if (register_line == NULL) { |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 382 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 383 | } |
| 384 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 385 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 388 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 389 | uint32_t dex_pc) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 390 | MethodHelper mh(m); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 391 | StackHandleScope<2> hs(Thread::Current()); |
| 392 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache())); |
| 393 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 394 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 395 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 396 | true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 397 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 400 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 401 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 402 | |
| 403 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 404 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 405 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 406 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 407 | bool success = Verify(); |
| 408 | if (!success) { |
| 409 | return NULL; |
| 410 | } |
| 411 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
| 412 | if (register_line == NULL) { |
| 413 | return NULL; |
| 414 | } |
| 415 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 416 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 417 | return GetQuickInvokedMethod(inst, register_line, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 420 | bool MethodVerifier::Verify() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 421 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 422 | if (code_item_ == NULL) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 423 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 424 | 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] | 425 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 426 | } else { |
| 427 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 428 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 429 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 430 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 431 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 432 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 433 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 434 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 435 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 436 | // Allocate and initialize an array to hold instruction data. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 437 | insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 438 | // Run through the instructions and see if the width checks out. |
| 439 | bool result = ComputeWidthsAndCountOps(); |
| 440 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 441 | result = result && ScanTryCatchBlocks(); |
| 442 | // Perform static instruction verification. |
| 443 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 444 | // Perform code-flow analysis and return. |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 445 | result = result && VerifyCodeFlow(); |
| 446 | // Compute information for compiler. |
| 447 | if (result && Runtime::Current()->IsCompiler()) { |
| 448 | result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this); |
| 449 | } |
| 450 | return result; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 453 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 454 | switch (error) { |
| 455 | case VERIFY_ERROR_NO_CLASS: |
| 456 | case VERIFY_ERROR_NO_FIELD: |
| 457 | case VERIFY_ERROR_NO_METHOD: |
| 458 | case VERIFY_ERROR_ACCESS_CLASS: |
| 459 | case VERIFY_ERROR_ACCESS_FIELD: |
| 460 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 461 | case VERIFY_ERROR_INSTANTIATION: |
| 462 | case VERIFY_ERROR_CLASS_CHANGE: |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 463 | if (Runtime::Current()->IsCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 464 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 465 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 466 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 467 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 468 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 469 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 470 | // to an interpreter. |
| 471 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 472 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 473 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 474 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 475 | have_pending_runtime_throw_failure_ = true; |
| 476 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 477 | break; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 478 | // Indication that verification should be retried at runtime. |
| 479 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 480 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 481 | have_pending_hard_failure_ = true; |
| 482 | } |
| 483 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 484 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 485 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 486 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
| 487 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 488 | ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 489 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 490 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 491 | have_pending_hard_failure_ = true; |
| 492 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 495 | failures_.push_back(error); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 496 | 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] | 497 | work_insn_idx_)); |
| 498 | std::ostringstream* failure_message = new std::ostringstream(location); |
| 499 | failure_messages_.push_back(failure_message); |
| 500 | return *failure_message; |
| 501 | } |
| 502 | |
| 503 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 504 | size_t failure_num = failure_messages_.size(); |
| 505 | DCHECK_NE(failure_num, 0U); |
| 506 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 507 | prepend += last_fail_message->str(); |
| 508 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend); |
| 509 | delete last_fail_message; |
| 510 | } |
| 511 | |
| 512 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 513 | size_t failure_num = failure_messages_.size(); |
| 514 | DCHECK_NE(failure_num, 0U); |
| 515 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 516 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 519 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 520 | const uint16_t* insns = code_item_->insns_; |
| 521 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 522 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 523 | size_t new_instance_count = 0; |
| 524 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 525 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 526 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 527 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 528 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 529 | switch (opcode) { |
| 530 | case Instruction::APUT_OBJECT: |
| 531 | case Instruction::CHECK_CAST: |
| 532 | has_check_casts_ = true; |
| 533 | break; |
| 534 | case Instruction::INVOKE_VIRTUAL: |
| 535 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 536 | case Instruction::INVOKE_INTERFACE: |
| 537 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 538 | has_virtual_or_interface_invokes_ = true; |
| 539 | break; |
| 540 | case Instruction::MONITOR_ENTER: |
| 541 | monitor_enter_count++; |
| 542 | break; |
| 543 | case Instruction::NEW_INSTANCE: |
| 544 | new_instance_count++; |
| 545 | break; |
| 546 | default: |
| 547 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 548 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 549 | size_t inst_size = inst->SizeInCodeUnits(); |
| 550 | insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size); |
| 551 | dex_pc += inst_size; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 552 | inst = inst->Next(); |
| 553 | } |
| 554 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 555 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 556 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 557 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 561 | new_instance_count_ = new_instance_count; |
| 562 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 566 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 567 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 568 | if (tries_size == 0) { |
| 569 | return true; |
| 570 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 571 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 572 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 573 | |
| 574 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 575 | const DexFile::TryItem* try_item = &tries[idx]; |
| 576 | uint32_t start = try_item->start_addr_; |
| 577 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 578 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 579 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 580 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 581 | return false; |
| 582 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 583 | if (!insn_flags_[start].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 584 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 585 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 586 | return false; |
| 587 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 588 | for (uint32_t dex_pc = start; dex_pc < end; |
| 589 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
| 590 | insn_flags_[dex_pc].SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 593 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 594 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 595 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 596 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 597 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 598 | CatchHandlerIterator iterator(handlers_ptr); |
| 599 | for (; iterator.HasNext(); iterator.Next()) { |
| 600 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 601 | if (!insn_flags_[dex_pc].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 602 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 603 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 604 | return false; |
| 605 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 606 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 607 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 608 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 609 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 610 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 611 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 612 | *dex_cache_, *class_loader_); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 613 | if (exception_type == NULL) { |
| 614 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 615 | Thread::Current()->ClearException(); |
| 616 | } |
| 617 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 618 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 619 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 620 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 624 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 625 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 626 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 627 | /* 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] | 628 | insn_flags_[0].SetBranchTarget(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 629 | insn_flags_[0].SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 630 | |
| 631 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 632 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 633 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 634 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 638 | // All invoke points are marked as "Throw" points already. |
| 639 | // We are relying on this to also count all the invokes as interesting. |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 640 | if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 641 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 642 | } else if (inst->IsReturn()) { |
| 643 | insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 644 | } |
| 645 | dex_pc += inst->SizeInCodeUnits(); |
| 646 | inst = inst->Next(); |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 651 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 652 | bool result = true; |
| 653 | switch (inst->GetVerifyTypeArgumentA()) { |
| 654 | case Instruction::kVerifyRegA: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 655 | result = result && CheckRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 656 | break; |
| 657 | case Instruction::kVerifyRegAWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 658 | result = result && CheckWideRegisterIndex(inst->VRegA()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | switch (inst->GetVerifyTypeArgumentB()) { |
| 662 | case Instruction::kVerifyRegB: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 663 | result = result && CheckRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 664 | break; |
| 665 | case Instruction::kVerifyRegBField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 666 | result = result && CheckFieldIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 667 | break; |
| 668 | case Instruction::kVerifyRegBMethod: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 669 | result = result && CheckMethodIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 670 | break; |
| 671 | case Instruction::kVerifyRegBNewInstance: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 672 | result = result && CheckNewInstance(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 673 | break; |
| 674 | case Instruction::kVerifyRegBString: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 675 | result = result && CheckStringIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 676 | break; |
| 677 | case Instruction::kVerifyRegBType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 678 | result = result && CheckTypeIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 679 | break; |
| 680 | case Instruction::kVerifyRegBWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 681 | result = result && CheckWideRegisterIndex(inst->VRegB()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | switch (inst->GetVerifyTypeArgumentC()) { |
| 685 | case Instruction::kVerifyRegC: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 686 | result = result && CheckRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 687 | break; |
| 688 | case Instruction::kVerifyRegCField: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 689 | result = result && CheckFieldIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 690 | break; |
| 691 | case Instruction::kVerifyRegCNewArray: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 692 | result = result && CheckNewArray(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 693 | break; |
| 694 | case Instruction::kVerifyRegCType: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 695 | result = result && CheckTypeIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 696 | break; |
| 697 | case Instruction::kVerifyRegCWide: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 698 | result = result && CheckWideRegisterIndex(inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 699 | break; |
| 700 | } |
| 701 | switch (inst->GetVerifyExtraFlags()) { |
| 702 | case Instruction::kVerifyArrayData: |
| 703 | result = result && CheckArrayData(code_offset); |
| 704 | break; |
| 705 | case Instruction::kVerifyBranchTarget: |
| 706 | result = result && CheckBranchTarget(code_offset); |
| 707 | break; |
| 708 | case Instruction::kVerifySwitchTargets: |
| 709 | result = result && CheckSwitchTargets(code_offset); |
| 710 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 711 | case Instruction::kVerifyVarArg: { |
| 712 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 713 | inst->GetVarArgs(args); |
| 714 | result = result && CheckVarArgRegs(inst->VRegA(), args); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 715 | break; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 716 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 717 | case Instruction::kVerifyVarArgRange: |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 718 | result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 719 | break; |
| 720 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 721 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 722 | result = false; |
| 723 | break; |
| 724 | } |
| 725 | return result; |
| 726 | } |
| 727 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 728 | bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 729 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 730 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 731 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 732 | return false; |
| 733 | } |
| 734 | return true; |
| 735 | } |
| 736 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 737 | bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 738 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 739 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 740 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | return true; |
| 744 | } |
| 745 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 746 | bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 747 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 748 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 749 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 750 | return false; |
| 751 | } |
| 752 | return true; |
| 753 | } |
| 754 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 755 | bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 756 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 757 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 758 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | return true; |
| 762 | } |
| 763 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 764 | bool MethodVerifier::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 765 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 766 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 767 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 768 | return false; |
| 769 | } |
| 770 | // 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] | 771 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 772 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 773 | 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] | 774 | return false; |
| 775 | } |
| 776 | return true; |
| 777 | } |
| 778 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 779 | bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 780 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 781 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 782 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 783 | return false; |
| 784 | } |
| 785 | return true; |
| 786 | } |
| 787 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 788 | bool MethodVerifier::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 789 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 790 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 791 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 792 | return false; |
| 793 | } |
| 794 | return true; |
| 795 | } |
| 796 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 797 | bool MethodVerifier::CheckNewArray(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 798 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 799 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 800 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 804 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 805 | const char* cp = descriptor; |
| 806 | while (*cp++ == '[') { |
| 807 | bracket_count++; |
| 808 | } |
| 809 | if (bracket_count == 0) { |
| 810 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 811 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 812 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 813 | return false; |
| 814 | } else if (bracket_count > 255) { |
| 815 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 816 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 817 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 818 | return false; |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 823 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 824 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 825 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 826 | const uint16_t* array_data; |
| 827 | int32_t array_data_offset; |
| 828 | |
| 829 | DCHECK_LT(cur_offset, insn_count); |
| 830 | /* make sure the start of the array data table is in range */ |
| 831 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 832 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 833 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 834 | 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] | 835 | << ", data offset " << array_data_offset |
| 836 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 837 | return false; |
| 838 | } |
| 839 | /* offset to array data table is a relative branch-style offset */ |
| 840 | array_data = insns + array_data_offset; |
| 841 | /* make sure the table is 32-bit aligned */ |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 842 | if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 843 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 844 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 845 | return false; |
| 846 | } |
| 847 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 848 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 849 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 850 | /* make sure the end of the switch is in range */ |
| 851 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 852 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 853 | << ", data offset " << array_data_offset << ", end " |
| 854 | << cur_offset + array_data_offset + table_size |
| 855 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 856 | return false; |
| 857 | } |
| 858 | return true; |
| 859 | } |
| 860 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 861 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 862 | int32_t offset; |
| 863 | bool isConditional, selfOkay; |
| 864 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 865 | return false; |
| 866 | } |
| 867 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 868 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 869 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 870 | return false; |
| 871 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 872 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 873 | // 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] | 874 | 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] | 875 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 876 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 880 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 881 | if (abs_offset < 0 || |
| 882 | (uint32_t) abs_offset >= insn_count || |
| 883 | !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 884 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 885 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 886 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 887 | return false; |
| 888 | } |
| 889 | insn_flags_[abs_offset].SetBranchTarget(); |
| 890 | return true; |
| 891 | } |
| 892 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 893 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 894 | bool* selfOkay) { |
| 895 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 896 | *pConditional = false; |
| 897 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 898 | switch (*insns & 0xff) { |
| 899 | case Instruction::GOTO: |
| 900 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 901 | break; |
| 902 | case Instruction::GOTO_32: |
| 903 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 904 | *selfOkay = true; |
| 905 | break; |
| 906 | case Instruction::GOTO_16: |
| 907 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 908 | break; |
| 909 | case Instruction::IF_EQ: |
| 910 | case Instruction::IF_NE: |
| 911 | case Instruction::IF_LT: |
| 912 | case Instruction::IF_GE: |
| 913 | case Instruction::IF_GT: |
| 914 | case Instruction::IF_LE: |
| 915 | case Instruction::IF_EQZ: |
| 916 | case Instruction::IF_NEZ: |
| 917 | case Instruction::IF_LTZ: |
| 918 | case Instruction::IF_GEZ: |
| 919 | case Instruction::IF_GTZ: |
| 920 | case Instruction::IF_LEZ: |
| 921 | *pOffset = (int16_t) insns[1]; |
| 922 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 923 | break; |
| 924 | default: |
| 925 | return false; |
| 926 | break; |
| 927 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 928 | return true; |
| 929 | } |
| 930 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 931 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 932 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 933 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 934 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 935 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 936 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
| 937 | 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] | 938 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 939 | << ", switch offset " << switch_offset |
| 940 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 941 | return false; |
| 942 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 943 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 944 | const uint16_t* switch_insns = insns + switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 945 | /* make sure the table is 32-bit aligned */ |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 946 | if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 947 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 948 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 949 | return false; |
| 950 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 951 | uint32_t switch_count = switch_insns[1]; |
| 952 | int32_t keys_offset, targets_offset; |
| 953 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 954 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 955 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 956 | targets_offset = 4; |
| 957 | keys_offset = -1; |
| 958 | expected_signature = Instruction::kPackedSwitchSignature; |
| 959 | } else { |
| 960 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 961 | keys_offset = 2; |
| 962 | targets_offset = 2 + 2 * switch_count; |
| 963 | expected_signature = Instruction::kSparseSwitchSignature; |
| 964 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 965 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 966 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 967 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 968 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 969 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 970 | return false; |
| 971 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 972 | /* make sure the end of the switch is in range */ |
| 973 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 974 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 975 | << ", switch offset " << switch_offset |
| 976 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 977 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 978 | return false; |
| 979 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 980 | /* for a sparse switch, verify the keys are in ascending order */ |
| 981 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 982 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 983 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 984 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 985 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 986 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 987 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 988 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 989 | return false; |
| 990 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 991 | last_key = key; |
| 992 | } |
| 993 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 994 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 995 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 996 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 997 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 998 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 999 | if (abs_offset < 0 || |
| 1000 | abs_offset >= (int32_t) insn_count || |
| 1001 | !insn_flags_[abs_offset].IsOpcode()) { |
| 1002 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 1003 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 1004 | << reinterpret_cast<void*>(cur_offset) |
| 1005 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1006 | return false; |
| 1007 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1008 | insn_flags_[abs_offset].SetBranchTarget(); |
| 1009 | } |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1013 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1014 | if (vA > Instruction::kMaxVarArgRegs) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1015 | 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] | 1016 | return false; |
| 1017 | } |
| 1018 | uint16_t registers_size = code_item_->registers_size_; |
| 1019 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1020 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1021 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1022 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1030 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1031 | uint16_t registers_size = code_item_->registers_size_; |
| 1032 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1033 | // integer overflow when adding them here. |
| 1034 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1035 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1036 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1037 | return false; |
| 1038 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1039 | return true; |
| 1040 | } |
| 1041 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1042 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1043 | uint16_t registers_size = code_item_->registers_size_; |
| 1044 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1045 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1046 | if (registers_size * insns_size > 4*1024*1024) { |
buzbee | 4922ef9 | 2012-02-24 14:32:20 -0800 | [diff] [blame] | 1047 | LOG(WARNING) << "warning: method is huge (regs=" << registers_size |
| 1048 | << " insns_size=" << insns_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1049 | } |
| 1050 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1051 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1052 | insn_flags_.get(), |
| 1053 | insns_size, |
| 1054 | registers_size, |
| 1055 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1056 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1057 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1058 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1059 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1060 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1061 | /* Initialize register types of method arguments. */ |
| 1062 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1063 | DCHECK_NE(failures_.size(), 0U); |
| 1064 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1065 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1066 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1067 | return false; |
| 1068 | } |
| 1069 | /* Perform code flow verification. */ |
| 1070 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1071 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1072 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1073 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1074 | return true; |
| 1075 | } |
| 1076 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1077 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1078 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1079 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1080 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1081 | } |
| 1082 | return os; |
| 1083 | } |
| 1084 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1085 | extern "C" void MethodVerifierGdbDump(MethodVerifier* v) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1086 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1087 | v->Dump(std::cerr); |
| 1088 | } |
| 1089 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1090 | void MethodVerifier::Dump(std::ostream& os) { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1091 | if (code_item_ == NULL) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1092 | os << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1093 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1094 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1095 | { |
| 1096 | os << "Register Types:\n"; |
| 1097 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1098 | std::ostream indent_os(&indent_filter); |
| 1099 | reg_types_.Dump(indent_os); |
| 1100 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1101 | os << "Dumping instructions and register lines:\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1102 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1103 | std::ostream indent_os(&indent_filter); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1104 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1105 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
| 1106 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1107 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1108 | if (reg_line != NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1109 | indent_os << reg_line->Dump() << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1110 | } |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1111 | indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1112 | const bool kDumpHexOfInstruction = false; |
| 1113 | if (kDumpHexOfInstruction) { |
| 1114 | indent_os << inst->DumpHex(5) << " "; |
| 1115 | } |
| 1116 | indent_os << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1117 | inst = inst->Next(); |
| 1118 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1121 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1122 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1123 | case 'I': |
| 1124 | case 'C': |
| 1125 | case 'S': |
| 1126 | case 'B': |
| 1127 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1128 | case 'F': |
| 1129 | case 'D': |
| 1130 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1131 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1132 | default: |
| 1133 | return false; |
| 1134 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1137 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1138 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1139 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1140 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1141 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1142 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1143 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1144 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1145 | if (!IsStatic()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1146 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1147 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1148 | // called. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1149 | const RegType& declaring_class = GetDeclaringClass(); |
| 1150 | if (IsConstructor() && !declaring_class.IsJavaLangObject()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1151 | reg_line->SetRegisterType(arg_start + cur_arg, |
| 1152 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1153 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1154 | reg_line->SetRegisterType(arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1155 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1156 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1159 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1160 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1161 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1162 | |
| 1163 | for (; iterator.HasNext(); iterator.Next()) { |
| 1164 | const char* descriptor = iterator.GetDescriptor(); |
| 1165 | if (descriptor == NULL) { |
| 1166 | LOG(FATAL) << "Null descriptor"; |
| 1167 | } |
| 1168 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1169 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1170 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | switch (descriptor[0]) { |
| 1174 | case 'L': |
| 1175 | case '[': |
| 1176 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1177 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1178 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1179 | // can return without doing anything or call virtual methods). |
| 1180 | { |
Sebastien Hertz | 2ed76f9 | 2014-04-22 17:11:08 +0200 | [diff] [blame] | 1181 | const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx()); |
| 1182 | if (!reg_type.IsNonZeroReferenceTypes()) { |
| 1183 | DCHECK(HasFailures()); |
| 1184 | return false; |
| 1185 | } |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1186 | reg_line->SetRegisterType(arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1187 | } |
| 1188 | break; |
| 1189 | case 'Z': |
| 1190 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); |
| 1191 | break; |
| 1192 | case 'C': |
| 1193 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); |
| 1194 | break; |
| 1195 | case 'B': |
| 1196 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); |
| 1197 | break; |
| 1198 | case 'I': |
| 1199 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); |
| 1200 | break; |
| 1201 | case 'S': |
| 1202 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); |
| 1203 | break; |
| 1204 | case 'F': |
| 1205 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); |
| 1206 | break; |
| 1207 | case 'J': |
| 1208 | case 'D': { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1209 | const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo(); |
| 1210 | const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi(); |
| 1211 | reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1212 | cur_arg++; |
| 1213 | break; |
| 1214 | } |
| 1215 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1216 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1217 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1218 | return false; |
| 1219 | } |
| 1220 | cur_arg++; |
| 1221 | } |
| 1222 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1223 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1224 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1225 | return false; |
| 1226 | } |
| 1227 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1228 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1229 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1230 | bool result; |
| 1231 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1232 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1233 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1234 | size_t i = 0; |
| 1235 | do { |
| 1236 | i++; |
| 1237 | } while (descriptor[i] == '['); // process leading [ |
| 1238 | if (descriptor[i] == 'L') { // object array |
| 1239 | do { |
| 1240 | i++; // find closing ; |
| 1241 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1242 | result = descriptor[i] == ';'; |
| 1243 | } else { // primitive array |
| 1244 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1245 | } |
| 1246 | } else if (descriptor[0] == 'L') { |
| 1247 | // could be more thorough here, but shouldn't be required |
| 1248 | size_t i = 0; |
| 1249 | do { |
| 1250 | i++; |
| 1251 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1252 | result = descriptor[i] == ';'; |
| 1253 | } else { |
| 1254 | result = false; |
| 1255 | } |
| 1256 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1257 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1258 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1259 | } |
| 1260 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1263 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1264 | const uint16_t* insns = code_item_->insns_; |
| 1265 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1266 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1267 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1268 | insn_flags_[0].SetChanged(); |
| 1269 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1270 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1271 | /* Continue until no instructions are marked "changed". */ |
| 1272 | while (true) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1273 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1274 | uint32_t insn_idx = start_guess; |
| 1275 | for (; insn_idx < insns_size; insn_idx++) { |
| 1276 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1277 | break; |
| 1278 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1279 | if (insn_idx == insns_size) { |
| 1280 | if (start_guess != 0) { |
| 1281 | /* try again, starting from the top */ |
| 1282 | start_guess = 0; |
| 1283 | continue; |
| 1284 | } else { |
| 1285 | /* all flags are clear */ |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1289 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1290 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1291 | // "changed" flags, we need to load the set of registers from the table. |
| 1292 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1293 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1294 | // target. |
| 1295 | work_insn_idx_ = insn_idx; |
| 1296 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1297 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1298 | } else { |
| 1299 | #ifndef NDEBUG |
| 1300 | /* |
| 1301 | * Sanity check: retrieve the stored register line (assuming |
| 1302 | * a full table) and make sure it actually matches. |
| 1303 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1304 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| 1305 | if (register_line != NULL) { |
| 1306 | if (work_line_->CompareLine(register_line) != 0) { |
| 1307 | Dump(std::cout); |
| 1308 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1309 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1310 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
| 1311 | << " work_line=" << *work_line_ << "\n" |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1312 | << " expected=" << *register_line; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1313 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1314 | } |
| 1315 | #endif |
| 1316 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1317 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1318 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1319 | prepend += " failed to verify: "; |
| 1320 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1323 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1324 | insn_flags_[insn_idx].SetVisited(); |
| 1325 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1326 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1327 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1328 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1329 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1330 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1331 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1332 | * down the line, possibly in the verifier. |
| 1333 | * |
| 1334 | * If we've substituted "always throw" instructions into the stream, |
| 1335 | * we are almost certainly going to have some dead code. |
| 1336 | */ |
| 1337 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1338 | uint32_t insn_idx = 0; |
| 1339 | for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1340 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1341 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1342 | * may or may not be preceded by a padding NOP (for alignment). |
| 1343 | */ |
| 1344 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1345 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1346 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1347 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1348 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1349 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1350 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1351 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1354 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1355 | if (dead_start < 0) |
| 1356 | dead_start = insn_idx; |
| 1357 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1358 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1359 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1360 | dead_start = -1; |
| 1361 | } |
| 1362 | } |
| 1363 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1364 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1365 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1366 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1367 | // To dump the state of the verify after a method, do something like: |
| 1368 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1369 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1370 | // LOG(INFO) << info_messages_.str(); |
| 1371 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1372 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1373 | return true; |
| 1374 | } |
| 1375 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1376 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1377 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1378 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1379 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1380 | // for a thread to be suspended). |
| 1381 | if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1382 | 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] | 1383 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1384 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1385 | } |
| 1386 | } |
| 1387 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1388 | /* |
| 1389 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1390 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1391 | * control to another statement: |
| 1392 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1393 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1394 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1395 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1396 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1397 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1398 | * throw an exception that is handled by an encompassing "try" |
| 1399 | * block. |
| 1400 | * |
| 1401 | * We can also return, in which case there is no successor instruction |
| 1402 | * from this point. |
| 1403 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1404 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1405 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1406 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1407 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1408 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1409 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1410 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1411 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1412 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1413 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1414 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
| 1415 | << *work_line_.get() << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1416 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1417 | |
| 1418 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1419 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1420 | * can throw an exception, we will copy/merge this into the "catch" |
| 1421 | * address rather than work_line, because we don't want the result |
| 1422 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1423 | * a type) to be visible to the exception handler. |
| 1424 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1425 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1426 | saved_line_->CopyFromLine(work_line_.get()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1427 | } else { |
| 1428 | #ifndef NDEBUG |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1429 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1430 | #endif |
| 1431 | } |
| 1432 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1433 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1434 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1435 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1436 | // branch target. |
| 1437 | UniquePtr<RegisterLine> branch_line; |
| 1438 | UniquePtr<RegisterLine> fallthrough_line; |
| 1439 | |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1440 | // We need precise constant types only for deoptimization which happens at runtime. |
| 1441 | const bool need_precise_constant = !Runtime::Current()->IsCompiler(); |
| 1442 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1443 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1444 | case Instruction::NOP: |
| 1445 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1446 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1447 | * a signature that looks like a NOP; if we see one of these in |
| 1448 | * the course of executing code then we have a problem. |
| 1449 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1450 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1451 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1452 | } |
| 1453 | break; |
| 1454 | |
| 1455 | case Instruction::MOVE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1456 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
| 1457 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1458 | case Instruction::MOVE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1459 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
| 1460 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1461 | case Instruction::MOVE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1462 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1463 | break; |
| 1464 | case Instruction::MOVE_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1465 | work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x()); |
| 1466 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1467 | case Instruction::MOVE_WIDE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1468 | work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x()); |
| 1469 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1470 | case Instruction::MOVE_WIDE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1471 | work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | case Instruction::MOVE_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1474 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
| 1475 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1476 | case Instruction::MOVE_OBJECT_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1477 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
| 1478 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1479 | case Instruction::MOVE_OBJECT_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1480 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | |
| 1483 | /* |
| 1484 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1485 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1486 | * might want to hold the result in an actual CPU register, so the |
| 1487 | * Dalvik spec requires that these only appear immediately after an |
| 1488 | * invoke or filled-new-array. |
| 1489 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1490 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1491 | * redundant with the reset done below, but it can make the debug info |
| 1492 | * easier to read in some cases.) |
| 1493 | */ |
| 1494 | case Instruction::MOVE_RESULT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1495 | work_line_->CopyResultRegister1(inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | case Instruction::MOVE_RESULT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1498 | work_line_->CopyResultRegister2(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1499 | break; |
| 1500 | case Instruction::MOVE_RESULT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1501 | work_line_->CopyResultRegister1(inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1502 | break; |
| 1503 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1504 | case Instruction::MOVE_EXCEPTION: { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1505 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1506 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1507 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1508 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1509 | const RegType& res_type = GetCaughtExceptionType(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1510 | work_line_->SetRegisterType(inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1511 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1512 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1513 | case Instruction::RETURN_VOID: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1514 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 1515 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1516 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1517 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1518 | } |
| 1519 | break; |
| 1520 | case Instruction::RETURN: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1521 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1522 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1523 | const RegType& return_type = GetMethodReturnType(); |
| 1524 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1525 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1526 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1527 | } else { |
| 1528 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1529 | // 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] | 1530 | const uint32_t vregA = inst->VRegA_11x(); |
| 1531 | const RegType& src_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1532 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1533 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1534 | return_type.IsShort() || return_type.IsChar()) && |
| 1535 | src_type.IsInteger())); |
| 1536 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1537 | bool success = |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1538 | work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1539 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1540 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1541 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | break; |
| 1545 | case Instruction::RETURN_WIDE: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1546 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1547 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1548 | const RegType& return_type = GetMethodReturnType(); |
| 1549 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1550 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1551 | } else { |
| 1552 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1553 | const uint32_t vregA = inst->VRegA_11x(); |
| 1554 | bool success = work_line_->VerifyRegisterType(vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1555 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1556 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1557 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | break; |
| 1561 | case Instruction::RETURN_OBJECT: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1562 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1563 | const RegType& return_type = GetMethodReturnType(); |
| 1564 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1565 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1566 | } else { |
| 1567 | /* return_type is the *expected* return type, not register value */ |
| 1568 | DCHECK(!return_type.IsZero()); |
| 1569 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1570 | const uint32_t vregA = inst->VRegA_11x(); |
| 1571 | const RegType& reg_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1572 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 1573 | // instance of the "return_type" |
| 1574 | if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1575 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1576 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1577 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1578 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1579 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1580 | << "' or '" << reg_type << "'"; |
| 1581 | } else { |
| 1582 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1583 | << "', but expected from declaration '" << return_type << "'"; |
| 1584 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | } |
| 1588 | break; |
| 1589 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1590 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1591 | case Instruction::CONST_4: { |
| 1592 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1593 | work_line_->SetRegisterType(inst->VRegA_11n(), |
| 1594 | DetermineCat1Constant(val, need_precise_constant)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1595 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1596 | } |
| 1597 | case Instruction::CONST_16: { |
| 1598 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1599 | work_line_->SetRegisterType(inst->VRegA_21s(), |
| 1600 | DetermineCat1Constant(val, need_precise_constant)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1601 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1602 | } |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1603 | case Instruction::CONST: { |
| 1604 | int32_t val = inst->VRegB_31i(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1605 | work_line_->SetRegisterType(inst->VRegA_31i(), |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1606 | DetermineCat1Constant(val, need_precise_constant)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1607 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1608 | } |
| 1609 | case Instruction::CONST_HIGH16: { |
| 1610 | int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1611 | work_line_->SetRegisterType(inst->VRegA_21h(), |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1612 | DetermineCat1Constant(val, need_precise_constant)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1613 | break; |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 1614 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1615 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1616 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1617 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1618 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1619 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1620 | work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1621 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1622 | } |
| 1623 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1624 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1625 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1626 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1627 | work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1628 | break; |
| 1629 | } |
| 1630 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1631 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1632 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1633 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1634 | work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1635 | break; |
| 1636 | } |
| 1637 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1638 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1639 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1640 | const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1641 | work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1642 | break; |
| 1643 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1644 | case Instruction::CONST_STRING: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1645 | work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString()); |
| 1646 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1647 | case Instruction::CONST_STRING_JUMBO: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1648 | work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1649 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1650 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1651 | // Get type from instruction if unresolved then we need an access check |
| 1652 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1653 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1654 | // Register holds class, ie its type is class, on error it will hold Conflict. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1655 | work_line_->SetRegisterType(inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1656 | res_type.IsConflict() ? res_type |
| 1657 | : reg_types_.JavaLangClass(true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1658 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1659 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1660 | case Instruction::MONITOR_ENTER: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1661 | work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1662 | break; |
| 1663 | case Instruction::MONITOR_EXIT: |
| 1664 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1665 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1666 | * 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] | 1667 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1668 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1669 | * feature that Dalvik doesn't support.) |
| 1670 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1671 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1672 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1673 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1674 | * structured locking checks are working, the former would have |
| 1675 | * failed on the -enter instruction, and the latter is impossible. |
| 1676 | * |
| 1677 | * This is fortunate, because issue 3221411 prevents us from |
| 1678 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1679 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1680 | * some catch blocks (which will show up as "dead" code when |
| 1681 | * we skip them here); if we can't, then the code path could be |
| 1682 | * "live" so we still need to check it. |
| 1683 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1684 | opcode_flags &= ~Instruction::kThrow; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1685 | work_line_->PopMonitor(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1686 | break; |
| 1687 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1688 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1689 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1690 | /* |
| 1691 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1692 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1693 | * |
| 1694 | * 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] | 1695 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1696 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1697 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1698 | const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 1699 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1700 | if (res_type.IsConflict()) { |
| 1701 | DCHECK_NE(failures_.size(), 0U); |
| 1702 | if (!is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1703 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1704 | } |
| 1705 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 1706 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1707 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1708 | uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c(); |
| 1709 | const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1710 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1711 | if (is_checkcast) { |
| 1712 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 1713 | } else { |
| 1714 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 1715 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1716 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1717 | if (is_checkcast) { |
| 1718 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 1719 | } else { |
| 1720 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 1721 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1722 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1723 | if (is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1724 | work_line_->SetRegisterType(inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1725 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1726 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1727 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1728 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1729 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1730 | } |
| 1731 | case Instruction::ARRAY_LENGTH: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1732 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1733 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1734 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1735 | 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] | 1736 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1737 | work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | break; |
| 1741 | } |
| 1742 | case Instruction::NEW_INSTANCE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1743 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1744 | if (res_type.IsConflict()) { |
| 1745 | DCHECK_NE(failures_.size(), 0U); |
| 1746 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1747 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1748 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 1749 | // can't create an instance of an interface or abstract class */ |
| 1750 | if (!res_type.IsInstantiableTypes()) { |
| 1751 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 1752 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1753 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1754 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1755 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| 1756 | // Any registers holding previous allocations from this address that have not yet been |
| 1757 | // initialized must be marked invalid. |
| 1758 | work_line_->MarkUninitRefsAsInvalid(uninit_type); |
| 1759 | // add the new uninitialized reference to the register state |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1760 | work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1761 | break; |
| 1762 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1763 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1764 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1765 | break; |
| 1766 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1767 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1768 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1769 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1770 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1771 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1772 | just_set_result = true; // Filled new array range sets result register |
| 1773 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1774 | case Instruction::CMPL_FLOAT: |
| 1775 | case Instruction::CMPG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1776 | if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1777 | break; |
| 1778 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1779 | if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1780 | break; |
| 1781 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1782 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1783 | break; |
| 1784 | case Instruction::CMPL_DOUBLE: |
| 1785 | case Instruction::CMPG_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1786 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1787 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1788 | break; |
| 1789 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1790 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1791 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1792 | break; |
| 1793 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1794 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1795 | break; |
| 1796 | case Instruction::CMP_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1797 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1798 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1799 | break; |
| 1800 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1801 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1802 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1803 | break; |
| 1804 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1805 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1806 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1807 | case Instruction::THROW: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1808 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1809 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1810 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 1811 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1812 | } |
| 1813 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1814 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1815 | case Instruction::GOTO: |
| 1816 | case Instruction::GOTO_16: |
| 1817 | case Instruction::GOTO_32: |
| 1818 | /* no effect on or use of registers */ |
| 1819 | break; |
| 1820 | |
| 1821 | case Instruction::PACKED_SWITCH: |
| 1822 | case Instruction::SPARSE_SWITCH: |
| 1823 | /* verify that vAA is an integer, or can be converted to one */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1824 | work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1825 | break; |
| 1826 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1827 | case Instruction::FILL_ARRAY_DATA: { |
| 1828 | /* Similar to the verification done for APUT */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1829 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1830 | /* array_type can be null if the reg type is Zero */ |
| 1831 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1832 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1833 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 1834 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1835 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1836 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1837 | class_loader_->Get()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1838 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1839 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1840 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 1841 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1842 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1843 | // Now verify if the element width in the table matches the element width declared in |
| 1844 | // the array |
| 1845 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 1846 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1847 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1848 | } else { |
| 1849 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 1850 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 1851 | // in the table and expected from the array class. |
| 1852 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1853 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 1854 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1855 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1856 | } |
| 1857 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 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::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1863 | case Instruction::IF_NE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1864 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1865 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1866 | bool mismatch = false; |
| 1867 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 1868 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 1869 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 1870 | mismatch = !reg_type2.IsReferenceTypes(); |
| 1871 | } else { // both integral? |
| 1872 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 1873 | } |
| 1874 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1875 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 1876 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1877 | } |
| 1878 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1879 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1880 | case Instruction::IF_LT: |
| 1881 | case Instruction::IF_GE: |
| 1882 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1883 | case Instruction::IF_LE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1884 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1885 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1886 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1887 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 1888 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1889 | } |
| 1890 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1891 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1892 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1893 | case Instruction::IF_NEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1894 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1895 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1896 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 1897 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1898 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1899 | |
| 1900 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1901 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1902 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1903 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1904 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1905 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1906 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1907 | CHECK(insn_flags_[instance_of_idx].IsOpcode()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1908 | } else { |
| 1909 | break; |
| 1910 | } |
| 1911 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1912 | 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] | 1913 | |
| 1914 | /* Check for peep-hole pattern of: |
| 1915 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1916 | * instance-of vX, vY, T; |
| 1917 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1918 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1919 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1920 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1921 | * and sharpen the type of vY to be type T. |
| 1922 | * Note, this pattern can't be if: |
| 1923 | * - if there are other branches to this branch, |
| 1924 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1925 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1926 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1927 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 1928 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 1929 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1930 | // Check that the we are not attempting conversion to interface types, |
| 1931 | // which is not done because of the multiple inheritance implications. |
Jeff Hao | c642ec8 | 2013-09-04 16:11:55 -0700 | [diff] [blame] | 1932 | // Also don't change the type if it would result in an upcast. |
| 1933 | const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1934 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1935 | |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1936 | if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
| 1937 | !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1938 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1939 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1940 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1941 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1942 | branch_line.reset(update_line); |
| 1943 | } |
| 1944 | update_line->CopyFromLine(work_line_.get()); |
| 1945 | update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type); |
| 1946 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 1947 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 1948 | // register encoding space of instance-of, and propagate type information to the source |
| 1949 | // of the move-object. |
| 1950 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1951 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1952 | move_idx--; |
| 1953 | } |
| 1954 | CHECK(insn_flags_[move_idx].IsOpcode()); |
| 1955 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 1956 | switch (move_inst->Opcode()) { |
| 1957 | case Instruction::MOVE_OBJECT: |
| 1958 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
| 1959 | update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type); |
| 1960 | } |
| 1961 | break; |
| 1962 | case Instruction::MOVE_OBJECT_FROM16: |
| 1963 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
| 1964 | update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type); |
| 1965 | } |
| 1966 | break; |
| 1967 | case Instruction::MOVE_OBJECT_16: |
| 1968 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
| 1969 | update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type); |
| 1970 | } |
| 1971 | break; |
| 1972 | default: |
| 1973 | break; |
| 1974 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1979 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1980 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1981 | case Instruction::IF_LTZ: |
| 1982 | case Instruction::IF_GEZ: |
| 1983 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1984 | case Instruction::IF_LEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1985 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1986 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1987 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 1988 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1989 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1990 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1991 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1992 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1993 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1994 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1995 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1996 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1997 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1998 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1999 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2000 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2001 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2002 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2003 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2004 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2005 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2006 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2007 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2008 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2009 | break; |
| 2010 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2011 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2012 | break; |
| 2013 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2014 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2015 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2016 | break; |
| 2017 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2018 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2019 | break; |
| 2020 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2021 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2022 | break; |
| 2023 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2024 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2025 | break; |
| 2026 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2027 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | break; |
| 2029 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2030 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2031 | break; |
| 2032 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2033 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2034 | break; |
| 2035 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2036 | case Instruction::IGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2037 | VerifyISGet(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2038 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2039 | case Instruction::IGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2040 | VerifyISGet(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2041 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2042 | case Instruction::IGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2043 | VerifyISGet(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2044 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | case Instruction::IGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2046 | VerifyISGet(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2047 | break; |
| 2048 | case Instruction::IGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2049 | VerifyISGet(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2050 | break; |
| 2051 | case Instruction::IGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2052 | VerifyISGet(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2053 | break; |
| 2054 | case Instruction::IGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2055 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2056 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2057 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2058 | case Instruction::IPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2059 | VerifyISPut(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2060 | break; |
| 2061 | case Instruction::IPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2062 | VerifyISPut(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | case Instruction::IPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2065 | VerifyISPut(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2066 | break; |
| 2067 | case Instruction::IPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2068 | VerifyISPut(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2069 | break; |
| 2070 | case Instruction::IPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2071 | VerifyISPut(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2072 | break; |
| 2073 | case Instruction::IPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2074 | VerifyISPut(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2075 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2076 | case Instruction::IPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2077 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2078 | break; |
| 2079 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2080 | case Instruction::SGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2081 | VerifyISGet(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2082 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2083 | case Instruction::SGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2084 | VerifyISGet(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2085 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2086 | case Instruction::SGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2087 | VerifyISGet(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2088 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2089 | case Instruction::SGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2090 | VerifyISGet(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2091 | break; |
| 2092 | case Instruction::SGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2093 | VerifyISGet(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | case Instruction::SGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2096 | VerifyISGet(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2097 | break; |
| 2098 | case Instruction::SGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2099 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2100 | break; |
| 2101 | |
| 2102 | case Instruction::SPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2103 | VerifyISPut(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2104 | break; |
| 2105 | case Instruction::SPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2106 | VerifyISPut(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2107 | break; |
| 2108 | case Instruction::SPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2109 | VerifyISPut(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2110 | break; |
| 2111 | case Instruction::SPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2112 | VerifyISPut(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2113 | break; |
| 2114 | case Instruction::SPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2115 | VerifyISPut(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2116 | break; |
| 2117 | case Instruction::SPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2118 | VerifyISPut(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2119 | break; |
| 2120 | case Instruction::SPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2121 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2122 | break; |
| 2123 | |
| 2124 | case Instruction::INVOKE_VIRTUAL: |
| 2125 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2126 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2127 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2128 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2129 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
| 2130 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2131 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2132 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2133 | is_range, is_super); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2134 | const RegType* return_type = nullptr; |
| 2135 | if (called_method != nullptr) { |
| 2136 | MethodHelper mh(called_method); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 2137 | mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2138 | if (return_type_class != nullptr) { |
| 2139 | return_type = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 2140 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| 2141 | } else { |
| 2142 | Thread* self = Thread::Current(); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 2143 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2144 | self->ClearException(); |
| 2145 | } |
| 2146 | } |
| 2147 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2148 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2149 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2150 | 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] | 2151 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2152 | return_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2153 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2154 | if (!return_type->IsLowHalf()) { |
| 2155 | work_line_->SetResultRegisterType(*return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2156 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2157 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2158 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2159 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2160 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2161 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2162 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2163 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2164 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2165 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2166 | is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2167 | const char* return_type_descriptor; |
| 2168 | bool is_constructor; |
| 2169 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2170 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2171 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2172 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2173 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2174 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2175 | } else { |
| 2176 | is_constructor = called_method->IsConstructor(); |
| 2177 | return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
| 2178 | } |
| 2179 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2180 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2181 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2182 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2183 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2184 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2185 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2186 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2187 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2188 | if (this_type.IsConflict()) // failure. |
| 2189 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2190 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2191 | /* no null refs allowed (?) */ |
| 2192 | if (this_type.IsZero()) { |
| 2193 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2194 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2195 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2196 | |
| 2197 | /* must be in same class or in superclass */ |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2198 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
| 2199 | // TODO: re-enable constructor type verification |
| 2200 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2201 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2202 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2203 | // break; |
| 2204 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2205 | |
| 2206 | /* arg must be an uninitialized reference */ |
| 2207 | if (!this_type.IsUninitializedTypes()) { |
| 2208 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2209 | << this_type; |
| 2210 | break; |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2215 | * registers that have the same object instance in them, not just the "this" register. |
| 2216 | */ |
| 2217 | work_line_->MarkRefsAsInitialized(this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2218 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2219 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2220 | return_type_descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2221 | if (!return_type.IsLowHalf()) { |
| 2222 | work_line_->SetResultRegisterType(return_type); |
| 2223 | } else { |
| 2224 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2225 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2226 | just_set_result = true; |
| 2227 | break; |
| 2228 | } |
| 2229 | case Instruction::INVOKE_STATIC: |
| 2230 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2231 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2232 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2233 | METHOD_STATIC, |
| 2234 | is_range, |
| 2235 | false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2236 | const char* descriptor; |
| 2237 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2238 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2239 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2240 | 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] | 2241 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2242 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2243 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2244 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2245 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2246 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2247 | if (!return_type.IsLowHalf()) { |
| 2248 | work_line_->SetResultRegisterType(return_type); |
| 2249 | } else { |
| 2250 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2251 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2252 | just_set_result = true; |
| 2253 | } |
| 2254 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2255 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2256 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2257 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2258 | mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2259 | METHOD_INTERFACE, |
| 2260 | is_range, |
| 2261 | false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2262 | if (abs_method != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2263 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2264 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2265 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2266 | << PrettyMethod(abs_method) << "'"; |
| 2267 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2268 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2269 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2270 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2271 | * interface or Object (see comments in RegType::JoinClass). |
| 2272 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2273 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2274 | if (this_type.IsZero()) { |
| 2275 | /* null pointer always passes (and always fails at runtime) */ |
| 2276 | } else { |
| 2277 | if (this_type.IsUninitializedTypes()) { |
| 2278 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2279 | << this_type; |
| 2280 | break; |
| 2281 | } |
| 2282 | // In the past we have tried to assert that "called_interface" is assignable |
| 2283 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2284 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2285 | // implemented by "this_type". For example, two classes may implement the same |
| 2286 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2287 | // join will set "this_type" to the parent class and a test that this implements |
| 2288 | // the interface will incorrectly fail. |
| 2289 | } |
| 2290 | /* |
| 2291 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2292 | * the type information is in the abstract method, so we're good. |
| 2293 | */ |
| 2294 | const char* descriptor; |
| 2295 | if (abs_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2296 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2297 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2298 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2299 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2300 | } else { |
| 2301 | descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor(); |
| 2302 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2303 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2304 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2305 | if (!return_type.IsLowHalf()) { |
| 2306 | work_line_->SetResultRegisterType(return_type); |
| 2307 | } else { |
| 2308 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2309 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2310 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2311 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2312 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2313 | case Instruction::NEG_INT: |
| 2314 | case Instruction::NOT_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2315 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2316 | break; |
| 2317 | case Instruction::NEG_LONG: |
| 2318 | case Instruction::NOT_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2319 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2320 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2321 | break; |
| 2322 | case Instruction::NEG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2323 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2324 | break; |
| 2325 | case Instruction::NEG_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2326 | work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2327 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2328 | break; |
| 2329 | case Instruction::INT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2330 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2331 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2332 | break; |
| 2333 | case Instruction::INT_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2334 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2335 | break; |
| 2336 | case Instruction::INT_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2337 | work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2338 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2339 | break; |
| 2340 | case Instruction::LONG_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2341 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2342 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | case Instruction::LONG_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2345 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2346 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2347 | break; |
| 2348 | case Instruction::LONG_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2349 | work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2350 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2351 | break; |
| 2352 | case Instruction::FLOAT_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2353 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2354 | break; |
| 2355 | case Instruction::FLOAT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2356 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2357 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2358 | break; |
| 2359 | case Instruction::FLOAT_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2360 | work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2361 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2362 | break; |
| 2363 | case Instruction::DOUBLE_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2364 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2365 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2366 | break; |
| 2367 | case Instruction::DOUBLE_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2368 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2369 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2370 | break; |
| 2371 | case Instruction::DOUBLE_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2372 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2373 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2374 | break; |
| 2375 | case Instruction::INT_TO_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2376 | work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2377 | break; |
| 2378 | case Instruction::INT_TO_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2379 | work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2380 | break; |
| 2381 | case Instruction::INT_TO_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2382 | work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2383 | break; |
| 2384 | |
| 2385 | case Instruction::ADD_INT: |
| 2386 | case Instruction::SUB_INT: |
| 2387 | case Instruction::MUL_INT: |
| 2388 | case Instruction::REM_INT: |
| 2389 | case Instruction::DIV_INT: |
| 2390 | case Instruction::SHL_INT: |
| 2391 | case Instruction::SHR_INT: |
| 2392 | case Instruction::USHR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2393 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2394 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2395 | break; |
| 2396 | case Instruction::AND_INT: |
| 2397 | case Instruction::OR_INT: |
| 2398 | case Instruction::XOR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2399 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2400 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2401 | break; |
| 2402 | case Instruction::ADD_LONG: |
| 2403 | case Instruction::SUB_LONG: |
| 2404 | case Instruction::MUL_LONG: |
| 2405 | case Instruction::DIV_LONG: |
| 2406 | case Instruction::REM_LONG: |
| 2407 | case Instruction::AND_LONG: |
| 2408 | case Instruction::OR_LONG: |
| 2409 | case Instruction::XOR_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2410 | work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2411 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2412 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2413 | break; |
| 2414 | case Instruction::SHL_LONG: |
| 2415 | case Instruction::SHR_LONG: |
| 2416 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2417 | /* shift distance is Int, making these different from other binary operations */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2418 | work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2419 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2420 | break; |
| 2421 | case Instruction::ADD_FLOAT: |
| 2422 | case Instruction::SUB_FLOAT: |
| 2423 | case Instruction::MUL_FLOAT: |
| 2424 | case Instruction::DIV_FLOAT: |
| 2425 | case Instruction::REM_FLOAT: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2426 | work_line_->CheckBinaryOp(inst, |
| 2427 | reg_types_.Float(), |
| 2428 | reg_types_.Float(), |
| 2429 | reg_types_.Float(), |
| 2430 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2431 | break; |
| 2432 | case Instruction::ADD_DOUBLE: |
| 2433 | case Instruction::SUB_DOUBLE: |
| 2434 | case Instruction::MUL_DOUBLE: |
| 2435 | case Instruction::DIV_DOUBLE: |
| 2436 | case Instruction::REM_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2437 | work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2438 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2439 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2440 | break; |
| 2441 | case Instruction::ADD_INT_2ADDR: |
| 2442 | case Instruction::SUB_INT_2ADDR: |
| 2443 | case Instruction::MUL_INT_2ADDR: |
| 2444 | case Instruction::REM_INT_2ADDR: |
| 2445 | case Instruction::SHL_INT_2ADDR: |
| 2446 | case Instruction::SHR_INT_2ADDR: |
| 2447 | case Instruction::USHR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2448 | work_line_->CheckBinaryOp2addr(inst, |
| 2449 | reg_types_.Integer(), |
| 2450 | reg_types_.Integer(), |
| 2451 | reg_types_.Integer(), |
| 2452 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2453 | break; |
| 2454 | case Instruction::AND_INT_2ADDR: |
| 2455 | case Instruction::OR_INT_2ADDR: |
| 2456 | case Instruction::XOR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2457 | work_line_->CheckBinaryOp2addr(inst, |
| 2458 | reg_types_.Integer(), |
| 2459 | reg_types_.Integer(), |
| 2460 | reg_types_.Integer(), |
| 2461 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2462 | break; |
| 2463 | case Instruction::DIV_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2464 | work_line_->CheckBinaryOp2addr(inst, |
| 2465 | reg_types_.Integer(), |
| 2466 | reg_types_.Integer(), |
| 2467 | reg_types_.Integer(), |
| 2468 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2469 | break; |
| 2470 | case Instruction::ADD_LONG_2ADDR: |
| 2471 | case Instruction::SUB_LONG_2ADDR: |
| 2472 | case Instruction::MUL_LONG_2ADDR: |
| 2473 | case Instruction::DIV_LONG_2ADDR: |
| 2474 | case Instruction::REM_LONG_2ADDR: |
| 2475 | case Instruction::AND_LONG_2ADDR: |
| 2476 | case Instruction::OR_LONG_2ADDR: |
| 2477 | case Instruction::XOR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2478 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2479 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2480 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2481 | break; |
| 2482 | case Instruction::SHL_LONG_2ADDR: |
| 2483 | case Instruction::SHR_LONG_2ADDR: |
| 2484 | case Instruction::USHR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2485 | work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2486 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2487 | break; |
| 2488 | case Instruction::ADD_FLOAT_2ADDR: |
| 2489 | case Instruction::SUB_FLOAT_2ADDR: |
| 2490 | case Instruction::MUL_FLOAT_2ADDR: |
| 2491 | case Instruction::DIV_FLOAT_2ADDR: |
| 2492 | case Instruction::REM_FLOAT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2493 | work_line_->CheckBinaryOp2addr(inst, |
| 2494 | reg_types_.Float(), |
| 2495 | reg_types_.Float(), |
| 2496 | reg_types_.Float(), |
| 2497 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2498 | break; |
| 2499 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2500 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2501 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2502 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2503 | case Instruction::REM_DOUBLE_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2504 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2505 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2506 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2507 | break; |
| 2508 | case Instruction::ADD_INT_LIT16: |
| 2509 | case Instruction::RSUB_INT: |
| 2510 | case Instruction::MUL_INT_LIT16: |
| 2511 | case Instruction::DIV_INT_LIT16: |
| 2512 | case Instruction::REM_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2513 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2514 | break; |
| 2515 | case Instruction::AND_INT_LIT16: |
| 2516 | case Instruction::OR_INT_LIT16: |
| 2517 | case Instruction::XOR_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2518 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2519 | break; |
| 2520 | case Instruction::ADD_INT_LIT8: |
| 2521 | case Instruction::RSUB_INT_LIT8: |
| 2522 | case Instruction::MUL_INT_LIT8: |
| 2523 | case Instruction::DIV_INT_LIT8: |
| 2524 | case Instruction::REM_INT_LIT8: |
| 2525 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2526 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2527 | case Instruction::USHR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2528 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2529 | break; |
| 2530 | case Instruction::AND_INT_LIT8: |
| 2531 | case Instruction::OR_INT_LIT8: |
| 2532 | case Instruction::XOR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2533 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2534 | break; |
| 2535 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2536 | // Special instructions. |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2537 | case Instruction::RETURN_VOID_BARRIER: |
Ian Rogers | 9fc16eb | 2013-07-31 14:49:16 -0700 | [diff] [blame] | 2538 | if (!IsConstructor() || IsStatic()) { |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2539 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected"; |
| 2540 | } |
| 2541 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2542 | // Note: the following instructions encode offsets derived from class linking. |
| 2543 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2544 | // meaning if the class linking and resolution were successful. |
| 2545 | case Instruction::IGET_QUICK: |
| 2546 | VerifyIGetQuick(inst, reg_types_.Integer(), true); |
| 2547 | break; |
| 2548 | case Instruction::IGET_WIDE_QUICK: |
| 2549 | VerifyIGetQuick(inst, reg_types_.LongLo(), true); |
| 2550 | break; |
| 2551 | case Instruction::IGET_OBJECT_QUICK: |
| 2552 | VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2553 | break; |
| 2554 | case Instruction::IPUT_QUICK: |
| 2555 | VerifyIPutQuick(inst, reg_types_.Integer(), true); |
| 2556 | break; |
| 2557 | case Instruction::IPUT_WIDE_QUICK: |
| 2558 | VerifyIPutQuick(inst, reg_types_.LongLo(), true); |
| 2559 | break; |
| 2560 | case Instruction::IPUT_OBJECT_QUICK: |
| 2561 | VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2562 | break; |
| 2563 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2564 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2565 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2566 | mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2567 | if (called_method != NULL) { |
| 2568 | const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2569 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2570 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2571 | if (!return_type.IsLowHalf()) { |
| 2572 | work_line_->SetResultRegisterType(return_type); |
| 2573 | } else { |
| 2574 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2575 | } |
| 2576 | just_set_result = true; |
| 2577 | } |
| 2578 | break; |
| 2579 | } |
| 2580 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2581 | /* These should never appear during verification. */ |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2582 | case Instruction::UNUSED_3E: |
| 2583 | case Instruction::UNUSED_3F: |
| 2584 | case Instruction::UNUSED_40: |
| 2585 | case Instruction::UNUSED_41: |
| 2586 | case Instruction::UNUSED_42: |
| 2587 | case Instruction::UNUSED_43: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2588 | case Instruction::UNUSED_79: |
| 2589 | case Instruction::UNUSED_7A: |
| 2590 | case Instruction::UNUSED_EB: |
| 2591 | case Instruction::UNUSED_EC: |
jeffhao | 9a4f003 | 2012-08-30 16:17:40 -0700 | [diff] [blame] | 2592 | case Instruction::UNUSED_ED: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2593 | case Instruction::UNUSED_EE: |
| 2594 | case Instruction::UNUSED_EF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2595 | case Instruction::UNUSED_F0: |
| 2596 | case Instruction::UNUSED_F1: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2597 | case Instruction::UNUSED_F2: |
| 2598 | case Instruction::UNUSED_F3: |
| 2599 | case Instruction::UNUSED_F4: |
| 2600 | case Instruction::UNUSED_F5: |
| 2601 | case Instruction::UNUSED_F6: |
| 2602 | case Instruction::UNUSED_F7: |
| 2603 | case Instruction::UNUSED_F8: |
| 2604 | case Instruction::UNUSED_F9: |
| 2605 | case Instruction::UNUSED_FA: |
| 2606 | case Instruction::UNUSED_FB: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2607 | case Instruction::UNUSED_FC: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2608 | case Instruction::UNUSED_FD: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2609 | case Instruction::UNUSED_FE: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2610 | case Instruction::UNUSED_FF: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2611 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2612 | break; |
| 2613 | |
| 2614 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2615 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2616 | * complain if an instruction is missing (which is desirable). |
| 2617 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2618 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2619 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2620 | if (have_pending_hard_failure_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2621 | if (Runtime::Current()->IsCompiler()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2622 | /* When compiling, check that the last failure is a hard failure */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2623 | CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2624 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2625 | /* immediate failure, reject class */ |
| 2626 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 2627 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2628 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2629 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2630 | opcode_flags = Instruction::kThrow; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2631 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2632 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2633 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2634 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2635 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2636 | */ |
| 2637 | if (!just_set_result) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2638 | work_line_->SetResultTypeToUnknown(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2639 | } |
| 2640 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2641 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2642 | |
| 2643 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2644 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2645 | * |
| 2646 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2647 | * 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] | 2648 | * somebody could get a reference field, check it for zero, and if the |
| 2649 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2650 | * 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] | 2651 | * that, and will reject the code. |
| 2652 | * |
| 2653 | * TODO: avoid re-fetching the branch target |
| 2654 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2655 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2656 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2657 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2658 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2659 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2660 | return false; |
| 2661 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2662 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2663 | if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2664 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2665 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2666 | /* update branch target, set "changed" if appropriate */ |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2667 | if (NULL != branch_line.get()) { |
| 2668 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) { |
| 2669 | return false; |
| 2670 | } |
| 2671 | } else { |
| 2672 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) { |
| 2673 | return false; |
| 2674 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2675 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2679 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2680 | * |
| 2681 | * We've already verified that the table is structurally sound, so we |
| 2682 | * just need to walk through and tag the targets. |
| 2683 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2684 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2685 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 2686 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 2687 | int switch_count = switch_insns[1]; |
| 2688 | int offset_to_targets, targ; |
| 2689 | |
| 2690 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 2691 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 2692 | offset_to_targets = 4; |
| 2693 | } else { |
| 2694 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2695 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2696 | offset_to_targets = 2 + 2 * switch_count; |
| 2697 | } |
| 2698 | |
| 2699 | /* verify each switch target */ |
| 2700 | for (targ = 0; targ < switch_count; targ++) { |
| 2701 | int offset; |
| 2702 | uint32_t abs_offset; |
| 2703 | |
| 2704 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 2705 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 2706 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2707 | abs_offset = work_insn_idx_ + offset; |
| 2708 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2709 | if (!CheckNotMoveException(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2710 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2711 | } |
| 2712 | if (!UpdateRegisters(abs_offset, work_line_.get())) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2713 | return false; |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2718 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 2719 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2720 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2721 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2722 | bool within_catch_all = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2723 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2725 | for (; iterator.HasNext(); iterator.Next()) { |
| 2726 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2727 | within_catch_all = true; |
| 2728 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2729 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2730 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 2731 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 2732 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2733 | */ |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2734 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2735 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2736 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2740 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 2741 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2742 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2743 | if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2745 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 2746 | * 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] | 2747 | * it will do so before grabbing the lock). |
| 2748 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2749 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2750 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2751 | << "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] | 2752 | return false; |
| 2753 | } |
| 2754 | } |
| 2755 | } |
| 2756 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2757 | /* Handle "continue". Tag the next consecutive instruction. |
| 2758 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 2759 | * because it changes work_line_ when performing peephole optimization |
| 2760 | * and this change should not be used in those cases. |
| 2761 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2762 | if ((opcode_flags & Instruction::kContinue) != 0) { |
| 2763 | uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits(); |
| 2764 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 2765 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 2766 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2767 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2768 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 2769 | // next instruction isn't one. |
| 2770 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 2771 | return false; |
| 2772 | } |
| 2773 | if (NULL != fallthrough_line.get()) { |
| 2774 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 2775 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 2776 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 2777 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 2778 | // For returns we only care about the operand to the return, all other registers are dead. |
| 2779 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 2780 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2781 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 2782 | work_line_->MarkAllRegistersAsConflicts(); |
| 2783 | } else { |
| 2784 | if (opcode == Instruction::RETURN_WIDE) { |
| 2785 | work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 2786 | } else { |
| 2787 | work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 2788 | } |
| 2789 | } |
| 2790 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2791 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| 2792 | if (next_line != NULL) { |
| 2793 | // Merge registers into what we have for the next instruction, |
| 2794 | // and set the "changed" flag if needed. |
| 2795 | if (!UpdateRegisters(next_insn_idx, work_line_.get())) { |
| 2796 | return false; |
| 2797 | } |
| 2798 | } else { |
| 2799 | /* |
| 2800 | * We're not recording register data for the next instruction, so we don't know what the |
| 2801 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 2802 | */ |
| 2803 | insn_flags_[next_insn_idx].SetChanged(); |
| 2804 | } |
| 2805 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2806 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2807 | /* 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] | 2808 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 2809 | if (!work_line_->VerifyMonitorStackEmpty()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2810 | return false; |
| 2811 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2815 | * Update start_guess. Advance to the next instruction of that's |
| 2816 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2817 | * neither of those exists we're in a return or throw; leave start_guess |
| 2818 | * alone and let the caller sort it out. |
| 2819 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2820 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2821 | *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2822 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2823 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2824 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2827 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 2828 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2829 | |
| 2830 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2831 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2832 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2833 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2834 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2835 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2836 | mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2837 | const RegType& result = |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 2838 | klass != NULL ? reg_types_.FromClass(descriptor, klass, |
| 2839 | klass->CannotBeAssignedFromOtherTypes()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2840 | : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2841 | if (result.IsConflict()) { |
| 2842 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 2843 | << "' in " << referrer; |
| 2844 | return result; |
| 2845 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2846 | if (klass == NULL && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2847 | (*dex_cache_)->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2848 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2849 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 2850 | // check at runtime if access is allowed and so pass here. If result is |
| 2851 | // primitive, skip the access check. |
| 2852 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 2853 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2854 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2855 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2856 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2857 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2860 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2861 | const RegType* common_super = NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2862 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2863 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2864 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2865 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2866 | CatchHandlerIterator iterator(handlers_ptr); |
| 2867 | for (; iterator.HasNext(); iterator.Next()) { |
| 2868 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 2869 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2870 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2871 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2872 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 2873 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 2874 | if (exception.IsUnresolvedTypes()) { |
| 2875 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 2876 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 2877 | return exception; |
| 2878 | } else { |
| 2879 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 2880 | return reg_types_.Conflict(); |
| 2881 | } |
Jeff Hao | b878f21 | 2014-04-24 16:25:36 -0700 | [diff] [blame] | 2882 | } else if (common_super == nullptr) { |
| 2883 | common_super = &exception; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2884 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2885 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2886 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2887 | common_super = &common_super->Merge(exception, ®_types_); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2888 | CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2889 | } |
| 2890 | } |
| 2891 | } |
| 2892 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2893 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | if (common_super == NULL) { |
| 2897 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2898 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2899 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2900 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2901 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2904 | mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2905 | MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2906 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2907 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2908 | if (klass_type.IsConflict()) { |
| 2909 | std::string append(" in attempt to access method "); |
| 2910 | append += dex_file_->GetMethodName(method_id); |
| 2911 | AppendToLastFailMessage(append); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2912 | return NULL; |
| 2913 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2914 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2915 | return NULL; // Can't resolve Class so no more to do here |
| 2916 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2917 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2918 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2919 | mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2920 | if (res_method == NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2921 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2922 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2923 | |
| 2924 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2925 | res_method = klass->FindDirectMethod(name, signature); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2926 | } else if (method_type == METHOD_INTERFACE) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2927 | res_method = klass->FindInterfaceMethod(name, signature); |
| 2928 | } else { |
| 2929 | res_method = klass->FindVirtualMethod(name, signature); |
| 2930 | } |
| 2931 | if (res_method != NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2932 | (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2933 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2934 | // If a virtual or interface method wasn't found with the expected type, look in |
| 2935 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 2936 | // a class has changed, and will be flagged as an error in later checks. |
| 2937 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
| 2938 | res_method = klass->FindDirectMethod(name, signature); |
| 2939 | } |
| 2940 | if (res_method == NULL) { |
| 2941 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 2942 | << PrettyDescriptor(klass) << "." << name |
| 2943 | << " " << signature; |
| 2944 | return NULL; |
| 2945 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2946 | } |
| 2947 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2948 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 2949 | // enforce them here. |
| 2950 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2951 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 2952 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2953 | return NULL; |
| 2954 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2955 | // Disallow any calls to class initializers. |
| 2956 | if (MethodHelper(res_method).IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2957 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 2958 | << PrettyMethod(res_method); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2959 | return NULL; |
| 2960 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2961 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2962 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2963 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2964 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2965 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2966 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 2967 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 2968 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2969 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 2970 | << PrettyMethod(res_method); |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 2971 | return NULL; |
| 2972 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2973 | // Check that interface methods match interface classes. |
| 2974 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 2975 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 2976 | << " is in an interface class " << PrettyClass(klass); |
| 2977 | return NULL; |
| 2978 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 2979 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 2980 | << " is in a non-interface class " << PrettyClass(klass); |
| 2981 | return NULL; |
| 2982 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2983 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 2984 | // target method. |
| 2985 | if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || |
| 2986 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 2987 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 2988 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 2989 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 2990 | " type of " << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2991 | return NULL; |
| 2992 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2993 | return res_method; |
| 2994 | } |
| 2995 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2996 | mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2997 | MethodType method_type, |
| 2998 | bool is_range, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2999 | bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3000 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3001 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3002 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3003 | mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3004 | if (res_method == NULL) { // error or class is unresolved |
| 3005 | return NULL; |
| 3006 | } |
| 3007 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3008 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3009 | // has a vtable entry for the target method. |
| 3010 | if (is_super) { |
| 3011 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3012 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3013 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3014 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3015 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3016 | << " to super " << PrettyMethod(res_method); |
| 3017 | return NULL; |
| 3018 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3019 | mirror::Class* super_klass = super.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3020 | if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3021 | MethodHelper mh(res_method); |
| 3022 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3023 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3024 | << " to super " << super |
| 3025 | << "." << mh.GetName() |
| 3026 | << mh.GetSignature(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3027 | return NULL; |
| 3028 | } |
| 3029 | } |
| 3030 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3031 | // match the call to the signature. Also, we might be calling through an abstract method |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3032 | // definition (which doesn't have register count values). |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3033 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3034 | /* caught by static verifier */ |
| 3035 | DCHECK(is_range || expected_args <= 5); |
| 3036 | if (expected_args > code_item_->outs_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3037 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3038 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3039 | return NULL; |
| 3040 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3041 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3042 | /* |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3043 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3044 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3045 | * rigorous check here (which is okay since we have to do it at runtime). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3046 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3047 | size_t actual_args = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3048 | if (!res_method->IsStatic()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3049 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3050 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3051 | return NULL; |
| 3052 | } |
| 3053 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3054 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3055 | return NULL; |
| 3056 | } |
| 3057 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3058 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3059 | const RegType& res_method_class = |
| 3060 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3061 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3062 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3063 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3064 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3065 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3066 | return NULL; |
| 3067 | } |
| 3068 | } |
| 3069 | actual_args++; |
| 3070 | } |
| 3071 | /* |
| 3072 | * Process the target method's signature. This signature may or may not |
| 3073 | * have been verified, so we can't assume it's properly formed. |
| 3074 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3075 | MethodHelper mh(res_method); |
| 3076 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3077 | size_t params_size = params == NULL ? 0 : params->Size(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3078 | uint32_t arg[5]; |
| 3079 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3080 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3081 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3082 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3083 | if (actual_args >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3084 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3085 | << "'. Expected " << expected_args << " arguments, processing argument " << actual_args |
| 3086 | << " (where longs/doubles count twice)."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3087 | return NULL; |
| 3088 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3089 | const char* descriptor = |
| 3090 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3091 | if (descriptor == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3092 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3093 | << " missing signature component"; |
| 3094 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3095 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3096 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3097 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; |
Jeff Hao | a6b22c5 | 2013-10-04 14:33:22 -0700 | [diff] [blame] | 3098 | if (reg_type.IsIntegralTypes()) { |
| 3099 | const RegType& src_type = work_line_->GetRegisterType(get_reg); |
| 3100 | if (!src_type.IsIntegralTypes()) { |
| 3101 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3102 | << " but expected " << reg_type; |
| 3103 | return res_method; |
| 3104 | } |
| 3105 | } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3106 | return res_method; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3107 | } |
| 3108 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3109 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3110 | if (actual_args != expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3111 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3112 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3113 | return NULL; |
| 3114 | } else { |
| 3115 | return res_method; |
| 3116 | } |
| 3117 | } |
| 3118 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3119 | mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3120 | RegisterLine* reg_line, bool is_range) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3121 | DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK || |
| 3122 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3123 | const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 3124 | if (!actual_arg_type.HasClass()) { |
| 3125 | VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'"; |
Andreas Gampe | 6398156 | 2014-04-17 12:28:43 -0700 | [diff] [blame] | 3126 | return nullptr; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3127 | } |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3128 | mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr; |
| 3129 | mirror::Class* klass = actual_arg_type.GetClass(); |
| 3130 | if (klass->IsInterface()) { |
| 3131 | // Derive Object.class from Class.class.getSuperclass(). |
| 3132 | mirror::Class* object_klass = klass->GetClass()->GetSuperClass(); |
| 3133 | CHECK(object_klass->IsObjectClass()); |
| 3134 | vtable = object_klass->GetVTable(); |
| 3135 | } else { |
| 3136 | vtable = klass->GetVTable(); |
| 3137 | } |
| 3138 | CHECK(vtable != nullptr) << PrettyDescriptor(klass); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3139 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | a4cf1df | 2014-05-07 19:47:17 -0700 | [diff] [blame] | 3140 | CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3141 | mirror::ArtMethod* res_method = vtable->Get(vtable_index); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3142 | CHECK(!Thread::Current()->IsExceptionPending()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3143 | return res_method; |
| 3144 | } |
| 3145 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3146 | mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3147 | bool is_range) { |
| 3148 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3149 | mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3150 | is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3151 | if (res_method == NULL) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3152 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3153 | return NULL; |
| 3154 | } |
| 3155 | CHECK(!res_method->IsDirect() && !res_method->IsStatic()); |
| 3156 | |
| 3157 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3158 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3159 | // definition (which doesn't have register count values). |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3160 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
| 3161 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3162 | return NULL; |
| 3163 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3164 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3165 | /* caught by static verifier */ |
| 3166 | DCHECK(is_range || expected_args <= 5); |
| 3167 | if (expected_args > code_item_->outs_size_) { |
| 3168 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3169 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3170 | return NULL; |
| 3171 | } |
| 3172 | |
| 3173 | /* |
| 3174 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3175 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3176 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3177 | */ |
| 3178 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3179 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3180 | return NULL; |
| 3181 | } |
| 3182 | if (!actual_arg_type.IsZero()) { |
| 3183 | mirror::Class* klass = res_method->GetDeclaringClass(); |
| 3184 | const RegType& res_method_class = |
| 3185 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3186 | klass->CannotBeAssignedFromOtherTypes()); |
| 3187 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3188 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3189 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3190 | << "' not instance of '" << res_method_class << "'"; |
| 3191 | return NULL; |
| 3192 | } |
| 3193 | } |
| 3194 | /* |
| 3195 | * Process the target method's signature. This signature may or may not |
| 3196 | * have been verified, so we can't assume it's properly formed. |
| 3197 | */ |
| 3198 | MethodHelper mh(res_method); |
| 3199 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3200 | size_t params_size = params == NULL ? 0 : params->Size(); |
| 3201 | uint32_t arg[5]; |
| 3202 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3203 | inst->GetVarArgs(arg); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3204 | } |
| 3205 | size_t actual_args = 1; |
| 3206 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3207 | if (actual_args >= expected_args) { |
| 3208 | 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] | 3209 | << "'. Expected " << expected_args |
| 3210 | << " arguments, processing argument " << actual_args |
| 3211 | << " (where longs/doubles count twice)."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3212 | return NULL; |
| 3213 | } |
| 3214 | const char* descriptor = |
| 3215 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3216 | if (descriptor == NULL) { |
| 3217 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3218 | << " missing signature component"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3219 | return NULL; |
| 3220 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3221 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3222 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; |
| 3223 | if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
| 3224 | return res_method; |
| 3225 | } |
| 3226 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3227 | } |
| 3228 | if (actual_args != expected_args) { |
| 3229 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3230 | << " expected " << expected_args << " arguments, found " << actual_args; |
| 3231 | return NULL; |
| 3232 | } else { |
| 3233 | return res_method; |
| 3234 | } |
| 3235 | } |
| 3236 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3237 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3238 | uint32_t type_idx; |
| 3239 | if (!is_filled) { |
| 3240 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3241 | type_idx = inst->VRegC_22c(); |
| 3242 | } else if (!is_range) { |
| 3243 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3244 | type_idx = inst->VRegB_35c(); |
| 3245 | } else { |
| 3246 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3247 | type_idx = inst->VRegB_3rc(); |
| 3248 | } |
| 3249 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3250 | if (res_type.IsConflict()) { // bad class |
| 3251 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3252 | } else { |
| 3253 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3254 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3255 | 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] | 3256 | } else if (!is_filled) { |
| 3257 | /* make sure "size" register is valid type */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3258 | work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3259 | /* set register type to array class */ |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3260 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3261 | work_line_->SetRegisterType(inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3262 | } else { |
| 3263 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3264 | // the list and fail. It's legal, if silly, for arg_count to be zero. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3265 | const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3266 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3267 | uint32_t arg[5]; |
| 3268 | if (!is_range) { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 3269 | inst->GetVarArgs(arg); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3270 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3271 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3272 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3273 | if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3274 | work_line_->SetResultRegisterType(reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3275 | return; |
| 3276 | } |
| 3277 | } |
| 3278 | // filled-array result goes into "result" register |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3279 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3280 | work_line_->SetResultRegisterType(precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3285 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3286 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3287 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3288 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3289 | 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] | 3290 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3291 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3292 | if (array_type.IsZero()) { |
| 3293 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3294 | // instruction type. TODO: have a proper notion of bottom here. |
| 3295 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3296 | // Reference or category 1 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3297 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3298 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3299 | // Category 2 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3300 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3301 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3302 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3303 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3304 | 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] | 3305 | } else { |
| 3306 | /* verify the class */ |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3307 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3308 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3309 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3310 | << " source for aget-object"; |
| 3311 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3312 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3313 | << " source for category 1 aget"; |
| 3314 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3315 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3316 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3317 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3318 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3319 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3320 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3321 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3322 | // doubles. |
| 3323 | if (!component_type.IsLowHalf()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3324 | work_line_->SetRegisterType(inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3325 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3326 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3327 | component_type.HighHalf(®_types_)); |
| 3328 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3329 | } |
| 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3334 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
| 3335 | const uint32_t vregA) { |
| 3336 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3337 | bool instruction_compatible; |
| 3338 | bool value_compatible; |
| 3339 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3340 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3341 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3342 | value_compatible = value_type.IsIntegralTypes(); |
| 3343 | } else if (target_type.IsFloat()) { |
| 3344 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3345 | value_compatible = value_type.IsFloatTypes(); |
| 3346 | } else if (target_type.IsLong()) { |
| 3347 | instruction_compatible = insn_type.IsLong(); |
| 3348 | value_compatible = value_type.IsLongTypes(); |
| 3349 | } else if (target_type.IsDouble()) { |
| 3350 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
| 3351 | value_compatible = value_type.IsDoubleTypes(); |
| 3352 | } else { |
| 3353 | instruction_compatible = false; // reference with primitive store |
| 3354 | value_compatible = false; // unused |
| 3355 | } |
| 3356 | if (!instruction_compatible) { |
| 3357 | // This is a global failure rather than a class change failure as the instructions and |
| 3358 | // the descriptors for the type should have been consistent within the same file at |
| 3359 | // compile time. |
| 3360 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3361 | << "' but expected type '" << target_type << "'"; |
| 3362 | return; |
| 3363 | } |
| 3364 | if (!value_compatible) { |
| 3365 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3366 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3367 | return; |
| 3368 | } |
| 3369 | } |
| 3370 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3371 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3372 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3373 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3374 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3375 | 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] | 3376 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3377 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3378 | if (array_type.IsZero()) { |
| 3379 | // Null array type; this code path will fail at runtime. Infer a merge-able type from the |
| 3380 | // instruction type. |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3381 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3382 | 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] | 3383 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3384 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3385 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3386 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3387 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3388 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3389 | if (!component_type.IsReferenceTypes()) { |
| 3390 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3391 | << " source for aput-object"; |
| 3392 | } else { |
| 3393 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3394 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3395 | // incompatible classes will be caught at runtime as an array store exception |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3396 | work_line_->VerifyRegisterType(vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3397 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | } |
| 3402 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3403 | mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3404 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3405 | // Check access to class |
| 3406 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3407 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3408 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3409 | field_idx, dex_file_->GetFieldName(field_id), |
| 3410 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3411 | return NULL; |
| 3412 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3413 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3414 | return NULL; // 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] | 3415 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3416 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3417 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3418 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3419 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3420 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3421 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3422 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3423 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3424 | Thread::Current()->ClearException(); |
| 3425 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3426 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3427 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3428 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3429 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3430 | return NULL; |
| 3431 | } else if (!field->IsStatic()) { |
| 3432 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
| 3433 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3434 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3435 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3436 | } |
| 3437 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3438 | mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3439 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3440 | // Check access to class |
| 3441 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3442 | if (klass_type.IsConflict()) { |
| 3443 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 3444 | field_idx, dex_file_->GetFieldName(field_id), |
| 3445 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3446 | return NULL; |
| 3447 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3448 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3449 | return NULL; // Can't resolve Class so no more to do here |
| 3450 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3451 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3452 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3453 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3454 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3455 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3456 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3457 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3458 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3459 | Thread::Current()->ClearException(); |
| 3460 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3461 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3462 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3463 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3464 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3465 | return NULL; |
| 3466 | } else if (field->IsStatic()) { |
| 3467 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3468 | << " to not be static"; |
| 3469 | return NULL; |
| 3470 | } else if (obj_type.IsZero()) { |
| 3471 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3472 | return field; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3473 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3474 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3475 | const RegType& field_klass = |
| 3476 | reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3477 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3478 | if (obj_type.IsUninitializedTypes() && |
| 3479 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 3480 | !field_klass.Equals(GetDeclaringClass()))) { |
| 3481 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3482 | // the field is declared in this class |
| 3483 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3484 | << " of a not fully initialized object within the context" |
| 3485 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3486 | return NULL; |
| 3487 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 3488 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3489 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3490 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3491 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3492 | << " from object of type " << obj_type; |
| 3493 | return NULL; |
| 3494 | } else { |
| 3495 | return field; |
| 3496 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3500 | void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type, |
| 3501 | bool is_primitive, bool is_static) { |
| 3502 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3503 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3504 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3505 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3506 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3507 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3508 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3509 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3510 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3511 | if (field != NULL) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3512 | FieldHelper fh(field); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3513 | mirror::Class* field_type_class = fh.GetType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3514 | if (field_type_class != nullptr) { |
| 3515 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3516 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3517 | } else { |
| 3518 | Thread* self = Thread::Current(); |
| 3519 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 3520 | self->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3521 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3522 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3523 | if (field_type == nullptr) { |
| 3524 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3525 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3526 | field_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3527 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3528 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3529 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3530 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3531 | if (field_type->Equals(insn_type) || |
| 3532 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 3533 | (field_type->IsDouble() && insn_type.IsLong())) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3534 | // expected that read is of the correct primitive type or that int reads are reading |
| 3535 | // floats or long reads are reading doubles |
| 3536 | } else { |
| 3537 | // This is a global failure rather than a class change failure as the instructions and |
| 3538 | // the descriptors for the type should have been consistent within the same file at |
| 3539 | // compile time |
| 3540 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3541 | << " to be of type '" << insn_type |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3542 | << "' but found type '" << *field_type << "' in get"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3543 | return; |
| 3544 | } |
| 3545 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3546 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3547 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3548 | << " to be compatible with type '" << insn_type |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3549 | << "' but found type '" << *field_type |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3550 | << "' in Get-object"; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3551 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3552 | return; |
| 3553 | } |
| 3554 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3555 | if (!field_type->IsLowHalf()) { |
| 3556 | work_line_->SetRegisterType(vregA, *field_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3557 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3558 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3559 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3560 | } |
| 3561 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3562 | void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type, |
| 3563 | bool is_primitive, bool is_static) { |
| 3564 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3565 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3566 | if (is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3567 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3568 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3569 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3570 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3571 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3572 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3573 | if (field != NULL) { |
| 3574 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3575 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3576 | << " from other class " << GetDeclaringClass(); |
| 3577 | return; |
| 3578 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3579 | FieldHelper fh(field); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3580 | mirror::Class* field_type_class = fh.GetType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3581 | if (field_type_class != nullptr) { |
| 3582 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3583 | field_type_class->CannotBeAssignedFromOtherTypes()); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3584 | } else { |
| 3585 | Thread* self = Thread::Current(); |
| 3586 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 3587 | self->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | if (field_type == nullptr) { |
| 3591 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3592 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3593 | field_type = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3594 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3595 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3596 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3597 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3598 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3599 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3600 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3601 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3602 | << " to be compatible with type '" << insn_type |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3603 | << "' but found type '" << *field_type |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3604 | << "' in put-object"; |
| 3605 | return; |
| 3606 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3607 | work_line_->VerifyRegisterType(vregA, *field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3611 | mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 3612 | RegisterLine* reg_line) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3613 | DCHECK(inst->Opcode() == Instruction::IGET_QUICK || |
| 3614 | inst->Opcode() == Instruction::IGET_WIDE_QUICK || |
| 3615 | inst->Opcode() == Instruction::IGET_OBJECT_QUICK || |
| 3616 | inst->Opcode() == Instruction::IPUT_QUICK || |
| 3617 | inst->Opcode() == Instruction::IPUT_WIDE_QUICK || |
| 3618 | inst->Opcode() == Instruction::IPUT_OBJECT_QUICK); |
| 3619 | const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | 9bc5440 | 2014-04-17 16:40:01 -0700 | [diff] [blame] | 3620 | if (!object_type.HasClass()) { |
| 3621 | VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'"; |
| 3622 | return nullptr; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3623 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3624 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 3625 | mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), |
| 3626 | field_offset); |
| 3627 | if (f == nullptr) { |
| 3628 | VLOG(verifier) << "Failed to find instance field at offset '" << field_offset |
| 3629 | << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'"; |
| 3630 | } |
| 3631 | return f; |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type, |
| 3635 | bool is_primitive) { |
| 3636 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3637 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3638 | if (field == NULL) { |
| 3639 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3640 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3641 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3642 | FieldHelper fh(field); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3643 | mirror::Class* field_type_class = fh.GetType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3644 | const RegType* field_type; |
| 3645 | if (field_type_class != nullptr) { |
| 3646 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3647 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3648 | } else { |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3649 | Thread* self = Thread::Current(); |
| 3650 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
| 3651 | self->ClearException(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3652 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 3653 | fh.GetTypeDescriptor(), false); |
| 3654 | } |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3655 | DCHECK(field_type != nullptr); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3656 | const uint32_t vregA = inst->VRegA_22c(); |
| 3657 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3658 | if (field_type->Equals(insn_type) || |
| 3659 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 3660 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3661 | // expected that read is of the correct primitive type or that int reads are reading |
| 3662 | // floats or long reads are reading doubles |
| 3663 | } else { |
| 3664 | // This is a global failure rather than a class change failure as the instructions and |
| 3665 | // the descriptors for the type should have been consistent within the same file at |
| 3666 | // compile time |
| 3667 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3668 | << " to be of type '" << insn_type |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3669 | << "' but found type '" << *field_type << "' in Get"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3670 | return; |
| 3671 | } |
| 3672 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3673 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3674 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3675 | << " to be compatible with type '" << insn_type |
Sebastien Hertz | 757b304 | 2014-03-28 14:34:28 +0100 | [diff] [blame] | 3676 | << "' but found type '" << *field_type |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3677 | << "' in get-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3678 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
| 3679 | return; |
| 3680 | } |
| 3681 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3682 | if (!field_type->IsLowHalf()) { |
| 3683 | work_line_->SetRegisterType(vregA, *field_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3684 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3685 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type, |
| 3690 | bool is_primitive) { |
| 3691 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3692 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3693 | if (field == NULL) { |
| 3694 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3695 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3696 | } |
| 3697 | const char* descriptor = FieldHelper(field).GetTypeDescriptor(); |
| 3698 | mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3699 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false); |
| 3700 | if (field != NULL) { |
| 3701 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3702 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3703 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3704 | return; |
| 3705 | } |
| 3706 | } |
| 3707 | const uint32_t vregA = inst->VRegA_22c(); |
| 3708 | if (is_primitive) { |
| 3709 | // Primitive field assignability rules are weaker than regular assignability rules |
| 3710 | bool instruction_compatible; |
| 3711 | bool value_compatible; |
| 3712 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3713 | if (field_type.IsIntegralTypes()) { |
| 3714 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 3715 | value_compatible = value_type.IsIntegralTypes(); |
| 3716 | } else if (field_type.IsFloat()) { |
| 3717 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 3718 | value_compatible = value_type.IsFloatTypes(); |
| 3719 | } else if (field_type.IsLong()) { |
| 3720 | instruction_compatible = insn_type.IsLong(); |
| 3721 | value_compatible = value_type.IsLongTypes(); |
| 3722 | } else if (field_type.IsDouble()) { |
| 3723 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 3724 | value_compatible = value_type.IsDoubleTypes(); |
| 3725 | } else { |
| 3726 | instruction_compatible = false; // reference field with primitive store |
| 3727 | value_compatible = false; // unused |
| 3728 | } |
| 3729 | if (!instruction_compatible) { |
| 3730 | // This is a global failure rather than a class change failure as the instructions and |
| 3731 | // the descriptors for the type should have been consistent within the same file at |
| 3732 | // compile time |
| 3733 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3734 | << " to be of type '" << insn_type |
| 3735 | << "' but found type '" << field_type |
| 3736 | << "' in put"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3737 | return; |
| 3738 | } |
| 3739 | if (!value_compatible) { |
| 3740 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3741 | << " of type " << value_type |
| 3742 | << " but expected " << field_type |
| 3743 | << " for store to " << PrettyField(field) << " in put"; |
| 3744 | return; |
| 3745 | } |
| 3746 | } else { |
| 3747 | if (!insn_type.IsAssignableFrom(field_type)) { |
| 3748 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3749 | << " to be compatible with type '" << insn_type |
| 3750 | << "' but found type '" << field_type |
| 3751 | << "' in put-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3752 | return; |
| 3753 | } |
| 3754 | work_line_->VerifyRegisterType(vregA, field_type); |
| 3755 | } |
| 3756 | } |
| 3757 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3758 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3759 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3760 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3761 | return false; |
| 3762 | } |
| 3763 | return true; |
| 3764 | } |
| 3765 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3766 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3767 | bool changed = true; |
| 3768 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 3769 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3770 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3771 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 3772 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 3773 | * 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] | 3774 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3775 | if (!insn_flags_[next_insn].IsReturn()) { |
| 3776 | target_line->CopyFromLine(merge_line); |
| 3777 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3778 | // Verify that the monitor stack is empty on return. |
| 3779 | if (!merge_line->VerifyMonitorStackEmpty()) { |
| 3780 | return false; |
| 3781 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3782 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3783 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 3784 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 3785 | Instruction::Code opcode = ret_inst->Opcode(); |
| 3786 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 3787 | target_line->MarkAllRegistersAsConflicts(); |
| 3788 | } else { |
| 3789 | target_line->CopyFromLine(merge_line); |
| 3790 | if (opcode == Instruction::RETURN_WIDE) { |
| 3791 | target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 3792 | } else { |
| 3793 | target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 3794 | } |
| 3795 | } |
| 3796 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3797 | } else { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3798 | UniquePtr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 3799 | RegisterLine::Create(target_line->NumRegs(), this) : |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3800 | NULL); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3801 | if (gDebugVerify) { |
| 3802 | copy->CopyFromLine(target_line); |
| 3803 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3804 | changed = target_line->MergeRegisters(merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3805 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3806 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3807 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3808 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 3809 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 3810 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
| 3811 | << *copy.get() << " MERGE\n" |
| 3812 | << *merge_line << " ==\n" |
| 3813 | << *target_line << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3814 | } |
| 3815 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3816 | if (changed) { |
| 3817 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3818 | } |
| 3819 | return true; |
| 3820 | } |
| 3821 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 3822 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3823 | return &insn_flags_[work_insn_idx_]; |
| 3824 | } |
| 3825 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3826 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3827 | if (return_type_ == nullptr) { |
| 3828 | if (mirror_method_ != NULL) { |
| 3829 | MethodHelper mh(mirror_method_); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3830 | mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3831 | if (return_type_class != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3832 | return_type_ = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 3833 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3834 | } else { |
| 3835 | Thread* self = Thread::Current(); |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 3836 | DCHECK(!can_load_classes_ || self->IsExceptionPending()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3837 | self->ClearException(); |
| 3838 | } |
| 3839 | } |
| 3840 | if (return_type_ == nullptr) { |
| 3841 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 3842 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 3843 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 3844 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3845 | return_type_ = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3846 | } |
| 3847 | } |
| 3848 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3852 | if (declaring_class_ == NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3853 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3854 | const char* descriptor |
| 3855 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3856 | if (mirror_method_ != NULL) { |
| 3857 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3858 | declaring_class_ = ®_types_.FromClass(descriptor, klass, |
| 3859 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3860 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3861 | declaring_class_ = ®_types_.FromDescriptor(class_loader_->Get(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3862 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3863 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3864 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3865 | } |
| 3866 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3867 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 3868 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
Sebastien Hertz | aa0c00c | 2014-03-14 17:58:54 +0100 | [diff] [blame] | 3869 | 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] | 3870 | std::vector<int32_t> result; |
| 3871 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
| 3872 | const RegType& type = line->GetRegisterType(i); |
| 3873 | if (type.IsConstant()) { |
| 3874 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
| 3875 | result.push_back(type.ConstantValue()); |
| 3876 | } else if (type.IsConstantLo()) { |
| 3877 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
| 3878 | result.push_back(type.ConstantValueLo()); |
| 3879 | } else if (type.IsConstantHi()) { |
| 3880 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
| 3881 | result.push_back(type.ConstantValueHi()); |
| 3882 | } else if (type.IsIntegralTypes()) { |
| 3883 | result.push_back(kIntVReg); |
| 3884 | result.push_back(0); |
| 3885 | } else if (type.IsFloat()) { |
| 3886 | result.push_back(kFloatVReg); |
| 3887 | result.push_back(0); |
| 3888 | } else if (type.IsLong()) { |
| 3889 | result.push_back(kLongLoVReg); |
| 3890 | result.push_back(0); |
| 3891 | result.push_back(kLongHiVReg); |
| 3892 | result.push_back(0); |
| 3893 | ++i; |
| 3894 | } else if (type.IsDouble()) { |
| 3895 | result.push_back(kDoubleLoVReg); |
| 3896 | result.push_back(0); |
| 3897 | result.push_back(kDoubleHiVReg); |
| 3898 | result.push_back(0); |
| 3899 | ++i; |
| 3900 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 3901 | result.push_back(kUndefined); |
| 3902 | result.push_back(0); |
| 3903 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 3904 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3905 | result.push_back(kReferenceVReg); |
| 3906 | result.push_back(0); |
| 3907 | } |
| 3908 | } |
| 3909 | return result; |
| 3910 | } |
| 3911 | |
Sebastien Hertz | 849600b | 2013-12-20 10:28:08 +0100 | [diff] [blame] | 3912 | const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) { |
| 3913 | if (precise) { |
| 3914 | // Precise constant type. |
| 3915 | return reg_types_.FromCat1Const(value, true); |
| 3916 | } else { |
| 3917 | // Imprecise constant type. |
| 3918 | if (value < -32768) { |
| 3919 | return reg_types_.IntConstant(); |
| 3920 | } else if (value < -128) { |
| 3921 | return reg_types_.ShortConstant(); |
| 3922 | } else if (value < 0) { |
| 3923 | return reg_types_.ByteConstant(); |
| 3924 | } else if (value == 0) { |
| 3925 | return reg_types_.Zero(); |
| 3926 | } else if (value == 1) { |
| 3927 | return reg_types_.One(); |
| 3928 | } else if (value < 128) { |
| 3929 | return reg_types_.PosByteConstant(); |
| 3930 | } else if (value < 32768) { |
| 3931 | return reg_types_.PosShortConstant(); |
| 3932 | } else if (value < 65536) { |
| 3933 | return reg_types_.CharConstant(); |
| 3934 | } else { |
| 3935 | return reg_types_.IntConstant(); |
| 3936 | } |
| 3937 | } |
| 3938 | } |
| 3939 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 3940 | void MethodVerifier::Init() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 3941 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3942 | } |
| 3943 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 3944 | void MethodVerifier::Shutdown() { |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 3945 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 3946 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 3947 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 3948 | void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) { |
| 3949 | reg_types_.VisitRoots(callback, arg); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3950 | } |
| 3951 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3952 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3953 | } // namespace art |