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 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 17 | #include "method_verifier.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" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 23 | #include "base/stringpiece.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 24 | #include "class_linker.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" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 43 | #include "verifier/dex_gc_map.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 46 | namespace verifier { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 47 | |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 48 | static const bool gDebugVerify = false; |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 49 | // TODO: Add a constant to method_verifier to turn on verbose logging? |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 50 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 51 | void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 52 | uint32_t insns_size, uint16_t registers_size, |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 53 | MethodVerifier* verifier) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 54 | DCHECK_GT(insns_size, 0U); |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 55 | register_lines_.reset(new RegisterLine*[insns_size]()); |
| 56 | size_ = insns_size; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 57 | for (uint32_t i = 0; i < insns_size; i++) { |
| 58 | bool interesting = false; |
| 59 | switch (mode) { |
| 60 | case kTrackRegsAll: |
| 61 | interesting = flags[i].IsOpcode(); |
| 62 | break; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 63 | case kTrackCompilerInterestPoints: |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 64 | interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 65 | break; |
| 66 | case kTrackRegsBranches: |
| 67 | interesting = flags[i].IsBranchTarget(); |
| 68 | break; |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | if (interesting) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 73 | register_lines_[i] = RegisterLine::Create(registers_size, verifier); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | PcToRegisterLineTable::~PcToRegisterLineTable() { |
| 79 | for (size_t i = 0; i < size_; i++) { |
| 80 | delete register_lines_[i]; |
| 81 | if (kIsDebugBuild) { |
| 82 | register_lines_[i] = nullptr; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 88 | bool allow_soft_failures, |
| 89 | std::string* error) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 90 | if (klass->IsVerified()) { |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 91 | return kNoFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 92 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 94 | if (super == NULL && strcmp("Ljava/lang/Object;", ClassHelper(klass).GetDescriptor()) != 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 95 | *error = "Verifier rejected class "; |
| 96 | *error += PrettyDescriptor(klass); |
| 97 | *error += " that has no super class"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 98 | return kHardFailure; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 99 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 100 | if (super != NULL && super->IsFinal()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 101 | *error = "Verifier rejected class "; |
| 102 | *error += PrettyDescriptor(klass); |
| 103 | *error += " that attempts to sub-class final class "; |
| 104 | *error += PrettyDescriptor(super); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 105 | return kHardFailure; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 106 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 107 | ClassHelper kh(klass); |
| 108 | const DexFile& dex_file = kh.GetDexFile(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 109 | const DexFile::ClassDef* class_def = kh.GetClassDef(); |
| 110 | if (class_def == NULL) { |
| 111 | *error = "Verifier rejected class "; |
| 112 | *error += PrettyDescriptor(klass); |
| 113 | *error += " that isn't present in dex file "; |
| 114 | *error += dex_file.GetLocation(); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 115 | return kHardFailure; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 116 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 117 | Thread* self = Thread::Current(); |
| 118 | SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache()); |
| 119 | SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader()); |
| 120 | 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] | 121 | } |
| 122 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 123 | MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 124 | SirtRef<mirror::DexCache>& dex_cache, |
| 125 | SirtRef<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 126 | const DexFile::ClassDef* class_def, |
| 127 | bool allow_soft_failures, |
| 128 | std::string* error) { |
| 129 | DCHECK(class_def != nullptr); |
| 130 | const byte* class_data = dex_file->GetClassData(*class_def); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 131 | if (class_data == NULL) { |
| 132 | // empty class, probably a marker interface |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 133 | return kNoFailure; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 134 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 135 | ClassDataItemIterator it(*dex_file, class_data); |
| 136 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 137 | it.Next(); |
| 138 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 139 | size_t error_count = 0; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 140 | bool hard_fail = false; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 141 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 142 | int64_t previous_direct_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 143 | while (it.HasNextDirectMethod()) { |
| 144 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 145 | if (method_idx == previous_direct_method_idx) { |
| 146 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 147 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 148 | it.Next(); |
| 149 | continue; |
| 150 | } |
| 151 | previous_direct_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 152 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 153 | mirror::ArtMethod* method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 154 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 155 | if (method == NULL) { |
| 156 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 157 | // We couldn't resolve the method, but continue regardless. |
| 158 | Thread::Current()->ClearException(); |
| 159 | } |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 160 | MethodVerifier::FailureKind result = VerifyMethod(method_idx, |
| 161 | dex_file, |
| 162 | dex_cache, |
| 163 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 164 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 165 | it.GetMethodCodeItem(), |
| 166 | method, |
| 167 | it.GetMemberAccessFlags(), |
| 168 | allow_soft_failures); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 169 | if (result != kNoFailure) { |
| 170 | if (result == kHardFailure) { |
| 171 | hard_fail = true; |
| 172 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 173 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 174 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 175 | *error = "Verifier rejected class "; |
| 176 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 177 | *error += " due to bad method "; |
| 178 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 179 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 180 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 181 | } |
| 182 | it.Next(); |
| 183 | } |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 184 | int64_t previous_virtual_method_idx = -1; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 185 | while (it.HasNextVirtualMethod()) { |
| 186 | uint32_t method_idx = it.GetMemberIndex(); |
jeffhao | 9b0b188 | 2012-10-01 16:51:22 -0700 | [diff] [blame] | 187 | if (method_idx == previous_virtual_method_idx) { |
| 188 | // smali can create dex files with two encoded_methods sharing the same method_idx |
| 189 | // http://code.google.com/p/smali/issues/detail?id=119 |
| 190 | it.Next(); |
| 191 | continue; |
| 192 | } |
| 193 | previous_virtual_method_idx = method_idx; |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 194 | InvokeType type = it.GetMethodInvokeType(*class_def); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 195 | mirror::ArtMethod* method = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 196 | linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 197 | if (method == NULL) { |
| 198 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 199 | // We couldn't resolve the method, but continue regardless. |
| 200 | Thread::Current()->ClearException(); |
| 201 | } |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 202 | MethodVerifier::FailureKind result = VerifyMethod(method_idx, |
| 203 | dex_file, |
| 204 | dex_cache, |
| 205 | class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 206 | class_def, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 207 | it.GetMethodCodeItem(), |
| 208 | method, |
| 209 | it.GetMemberAccessFlags(), |
| 210 | allow_soft_failures); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 211 | if (result != kNoFailure) { |
| 212 | if (result == kHardFailure) { |
| 213 | hard_fail = true; |
| 214 | if (error_count > 0) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 215 | *error += "\n"; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 216 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 217 | *error = "Verifier rejected class "; |
| 218 | *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)); |
| 219 | *error += " due to bad method "; |
| 220 | *error += PrettyMethod(method_idx, *dex_file); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 221 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 222 | ++error_count; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 223 | } |
| 224 | it.Next(); |
| 225 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 226 | if (error_count == 0) { |
| 227 | return kNoFailure; |
| 228 | } else { |
| 229 | return hard_fail ? kHardFailure : kSoftFailure; |
| 230 | } |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 233 | MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx, |
| 234 | const DexFile* dex_file, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 235 | SirtRef<mirror::DexCache>& dex_cache, |
| 236 | SirtRef<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 237 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 238 | const DexFile::CodeItem* code_item, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 239 | mirror::ArtMethod* method, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 240 | uint32_t method_access_flags, |
| 241 | bool allow_soft_failures) { |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 242 | MethodVerifier::FailureKind result = kNoFailure; |
| 243 | uint64_t start_ns = NanoTime(); |
| 244 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 245 | MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item, |
| 246 | method_idx, method, method_access_flags, true, allow_soft_failures); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 247 | if (verifier_.Verify()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 248 | // Verification completed, however failures may be pending that didn't cause the verification |
| 249 | // to hard fail. |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 250 | CHECK(!verifier_.have_pending_hard_failure_); |
| 251 | if (verifier_.failures_.size() != 0) { |
| 252 | if (VLOG_IS_ON(verifier)) { |
| 253 | verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in " |
| 254 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
| 255 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 256 | result = kSoftFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 257 | } |
| 258 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 259 | // Bad method data. |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 260 | CHECK_NE(verifier_.failures_.size(), 0U); |
| 261 | CHECK(verifier_.have_pending_hard_failure_); |
| 262 | verifier_.DumpFailures(LOG(INFO) << "Verification error in " |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 263 | << PrettyMethod(method_idx, *dex_file) << "\n"); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 264 | if (gDebugVerify) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 265 | std::cout << "\n" << verifier_.info_messages_.str(); |
| 266 | verifier_.Dump(std::cout); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 267 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 268 | result = kHardFailure; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 269 | } |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 270 | uint64_t duration_ns = NanoTime() - start_ns; |
| 271 | if (duration_ns > MsToNs(100)) { |
| 272 | LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file) |
| 273 | << " took " << PrettyDuration(duration_ns); |
| 274 | } |
| 275 | return result; |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 278 | void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 279 | const DexFile* dex_file, |
| 280 | SirtRef<mirror::DexCache>& dex_cache, |
| 281 | SirtRef<mirror::ClassLoader>& class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 282 | const DexFile::ClassDef* class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 283 | const DexFile::CodeItem* code_item, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 284 | mirror::ArtMethod* method, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 285 | uint32_t method_access_flags) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 286 | MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item, |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 287 | dex_method_idx, method, method_access_flags, true, true); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 288 | verifier.Verify(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 289 | verifier.DumpFailures(os); |
| 290 | os << verifier.info_messages_.str(); |
| 291 | verifier.Dump(os); |
| 292 | } |
| 293 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 294 | MethodVerifier::MethodVerifier(const DexFile* dex_file, SirtRef<mirror::DexCache>* dex_cache, |
| 295 | SirtRef<mirror::ClassLoader>* class_loader, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 296 | const DexFile::ClassDef* class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 297 | const DexFile::CodeItem* code_item, uint32_t dex_method_idx, |
| 298 | mirror::ArtMethod* method, uint32_t method_access_flags, |
| 299 | bool can_load_classes, bool allow_soft_failures) |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 300 | : reg_types_(can_load_classes), |
| 301 | work_insn_idx_(-1), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 302 | dex_method_idx_(dex_method_idx), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 303 | mirror_method_(method), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 304 | method_access_flags_(method_access_flags), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 305 | return_type_(nullptr), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 306 | dex_file_(dex_file), |
| 307 | dex_cache_(dex_cache), |
| 308 | class_loader_(class_loader), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 309 | class_def_(class_def), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 310 | code_item_(code_item), |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 311 | declaring_class_(NULL), |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 312 | interesting_dex_pc_(-1), |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 313 | monitor_enter_dex_pcs_(nullptr), |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 314 | have_pending_hard_failure_(false), |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 315 | have_pending_runtime_throw_failure_(false), |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 316 | new_instance_count_(0), |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 317 | monitor_enter_count_(0), |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 318 | can_load_classes_(can_load_classes), |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 319 | allow_soft_failures_(allow_soft_failures), |
| 320 | has_check_casts_(false), |
| 321 | has_virtual_or_interface_invokes_(false) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 322 | DCHECK(class_def != nullptr); |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 325 | MethodVerifier::~MethodVerifier() { |
| 326 | STLDeleteElements(&failure_messages_); |
| 327 | } |
| 328 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 329 | void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 330 | std::vector<uint32_t>& monitor_enter_dex_pcs) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 331 | MethodHelper mh(m); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 332 | Thread* self = Thread::Current(); |
| 333 | SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache()); |
| 334 | SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader()); |
| 335 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
| 336 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false, |
| 337 | true); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 338 | verifier.interesting_dex_pc_ = dex_pc; |
| 339 | verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs; |
| 340 | verifier.FindLocksAtDexPc(); |
| 341 | } |
| 342 | |
| 343 | void MethodVerifier::FindLocksAtDexPc() { |
| 344 | CHECK(monitor_enter_dex_pcs_ != NULL); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 345 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 346 | |
| 347 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 348 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 349 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 350 | // got what we wanted. |
| 351 | Verify(); |
| 352 | } |
| 353 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 354 | mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 355 | uint32_t dex_pc) { |
| 356 | MethodHelper mh(m); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 357 | Thread* self = Thread::Current(); |
| 358 | SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache()); |
| 359 | SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader()); |
| 360 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
| 361 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false, |
| 362 | true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 363 | return verifier.FindAccessedFieldAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 366 | mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 367 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 368 | |
| 369 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 370 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 371 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 372 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 373 | bool success = Verify(); |
| 374 | if (!success) { |
| 375 | return NULL; |
| 376 | } |
| 377 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
| 378 | if (register_line == NULL) { |
| 379 | return NULL; |
| 380 | } |
| 381 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 382 | return GetQuickFieldAccess(inst, register_line); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 385 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 386 | uint32_t dex_pc) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 387 | MethodHelper mh(m); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 388 | Thread* self = Thread::Current(); |
| 389 | SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache()); |
| 390 | SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader()); |
| 391 | MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(), |
| 392 | mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false, |
| 393 | true); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 394 | return verifier.FindInvokedMethodAtDexPc(dex_pc); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 397 | mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 398 | CHECK(code_item_ != NULL); // This only makes sense for methods with code. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 399 | |
| 400 | // Strictly speaking, we ought to be able to get away with doing a subset of the full method |
| 401 | // verification. In practice, the phase we want relies on data structures set up by all the |
| 402 | // earlier passes, so we just run the full method verification and bail out early when we've |
| 403 | // got what we wanted. |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 404 | bool success = Verify(); |
| 405 | if (!success) { |
| 406 | return NULL; |
| 407 | } |
| 408 | RegisterLine* register_line = reg_table_.GetLine(dex_pc); |
| 409 | if (register_line == NULL) { |
| 410 | return NULL; |
| 411 | } |
| 412 | const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc); |
| 413 | const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 414 | return GetQuickInvokedMethod(inst, register_line, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 417 | bool MethodVerifier::Verify() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 418 | // If there aren't any instructions, make sure that's expected, then exit successfully. |
| 419 | if (code_item_ == NULL) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 420 | if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 421 | 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] | 422 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 423 | } else { |
| 424 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 425 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 426 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 427 | // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers. |
| 428 | if (code_item_->ins_size_ > code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 429 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_ |
| 430 | << " regs=" << code_item_->registers_size_; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 431 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 432 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 433 | // Allocate and initialize an array to hold instruction data. |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 434 | insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 435 | // Run through the instructions and see if the width checks out. |
| 436 | bool result = ComputeWidthsAndCountOps(); |
| 437 | // Flag instructions guarded by a "try" block and check exception handlers. |
| 438 | result = result && ScanTryCatchBlocks(); |
| 439 | // Perform static instruction verification. |
| 440 | result = result && VerifyInstructions(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 441 | // Perform code-flow analysis and return. |
| 442 | return result && VerifyCodeFlow(); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 445 | std::ostream& MethodVerifier::Fail(VerifyError error) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 446 | switch (error) { |
| 447 | case VERIFY_ERROR_NO_CLASS: |
| 448 | case VERIFY_ERROR_NO_FIELD: |
| 449 | case VERIFY_ERROR_NO_METHOD: |
| 450 | case VERIFY_ERROR_ACCESS_CLASS: |
| 451 | case VERIFY_ERROR_ACCESS_FIELD: |
| 452 | case VERIFY_ERROR_ACCESS_METHOD: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 453 | case VERIFY_ERROR_INSTANTIATION: |
| 454 | case VERIFY_ERROR_CLASS_CHANGE: |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 455 | if (Runtime::Current()->IsCompiler() || !can_load_classes_) { |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 456 | // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx, |
| 457 | // class change and instantiation errors into soft verification errors so that we re-verify |
| 458 | // at runtime. We may fail to find or to agree on access because of not yet available class |
| 459 | // loaders, or class loaders that will differ at runtime. In these cases, we don't want to |
| 460 | // affect the soundness of the code being compiled. Instead, the generated code runs "slow |
| 461 | // paths" that dynamically perform the verification and cause the behavior to be that akin |
| 462 | // to an interpreter. |
| 463 | error = VERIFY_ERROR_BAD_CLASS_SOFT; |
| 464 | } else { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 465 | // If we fail again at runtime, mark that this instruction would throw and force this |
| 466 | // method to be executed using the interpreter with checks. |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 467 | have_pending_runtime_throw_failure_ = true; |
| 468 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 469 | break; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 470 | // Indication that verification should be retried at runtime. |
| 471 | case VERIFY_ERROR_BAD_CLASS_SOFT: |
Jeff Hao | ee98895 | 2013-04-16 14:23:47 -0700 | [diff] [blame] | 472 | if (!allow_soft_failures_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 473 | have_pending_hard_failure_ = true; |
| 474 | } |
| 475 | break; |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 476 | // Hard verification failures at compile time will still fail at runtime, so the class is |
| 477 | // marked as rejected to prevent it from being compiled. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 478 | case VERIFY_ERROR_BAD_CLASS_HARD: { |
| 479 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 480 | ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_)); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 481 | AddRejectedClass(ref); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 482 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 483 | have_pending_hard_failure_ = true; |
| 484 | break; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 487 | failures_.push_back(error); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 488 | 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] | 489 | work_insn_idx_)); |
| 490 | std::ostringstream* failure_message = new std::ostringstream(location); |
| 491 | failure_messages_.push_back(failure_message); |
| 492 | return *failure_message; |
| 493 | } |
| 494 | |
| 495 | void MethodVerifier::PrependToLastFailMessage(std::string prepend) { |
| 496 | size_t failure_num = failure_messages_.size(); |
| 497 | DCHECK_NE(failure_num, 0U); |
| 498 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 499 | prepend += last_fail_message->str(); |
| 500 | failure_messages_[failure_num - 1] = new std::ostringstream(prepend); |
| 501 | delete last_fail_message; |
| 502 | } |
| 503 | |
| 504 | void MethodVerifier::AppendToLastFailMessage(std::string append) { |
| 505 | size_t failure_num = failure_messages_.size(); |
| 506 | DCHECK_NE(failure_num, 0U); |
| 507 | std::ostringstream* last_fail_message = failure_messages_[failure_num - 1]; |
| 508 | (*last_fail_message) << append; |
Ian Rogers | 47a0588 | 2012-02-03 12:23:33 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 511 | bool MethodVerifier::ComputeWidthsAndCountOps() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 512 | const uint16_t* insns = code_item_->insns_; |
| 513 | size_t insns_size = code_item_->insns_size_in_code_units_; |
| 514 | const Instruction* inst = Instruction::At(insns); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 515 | size_t new_instance_count = 0; |
| 516 | size_t monitor_enter_count = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 517 | size_t dex_pc = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 518 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 519 | while (dex_pc < insns_size) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 520 | Instruction::Code opcode = inst->Opcode(); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 521 | switch (opcode) { |
| 522 | case Instruction::APUT_OBJECT: |
| 523 | case Instruction::CHECK_CAST: |
| 524 | has_check_casts_ = true; |
| 525 | break; |
| 526 | case Instruction::INVOKE_VIRTUAL: |
| 527 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 528 | case Instruction::INVOKE_INTERFACE: |
| 529 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 530 | has_virtual_or_interface_invokes_ = true; |
| 531 | break; |
| 532 | case Instruction::MONITOR_ENTER: |
| 533 | monitor_enter_count++; |
| 534 | break; |
| 535 | case Instruction::NEW_INSTANCE: |
| 536 | new_instance_count++; |
| 537 | break; |
| 538 | default: |
| 539 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 540 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 541 | size_t inst_size = inst->SizeInCodeUnits(); |
| 542 | insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size); |
| 543 | dex_pc += inst_size; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 544 | inst = inst->Next(); |
| 545 | } |
| 546 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 547 | if (dex_pc != insns_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 548 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected (" |
| 549 | << dex_pc << " vs. " << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 550 | return false; |
| 551 | } |
| 552 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 553 | new_instance_count_ = new_instance_count; |
| 554 | monitor_enter_count_ = monitor_enter_count; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 555 | return true; |
| 556 | } |
| 557 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 558 | bool MethodVerifier::ScanTryCatchBlocks() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 559 | uint32_t tries_size = code_item_->tries_size_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 560 | if (tries_size == 0) { |
| 561 | return true; |
| 562 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 563 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 564 | const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 565 | |
| 566 | for (uint32_t idx = 0; idx < tries_size; idx++) { |
| 567 | const DexFile::TryItem* try_item = &tries[idx]; |
| 568 | uint32_t start = try_item->start_addr_; |
| 569 | uint32_t end = start + try_item->insn_count_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 570 | if ((start >= end) || (start >= insns_size) || (end > insns_size)) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 571 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start |
| 572 | << " endAddr=" << end << " (size=" << insns_size << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 573 | return false; |
| 574 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 575 | if (!insn_flags_[start].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 576 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 577 | << "'try' block starts inside an instruction (" << start << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 580 | for (uint32_t dex_pc = start; dex_pc < end; |
| 581 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
| 582 | insn_flags_[dex_pc].SetInTry(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 585 | // Iterate over each of the handlers to verify target addresses. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 586 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 587 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 588 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 589 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 590 | CatchHandlerIterator iterator(handlers_ptr); |
| 591 | for (; iterator.HasNext(); iterator.Next()) { |
| 592 | uint32_t dex_pc= iterator.GetHandlerAddress(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 593 | if (!insn_flags_[dex_pc].IsOpcode()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 594 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 595 | << "exception handler starts at bad address (" << dex_pc << ")"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 596 | return false; |
| 597 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 598 | insn_flags_[dex_pc].SetBranchTarget(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 599 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 600 | // unresolved exception types will be ignored by exception delivery |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 601 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 602 | mirror::Class* exception_type = linker->ResolveType(*dex_file_, |
| 603 | iterator.GetHandlerTypeIndex(), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 604 | *dex_cache_, *class_loader_); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 605 | if (exception_type == NULL) { |
| 606 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 607 | Thread::Current()->ClearException(); |
| 608 | } |
| 609 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 610 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 611 | handlers_ptr = iterator.EndDataPointer(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 612 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 613 | return true; |
| 614 | } |
| 615 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 616 | bool MethodVerifier::VerifyInstructions() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 617 | const Instruction* inst = Instruction::At(code_item_->insns_); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 618 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 619 | /* 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] | 620 | insn_flags_[0].SetBranchTarget(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 621 | insn_flags_[0].SetCompileTimeInfoPoint(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 622 | |
| 623 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 624 | for (uint32_t dex_pc = 0; dex_pc < insns_size;) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 625 | if (!VerifyInstruction(inst, dex_pc)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 626 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | /* Flag instructions that are garbage collection points */ |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 630 | // All invoke points are marked as "Throw" points already. |
| 631 | // 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] | 632 | if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 633 | insn_flags_[dex_pc].SetCompileTimeInfoPoint(); |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 634 | } else if (inst->IsReturn()) { |
| 635 | insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 636 | } |
| 637 | dex_pc += inst->SizeInCodeUnits(); |
| 638 | inst = inst->Next(); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 643 | bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 644 | DecodedInstruction dec_insn(inst); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 645 | bool result = true; |
| 646 | switch (inst->GetVerifyTypeArgumentA()) { |
| 647 | case Instruction::kVerifyRegA: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 648 | result = result && CheckRegisterIndex(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 649 | break; |
| 650 | case Instruction::kVerifyRegAWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 651 | result = result && CheckWideRegisterIndex(dec_insn.vA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | switch (inst->GetVerifyTypeArgumentB()) { |
| 655 | case Instruction::kVerifyRegB: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 656 | result = result && CheckRegisterIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 657 | break; |
| 658 | case Instruction::kVerifyRegBField: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 659 | result = result && CheckFieldIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 660 | break; |
| 661 | case Instruction::kVerifyRegBMethod: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 662 | result = result && CheckMethodIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 663 | break; |
| 664 | case Instruction::kVerifyRegBNewInstance: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 665 | result = result && CheckNewInstance(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 666 | break; |
| 667 | case Instruction::kVerifyRegBString: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 668 | result = result && CheckStringIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 669 | break; |
| 670 | case Instruction::kVerifyRegBType: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 671 | result = result && CheckTypeIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 672 | break; |
| 673 | case Instruction::kVerifyRegBWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 674 | result = result && CheckWideRegisterIndex(dec_insn.vB); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | switch (inst->GetVerifyTypeArgumentC()) { |
| 678 | case Instruction::kVerifyRegC: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 679 | result = result && CheckRegisterIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 680 | break; |
| 681 | case Instruction::kVerifyRegCField: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 682 | result = result && CheckFieldIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 683 | break; |
| 684 | case Instruction::kVerifyRegCNewArray: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 685 | result = result && CheckNewArray(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 686 | break; |
| 687 | case Instruction::kVerifyRegCType: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 688 | result = result && CheckTypeIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 689 | break; |
| 690 | case Instruction::kVerifyRegCWide: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 691 | result = result && CheckWideRegisterIndex(dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | switch (inst->GetVerifyExtraFlags()) { |
| 695 | case Instruction::kVerifyArrayData: |
| 696 | result = result && CheckArrayData(code_offset); |
| 697 | break; |
| 698 | case Instruction::kVerifyBranchTarget: |
| 699 | result = result && CheckBranchTarget(code_offset); |
| 700 | break; |
| 701 | case Instruction::kVerifySwitchTargets: |
| 702 | result = result && CheckSwitchTargets(code_offset); |
| 703 | break; |
| 704 | case Instruction::kVerifyVarArg: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 705 | result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 706 | break; |
| 707 | case Instruction::kVerifyVarArgRange: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 708 | result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 709 | break; |
| 710 | case Instruction::kVerifyError: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 711 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 712 | result = false; |
| 713 | break; |
| 714 | } |
| 715 | return result; |
| 716 | } |
| 717 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 718 | bool MethodVerifier::CheckRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 719 | if (idx >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 720 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= " |
| 721 | << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 722 | return false; |
| 723 | } |
| 724 | return true; |
| 725 | } |
| 726 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 727 | bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 728 | if (idx + 1 >= code_item_->registers_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 729 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx |
| 730 | << "+1 >= " << code_item_->registers_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 736 | bool MethodVerifier::CheckFieldIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 737 | if (idx >= dex_file_->GetHeader().field_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 738 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max " |
| 739 | << dex_file_->GetHeader().field_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 745 | bool MethodVerifier::CheckMethodIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 746 | if (idx >= dex_file_->GetHeader().method_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 747 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max " |
| 748 | << dex_file_->GetHeader().method_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 749 | return false; |
| 750 | } |
| 751 | return true; |
| 752 | } |
| 753 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 754 | bool MethodVerifier::CheckNewInstance(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 755 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 756 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 757 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | // 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] | 761 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 762 | if (descriptor[0] != 'L') { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 763 | 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] | 764 | return false; |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 769 | bool MethodVerifier::CheckStringIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 770 | if (idx >= dex_file_->GetHeader().string_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 771 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max " |
| 772 | << dex_file_->GetHeader().string_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 773 | return false; |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 778 | bool MethodVerifier::CheckTypeIndex(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 779 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 780 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 781 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | return true; |
| 785 | } |
| 786 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 787 | bool MethodVerifier::CheckNewArray(uint32_t idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 788 | if (idx >= dex_file_->GetHeader().type_ids_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 789 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max " |
| 790 | << dex_file_->GetHeader().type_ids_size_ << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | int bracket_count = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 794 | const char* descriptor = dex_file_->StringByTypeIdx(idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 795 | const char* cp = descriptor; |
| 796 | while (*cp++ == '[') { |
| 797 | bracket_count++; |
| 798 | } |
| 799 | if (bracket_count == 0) { |
| 800 | /* The given class must be an array type. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 801 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 802 | << "can't new-array class '" << descriptor << "' (not an array)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 803 | return false; |
| 804 | } else if (bracket_count > 255) { |
| 805 | /* It is illegal to create an array of more than 255 dimensions. */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 806 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 807 | << "can't new-array class '" << descriptor << "' (exceeds limit)"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 808 | return false; |
| 809 | } |
| 810 | return true; |
| 811 | } |
| 812 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 813 | bool MethodVerifier::CheckArrayData(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 814 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 815 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 816 | const uint16_t* array_data; |
| 817 | int32_t array_data_offset; |
| 818 | |
| 819 | DCHECK_LT(cur_offset, insn_count); |
| 820 | /* make sure the start of the array data table is in range */ |
| 821 | array_data_offset = insns[1] | (((int32_t) insns[2]) << 16); |
| 822 | if ((int32_t) cur_offset + array_data_offset < 0 || |
| 823 | cur_offset + array_data_offset + 2 >= insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 824 | 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] | 825 | << ", data offset " << array_data_offset |
| 826 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 827 | return false; |
| 828 | } |
| 829 | /* offset to array data table is a relative branch-style offset */ |
| 830 | array_data = insns + array_data_offset; |
| 831 | /* make sure the table is 32-bit aligned */ |
| 832 | if ((((uint32_t) array_data) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 833 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset |
| 834 | << ", data offset " << array_data_offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 835 | return false; |
| 836 | } |
| 837 | uint32_t value_width = array_data[1]; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 838 | uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 839 | uint32_t table_size = 4 + (value_width * value_count + 1) / 2; |
| 840 | /* make sure the end of the switch is in range */ |
| 841 | if (cur_offset + array_data_offset + table_size > insn_count) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 842 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset |
| 843 | << ", data offset " << array_data_offset << ", end " |
| 844 | << cur_offset + array_data_offset + table_size |
| 845 | << ", count " << insn_count; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 846 | return false; |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 851 | bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 852 | int32_t offset; |
| 853 | bool isConditional, selfOkay; |
| 854 | if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) { |
| 855 | return false; |
| 856 | } |
| 857 | if (!selfOkay && offset == 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 858 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" |
| 859 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 860 | return false; |
| 861 | } |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 862 | // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime |
| 863 | // 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] | 864 | 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] | 865 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " |
| 866 | << reinterpret_cast<void*>(cur_offset) << " +" << offset; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
| 870 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 871 | if (abs_offset < 0 || |
| 872 | (uint32_t) abs_offset >= insn_count || |
| 873 | !insn_flags_[abs_offset].IsOpcode()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 874 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> " |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 875 | << reinterpret_cast<void*>(abs_offset) << ") at " |
| 876 | << reinterpret_cast<void*>(cur_offset); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | insn_flags_[abs_offset].SetBranchTarget(); |
| 880 | return true; |
| 881 | } |
| 882 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 883 | bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 884 | bool* selfOkay) { |
| 885 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
| 886 | *pConditional = false; |
| 887 | *selfOkay = false; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 888 | switch (*insns & 0xff) { |
| 889 | case Instruction::GOTO: |
| 890 | *pOffset = ((int16_t) *insns) >> 8; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 891 | break; |
| 892 | case Instruction::GOTO_32: |
| 893 | *pOffset = insns[1] | (((uint32_t) insns[2]) << 16); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 894 | *selfOkay = true; |
| 895 | break; |
| 896 | case Instruction::GOTO_16: |
| 897 | *pOffset = (int16_t) insns[1]; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 898 | break; |
| 899 | case Instruction::IF_EQ: |
| 900 | case Instruction::IF_NE: |
| 901 | case Instruction::IF_LT: |
| 902 | case Instruction::IF_GE: |
| 903 | case Instruction::IF_GT: |
| 904 | case Instruction::IF_LE: |
| 905 | case Instruction::IF_EQZ: |
| 906 | case Instruction::IF_NEZ: |
| 907 | case Instruction::IF_LTZ: |
| 908 | case Instruction::IF_GEZ: |
| 909 | case Instruction::IF_GTZ: |
| 910 | case Instruction::IF_LEZ: |
| 911 | *pOffset = (int16_t) insns[1]; |
| 912 | *pConditional = true; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 913 | break; |
| 914 | default: |
| 915 | return false; |
| 916 | break; |
| 917 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 918 | return true; |
| 919 | } |
| 920 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 921 | bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 922 | const uint32_t insn_count = code_item_->insns_size_in_code_units_; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 923 | DCHECK_LT(cur_offset, insn_count); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 924 | const uint16_t* insns = code_item_->insns_ + cur_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 925 | /* make sure the start of the switch is in range */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 926 | int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16; |
| 927 | 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] | 928 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 929 | << ", switch offset " << switch_offset |
| 930 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 931 | return false; |
| 932 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 933 | /* offset to switch table is a relative branch-style offset */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 934 | const uint16_t* switch_insns = insns + switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 935 | /* make sure the table is 32-bit aligned */ |
| 936 | if ((((uint32_t) switch_insns) & 0x03) != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 937 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset |
| 938 | << ", switch offset " << switch_offset; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 939 | return false; |
| 940 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 941 | uint32_t switch_count = switch_insns[1]; |
| 942 | int32_t keys_offset, targets_offset; |
| 943 | uint16_t expected_signature; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 944 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 945 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 946 | targets_offset = 4; |
| 947 | keys_offset = -1; |
| 948 | expected_signature = Instruction::kPackedSwitchSignature; |
| 949 | } else { |
| 950 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 951 | keys_offset = 2; |
| 952 | targets_offset = 2 + 2 * switch_count; |
| 953 | expected_signature = Instruction::kSparseSwitchSignature; |
| 954 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 955 | uint32_t table_size = targets_offset + switch_count * 2; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 956 | if (switch_insns[0] != expected_signature) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 957 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
| 958 | << StringPrintf("wrong signature for switch table (%x, wanted %x)", |
| 959 | switch_insns[0], expected_signature); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 960 | return false; |
| 961 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 962 | /* make sure the end of the switch is in range */ |
| 963 | if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 964 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset |
| 965 | << ", switch offset " << switch_offset |
| 966 | << ", end " << (cur_offset + switch_offset + table_size) |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 967 | << ", count " << insn_count; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 968 | return false; |
| 969 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 970 | /* for a sparse switch, verify the keys are in ascending order */ |
| 971 | if (keys_offset > 0 && switch_count > 1) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 972 | int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16); |
| 973 | for (uint32_t targ = 1; targ < switch_count; targ++) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 974 | int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] | |
| 975 | (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16); |
| 976 | if (key <= last_key) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 977 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key |
| 978 | << ", this=" << key; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 979 | return false; |
| 980 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 981 | last_key = key; |
| 982 | } |
| 983 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 984 | /* verify each switch target */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 985 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
| 986 | int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] | |
| 987 | (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16); |
| 988 | int32_t abs_offset = cur_offset + offset; |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 989 | if (abs_offset < 0 || |
| 990 | abs_offset >= (int32_t) insn_count || |
| 991 | !insn_flags_[abs_offset].IsOpcode()) { |
| 992 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset |
| 993 | << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at " |
| 994 | << reinterpret_cast<void*>(cur_offset) |
| 995 | << "[" << targ << "]"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 996 | return false; |
| 997 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 998 | insn_flags_[abs_offset].SetBranchTarget(); |
| 999 | } |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1003 | bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1004 | if (vA > 5) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1005 | 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] | 1006 | return false; |
| 1007 | } |
| 1008 | uint16_t registers_size = code_item_->registers_size_; |
| 1009 | for (uint32_t idx = 0; idx < vA; idx++) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1010 | if (arg[idx] >= registers_size) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1011 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx] |
| 1012 | << ") in non-range invoke (>= " << registers_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1013 | return false; |
| 1014 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1020 | bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1021 | uint16_t registers_size = code_item_->registers_size_; |
| 1022 | // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of |
| 1023 | // integer overflow when adding them here. |
| 1024 | if (vA + vC > registers_size) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1025 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC |
| 1026 | << " in range invoke (> " << registers_size << ")"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1027 | return false; |
| 1028 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1032 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1033 | uint16_t registers_size = code_item_->registers_size_; |
| 1034 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1035 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1036 | if (registers_size * insns_size > 4*1024*1024) { |
buzbee | 4922ef9 | 2012-02-24 14:32:20 -0800 | [diff] [blame] | 1037 | LOG(WARNING) << "warning: method is huge (regs=" << registers_size |
| 1038 | << " insns_size=" << insns_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1039 | } |
| 1040 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1041 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1042 | insn_flags_.get(), |
| 1043 | insns_size, |
| 1044 | registers_size, |
| 1045 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1046 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1047 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1048 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1049 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1050 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1051 | /* Initialize register types of method arguments. */ |
| 1052 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1053 | DCHECK_NE(failures_.size(), 0U); |
| 1054 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1055 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1056 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
| 1059 | /* Perform code flow verification. */ |
| 1060 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1061 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1062 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1065 | // Compute information for compiler. |
| 1066 | if (Runtime::Current()->IsCompiler()) { |
| 1067 | MethodReference ref(dex_file_, dex_method_idx_); |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 1068 | bool compile = IsCandidateForCompilation(ref, method_access_flags_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1069 | if (compile) { |
| 1070 | /* Generate a register map and add it to the method. */ |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 1071 | const std::vector<uint8_t>* dex_gc_map = GenerateLengthPrefixedGcMap(); |
| 1072 | if (dex_gc_map == NULL) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1073 | DCHECK_NE(failures_.size(), 0U); |
| 1074 | return false; // Not a real failure, but a failure to encode |
| 1075 | } |
| 1076 | if (kIsDebugBuild) { |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 1077 | VerifyLengthPrefixedGcMap(*dex_gc_map); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1078 | } |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 1079 | verifier::MethodVerifier::SetDexGcMap(ref, dex_gc_map); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1080 | } |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1081 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1082 | if (has_check_casts_) { |
| 1083 | MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet(); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1084 | if (method_to_safe_casts != NULL) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1085 | SetSafeCastMap(ref, method_to_safe_casts); |
| 1086 | } |
| 1087 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1088 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1089 | if (has_virtual_or_interface_invokes_) { |
| 1090 | MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap(); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1091 | if (pc_to_concrete_method != NULL) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1092 | SetDevirtMap(ref, pc_to_concrete_method); |
| 1093 | } |
| 1094 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1095 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1096 | return true; |
| 1097 | } |
| 1098 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1099 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1100 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Jeff Hao | 4137f48 | 2013-11-22 11:44:57 -0800 | [diff] [blame] | 1101 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1102 | os << failure_messages_[i]->str() << "\n"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1103 | } |
| 1104 | return os; |
| 1105 | } |
| 1106 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1107 | extern "C" void MethodVerifierGdbDump(MethodVerifier* v) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1108 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1109 | v->Dump(std::cerr); |
| 1110 | } |
| 1111 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1112 | void MethodVerifier::Dump(std::ostream& os) { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1113 | if (code_item_ == NULL) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1114 | os << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1115 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1116 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1117 | { |
| 1118 | os << "Register Types:\n"; |
| 1119 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1120 | std::ostream indent_os(&indent_filter); |
| 1121 | reg_types_.Dump(indent_os); |
| 1122 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1123 | os << "Dumping instructions and register lines:\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1124 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1125 | std::ostream indent_os(&indent_filter); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1126 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1127 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
| 1128 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1129 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1130 | if (reg_line != NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1131 | indent_os << reg_line->Dump() << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1132 | } |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1133 | indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1134 | const bool kDumpHexOfInstruction = false; |
| 1135 | if (kDumpHexOfInstruction) { |
| 1136 | indent_os << inst->DumpHex(5) << " "; |
| 1137 | } |
| 1138 | indent_os << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1139 | inst = inst->Next(); |
| 1140 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1143 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1144 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1145 | case 'I': |
| 1146 | case 'C': |
| 1147 | case 'S': |
| 1148 | case 'B': |
| 1149 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1150 | case 'F': |
| 1151 | case 'D': |
| 1152 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1153 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1154 | default: |
| 1155 | return false; |
| 1156 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1159 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1160 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1161 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1162 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1163 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1164 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1165 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1166 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1167 | if (!IsStatic()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1168 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1169 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1170 | // called. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1171 | const RegType& declaring_class = GetDeclaringClass(); |
| 1172 | if (IsConstructor() && !declaring_class.IsJavaLangObject()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1173 | reg_line->SetRegisterType(arg_start + cur_arg, |
| 1174 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1175 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1176 | reg_line->SetRegisterType(arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1177 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1178 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1181 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1182 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1183 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1184 | |
| 1185 | for (; iterator.HasNext(); iterator.Next()) { |
| 1186 | const char* descriptor = iterator.GetDescriptor(); |
| 1187 | if (descriptor == NULL) { |
| 1188 | LOG(FATAL) << "Null descriptor"; |
| 1189 | } |
| 1190 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1191 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1192 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | switch (descriptor[0]) { |
| 1196 | case 'L': |
| 1197 | case '[': |
| 1198 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1199 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1200 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1201 | // can return without doing anything or call virtual methods). |
| 1202 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1203 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 1204 | false); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1205 | reg_line->SetRegisterType(arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1206 | } |
| 1207 | break; |
| 1208 | case 'Z': |
| 1209 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); |
| 1210 | break; |
| 1211 | case 'C': |
| 1212 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); |
| 1213 | break; |
| 1214 | case 'B': |
| 1215 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); |
| 1216 | break; |
| 1217 | case 'I': |
| 1218 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); |
| 1219 | break; |
| 1220 | case 'S': |
| 1221 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); |
| 1222 | break; |
| 1223 | case 'F': |
| 1224 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); |
| 1225 | break; |
| 1226 | case 'J': |
| 1227 | case 'D': { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1228 | const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo(); |
| 1229 | const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi(); |
| 1230 | reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1231 | cur_arg++; |
| 1232 | break; |
| 1233 | } |
| 1234 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1235 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1236 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | cur_arg++; |
| 1240 | } |
| 1241 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1242 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1243 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1244 | return false; |
| 1245 | } |
| 1246 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1247 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1248 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1249 | bool result; |
| 1250 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1251 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1252 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1253 | size_t i = 0; |
| 1254 | do { |
| 1255 | i++; |
| 1256 | } while (descriptor[i] == '['); // process leading [ |
| 1257 | if (descriptor[i] == 'L') { // object array |
| 1258 | do { |
| 1259 | i++; // find closing ; |
| 1260 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1261 | result = descriptor[i] == ';'; |
| 1262 | } else { // primitive array |
| 1263 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1264 | } |
| 1265 | } else if (descriptor[0] == 'L') { |
| 1266 | // could be more thorough here, but shouldn't be required |
| 1267 | size_t i = 0; |
| 1268 | do { |
| 1269 | i++; |
| 1270 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1271 | result = descriptor[i] == ';'; |
| 1272 | } else { |
| 1273 | result = false; |
| 1274 | } |
| 1275 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1276 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1277 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1278 | } |
| 1279 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1282 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1283 | const uint16_t* insns = code_item_->insns_; |
| 1284 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1285 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1286 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1287 | insn_flags_[0].SetChanged(); |
| 1288 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1289 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1290 | /* Continue until no instructions are marked "changed". */ |
| 1291 | while (true) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1292 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1293 | uint32_t insn_idx = start_guess; |
| 1294 | for (; insn_idx < insns_size; insn_idx++) { |
| 1295 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1296 | break; |
| 1297 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1298 | if (insn_idx == insns_size) { |
| 1299 | if (start_guess != 0) { |
| 1300 | /* try again, starting from the top */ |
| 1301 | start_guess = 0; |
| 1302 | continue; |
| 1303 | } else { |
| 1304 | /* all flags are clear */ |
| 1305 | break; |
| 1306 | } |
| 1307 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1308 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1309 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1310 | // "changed" flags, we need to load the set of registers from the table. |
| 1311 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1312 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1313 | // target. |
| 1314 | work_insn_idx_ = insn_idx; |
| 1315 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1316 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1317 | } else { |
| 1318 | #ifndef NDEBUG |
| 1319 | /* |
| 1320 | * Sanity check: retrieve the stored register line (assuming |
| 1321 | * a full table) and make sure it actually matches. |
| 1322 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1323 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| 1324 | if (register_line != NULL) { |
| 1325 | if (work_line_->CompareLine(register_line) != 0) { |
| 1326 | Dump(std::cout); |
| 1327 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1328 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1329 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
| 1330 | << " work_line=" << *work_line_ << "\n" |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1331 | << " expected=" << *register_line; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1332 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1333 | } |
| 1334 | #endif |
| 1335 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1336 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1337 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1338 | prepend += " failed to verify: "; |
| 1339 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1340 | return false; |
| 1341 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1342 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1343 | insn_flags_[insn_idx].SetVisited(); |
| 1344 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1345 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1346 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1347 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1348 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1349 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1350 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1351 | * down the line, possibly in the verifier. |
| 1352 | * |
| 1353 | * If we've substituted "always throw" instructions into the stream, |
| 1354 | * we are almost certainly going to have some dead code. |
| 1355 | */ |
| 1356 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1357 | uint32_t insn_idx = 0; |
| 1358 | for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1359 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1360 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1361 | * may or may not be preceded by a padding NOP (for alignment). |
| 1362 | */ |
| 1363 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1364 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1365 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1366 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1367 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1368 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1369 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1370 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1373 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1374 | if (dead_start < 0) |
| 1375 | dead_start = insn_idx; |
| 1376 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1377 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1378 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1379 | dead_start = -1; |
| 1380 | } |
| 1381 | } |
| 1382 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1383 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1384 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1385 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1386 | // To dump the state of the verify after a method, do something like: |
| 1387 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1388 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1389 | // LOG(INFO) << info_messages_.str(); |
| 1390 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1391 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1392 | return true; |
| 1393 | } |
| 1394 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1395 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1396 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1397 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1398 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1399 | // for a thread to be suspended). |
| 1400 | if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1401 | 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] | 1402 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1403 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1404 | } |
| 1405 | } |
| 1406 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1407 | /* |
| 1408 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1409 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1410 | * control to another statement: |
| 1411 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1412 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1413 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1414 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1415 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1416 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1417 | * throw an exception that is handled by an encompassing "try" |
| 1418 | * block. |
| 1419 | * |
| 1420 | * We can also return, in which case there is no successor instruction |
| 1421 | * from this point. |
| 1422 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1423 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1424 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1425 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1426 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1427 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1428 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1429 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1430 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1431 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1432 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1433 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
| 1434 | << *work_line_.get() << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1435 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1436 | |
| 1437 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1438 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1439 | * can throw an exception, we will copy/merge this into the "catch" |
| 1440 | * address rather than work_line, because we don't want the result |
| 1441 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1442 | * a type) to be visible to the exception handler. |
| 1443 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1444 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1445 | saved_line_->CopyFromLine(work_line_.get()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1446 | } else { |
| 1447 | #ifndef NDEBUG |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1448 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1449 | #endif |
| 1450 | } |
| 1451 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1452 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1453 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1454 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1455 | // branch target. |
| 1456 | UniquePtr<RegisterLine> branch_line; |
| 1457 | UniquePtr<RegisterLine> fallthrough_line; |
| 1458 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1459 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1460 | case Instruction::NOP: |
| 1461 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1462 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1463 | * a signature that looks like a NOP; if we see one of these in |
| 1464 | * the course of executing code then we have a problem. |
| 1465 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1466 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1467 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1468 | } |
| 1469 | break; |
| 1470 | |
| 1471 | case Instruction::MOVE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1472 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
| 1473 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1474 | case Instruction::MOVE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1475 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
| 1476 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1477 | case Instruction::MOVE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1478 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | case Instruction::MOVE_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1481 | work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x()); |
| 1482 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1483 | case Instruction::MOVE_WIDE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1484 | work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x()); |
| 1485 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1486 | case Instruction::MOVE_WIDE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1487 | work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1488 | break; |
| 1489 | case Instruction::MOVE_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1490 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
| 1491 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1492 | case Instruction::MOVE_OBJECT_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1493 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
| 1494 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1495 | case Instruction::MOVE_OBJECT_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1496 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1497 | break; |
| 1498 | |
| 1499 | /* |
| 1500 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1501 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1502 | * might want to hold the result in an actual CPU register, so the |
| 1503 | * Dalvik spec requires that these only appear immediately after an |
| 1504 | * invoke or filled-new-array. |
| 1505 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1506 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1507 | * redundant with the reset done below, but it can make the debug info |
| 1508 | * easier to read in some cases.) |
| 1509 | */ |
| 1510 | case Instruction::MOVE_RESULT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1511 | work_line_->CopyResultRegister1(inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1512 | break; |
| 1513 | case Instruction::MOVE_RESULT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1514 | work_line_->CopyResultRegister2(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1515 | break; |
| 1516 | case Instruction::MOVE_RESULT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1517 | work_line_->CopyResultRegister1(inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1520 | case Instruction::MOVE_EXCEPTION: { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1521 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1522 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1523 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1524 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1525 | const RegType& res_type = GetCaughtExceptionType(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1526 | work_line_->SetRegisterType(inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1527 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1528 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1529 | case Instruction::RETURN_VOID: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1530 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 1531 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1532 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1533 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1534 | } |
| 1535 | break; |
| 1536 | case Instruction::RETURN: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1537 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1538 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1539 | const RegType& return_type = GetMethodReturnType(); |
| 1540 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1541 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1542 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1543 | } else { |
| 1544 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1545 | // 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] | 1546 | const uint32_t vregA = inst->VRegA_11x(); |
| 1547 | const RegType& src_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1548 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1549 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1550 | return_type.IsShort() || return_type.IsChar()) && |
| 1551 | src_type.IsInteger())); |
| 1552 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1553 | bool success = |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1554 | work_line_->VerifyRegisterType(vregA, use_src ? src_type : 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-1nr 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_WIDE: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1562 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1563 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1564 | const RegType& return_type = GetMethodReturnType(); |
| 1565 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1566 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1567 | } else { |
| 1568 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1569 | const uint32_t vregA = inst->VRegA_11x(); |
| 1570 | bool success = work_line_->VerifyRegisterType(vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1571 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1572 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1573 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | break; |
| 1577 | case Instruction::RETURN_OBJECT: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1578 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1579 | const RegType& return_type = GetMethodReturnType(); |
| 1580 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1581 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1582 | } else { |
| 1583 | /* return_type is the *expected* return type, not register value */ |
| 1584 | DCHECK(!return_type.IsZero()); |
| 1585 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1586 | const uint32_t vregA = inst->VRegA_11x(); |
| 1587 | const RegType& reg_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1588 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 1589 | // instance of the "return_type" |
| 1590 | if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1591 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1592 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1593 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1594 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1595 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1596 | << "' or '" << reg_type << "'"; |
| 1597 | } else { |
| 1598 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1599 | << "', but expected from declaration '" << return_type << "'"; |
| 1600 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | break; |
| 1605 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1606 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1607 | case Instruction::CONST_4: { |
| 1608 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
| 1609 | work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1610 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1611 | } |
| 1612 | case Instruction::CONST_16: { |
| 1613 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
| 1614 | work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1615 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1616 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1617 | case Instruction::CONST: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1618 | work_line_->SetRegisterType(inst->VRegA_31i(), |
| 1619 | reg_types_.FromCat1Const(inst->VRegB_31i(), true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1620 | break; |
| 1621 | case Instruction::CONST_HIGH16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1622 | work_line_->SetRegisterType(inst->VRegA_21h(), |
| 1623 | reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1624 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1625 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1626 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1627 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1628 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1629 | 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] | 1630 | work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1631 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1632 | } |
| 1633 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1634 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1635 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1636 | 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] | 1637 | work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1638 | break; |
| 1639 | } |
| 1640 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1641 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1642 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1643 | 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] | 1644 | work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1645 | break; |
| 1646 | } |
| 1647 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1648 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1649 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1650 | 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] | 1651 | work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1652 | break; |
| 1653 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1654 | case Instruction::CONST_STRING: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1655 | work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString()); |
| 1656 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1657 | case Instruction::CONST_STRING_JUMBO: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1658 | work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1659 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1660 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1661 | // Get type from instruction if unresolved then we need an access check |
| 1662 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1663 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1664 | // 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] | 1665 | work_line_->SetRegisterType(inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1666 | res_type.IsConflict() ? res_type |
| 1667 | : reg_types_.JavaLangClass(true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1668 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1669 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1670 | case Instruction::MONITOR_ENTER: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1671 | work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1672 | break; |
| 1673 | case Instruction::MONITOR_EXIT: |
| 1674 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1675 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1676 | * 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] | 1677 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1678 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1679 | * feature that Dalvik doesn't support.) |
| 1680 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1681 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1682 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1683 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1684 | * structured locking checks are working, the former would have |
| 1685 | * failed on the -enter instruction, and the latter is impossible. |
| 1686 | * |
| 1687 | * This is fortunate, because issue 3221411 prevents us from |
| 1688 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1689 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1690 | * some catch blocks (which will show up as "dead" code when |
| 1691 | * we skip them here); if we can't, then the code path could be |
| 1692 | * "live" so we still need to check it. |
| 1693 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1694 | opcode_flags &= ~Instruction::kThrow; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1695 | work_line_->PopMonitor(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1696 | break; |
| 1697 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1698 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1699 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1702 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1703 | * |
| 1704 | * 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] | 1705 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1706 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1707 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1708 | const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 1709 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1710 | if (res_type.IsConflict()) { |
| 1711 | DCHECK_NE(failures_.size(), 0U); |
| 1712 | if (!is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1713 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1714 | } |
| 1715 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 1716 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1717 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1718 | uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c(); |
| 1719 | const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1720 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1721 | if (is_checkcast) { |
| 1722 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 1723 | } else { |
| 1724 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 1725 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1726 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1727 | if (is_checkcast) { |
| 1728 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 1729 | } else { |
| 1730 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 1731 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1732 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1733 | if (is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1734 | work_line_->SetRegisterType(inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1735 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1736 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1737 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1738 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1739 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1740 | } |
| 1741 | case Instruction::ARRAY_LENGTH: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1742 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1743 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1744 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1745 | 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] | 1746 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1747 | work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | break; |
| 1751 | } |
| 1752 | case Instruction::NEW_INSTANCE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1753 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1754 | if (res_type.IsConflict()) { |
| 1755 | DCHECK_NE(failures_.size(), 0U); |
| 1756 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1757 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1758 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 1759 | // can't create an instance of an interface or abstract class */ |
| 1760 | if (!res_type.IsInstantiableTypes()) { |
| 1761 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 1762 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1763 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1764 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1765 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| 1766 | // Any registers holding previous allocations from this address that have not yet been |
| 1767 | // initialized must be marked invalid. |
| 1768 | work_line_->MarkUninitRefsAsInvalid(uninit_type); |
| 1769 | // add the new uninitialized reference to the register state |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1770 | work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1771 | break; |
| 1772 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1773 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1774 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1777 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1778 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1779 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1780 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1781 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1782 | just_set_result = true; // Filled new array range sets result register |
| 1783 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1784 | case Instruction::CMPL_FLOAT: |
| 1785 | case Instruction::CMPG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1786 | if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1787 | break; |
| 1788 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1789 | if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1790 | break; |
| 1791 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1792 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1793 | break; |
| 1794 | case Instruction::CMPL_DOUBLE: |
| 1795 | case Instruction::CMPG_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1796 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1797 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1798 | break; |
| 1799 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1800 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1801 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1802 | break; |
| 1803 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1804 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1805 | break; |
| 1806 | case Instruction::CMP_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1807 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1808 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1809 | break; |
| 1810 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1811 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1812 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1813 | break; |
| 1814 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1815 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1816 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1817 | case Instruction::THROW: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1818 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1819 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1820 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 1821 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1822 | } |
| 1823 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1824 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1825 | case Instruction::GOTO: |
| 1826 | case Instruction::GOTO_16: |
| 1827 | case Instruction::GOTO_32: |
| 1828 | /* no effect on or use of registers */ |
| 1829 | break; |
| 1830 | |
| 1831 | case Instruction::PACKED_SWITCH: |
| 1832 | case Instruction::SPARSE_SWITCH: |
| 1833 | /* verify that vAA is an integer, or can be converted to one */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1834 | work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1835 | break; |
| 1836 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1837 | case Instruction::FILL_ARRAY_DATA: { |
| 1838 | /* Similar to the verification done for APUT */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1839 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1840 | /* array_type can be null if the reg type is Zero */ |
| 1841 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1842 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1843 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 1844 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1845 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1846 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 1847 | class_loader_->get()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1848 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1849 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1850 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 1851 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1852 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1853 | // Now verify if the element width in the table matches the element width declared in |
| 1854 | // the array |
| 1855 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 1856 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1857 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1858 | } else { |
| 1859 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 1860 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 1861 | // in the table and expected from the array class. |
| 1862 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1863 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 1864 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1865 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1866 | } |
| 1867 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1868 | } |
| 1869 | } |
| 1870 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1871 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1872 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1873 | case Instruction::IF_NE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1874 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1875 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1876 | bool mismatch = false; |
| 1877 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 1878 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 1879 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 1880 | mismatch = !reg_type2.IsReferenceTypes(); |
| 1881 | } else { // both integral? |
| 1882 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 1883 | } |
| 1884 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1885 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 1886 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1887 | } |
| 1888 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1889 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1890 | case Instruction::IF_LT: |
| 1891 | case Instruction::IF_GE: |
| 1892 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1893 | case Instruction::IF_LE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1894 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1895 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1896 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1897 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 1898 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1899 | } |
| 1900 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1901 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1902 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1903 | case Instruction::IF_NEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1904 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1905 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1906 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 1907 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1908 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1909 | |
| 1910 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1911 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1912 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1913 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1914 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1915 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1916 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1917 | CHECK(insn_flags_[instance_of_idx].IsOpcode()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1918 | } else { |
| 1919 | break; |
| 1920 | } |
| 1921 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1922 | 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] | 1923 | |
| 1924 | /* Check for peep-hole pattern of: |
| 1925 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1926 | * instance-of vX, vY, T; |
| 1927 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1928 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1929 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1930 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1931 | * and sharpen the type of vY to be type T. |
| 1932 | * Note, this pattern can't be if: |
| 1933 | * - if there are other branches to this branch, |
| 1934 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1935 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1936 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1937 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 1938 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 1939 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1940 | // Check that the we are not attempting conversion to interface types, |
| 1941 | // which is not done because of the multiple inheritance implications. |
Jeff Hao | c642ec8 | 2013-09-04 16:11:55 -0700 | [diff] [blame] | 1942 | // Also don't change the type if it would result in an upcast. |
| 1943 | const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1944 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1945 | |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1946 | if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
| 1947 | !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1948 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1949 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1950 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1951 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1952 | branch_line.reset(update_line); |
| 1953 | } |
| 1954 | update_line->CopyFromLine(work_line_.get()); |
| 1955 | update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type); |
| 1956 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 1957 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 1958 | // register encoding space of instance-of, and propagate type information to the source |
| 1959 | // of the move-object. |
| 1960 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1961 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1962 | move_idx--; |
| 1963 | } |
| 1964 | CHECK(insn_flags_[move_idx].IsOpcode()); |
| 1965 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 1966 | switch (move_inst->Opcode()) { |
| 1967 | case Instruction::MOVE_OBJECT: |
| 1968 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
| 1969 | update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type); |
| 1970 | } |
| 1971 | break; |
| 1972 | case Instruction::MOVE_OBJECT_FROM16: |
| 1973 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
| 1974 | update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type); |
| 1975 | } |
| 1976 | break; |
| 1977 | case Instruction::MOVE_OBJECT_16: |
| 1978 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
| 1979 | update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type); |
| 1980 | } |
| 1981 | break; |
| 1982 | default: |
| 1983 | break; |
| 1984 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1989 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1990 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1991 | case Instruction::IF_LTZ: |
| 1992 | case Instruction::IF_GEZ: |
| 1993 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1994 | case Instruction::IF_LEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1995 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1996 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1997 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 1998 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1999 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2000 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2001 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2002 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2003 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2004 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2005 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2006 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2007 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2008 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2009 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2010 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2011 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2012 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2013 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2014 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2015 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2016 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2017 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2018 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2019 | break; |
| 2020 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2021 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2022 | break; |
| 2023 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2024 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2025 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2026 | break; |
| 2027 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2028 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2029 | break; |
| 2030 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2031 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2032 | break; |
| 2033 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2034 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2035 | break; |
| 2036 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2037 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2040 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2041 | break; |
| 2042 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2043 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2044 | break; |
| 2045 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2046 | case Instruction::IGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2047 | VerifyISGet(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2048 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2049 | case Instruction::IGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2050 | VerifyISGet(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2051 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2052 | case Instruction::IGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2053 | VerifyISGet(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2054 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2055 | case Instruction::IGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2056 | VerifyISGet(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2057 | break; |
| 2058 | case Instruction::IGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2059 | VerifyISGet(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2060 | break; |
| 2061 | case Instruction::IGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2062 | VerifyISGet(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2063 | break; |
| 2064 | case Instruction::IGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2065 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2066 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2068 | case Instruction::IPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2069 | VerifyISPut(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2070 | break; |
| 2071 | case Instruction::IPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2072 | VerifyISPut(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2073 | break; |
| 2074 | case Instruction::IPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2075 | VerifyISPut(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2076 | break; |
| 2077 | case Instruction::IPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2078 | VerifyISPut(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2079 | break; |
| 2080 | case Instruction::IPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2081 | VerifyISPut(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2082 | break; |
| 2083 | case Instruction::IPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2084 | VerifyISPut(inst, reg_types_.LongLo(), true, false); |
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::IPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2087 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2088 | break; |
| 2089 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2090 | case Instruction::SGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2091 | VerifyISGet(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2092 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2093 | case Instruction::SGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2094 | VerifyISGet(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2095 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2096 | case Instruction::SGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2097 | VerifyISGet(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2098 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2099 | case Instruction::SGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2100 | VerifyISGet(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2101 | break; |
| 2102 | case Instruction::SGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2103 | VerifyISGet(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2104 | break; |
| 2105 | case Instruction::SGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2106 | VerifyISGet(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2107 | break; |
| 2108 | case Instruction::SGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2109 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2110 | break; |
| 2111 | |
| 2112 | case Instruction::SPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2113 | VerifyISPut(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2114 | break; |
| 2115 | case Instruction::SPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2116 | VerifyISPut(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2117 | break; |
| 2118 | case Instruction::SPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2119 | VerifyISPut(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2120 | break; |
| 2121 | case Instruction::SPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2122 | VerifyISPut(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2123 | break; |
| 2124 | case Instruction::SPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2125 | VerifyISPut(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2126 | break; |
| 2127 | case Instruction::SPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2128 | VerifyISPut(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2129 | break; |
| 2130 | case Instruction::SPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2131 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2132 | break; |
| 2133 | |
| 2134 | case Instruction::INVOKE_VIRTUAL: |
| 2135 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2136 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2137 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2138 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2139 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
| 2140 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2141 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2142 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2143 | is_range, is_super); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2144 | const RegType* return_type = nullptr; |
| 2145 | if (called_method != nullptr) { |
| 2146 | MethodHelper mh(called_method); |
| 2147 | mirror::Class* return_type_class = mh.GetReturnType(); |
| 2148 | if (return_type_class != nullptr) { |
| 2149 | return_type = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 2150 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| 2151 | } else { |
| 2152 | Thread* self = Thread::Current(); |
| 2153 | DCHECK(self->IsExceptionPending()); |
| 2154 | self->ClearException(); |
| 2155 | } |
| 2156 | } |
| 2157 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2158 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2159 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2160 | 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] | 2161 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2162 | return_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2163 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2164 | if (!return_type->IsLowHalf()) { |
| 2165 | work_line_->SetResultRegisterType(*return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2166 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2167 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2168 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2169 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2170 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2171 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2172 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2173 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2174 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2175 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2176 | is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2177 | const char* return_type_descriptor; |
| 2178 | bool is_constructor; |
| 2179 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2180 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2181 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2182 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2183 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2184 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2185 | } else { |
| 2186 | is_constructor = called_method->IsConstructor(); |
| 2187 | return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
| 2188 | } |
| 2189 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2190 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2191 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2192 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2193 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2194 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2195 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2196 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2197 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2198 | if (this_type.IsConflict()) // failure. |
| 2199 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2200 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2201 | /* no null refs allowed (?) */ |
| 2202 | if (this_type.IsZero()) { |
| 2203 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2204 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2205 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2206 | |
| 2207 | /* must be in same class or in superclass */ |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2208 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
| 2209 | // TODO: re-enable constructor type verification |
| 2210 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2211 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2212 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2213 | // break; |
| 2214 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2215 | |
| 2216 | /* arg must be an uninitialized reference */ |
| 2217 | if (!this_type.IsUninitializedTypes()) { |
| 2218 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2219 | << this_type; |
| 2220 | break; |
| 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2225 | * registers that have the same object instance in them, not just the "this" register. |
| 2226 | */ |
| 2227 | work_line_->MarkRefsAsInitialized(this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2228 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2229 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), |
| 2230 | return_type_descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2231 | if (!return_type.IsLowHalf()) { |
| 2232 | work_line_->SetResultRegisterType(return_type); |
| 2233 | } else { |
| 2234 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2235 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2236 | just_set_result = true; |
| 2237 | break; |
| 2238 | } |
| 2239 | case Instruction::INVOKE_STATIC: |
| 2240 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2241 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2242 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2243 | METHOD_STATIC, |
| 2244 | is_range, |
| 2245 | false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2246 | const char* descriptor; |
| 2247 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2248 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2249 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2250 | 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] | 2251 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2252 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2253 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2254 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2255 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2256 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2257 | if (!return_type.IsLowHalf()) { |
| 2258 | work_line_->SetResultRegisterType(return_type); |
| 2259 | } else { |
| 2260 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2261 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2262 | just_set_result = true; |
| 2263 | } |
| 2264 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2265 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2266 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2267 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2268 | mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2269 | METHOD_INTERFACE, |
| 2270 | is_range, |
| 2271 | false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2272 | if (abs_method != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2273 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2274 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2275 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2276 | << PrettyMethod(abs_method) << "'"; |
| 2277 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2278 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2279 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2280 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2281 | * interface or Object (see comments in RegType::JoinClass). |
| 2282 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2283 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2284 | if (this_type.IsZero()) { |
| 2285 | /* null pointer always passes (and always fails at runtime) */ |
| 2286 | } else { |
| 2287 | if (this_type.IsUninitializedTypes()) { |
| 2288 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2289 | << this_type; |
| 2290 | break; |
| 2291 | } |
| 2292 | // In the past we have tried to assert that "called_interface" is assignable |
| 2293 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2294 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2295 | // implemented by "this_type". For example, two classes may implement the same |
| 2296 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2297 | // join will set "this_type" to the parent class and a test that this implements |
| 2298 | // the interface will incorrectly fail. |
| 2299 | } |
| 2300 | /* |
| 2301 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2302 | * the type information is in the abstract method, so we're good. |
| 2303 | */ |
| 2304 | const char* descriptor; |
| 2305 | if (abs_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2306 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2307 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2308 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2309 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2310 | } else { |
| 2311 | descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor(); |
| 2312 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2313 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2314 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2315 | if (!return_type.IsLowHalf()) { |
| 2316 | work_line_->SetResultRegisterType(return_type); |
| 2317 | } else { |
| 2318 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2319 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2320 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2321 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2322 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2323 | case Instruction::NEG_INT: |
| 2324 | case Instruction::NOT_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2325 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2326 | break; |
| 2327 | case Instruction::NEG_LONG: |
| 2328 | case Instruction::NOT_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2329 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2330 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2331 | break; |
| 2332 | case Instruction::NEG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2333 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2334 | break; |
| 2335 | case Instruction::NEG_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2336 | work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2337 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2338 | break; |
| 2339 | case Instruction::INT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2340 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2341 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2342 | break; |
| 2343 | case Instruction::INT_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2344 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2345 | break; |
| 2346 | case Instruction::INT_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2347 | work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2348 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2349 | break; |
| 2350 | case Instruction::LONG_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2351 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2352 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2353 | break; |
| 2354 | case Instruction::LONG_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2355 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2356 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2357 | break; |
| 2358 | case Instruction::LONG_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2359 | work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2360 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2361 | break; |
| 2362 | case Instruction::FLOAT_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2363 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2364 | break; |
| 2365 | case Instruction::FLOAT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2366 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2367 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2368 | break; |
| 2369 | case Instruction::FLOAT_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2370 | work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2371 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2372 | break; |
| 2373 | case Instruction::DOUBLE_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2374 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2375 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2376 | break; |
| 2377 | case Instruction::DOUBLE_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2378 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2379 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2380 | break; |
| 2381 | case Instruction::DOUBLE_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2382 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2383 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2384 | break; |
| 2385 | case Instruction::INT_TO_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2386 | work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2387 | break; |
| 2388 | case Instruction::INT_TO_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2389 | work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2390 | break; |
| 2391 | case Instruction::INT_TO_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2392 | work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2393 | break; |
| 2394 | |
| 2395 | case Instruction::ADD_INT: |
| 2396 | case Instruction::SUB_INT: |
| 2397 | case Instruction::MUL_INT: |
| 2398 | case Instruction::REM_INT: |
| 2399 | case Instruction::DIV_INT: |
| 2400 | case Instruction::SHL_INT: |
| 2401 | case Instruction::SHR_INT: |
| 2402 | case Instruction::USHR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2403 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2404 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2405 | break; |
| 2406 | case Instruction::AND_INT: |
| 2407 | case Instruction::OR_INT: |
| 2408 | case Instruction::XOR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2409 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2410 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2411 | break; |
| 2412 | case Instruction::ADD_LONG: |
| 2413 | case Instruction::SUB_LONG: |
| 2414 | case Instruction::MUL_LONG: |
| 2415 | case Instruction::DIV_LONG: |
| 2416 | case Instruction::REM_LONG: |
| 2417 | case Instruction::AND_LONG: |
| 2418 | case Instruction::OR_LONG: |
| 2419 | case Instruction::XOR_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2420 | work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2421 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2422 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2423 | break; |
| 2424 | case Instruction::SHL_LONG: |
| 2425 | case Instruction::SHR_LONG: |
| 2426 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2427 | /* shift distance is Int, making these different from other binary operations */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2428 | work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2429 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2430 | break; |
| 2431 | case Instruction::ADD_FLOAT: |
| 2432 | case Instruction::SUB_FLOAT: |
| 2433 | case Instruction::MUL_FLOAT: |
| 2434 | case Instruction::DIV_FLOAT: |
| 2435 | case Instruction::REM_FLOAT: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2436 | work_line_->CheckBinaryOp(inst, |
| 2437 | reg_types_.Float(), |
| 2438 | reg_types_.Float(), |
| 2439 | reg_types_.Float(), |
| 2440 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2441 | break; |
| 2442 | case Instruction::ADD_DOUBLE: |
| 2443 | case Instruction::SUB_DOUBLE: |
| 2444 | case Instruction::MUL_DOUBLE: |
| 2445 | case Instruction::DIV_DOUBLE: |
| 2446 | case Instruction::REM_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2447 | work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2448 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2449 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2450 | break; |
| 2451 | case Instruction::ADD_INT_2ADDR: |
| 2452 | case Instruction::SUB_INT_2ADDR: |
| 2453 | case Instruction::MUL_INT_2ADDR: |
| 2454 | case Instruction::REM_INT_2ADDR: |
| 2455 | case Instruction::SHL_INT_2ADDR: |
| 2456 | case Instruction::SHR_INT_2ADDR: |
| 2457 | case Instruction::USHR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2458 | work_line_->CheckBinaryOp2addr(inst, |
| 2459 | reg_types_.Integer(), |
| 2460 | reg_types_.Integer(), |
| 2461 | reg_types_.Integer(), |
| 2462 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2463 | break; |
| 2464 | case Instruction::AND_INT_2ADDR: |
| 2465 | case Instruction::OR_INT_2ADDR: |
| 2466 | case Instruction::XOR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2467 | work_line_->CheckBinaryOp2addr(inst, |
| 2468 | reg_types_.Integer(), |
| 2469 | reg_types_.Integer(), |
| 2470 | reg_types_.Integer(), |
| 2471 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2472 | break; |
| 2473 | case Instruction::DIV_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2474 | work_line_->CheckBinaryOp2addr(inst, |
| 2475 | reg_types_.Integer(), |
| 2476 | reg_types_.Integer(), |
| 2477 | reg_types_.Integer(), |
| 2478 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2479 | break; |
| 2480 | case Instruction::ADD_LONG_2ADDR: |
| 2481 | case Instruction::SUB_LONG_2ADDR: |
| 2482 | case Instruction::MUL_LONG_2ADDR: |
| 2483 | case Instruction::DIV_LONG_2ADDR: |
| 2484 | case Instruction::REM_LONG_2ADDR: |
| 2485 | case Instruction::AND_LONG_2ADDR: |
| 2486 | case Instruction::OR_LONG_2ADDR: |
| 2487 | case Instruction::XOR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2488 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2489 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2490 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2491 | break; |
| 2492 | case Instruction::SHL_LONG_2ADDR: |
| 2493 | case Instruction::SHR_LONG_2ADDR: |
| 2494 | case Instruction::USHR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2495 | work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2496 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2497 | break; |
| 2498 | case Instruction::ADD_FLOAT_2ADDR: |
| 2499 | case Instruction::SUB_FLOAT_2ADDR: |
| 2500 | case Instruction::MUL_FLOAT_2ADDR: |
| 2501 | case Instruction::DIV_FLOAT_2ADDR: |
| 2502 | case Instruction::REM_FLOAT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2503 | work_line_->CheckBinaryOp2addr(inst, |
| 2504 | reg_types_.Float(), |
| 2505 | reg_types_.Float(), |
| 2506 | reg_types_.Float(), |
| 2507 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2508 | break; |
| 2509 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2510 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2511 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2512 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2513 | case Instruction::REM_DOUBLE_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2514 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2515 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2516 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2517 | break; |
| 2518 | case Instruction::ADD_INT_LIT16: |
| 2519 | case Instruction::RSUB_INT: |
| 2520 | case Instruction::MUL_INT_LIT16: |
| 2521 | case Instruction::DIV_INT_LIT16: |
| 2522 | case Instruction::REM_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2523 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2524 | break; |
| 2525 | case Instruction::AND_INT_LIT16: |
| 2526 | case Instruction::OR_INT_LIT16: |
| 2527 | case Instruction::XOR_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2528 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2529 | break; |
| 2530 | case Instruction::ADD_INT_LIT8: |
| 2531 | case Instruction::RSUB_INT_LIT8: |
| 2532 | case Instruction::MUL_INT_LIT8: |
| 2533 | case Instruction::DIV_INT_LIT8: |
| 2534 | case Instruction::REM_INT_LIT8: |
| 2535 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2536 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2537 | case Instruction::USHR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2538 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2539 | break; |
| 2540 | case Instruction::AND_INT_LIT8: |
| 2541 | case Instruction::OR_INT_LIT8: |
| 2542 | case Instruction::XOR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2543 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2544 | break; |
| 2545 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2546 | // Special instructions. |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2547 | case Instruction::RETURN_VOID_BARRIER: |
Ian Rogers | 9fc16eb | 2013-07-31 14:49:16 -0700 | [diff] [blame] | 2548 | DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_); |
| 2549 | if (!IsConstructor() || IsStatic()) { |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2550 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected"; |
| 2551 | } |
| 2552 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2553 | // Note: the following instructions encode offsets derived from class linking. |
| 2554 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2555 | // meaning if the class linking and resolution were successful. |
| 2556 | case Instruction::IGET_QUICK: |
| 2557 | VerifyIGetQuick(inst, reg_types_.Integer(), true); |
| 2558 | break; |
| 2559 | case Instruction::IGET_WIDE_QUICK: |
| 2560 | VerifyIGetQuick(inst, reg_types_.LongLo(), true); |
| 2561 | break; |
| 2562 | case Instruction::IGET_OBJECT_QUICK: |
| 2563 | VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2564 | break; |
| 2565 | case Instruction::IPUT_QUICK: |
| 2566 | VerifyIPutQuick(inst, reg_types_.Integer(), true); |
| 2567 | break; |
| 2568 | case Instruction::IPUT_WIDE_QUICK: |
| 2569 | VerifyIPutQuick(inst, reg_types_.LongLo(), true); |
| 2570 | break; |
| 2571 | case Instruction::IPUT_OBJECT_QUICK: |
| 2572 | VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2573 | break; |
| 2574 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2575 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2576 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2577 | mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2578 | if (called_method != NULL) { |
| 2579 | const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2580 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2581 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2582 | if (!return_type.IsLowHalf()) { |
| 2583 | work_line_->SetResultRegisterType(return_type); |
| 2584 | } else { |
| 2585 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2586 | } |
| 2587 | just_set_result = true; |
| 2588 | } |
| 2589 | break; |
| 2590 | } |
| 2591 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2592 | /* These should never appear during verification. */ |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2593 | case Instruction::UNUSED_3E: |
| 2594 | case Instruction::UNUSED_3F: |
| 2595 | case Instruction::UNUSED_40: |
| 2596 | case Instruction::UNUSED_41: |
| 2597 | case Instruction::UNUSED_42: |
| 2598 | case Instruction::UNUSED_43: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2599 | case Instruction::UNUSED_79: |
| 2600 | case Instruction::UNUSED_7A: |
| 2601 | case Instruction::UNUSED_EB: |
| 2602 | case Instruction::UNUSED_EC: |
jeffhao | 9a4f003 | 2012-08-30 16:17:40 -0700 | [diff] [blame] | 2603 | case Instruction::UNUSED_ED: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2604 | case Instruction::UNUSED_EE: |
| 2605 | case Instruction::UNUSED_EF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2606 | case Instruction::UNUSED_F0: |
| 2607 | case Instruction::UNUSED_F1: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2608 | case Instruction::UNUSED_F2: |
| 2609 | case Instruction::UNUSED_F3: |
| 2610 | case Instruction::UNUSED_F4: |
| 2611 | case Instruction::UNUSED_F5: |
| 2612 | case Instruction::UNUSED_F6: |
| 2613 | case Instruction::UNUSED_F7: |
| 2614 | case Instruction::UNUSED_F8: |
| 2615 | case Instruction::UNUSED_F9: |
| 2616 | case Instruction::UNUSED_FA: |
| 2617 | case Instruction::UNUSED_FB: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2618 | case Instruction::UNUSED_FC: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2619 | case Instruction::UNUSED_FD: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2620 | case Instruction::UNUSED_FE: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2621 | case Instruction::UNUSED_FF: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2622 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2623 | break; |
| 2624 | |
| 2625 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2626 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2627 | * complain if an instruction is missing (which is desirable). |
| 2628 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2629 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2630 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2631 | if (have_pending_hard_failure_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2632 | if (Runtime::Current()->IsCompiler()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2633 | /* When compiling, check that the last failure is a hard failure */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2634 | CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2635 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2636 | /* immediate failure, reject class */ |
| 2637 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 2638 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2639 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2640 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2641 | opcode_flags = Instruction::kThrow; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2642 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2643 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2644 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2645 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2646 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2647 | */ |
| 2648 | if (!just_set_result) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2649 | work_line_->SetResultTypeToUnknown(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2652 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2653 | |
| 2654 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2655 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2656 | * |
| 2657 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2658 | * 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] | 2659 | * somebody could get a reference field, check it for zero, and if the |
| 2660 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2661 | * 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] | 2662 | * that, and will reject the code. |
| 2663 | * |
| 2664 | * TODO: avoid re-fetching the branch target |
| 2665 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2666 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2667 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2668 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2669 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2670 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2671 | return false; |
| 2672 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2673 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2674 | if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2675 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2676 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2677 | /* update branch target, set "changed" if appropriate */ |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2678 | if (NULL != branch_line.get()) { |
| 2679 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) { |
| 2680 | return false; |
| 2681 | } |
| 2682 | } else { |
| 2683 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) { |
| 2684 | return false; |
| 2685 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2686 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2690 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2691 | * |
| 2692 | * We've already verified that the table is structurally sound, so we |
| 2693 | * just need to walk through and tag the targets. |
| 2694 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2695 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2696 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 2697 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 2698 | int switch_count = switch_insns[1]; |
| 2699 | int offset_to_targets, targ; |
| 2700 | |
| 2701 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 2702 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 2703 | offset_to_targets = 4; |
| 2704 | } else { |
| 2705 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2706 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2707 | offset_to_targets = 2 + 2 * switch_count; |
| 2708 | } |
| 2709 | |
| 2710 | /* verify each switch target */ |
| 2711 | for (targ = 0; targ < switch_count; targ++) { |
| 2712 | int offset; |
| 2713 | uint32_t abs_offset; |
| 2714 | |
| 2715 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 2716 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 2717 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2718 | abs_offset = work_insn_idx_ + offset; |
| 2719 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2720 | if (!CheckNotMoveException(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2721 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2722 | } |
| 2723 | if (!UpdateRegisters(abs_offset, work_line_.get())) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2724 | return false; |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2729 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 2730 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2731 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2732 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2733 | bool within_catch_all = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2734 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2735 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2736 | for (; iterator.HasNext(); iterator.Next()) { |
| 2737 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2738 | within_catch_all = true; |
| 2739 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2740 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2741 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 2742 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 2743 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | */ |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2745 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2746 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2747 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2751 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 2752 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2753 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2754 | if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2755 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2756 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 2757 | * 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] | 2758 | * it will do so before grabbing the lock). |
| 2759 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2760 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2761 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2762 | << "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] | 2763 | return false; |
| 2764 | } |
| 2765 | } |
| 2766 | } |
| 2767 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2768 | /* Handle "continue". Tag the next consecutive instruction. |
| 2769 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 2770 | * because it changes work_line_ when performing peephole optimization |
| 2771 | * and this change should not be used in those cases. |
| 2772 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2773 | if ((opcode_flags & Instruction::kContinue) != 0) { |
| 2774 | uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits(); |
| 2775 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 2776 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 2777 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2778 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2779 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 2780 | // next instruction isn't one. |
| 2781 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 2782 | return false; |
| 2783 | } |
| 2784 | if (NULL != fallthrough_line.get()) { |
| 2785 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 2786 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 2787 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 2788 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 2789 | // For returns we only care about the operand to the return, all other registers are dead. |
| 2790 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 2791 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2792 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 2793 | work_line_->MarkAllRegistersAsConflicts(); |
| 2794 | } else { |
| 2795 | if (opcode == Instruction::RETURN_WIDE) { |
| 2796 | work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 2797 | } else { |
| 2798 | work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 2799 | } |
| 2800 | } |
| 2801 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2802 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| 2803 | if (next_line != NULL) { |
| 2804 | // Merge registers into what we have for the next instruction, |
| 2805 | // and set the "changed" flag if needed. |
| 2806 | if (!UpdateRegisters(next_insn_idx, work_line_.get())) { |
| 2807 | return false; |
| 2808 | } |
| 2809 | } else { |
| 2810 | /* |
| 2811 | * We're not recording register data for the next instruction, so we don't know what the |
| 2812 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 2813 | */ |
| 2814 | insn_flags_[next_insn_idx].SetChanged(); |
| 2815 | } |
| 2816 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2817 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2818 | /* 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] | 2819 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 2820 | if (!work_line_->VerifyMonitorStackEmpty()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2821 | return false; |
| 2822 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2826 | * Update start_guess. Advance to the next instruction of that's |
| 2827 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2828 | * neither of those exists we're in a return or throw; leave start_guess |
| 2829 | * alone and let the caller sort it out. |
| 2830 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2831 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2832 | *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2833 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2834 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2835 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2838 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 2839 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2840 | |
| 2841 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2842 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2843 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2844 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2845 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2846 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2847 | mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2848 | const RegType& result = |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 2849 | klass != NULL ? reg_types_.FromClass(descriptor, klass, |
| 2850 | klass->CannotBeAssignedFromOtherTypes()) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2851 | : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2852 | if (result.IsConflict()) { |
| 2853 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 2854 | << "' in " << referrer; |
| 2855 | return result; |
| 2856 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2857 | if (klass == NULL && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2858 | (*dex_cache_)->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2859 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2860 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 2861 | // check at runtime if access is allowed and so pass here. If result is |
| 2862 | // primitive, skip the access check. |
| 2863 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 2864 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2865 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2866 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2867 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2868 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2871 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2872 | const RegType* common_super = NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2873 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2874 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2875 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2876 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2877 | CatchHandlerIterator iterator(handlers_ptr); |
| 2878 | for (; iterator.HasNext(); iterator.Next()) { |
| 2879 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 2880 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2881 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2882 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2883 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2884 | if (common_super == NULL) { |
| 2885 | // Unconditionally assign for the first handler. We don't assert this is a Throwable |
| 2886 | // as that is caught at runtime |
| 2887 | common_super = &exception; |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2888 | } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 2889 | if (exception.IsUnresolvedTypes()) { |
| 2890 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 2891 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 2892 | return exception; |
| 2893 | } else { |
| 2894 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 2895 | return reg_types_.Conflict(); |
| 2896 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2897 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2898 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2899 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2900 | common_super = &common_super->Merge(exception, ®_types_); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2901 | CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | } |
| 2905 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2906 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | if (common_super == NULL) { |
| 2910 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2911 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2912 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2913 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2914 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2917 | mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2918 | MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2919 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2920 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2921 | if (klass_type.IsConflict()) { |
| 2922 | std::string append(" in attempt to access method "); |
| 2923 | append += dex_file_->GetMethodName(method_id); |
| 2924 | AppendToLastFailMessage(append); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2925 | return NULL; |
| 2926 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2927 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2928 | return NULL; // Can't resolve Class so no more to do here |
| 2929 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2930 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2931 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2932 | mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2933 | if (res_method == NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2934 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2935 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2936 | |
| 2937 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2938 | res_method = klass->FindDirectMethod(name, signature); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2939 | } else if (method_type == METHOD_INTERFACE) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2940 | res_method = klass->FindInterfaceMethod(name, signature); |
| 2941 | } else { |
| 2942 | res_method = klass->FindVirtualMethod(name, signature); |
| 2943 | } |
| 2944 | if (res_method != NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2945 | (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2946 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2947 | // If a virtual or interface method wasn't found with the expected type, look in |
| 2948 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 2949 | // a class has changed, and will be flagged as an error in later checks. |
| 2950 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
| 2951 | res_method = klass->FindDirectMethod(name, signature); |
| 2952 | } |
| 2953 | if (res_method == NULL) { |
| 2954 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 2955 | << PrettyDescriptor(klass) << "." << name |
| 2956 | << " " << signature; |
| 2957 | return NULL; |
| 2958 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2959 | } |
| 2960 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2961 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 2962 | // enforce them here. |
| 2963 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2964 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 2965 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2966 | return NULL; |
| 2967 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2968 | // Disallow any calls to class initializers. |
| 2969 | if (MethodHelper(res_method).IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2970 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 2971 | << PrettyMethod(res_method); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2972 | return NULL; |
| 2973 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2974 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2975 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2976 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2977 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2978 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2979 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 2980 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 2981 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2982 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 2983 | << PrettyMethod(res_method); |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 2984 | return NULL; |
| 2985 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2986 | // Check that interface methods match interface classes. |
| 2987 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 2988 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 2989 | << " is in an interface class " << PrettyClass(klass); |
| 2990 | return NULL; |
| 2991 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 2992 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 2993 | << " is in a non-interface class " << PrettyClass(klass); |
| 2994 | return NULL; |
| 2995 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2996 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 2997 | // target method. |
| 2998 | if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || |
| 2999 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3000 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3001 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3002 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3003 | " type of " << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3004 | return NULL; |
| 3005 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3006 | return res_method; |
| 3007 | } |
| 3008 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3009 | mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3010 | MethodType method_type, |
| 3011 | bool is_range, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3012 | bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3013 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3014 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3015 | 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] | 3016 | mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3017 | if (res_method == NULL) { // error or class is unresolved |
| 3018 | return NULL; |
| 3019 | } |
| 3020 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3021 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3022 | // has a vtable entry for the target method. |
| 3023 | if (is_super) { |
| 3024 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3025 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3026 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3027 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3028 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3029 | << " to super " << PrettyMethod(res_method); |
| 3030 | return NULL; |
| 3031 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3032 | mirror::Class* super_klass = super.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3033 | if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3034 | MethodHelper mh(res_method); |
| 3035 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3036 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3037 | << " to super " << super |
| 3038 | << "." << mh.GetName() |
| 3039 | << mh.GetSignature(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3040 | return NULL; |
| 3041 | } |
| 3042 | } |
| 3043 | // 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] | 3044 | // 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] | 3045 | // definition (which doesn't have register count values). |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3046 | 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] | 3047 | /* caught by static verifier */ |
| 3048 | DCHECK(is_range || expected_args <= 5); |
| 3049 | if (expected_args > code_item_->outs_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3050 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3051 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3052 | return NULL; |
| 3053 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3054 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3055 | /* |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3056 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3057 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3058 | * 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] | 3059 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3060 | size_t actual_args = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3061 | if (!res_method->IsStatic()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3062 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3063 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3064 | return NULL; |
| 3065 | } |
| 3066 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3067 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3068 | return NULL; |
| 3069 | } |
| 3070 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3071 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3072 | const RegType& res_method_class = |
| 3073 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3074 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3075 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3076 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3077 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3078 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3079 | return NULL; |
| 3080 | } |
| 3081 | } |
| 3082 | actual_args++; |
| 3083 | } |
| 3084 | /* |
| 3085 | * Process the target method's signature. This signature may or may not |
| 3086 | * have been verified, so we can't assume it's properly formed. |
| 3087 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3088 | MethodHelper mh(res_method); |
| 3089 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3090 | size_t params_size = params == NULL ? 0 : params->Size(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3091 | uint32_t arg[5]; |
| 3092 | if (!is_range) { |
| 3093 | inst->GetArgs(arg); |
| 3094 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3095 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3096 | if (actual_args >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3097 | 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] | 3098 | << "'. Expected " << expected_args << " arguments, processing argument " << actual_args |
| 3099 | << " (where longs/doubles count twice)."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3100 | return NULL; |
| 3101 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3102 | const char* descriptor = |
| 3103 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3104 | if (descriptor == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3105 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3106 | << " missing signature component"; |
| 3107 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3108 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3109 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3110 | 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] | 3111 | if (reg_type.IsIntegralTypes()) { |
| 3112 | const RegType& src_type = work_line_->GetRegisterType(get_reg); |
| 3113 | if (!src_type.IsIntegralTypes()) { |
| 3114 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3115 | << " but expected " << reg_type; |
| 3116 | return res_method; |
| 3117 | } |
| 3118 | } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3119 | return res_method; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3120 | } |
| 3121 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3122 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3123 | if (actual_args != expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3124 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3125 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3126 | return NULL; |
| 3127 | } else { |
| 3128 | return res_method; |
| 3129 | } |
| 3130 | } |
| 3131 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3132 | mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3133 | RegisterLine* reg_line, bool is_range) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3134 | DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK || |
| 3135 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3136 | const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3137 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3138 | return NULL; |
Sebastien Hertz | 8249b42 | 2013-10-29 17:50:55 +0100 | [diff] [blame] | 3139 | } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further. |
| 3140 | return NULL; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3141 | } |
| 3142 | mirror::Class* this_class = NULL; |
| 3143 | if (!actual_arg_type.IsUnresolvedTypes()) { |
| 3144 | this_class = actual_arg_type.GetClass(); |
| 3145 | } else { |
| 3146 | const std::string& descriptor(actual_arg_type.GetDescriptor()); |
| 3147 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3148 | this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3149 | if (this_class == NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3150 | Thread* self = Thread::Current(); |
| 3151 | self->ClearException(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3152 | // Look for a system class |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3153 | SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr); |
| 3154 | this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3155 | } |
| 3156 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3157 | if (this_class == NULL) { |
| 3158 | return NULL; |
| 3159 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3160 | mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3161 | CHECK(vtable != NULL); |
| 3162 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3163 | CHECK(vtable_index < vtable->GetLength()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3164 | mirror::ArtMethod* res_method = vtable->Get(vtable_index); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3165 | CHECK(!Thread::Current()->IsExceptionPending()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3166 | return res_method; |
| 3167 | } |
| 3168 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3169 | mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3170 | bool is_range) { |
| 3171 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3172 | mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3173 | is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3174 | if (res_method == NULL) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3175 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3176 | return NULL; |
| 3177 | } |
| 3178 | CHECK(!res_method->IsDirect() && !res_method->IsStatic()); |
| 3179 | |
| 3180 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3181 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3182 | // definition (which doesn't have register count values). |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3183 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
| 3184 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3185 | return NULL; |
| 3186 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3187 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3188 | /* caught by static verifier */ |
| 3189 | DCHECK(is_range || expected_args <= 5); |
| 3190 | if (expected_args > code_item_->outs_size_) { |
| 3191 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3192 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3193 | return NULL; |
| 3194 | } |
| 3195 | |
| 3196 | /* |
| 3197 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3198 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3199 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3200 | */ |
| 3201 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3202 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3203 | return NULL; |
| 3204 | } |
| 3205 | if (!actual_arg_type.IsZero()) { |
| 3206 | mirror::Class* klass = res_method->GetDeclaringClass(); |
| 3207 | const RegType& res_method_class = |
| 3208 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3209 | klass->CannotBeAssignedFromOtherTypes()); |
| 3210 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3211 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3212 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3213 | << "' not instance of '" << res_method_class << "'"; |
| 3214 | return NULL; |
| 3215 | } |
| 3216 | } |
| 3217 | /* |
| 3218 | * Process the target method's signature. This signature may or may not |
| 3219 | * have been verified, so we can't assume it's properly formed. |
| 3220 | */ |
| 3221 | MethodHelper mh(res_method); |
| 3222 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3223 | size_t params_size = params == NULL ? 0 : params->Size(); |
| 3224 | uint32_t arg[5]; |
| 3225 | if (!is_range) { |
| 3226 | inst->GetArgs(arg); |
| 3227 | } |
| 3228 | size_t actual_args = 1; |
| 3229 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3230 | if (actual_args >= expected_args) { |
| 3231 | 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] | 3232 | << "'. Expected " << expected_args |
| 3233 | << " arguments, processing argument " << actual_args |
| 3234 | << " (where longs/doubles count twice)."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3235 | return NULL; |
| 3236 | } |
| 3237 | const char* descriptor = |
| 3238 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3239 | if (descriptor == NULL) { |
| 3240 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3241 | << " missing signature component"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3242 | return NULL; |
| 3243 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3244 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3245 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; |
| 3246 | if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
| 3247 | return res_method; |
| 3248 | } |
| 3249 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3250 | } |
| 3251 | if (actual_args != expected_args) { |
| 3252 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3253 | << " expected " << expected_args << " arguments, found " << actual_args; |
| 3254 | return NULL; |
| 3255 | } else { |
| 3256 | return res_method; |
| 3257 | } |
| 3258 | } |
| 3259 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3260 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3261 | uint32_t type_idx; |
| 3262 | if (!is_filled) { |
| 3263 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3264 | type_idx = inst->VRegC_22c(); |
| 3265 | } else if (!is_range) { |
| 3266 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3267 | type_idx = inst->VRegB_35c(); |
| 3268 | } else { |
| 3269 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3270 | type_idx = inst->VRegB_3rc(); |
| 3271 | } |
| 3272 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3273 | if (res_type.IsConflict()) { // bad class |
| 3274 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3275 | } else { |
| 3276 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3277 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3278 | 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] | 3279 | } else if (!is_filled) { |
| 3280 | /* make sure "size" register is valid type */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3281 | work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3282 | /* set register type to array class */ |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3283 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3284 | work_line_->SetRegisterType(inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3285 | } else { |
| 3286 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3287 | // the list and fail. It's legal, if silly, for arg_count to be zero. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3288 | const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3289 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3290 | uint32_t arg[5]; |
| 3291 | if (!is_range) { |
| 3292 | inst->GetArgs(arg); |
| 3293 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3294 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3295 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3296 | if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3297 | work_line_->SetResultRegisterType(reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3298 | return; |
| 3299 | } |
| 3300 | } |
| 3301 | // filled-array result goes into "result" register |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3302 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3303 | work_line_->SetResultRegisterType(precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | } |
| 3307 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3308 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3309 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3310 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3311 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3312 | 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] | 3313 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3314 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3315 | if (array_type.IsZero()) { |
| 3316 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3317 | // instruction type. TODO: have a proper notion of bottom here. |
| 3318 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3319 | // Reference or category 1 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3320 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3321 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3322 | // Category 2 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3323 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3324 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3325 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3326 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3327 | 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] | 3328 | } else { |
| 3329 | /* verify the class */ |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3330 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3331 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3332 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3333 | << " source for aget-object"; |
| 3334 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3335 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3336 | << " source for category 1 aget"; |
| 3337 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3338 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3339 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3340 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3341 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3342 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3343 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3344 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3345 | // doubles. |
| 3346 | if (!component_type.IsLowHalf()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3347 | work_line_->SetRegisterType(inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3348 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3349 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3350 | component_type.HighHalf(®_types_)); |
| 3351 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3357 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
| 3358 | const uint32_t vregA) { |
| 3359 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3360 | bool instruction_compatible; |
| 3361 | bool value_compatible; |
| 3362 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3363 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3364 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3365 | value_compatible = value_type.IsIntegralTypes(); |
| 3366 | } else if (target_type.IsFloat()) { |
| 3367 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3368 | value_compatible = value_type.IsFloatTypes(); |
| 3369 | } else if (target_type.IsLong()) { |
| 3370 | instruction_compatible = insn_type.IsLong(); |
| 3371 | value_compatible = value_type.IsLongTypes(); |
| 3372 | } else if (target_type.IsDouble()) { |
| 3373 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
| 3374 | value_compatible = value_type.IsDoubleTypes(); |
| 3375 | } else { |
| 3376 | instruction_compatible = false; // reference with primitive store |
| 3377 | value_compatible = false; // unused |
| 3378 | } |
| 3379 | if (!instruction_compatible) { |
| 3380 | // This is a global failure rather than a class change failure as the instructions and |
| 3381 | // the descriptors for the type should have been consistent within the same file at |
| 3382 | // compile time. |
| 3383 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3384 | << "' but expected type '" << target_type << "'"; |
| 3385 | return; |
| 3386 | } |
| 3387 | if (!value_compatible) { |
| 3388 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3389 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3390 | return; |
| 3391 | } |
| 3392 | } |
| 3393 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3394 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3395 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3396 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3397 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3398 | 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] | 3399 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3400 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3401 | if (array_type.IsZero()) { |
| 3402 | // Null array type; this code path will fail at runtime. Infer a merge-able type from the |
| 3403 | // instruction type. |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3404 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3405 | 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] | 3406 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3407 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3408 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3409 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3410 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3411 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3412 | if (!component_type.IsReferenceTypes()) { |
| 3413 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3414 | << " source for aput-object"; |
| 3415 | } else { |
| 3416 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3417 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3418 | // incompatible classes will be caught at runtime as an array store exception |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3419 | work_line_->VerifyRegisterType(vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3420 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | } |
| 3425 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3426 | mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3427 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3428 | // Check access to class |
| 3429 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3430 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3431 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3432 | field_idx, dex_file_->GetFieldName(field_id), |
| 3433 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3434 | return NULL; |
| 3435 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3436 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3437 | 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] | 3438 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3439 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3440 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3441 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3442 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3443 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3444 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3445 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3446 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3447 | Thread::Current()->ClearException(); |
| 3448 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3449 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3450 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3451 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3452 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3453 | return NULL; |
| 3454 | } else if (!field->IsStatic()) { |
| 3455 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
| 3456 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3457 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3458 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3459 | } |
| 3460 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3461 | mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3462 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3463 | // Check access to class |
| 3464 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3465 | if (klass_type.IsConflict()) { |
| 3466 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 3467 | field_idx, dex_file_->GetFieldName(field_id), |
| 3468 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3469 | return NULL; |
| 3470 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3471 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3472 | return NULL; // Can't resolve Class so no more to do here |
| 3473 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3474 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3475 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3476 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3477 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3478 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3479 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3480 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3481 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3482 | Thread::Current()->ClearException(); |
| 3483 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3484 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3485 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3486 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3487 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3488 | return NULL; |
| 3489 | } else if (field->IsStatic()) { |
| 3490 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3491 | << " to not be static"; |
| 3492 | return NULL; |
| 3493 | } else if (obj_type.IsZero()) { |
| 3494 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3495 | return field; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3496 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3497 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3498 | const RegType& field_klass = |
| 3499 | reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3500 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3501 | if (obj_type.IsUninitializedTypes() && |
| 3502 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 3503 | !field_klass.Equals(GetDeclaringClass()))) { |
| 3504 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3505 | // the field is declared in this class |
| 3506 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3507 | << " of a not fully initialized object within the context" |
| 3508 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3509 | return NULL; |
| 3510 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 3511 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3512 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3513 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3514 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3515 | << " from object of type " << obj_type; |
| 3516 | return NULL; |
| 3517 | } else { |
| 3518 | return field; |
| 3519 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3520 | } |
| 3521 | } |
| 3522 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3523 | void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type, |
| 3524 | bool is_primitive, bool is_static) { |
| 3525 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3526 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3527 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3528 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3529 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3530 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3531 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3532 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3533 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3534 | if (field != NULL) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3535 | FieldHelper fh(field); |
| 3536 | mirror::Class* field_type_class = fh.GetType(false); |
| 3537 | if (field_type_class != nullptr) { |
| 3538 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3539 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3540 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3541 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3542 | if (field_type == nullptr) { |
| 3543 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3544 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3545 | field_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3546 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3547 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3548 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3549 | if (field_type->Equals(insn_type) || |
| 3550 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 3551 | (field_type->IsDouble() && insn_type.IsLong())) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3552 | // expected that read is of the correct primitive type or that int reads are reading |
| 3553 | // floats or long reads are reading doubles |
| 3554 | } else { |
| 3555 | // This is a global failure rather than a class change failure as the instructions and |
| 3556 | // the descriptors for the type should have been consistent within the same file at |
| 3557 | // compile time |
| 3558 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3559 | << " to be of type '" << insn_type |
| 3560 | << "' but found type '" << field_type << "' in get"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3561 | return; |
| 3562 | } |
| 3563 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3564 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3565 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3566 | << " to be compatible with type '" << insn_type |
| 3567 | << "' but found type '" << field_type |
| 3568 | << "' in get-object"; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3569 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3570 | return; |
| 3571 | } |
| 3572 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3573 | if (!field_type->IsLowHalf()) { |
| 3574 | work_line_->SetRegisterType(vregA, *field_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3575 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3576 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3577 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3580 | void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type, |
| 3581 | bool is_primitive, bool is_static) { |
| 3582 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3583 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3584 | if (is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3585 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3586 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3587 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3588 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3589 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3590 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3591 | if (field != NULL) { |
| 3592 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3593 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3594 | << " from other class " << GetDeclaringClass(); |
| 3595 | return; |
| 3596 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3597 | FieldHelper fh(field); |
| 3598 | mirror::Class* field_type_class = fh.GetType(false); |
| 3599 | if (field_type_class != nullptr) { |
| 3600 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3601 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3602 | } |
| 3603 | } |
| 3604 | if (field_type == nullptr) { |
| 3605 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3606 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3607 | field_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3608 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3609 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3610 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3611 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3612 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3613 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3614 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3615 | << " to be compatible with type '" << insn_type |
| 3616 | << "' but found type '" << field_type |
| 3617 | << "' in put-object"; |
| 3618 | return; |
| 3619 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3620 | work_line_->VerifyRegisterType(vregA, *field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3621 | } |
| 3622 | } |
| 3623 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3624 | // Look for an instance field with this offset. |
| 3625 | // TODO: we may speed up the search if offsets are sorted by doing a quick search. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3626 | static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3627 | uint32_t field_offset) |
| 3628 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3629 | const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3630 | if (instance_fields != NULL) { |
| 3631 | for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3632 | mirror::ArtField* field = instance_fields->Get(i); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3633 | if (field->GetOffset().Uint32Value() == field_offset) { |
| 3634 | return field; |
| 3635 | } |
| 3636 | } |
| 3637 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3638 | // We did not find field in class: look into superclass. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3639 | if (klass->GetSuperClass() != NULL) { |
| 3640 | return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset); |
| 3641 | } else { |
| 3642 | return NULL; |
| 3643 | } |
| 3644 | } |
| 3645 | |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3646 | // Returns the access field of a quick field access (iget/iput-quick) or NULL |
| 3647 | // if it cannot be found. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3648 | mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3649 | RegisterLine* reg_line) { |
| 3650 | DCHECK(inst->Opcode() == Instruction::IGET_QUICK || |
| 3651 | inst->Opcode() == Instruction::IGET_WIDE_QUICK || |
| 3652 | inst->Opcode() == Instruction::IGET_OBJECT_QUICK || |
| 3653 | inst->Opcode() == Instruction::IPUT_QUICK || |
| 3654 | inst->Opcode() == Instruction::IPUT_WIDE_QUICK || |
| 3655 | inst->Opcode() == Instruction::IPUT_OBJECT_QUICK); |
| 3656 | const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3657 | mirror::Class* object_class = NULL; |
| 3658 | if (!object_type.IsUnresolvedTypes()) { |
| 3659 | object_class = object_type.GetClass(); |
| 3660 | } else { |
| 3661 | // We need to resolve the class from its descriptor. |
| 3662 | const std::string& descriptor(object_type.GetDescriptor()); |
| 3663 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3664 | Thread* self = Thread::Current(); |
| 3665 | object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3666 | if (object_class == NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3667 | self->ClearException(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3668 | // Look for a system class |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3669 | SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr); |
| 3670 | object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3671 | } |
| 3672 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3673 | if (object_class == NULL) { |
| 3674 | // Failed to get the Class* from reg type. |
| 3675 | LOG(WARNING) << "Failed to get Class* from " << object_type; |
| 3676 | return NULL; |
| 3677 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3678 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3679 | return FindInstanceFieldWithOffset(object_class, field_offset); |
| 3680 | } |
| 3681 | |
| 3682 | void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type, |
| 3683 | bool is_primitive) { |
| 3684 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3685 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3686 | if (field == NULL) { |
| 3687 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3688 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3689 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3690 | FieldHelper fh(field); |
| 3691 | mirror::Class* field_type_class = fh.GetType(false); |
| 3692 | const RegType* field_type; |
| 3693 | if (field_type_class != nullptr) { |
| 3694 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3695 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3696 | } else { |
| 3697 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 3698 | fh.GetTypeDescriptor(), false); |
| 3699 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3700 | const uint32_t vregA = inst->VRegA_22c(); |
| 3701 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3702 | if (field_type->Equals(insn_type) || |
| 3703 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 3704 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3705 | // expected that read is of the correct primitive type or that int reads are reading |
| 3706 | // floats or long reads are reading doubles |
| 3707 | } else { |
| 3708 | // This is a global failure rather than a class change failure as the instructions and |
| 3709 | // the descriptors for the type should have been consistent within the same file at |
| 3710 | // compile time |
| 3711 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3712 | << " to be of type '" << insn_type |
| 3713 | << "' but found type '" << field_type << "' in get"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3717 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3718 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3719 | << " to be compatible with type '" << insn_type |
| 3720 | << "' but found type '" << field_type |
| 3721 | << "' in get-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3722 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
| 3723 | return; |
| 3724 | } |
| 3725 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3726 | if (!field_type->IsLowHalf()) { |
| 3727 | work_line_->SetRegisterType(vregA, *field_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3728 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3729 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3730 | } |
| 3731 | } |
| 3732 | |
| 3733 | void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type, |
| 3734 | bool is_primitive) { |
| 3735 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3736 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3737 | if (field == NULL) { |
| 3738 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3739 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3740 | } |
| 3741 | const char* descriptor = FieldHelper(field).GetTypeDescriptor(); |
| 3742 | mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3743 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false); |
| 3744 | if (field != NULL) { |
| 3745 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3746 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3747 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3748 | return; |
| 3749 | } |
| 3750 | } |
| 3751 | const uint32_t vregA = inst->VRegA_22c(); |
| 3752 | if (is_primitive) { |
| 3753 | // Primitive field assignability rules are weaker than regular assignability rules |
| 3754 | bool instruction_compatible; |
| 3755 | bool value_compatible; |
| 3756 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3757 | if (field_type.IsIntegralTypes()) { |
| 3758 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 3759 | value_compatible = value_type.IsIntegralTypes(); |
| 3760 | } else if (field_type.IsFloat()) { |
| 3761 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 3762 | value_compatible = value_type.IsFloatTypes(); |
| 3763 | } else if (field_type.IsLong()) { |
| 3764 | instruction_compatible = insn_type.IsLong(); |
| 3765 | value_compatible = value_type.IsLongTypes(); |
| 3766 | } else if (field_type.IsDouble()) { |
| 3767 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 3768 | value_compatible = value_type.IsDoubleTypes(); |
| 3769 | } else { |
| 3770 | instruction_compatible = false; // reference field with primitive store |
| 3771 | value_compatible = false; // unused |
| 3772 | } |
| 3773 | if (!instruction_compatible) { |
| 3774 | // This is a global failure rather than a class change failure as the instructions and |
| 3775 | // the descriptors for the type should have been consistent within the same file at |
| 3776 | // compile time |
| 3777 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3778 | << " to be of type '" << insn_type |
| 3779 | << "' but found type '" << field_type |
| 3780 | << "' in put"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3781 | return; |
| 3782 | } |
| 3783 | if (!value_compatible) { |
| 3784 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3785 | << " of type " << value_type |
| 3786 | << " but expected " << field_type |
| 3787 | << " for store to " << PrettyField(field) << " in put"; |
| 3788 | return; |
| 3789 | } |
| 3790 | } else { |
| 3791 | if (!insn_type.IsAssignableFrom(field_type)) { |
| 3792 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3793 | << " to be compatible with type '" << insn_type |
| 3794 | << "' but found type '" << field_type |
| 3795 | << "' in put-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3796 | return; |
| 3797 | } |
| 3798 | work_line_->VerifyRegisterType(vregA, field_type); |
| 3799 | } |
| 3800 | } |
| 3801 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3802 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3803 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3804 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3805 | return false; |
| 3806 | } |
| 3807 | return true; |
| 3808 | } |
| 3809 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3810 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3811 | bool changed = true; |
| 3812 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 3813 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3814 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3815 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 3816 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 3817 | * 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] | 3818 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3819 | if (!insn_flags_[next_insn].IsReturn()) { |
| 3820 | target_line->CopyFromLine(merge_line); |
| 3821 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3822 | // Verify that the monitor stack is empty on return. |
| 3823 | if (!merge_line->VerifyMonitorStackEmpty()) { |
| 3824 | return false; |
| 3825 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3826 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3827 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 3828 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 3829 | Instruction::Code opcode = ret_inst->Opcode(); |
| 3830 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 3831 | target_line->MarkAllRegistersAsConflicts(); |
| 3832 | } else { |
| 3833 | target_line->CopyFromLine(merge_line); |
| 3834 | if (opcode == Instruction::RETURN_WIDE) { |
| 3835 | target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 3836 | } else { |
| 3837 | target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 3838 | } |
| 3839 | } |
| 3840 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3841 | } else { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3842 | UniquePtr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 3843 | RegisterLine::Create(target_line->NumRegs(), this) : |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3844 | NULL); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3845 | if (gDebugVerify) { |
| 3846 | copy->CopyFromLine(target_line); |
| 3847 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3848 | changed = target_line->MergeRegisters(merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3849 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3850 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3851 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3852 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 3853 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 3854 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
| 3855 | << *copy.get() << " MERGE\n" |
| 3856 | << *merge_line << " ==\n" |
| 3857 | << *target_line << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3858 | } |
| 3859 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3860 | if (changed) { |
| 3861 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3862 | } |
| 3863 | return true; |
| 3864 | } |
| 3865 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 3866 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3867 | return &insn_flags_[work_insn_idx_]; |
| 3868 | } |
| 3869 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3870 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3871 | if (return_type_ == nullptr) { |
| 3872 | if (mirror_method_ != NULL) { |
| 3873 | MethodHelper mh(mirror_method_); |
| 3874 | mirror::Class* return_type_class = mh.GetReturnType(); |
| 3875 | if (return_type_class != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3876 | return_type_ = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 3877 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3878 | } else { |
| 3879 | Thread* self = Thread::Current(); |
| 3880 | DCHECK(self->IsExceptionPending()); |
| 3881 | self->ClearException(); |
| 3882 | } |
| 3883 | } |
| 3884 | if (return_type_ == nullptr) { |
| 3885 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 3886 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 3887 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 3888 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3889 | return_type_ = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3890 | } |
| 3891 | } |
| 3892 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
| 3895 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3896 | if (declaring_class_ == NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3897 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3898 | const char* descriptor |
| 3899 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3900 | if (mirror_method_ != NULL) { |
| 3901 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3902 | declaring_class_ = ®_types_.FromClass(descriptor, klass, |
| 3903 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3904 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3905 | declaring_class_ = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3906 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3907 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3908 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3909 | } |
| 3910 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3911 | void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3912 | size_t* log2_max_gc_pc) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3913 | size_t local_gc_points = 0; |
| 3914 | size_t max_insn = 0; |
| 3915 | size_t max_ref_reg = -1; |
| 3916 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 3917 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3918 | local_gc_points++; |
| 3919 | max_insn = i; |
| 3920 | RegisterLine* line = reg_table_.GetLine(i); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3921 | max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3922 | } |
| 3923 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3924 | *gc_points = local_gc_points; |
| 3925 | *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1) |
| 3926 | size_t i = 0; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3927 | while ((1U << i) <= max_insn) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3928 | i++; |
| 3929 | } |
| 3930 | *log2_max_gc_pc = i; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3933 | MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() { |
| 3934 | /* |
| 3935 | * Walks over the method code and adds any cast instructions in which |
| 3936 | * the type cast is implicit to a set, which is used in the code generation |
| 3937 | * to elide these casts. |
| 3938 | */ |
| 3939 | if (!failure_messages_.empty()) { |
| 3940 | return NULL; |
| 3941 | } |
| 3942 | UniquePtr<MethodSafeCastSet> mscs; |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3943 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 3944 | const Instruction* end = Instruction::At(code_item_->insns_ + |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 3945 | code_item_->insns_size_in_code_units_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3946 | |
| 3947 | for (; inst < end; inst = inst->Next()) { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3948 | Instruction::Code code = inst->Opcode(); |
| 3949 | if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) { |
| 3950 | uint32_t dex_pc = inst->GetDexPc(code_item_->insns_); |
| 3951 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 3952 | bool is_safe_cast = false; |
| 3953 | if (code == Instruction::CHECK_CAST) { |
| 3954 | const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c())); |
| 3955 | const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
| 3956 | is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type); |
| 3957 | } else { |
| 3958 | const RegType& array_type(line->GetRegisterType(inst->VRegB_23x())); |
| 3959 | // We only know its safe to assign to an array if the array type is precise. For example, |
| 3960 | // an Object[] can have any type of object stored in it, but it may also be assigned a |
| 3961 | // String[] in which case the stores need to be of Strings. |
| 3962 | if (array_type.IsPreciseReference()) { |
| 3963 | const RegType& value_type(line->GetRegisterType(inst->VRegA_23x())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3964 | const RegType& component_type(reg_types_.GetComponentType(array_type, |
| 3965 | class_loader_->get())); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3966 | is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type); |
| 3967 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3968 | } |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3969 | if (is_safe_cast) { |
| 3970 | if (mscs.get() == NULL) { |
| 3971 | mscs.reset(new MethodSafeCastSet()); |
| 3972 | } |
| 3973 | mscs->insert(dex_pc); |
| 3974 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3975 | } |
| 3976 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3977 | return mscs.release(); |
| 3978 | } |
| 3979 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 3980 | MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 3981 | // It is risky to rely on reg_types for sharpening in cases of soft |
| 3982 | // verification, we might end up sharpening to a wrong implementation. Just abort. |
| 3983 | if (!failure_messages_.empty()) { |
| 3984 | return NULL; |
| 3985 | } |
| 3986 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 3987 | UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 3988 | const uint16_t* insns = code_item_->insns_; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 3989 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 3990 | const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 3991 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 3992 | for (; inst < end; inst = inst->Next()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 3993 | bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) || |
| 3994 | (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE); |
| 3995 | bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) || |
| 3996 | (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
| 3997 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 3998 | if (!is_interface && !is_virtual) { |
Dragos Sbirlea | 29e2e7e | 2013-05-22 14:52:11 -0700 | [diff] [blame] | 3999 | continue; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4000 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4001 | // Get reg type for register holding the reference to the object that will be dispatched upon. |
| 4002 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 4003 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 4004 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) || |
| 4005 | (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
| 4006 | const RegType& |
| 4007 | reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c())); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4008 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4009 | if (!reg_type.HasClass()) { |
| 4010 | // We will compute devirtualization information only when we know the Class of the reg type. |
| 4011 | continue; |
| 4012 | } |
| 4013 | mirror::Class* reg_class = reg_type.GetClass(); |
| 4014 | if (reg_class->IsInterface()) { |
| 4015 | // We can't devirtualize when the known type of the register is an interface. |
| 4016 | continue; |
| 4017 | } |
| 4018 | if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) { |
| 4019 | // We can't devirtualize abstract classes except on arrays of abstract classes. |
| 4020 | continue; |
| 4021 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4022 | mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod( |
| 4023 | is_range ? inst->VRegB_3rc() : inst->VRegB_35c()); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 4024 | if (abstract_method == NULL) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4025 | // If the method is not found in the cache this means that it was never found |
| 4026 | // by ResolveMethodAndCheckAccess() called when verifying invoke_*. |
| 4027 | continue; |
| 4028 | } |
| 4029 | // Find the concrete method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4030 | mirror::ArtMethod* concrete_method = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4031 | if (is_interface) { |
| 4032 | concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method); |
| 4033 | } |
| 4034 | if (is_virtual) { |
| 4035 | concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method); |
| 4036 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4037 | if (concrete_method == NULL || concrete_method->IsAbstract()) { |
| 4038 | // In cases where concrete_method is not found, or is abstract, continue to the next invoke. |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4039 | continue; |
| 4040 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4041 | if (reg_type.IsPreciseReference() || concrete_method->IsFinal() || |
| 4042 | concrete_method->GetDeclaringClass()->IsFinal()) { |
| 4043 | // If we knew exactly the class being dispatched upon, or if the target method cannot be |
| 4044 | // overridden record the target to be used in the compiler driver. |
| 4045 | if (pc_to_concrete_method_map.get() == NULL) { |
| 4046 | pc_to_concrete_method_map.reset(new PcToConcreteMethodMap()); |
| 4047 | } |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4048 | MethodReference concrete_ref( |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4049 | concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(), |
| 4050 | concrete_method->GetDexMethodIndex()); |
| 4051 | pc_to_concrete_method_map->Put(dex_pc, concrete_ref); |
| 4052 | } |
Dragos Sbirlea | 29e2e7e | 2013-05-22 14:52:11 -0700 | [diff] [blame] | 4053 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4054 | return pc_to_concrete_method_map.release(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4057 | const std::vector<uint8_t>* MethodVerifier::GenerateLengthPrefixedGcMap() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4058 | size_t num_entries, ref_bitmap_bits, pc_bits; |
| 4059 | ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits); |
| 4060 | // There's a single byte to encode the size of each bitmap |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 4061 | if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4062 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4063 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4064 | << ref_bitmap_bits << " registers"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4065 | return NULL; |
| 4066 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4067 | size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8; |
| 4068 | // There are 2 bytes to encode the number of entries |
| 4069 | if (num_entries >= 65536) { |
| 4070 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4071 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4072 | << num_entries << " entries"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4073 | return NULL; |
| 4074 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4075 | size_t pc_bytes; |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4076 | RegisterMapFormat format; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 4077 | if (pc_bits <= 8) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4078 | format = kRegMapFormatCompact8; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4079 | pc_bytes = 1; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 4080 | } else if (pc_bits <= 16) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4081 | format = kRegMapFormatCompact16; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4082 | pc_bytes = 2; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4083 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4084 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4085 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with " |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4086 | << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)"; |
| 4087 | return NULL; |
| 4088 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4089 | size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4090 | std::vector<uint8_t>* table = new std::vector<uint8_t>; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4091 | if (table == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4092 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4093 | return NULL; |
| 4094 | } |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4095 | table->reserve(table_size + 4); // table_size plus the length prefix |
| 4096 | // Write table size |
| 4097 | table->push_back((table_size & 0xff000000) >> 24); |
| 4098 | table->push_back((table_size & 0x00ff0000) >> 16); |
| 4099 | table->push_back((table_size & 0x0000ff00) >> 8); |
| 4100 | table->push_back((table_size & 0x000000ff) >> 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4101 | // Write table header |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 4102 | table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) & |
| 4103 | ~DexPcToReferenceMap::kRegMapFormatMask)); |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 4104 | table->push_back(ref_bitmap_bytes & 0xFF); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4105 | table->push_back(num_entries & 0xFF); |
| 4106 | table->push_back((num_entries >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4107 | // Write table data |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4108 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4109 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4110 | table->push_back(i & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4111 | if (pc_bytes == 2) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4112 | table->push_back((i >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4113 | } |
| 4114 | RegisterLine* line = reg_table_.GetLine(i); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4115 | line->WriteReferenceBitMap(*table, ref_bitmap_bytes); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4116 | } |
| 4117 | } |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4118 | DCHECK_EQ(table->size(), table_size + 4); // table_size plus the length prefix |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4119 | return table; |
| 4120 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4121 | |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4122 | void MethodVerifier::VerifyLengthPrefixedGcMap(const std::vector<uint8_t>& data) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4123 | // Check that for every GC point there is a map entry, there aren't entries for non-GC points, |
| 4124 | // that the table data is well formed and all references are marked (or not) in the bitmap |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4125 | DCHECK_GE(data.size(), 4u); |
| 4126 | size_t table_size = data.size() - 4u; |
| 4127 | DCHECK_EQ(table_size, static_cast<size_t>((data[0] << 24) | (data[1] << 16) | |
| 4128 | (data[2] << 8) | (data[3] << 0))); |
| 4129 | DexPcToReferenceMap map(&data[4], table_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4130 | size_t map_index = 0; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4131 | for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4132 | const uint8_t* reg_bitmap = map.FindBitMap(i, false); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4133 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4134 | CHECK_LT(map_index, map.NumEntries()); |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 4135 | CHECK_EQ(map.GetDexPc(map_index), i); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4136 | CHECK_EQ(map.GetBitMap(map_index), reg_bitmap); |
| 4137 | map_index++; |
| 4138 | RegisterLine* line = reg_table_.GetLine(i); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4139 | for (size_t j = 0; j < code_item_->registers_size_; j++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 4140 | if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4141 | CHECK_LT(j / 8, map.RegWidth()); |
| 4142 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1); |
| 4143 | } else if ((j / 8) < map.RegWidth()) { |
| 4144 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0); |
| 4145 | } else { |
| 4146 | // If a register doesn't contain a reference then the bitmap may be shorter than the line |
| 4147 | } |
| 4148 | } |
| 4149 | } else { |
| 4150 | CHECK(reg_bitmap == NULL); |
| 4151 | } |
| 4152 | } |
| 4153 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4154 | |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4155 | void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4156 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4157 | { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4158 | WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4159 | DexGcMapTable::iterator it = dex_gc_maps_->find(ref); |
| 4160 | if (it != dex_gc_maps_->end()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4161 | delete it->second; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4162 | dex_gc_maps_->erase(it); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4163 | } |
Vladimir Marko | c255e97 | 2013-11-19 11:21:24 +0000 | [diff] [blame] | 4164 | dex_gc_maps_->Put(ref, gc_map); |
Brian Carlstrom | 73a15f4 | 2012-01-17 18:14:39 -0800 | [diff] [blame] | 4165 | } |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 4166 | DCHECK(GetDexGcMap(ref) != NULL); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4169 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4170 | void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4171 | DCHECK(Runtime::Current()->IsCompiler()); |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4172 | WriterMutexLock mu(Thread::Current(), *safecast_map_lock_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4173 | SafeCastMap::iterator it = safecast_map_->find(ref); |
| 4174 | if (it != safecast_map_->end()) { |
| 4175 | delete it->second; |
| 4176 | safecast_map_->erase(it); |
| 4177 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4178 | safecast_map_->Put(ref, cast_set); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4179 | DCHECK(safecast_map_->find(ref) != safecast_map_->end()); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4182 | bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4183 | DCHECK(Runtime::Current()->IsCompiler()); |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4184 | ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4185 | SafeCastMap::const_iterator it = safecast_map_->find(ref); |
| 4186 | if (it == safecast_map_->end()) { |
| 4187 | return false; |
| 4188 | } |
| 4189 | |
| 4190 | // Look up the cast address in the set of safe casts |
| 4191 | MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc); |
| 4192 | return cast_it != it->second->end(); |
| 4193 | } |
| 4194 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4195 | const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4196 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4197 | ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_); |
| 4198 | DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref); |
Ian Rogers | 0f40ac3 | 2013-08-13 22:10:30 -0700 | [diff] [blame] | 4199 | CHECK(it != dex_gc_maps_->end()) |
| 4200 | << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file); |
| 4201 | CHECK(it->second != NULL); |
| 4202 | return it->second; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4203 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4204 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4205 | void MethodVerifier::SetDevirtMap(MethodReference ref, |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4206 | const PcToConcreteMethodMap* devirt_map) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4207 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4208 | WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4209 | DevirtualizationMapTable::iterator it = devirt_maps_->find(ref); |
| 4210 | if (it != devirt_maps_->end()) { |
| 4211 | delete it->second; |
| 4212 | devirt_maps_->erase(it); |
| 4213 | } |
| 4214 | |
| 4215 | devirt_maps_->Put(ref, devirt_map); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4216 | DCHECK(devirt_maps_->find(ref) != devirt_maps_->end()); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4219 | const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref, |
Ian Rogers | e3cd2f0 | 2013-05-24 15:32:56 -0700 | [diff] [blame] | 4220 | uint32_t dex_pc) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4221 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4222 | ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4223 | DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref); |
| 4224 | if (it == devirt_maps_->end()) { |
| 4225 | return NULL; |
| 4226 | } |
| 4227 | |
| 4228 | // Look up the PC in the map, get the concrete method to execute and return its reference. |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 4229 | MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method |
| 4230 | = it->second->find(dex_pc); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 4231 | if (pc_to_concrete_method != it->second->end()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4232 | return &(pc_to_concrete_method->second); |
| 4233 | } else { |
| 4234 | return NULL; |
| 4235 | } |
| 4236 | } |
| 4237 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4238 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4239 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 4240 | std::vector<int32_t> result; |
| 4241 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
| 4242 | const RegType& type = line->GetRegisterType(i); |
| 4243 | if (type.IsConstant()) { |
| 4244 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
| 4245 | result.push_back(type.ConstantValue()); |
| 4246 | } else if (type.IsConstantLo()) { |
| 4247 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
| 4248 | result.push_back(type.ConstantValueLo()); |
| 4249 | } else if (type.IsConstantHi()) { |
| 4250 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
| 4251 | result.push_back(type.ConstantValueHi()); |
| 4252 | } else if (type.IsIntegralTypes()) { |
| 4253 | result.push_back(kIntVReg); |
| 4254 | result.push_back(0); |
| 4255 | } else if (type.IsFloat()) { |
| 4256 | result.push_back(kFloatVReg); |
| 4257 | result.push_back(0); |
| 4258 | } else if (type.IsLong()) { |
| 4259 | result.push_back(kLongLoVReg); |
| 4260 | result.push_back(0); |
| 4261 | result.push_back(kLongHiVReg); |
| 4262 | result.push_back(0); |
| 4263 | ++i; |
| 4264 | } else if (type.IsDouble()) { |
| 4265 | result.push_back(kDoubleLoVReg); |
| 4266 | result.push_back(0); |
| 4267 | result.push_back(kDoubleHiVReg); |
| 4268 | result.push_back(0); |
| 4269 | ++i; |
| 4270 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 4271 | result.push_back(kUndefined); |
| 4272 | result.push_back(0); |
| 4273 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4274 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4275 | result.push_back(kReferenceVReg); |
| 4276 | result.push_back(0); |
| 4277 | } |
| 4278 | } |
| 4279 | return result; |
| 4280 | } |
| 4281 | |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 4282 | bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref, |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4283 | const uint32_t access_flags) { |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 4284 | #ifdef ART_SEA_IR_MODE |
| 4285 | bool use_sea = Runtime::Current()->IsSeaIRMode(); |
| 4286 | use_sea = use_sea && (std::string::npos != PrettyMethod( |
| 4287 | method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci")); |
| 4288 | if (use_sea) return true; |
| 4289 | #endif |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4290 | // Don't compile class initializers, ever. |
| 4291 | if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) { |
| 4292 | return false; |
| 4293 | } |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 4294 | return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4295 | } |
| 4296 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4297 | ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4298 | MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4299 | |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4300 | ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL; |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4301 | MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL; |
| 4302 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4303 | ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4304 | MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL; |
| 4305 | |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4306 | ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4307 | MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL; |
| 4308 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4309 | void MethodVerifier::Init() { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4310 | if (Runtime::Current()->IsCompiler()) { |
| 4311 | dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock"); |
| 4312 | Thread* self = Thread::Current(); |
| 4313 | { |
| 4314 | WriterMutexLock mu(self, *dex_gc_maps_lock_); |
| 4315 | dex_gc_maps_ = new MethodVerifier::DexGcMapTable; |
| 4316 | } |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4317 | |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4318 | safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock"); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4319 | { |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4320 | WriterMutexLock mu(self, *safecast_map_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4321 | safecast_map_ = new MethodVerifier::SafeCastMap(); |
| 4322 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4323 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4324 | devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock"); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4325 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4326 | { |
| 4327 | WriterMutexLock mu(self, *devirt_maps_lock_); |
| 4328 | devirt_maps_ = new MethodVerifier::DevirtualizationMapTable(); |
| 4329 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4330 | |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4331 | rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock"); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4332 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4333 | WriterMutexLock mu(self, *rejected_classes_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4334 | rejected_classes_ = new MethodVerifier::RejectedClassesTable; |
| 4335 | } |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4336 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4337 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4338 | } |
| 4339 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4340 | void MethodVerifier::Shutdown() { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4341 | if (Runtime::Current()->IsCompiler()) { |
| 4342 | Thread* self = Thread::Current(); |
| 4343 | { |
| 4344 | WriterMutexLock mu(self, *dex_gc_maps_lock_); |
| 4345 | STLDeleteValues(dex_gc_maps_); |
| 4346 | delete dex_gc_maps_; |
| 4347 | dex_gc_maps_ = NULL; |
| 4348 | } |
| 4349 | delete dex_gc_maps_lock_; |
| 4350 | dex_gc_maps_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4351 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4352 | { |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4353 | WriterMutexLock mu(self, *safecast_map_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4354 | STLDeleteValues(safecast_map_); |
| 4355 | delete safecast_map_; |
| 4356 | safecast_map_ = NULL; |
| 4357 | } |
| 4358 | delete safecast_map_lock_; |
| 4359 | safecast_map_lock_ = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4360 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4361 | { |
| 4362 | WriterMutexLock mu(self, *devirt_maps_lock_); |
| 4363 | STLDeleteValues(devirt_maps_); |
| 4364 | delete devirt_maps_; |
| 4365 | devirt_maps_ = NULL; |
| 4366 | } |
| 4367 | delete devirt_maps_lock_; |
| 4368 | devirt_maps_lock_ = NULL; |
| 4369 | |
| 4370 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4371 | WriterMutexLock mu(self, *rejected_classes_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4372 | delete rejected_classes_; |
| 4373 | rejected_classes_ = NULL; |
| 4374 | } |
| 4375 | delete rejected_classes_lock_; |
| 4376 | rejected_classes_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4377 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4378 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4379 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4380 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4381 | void MethodVerifier::AddRejectedClass(ClassReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4382 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4383 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4384 | WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4385 | rejected_classes_->insert(ref); |
| 4386 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4387 | CHECK(IsClassRejected(ref)); |
| 4388 | } |
| 4389 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4390 | bool MethodVerifier::IsClassRejected(ClassReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4391 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4392 | ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_); |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4393 | return (rejected_classes_->find(ref) != rejected_classes_->end()); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4394 | } |
| 4395 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4396 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4397 | } // namespace art |