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 | |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1032 | static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap( |
| 1033 | const std::vector<uint8_t>& gc_map) { |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1034 | std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 1035 | length_prefixed_gc_map->reserve(gc_map.size() + 4); |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 1036 | length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24); |
| 1037 | length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16); |
| 1038 | length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8); |
| 1039 | length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0); |
| 1040 | length_prefixed_gc_map->insert(length_prefixed_gc_map->end(), |
| 1041 | gc_map.begin(), |
| 1042 | gc_map.end()); |
| 1043 | DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size()); |
| 1044 | DCHECK_EQ(gc_map.size(), |
| 1045 | static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) | |
| 1046 | (length_prefixed_gc_map->at(1) << 16) | |
| 1047 | (length_prefixed_gc_map->at(2) << 8) | |
| 1048 | (length_prefixed_gc_map->at(3) << 0))); |
| 1049 | return length_prefixed_gc_map; |
| 1050 | } |
| 1051 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1052 | bool MethodVerifier::VerifyCodeFlow() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1053 | uint16_t registers_size = code_item_->registers_size_; |
| 1054 | uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1055 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1056 | if (registers_size * insns_size > 4*1024*1024) { |
buzbee | 4922ef9 | 2012-02-24 14:32:20 -0800 | [diff] [blame] | 1057 | LOG(WARNING) << "warning: method is huge (regs=" << registers_size |
| 1058 | << " insns_size=" << insns_size << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1059 | } |
| 1060 | /* Create and initialize table holding register status */ |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1061 | reg_table_.Init(kTrackCompilerInterestPoints, |
| 1062 | insn_flags_.get(), |
| 1063 | insns_size, |
| 1064 | registers_size, |
| 1065 | this); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1066 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1067 | |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1068 | work_line_.reset(RegisterLine::Create(registers_size, this)); |
| 1069 | saved_line_.reset(RegisterLine::Create(registers_size, this)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1070 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1071 | /* Initialize register types of method arguments. */ |
| 1072 | if (!SetTypesFromSignature()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1073 | DCHECK_NE(failures_.size(), 0U); |
| 1074 | std::string prepend("Bad signature in "); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1075 | prepend += PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1076 | PrependToLastFailMessage(prepend); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1077 | return false; |
| 1078 | } |
| 1079 | /* Perform code flow verification. */ |
| 1080 | if (!CodeFlowVerifyMethod()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1081 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1082 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1085 | // Compute information for compiler. |
| 1086 | if (Runtime::Current()->IsCompiler()) { |
| 1087 | MethodReference ref(dex_file_, dex_method_idx_); |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 1088 | bool compile = IsCandidateForCompilation(ref, method_access_flags_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1089 | if (compile) { |
| 1090 | /* Generate a register map and add it to the method. */ |
| 1091 | UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap()); |
| 1092 | if (map.get() == NULL) { |
| 1093 | DCHECK_NE(failures_.size(), 0U); |
| 1094 | return false; // Not a real failure, but a failure to encode |
| 1095 | } |
| 1096 | if (kIsDebugBuild) { |
| 1097 | VerifyGcMap(*map); |
| 1098 | } |
| 1099 | const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get())); |
| 1100 | verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map); |
| 1101 | } |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1102 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1103 | if (has_check_casts_) { |
| 1104 | MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet(); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1105 | if (method_to_safe_casts != NULL) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1106 | SetSafeCastMap(ref, method_to_safe_casts); |
| 1107 | } |
| 1108 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1109 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1110 | if (has_virtual_or_interface_invokes_) { |
| 1111 | MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap(); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1112 | if (pc_to_concrete_method != NULL) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 1113 | SetDevirtMap(ref, pc_to_concrete_method); |
| 1114 | } |
| 1115 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 1116 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1117 | return true; |
| 1118 | } |
| 1119 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1120 | std::ostream& MethodVerifier::DumpFailures(std::ostream& os) { |
| 1121 | DCHECK_EQ(failures_.size(), failure_messages_.size()); |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 1122 | if (VLOG_IS_ON(verifier)) { |
| 1123 | for (size_t i = 0; i < failures_.size(); ++i) { |
| 1124 | os << failure_messages_[i]->str() << "\n"; |
| 1125 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1126 | } |
| 1127 | return os; |
| 1128 | } |
| 1129 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1130 | extern "C" void MethodVerifierGdbDump(MethodVerifier* v) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1131 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1132 | v->Dump(std::cerr); |
| 1133 | } |
| 1134 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1135 | void MethodVerifier::Dump(std::ostream& os) { |
jeffhao | f56197c | 2012-03-05 18:01:54 -0800 | [diff] [blame] | 1136 | if (code_item_ == NULL) { |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1137 | os << "Native method\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1138 | return; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1139 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1140 | { |
| 1141 | os << "Register Types:\n"; |
| 1142 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1143 | std::ostream indent_os(&indent_filter); |
| 1144 | reg_types_.Dump(indent_os); |
| 1145 | } |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1146 | os << "Dumping instructions and register lines:\n"; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1147 | Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); |
| 1148 | std::ostream indent_os(&indent_filter); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1149 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 1150 | for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_; |
| 1151 | dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1152 | RegisterLine* reg_line = reg_table_.GetLine(dex_pc); |
| 1153 | if (reg_line != NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1154 | indent_os << reg_line->Dump() << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1155 | } |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 1156 | indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " "; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1157 | const bool kDumpHexOfInstruction = false; |
| 1158 | if (kDumpHexOfInstruction) { |
| 1159 | indent_os << inst->DumpHex(5) << " "; |
| 1160 | } |
| 1161 | indent_os << inst->DumpString(dex_file_) << "\n"; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1162 | inst = inst->Next(); |
| 1163 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1166 | static bool IsPrimitiveDescriptor(char descriptor) { |
| 1167 | switch (descriptor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1168 | case 'I': |
| 1169 | case 'C': |
| 1170 | case 'S': |
| 1171 | case 'B': |
| 1172 | case 'Z': |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1173 | case 'F': |
| 1174 | case 'D': |
| 1175 | case 'J': |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1176 | return true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1177 | default: |
| 1178 | return false; |
| 1179 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1182 | bool MethodVerifier::SetTypesFromSignature() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1183 | RegisterLine* reg_line = reg_table_.GetLine(0); |
| 1184 | int arg_start = code_item_->registers_size_ - code_item_->ins_size_; |
| 1185 | size_t expected_args = code_item_->ins_size_; /* long/double count as two */ |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1186 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1187 | DCHECK_GE(arg_start, 0); /* should have been verified earlier */ |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1188 | // Include the "this" pointer. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1189 | size_t cur_arg = 0; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1190 | if (!IsStatic()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1191 | // If this is a constructor for a class other than java.lang.Object, mark the first ("this") |
| 1192 | // argument as uninitialized. This restricts field access until the superclass constructor is |
| 1193 | // called. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1194 | const RegType& declaring_class = GetDeclaringClass(); |
| 1195 | if (IsConstructor() && !declaring_class.IsJavaLangObject()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1196 | reg_line->SetRegisterType(arg_start + cur_arg, |
| 1197 | reg_types_.UninitializedThisArgument(declaring_class)); |
| 1198 | } else { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1199 | reg_line->SetRegisterType(arg_start + cur_arg, declaring_class); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1200 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1201 | cur_arg++; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1204 | const DexFile::ProtoId& proto_id = |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1205 | dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_)); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1206 | DexFileParameterIterator iterator(*dex_file_, proto_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1207 | |
| 1208 | for (; iterator.HasNext(); iterator.Next()) { |
| 1209 | const char* descriptor = iterator.GetDescriptor(); |
| 1210 | if (descriptor == NULL) { |
| 1211 | LOG(FATAL) << "Null descriptor"; |
| 1212 | } |
| 1213 | if (cur_arg >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1214 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1215 | << " args, found more (" << descriptor << ")"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1216 | return false; |
| 1217 | } |
| 1218 | switch (descriptor[0]) { |
| 1219 | case 'L': |
| 1220 | case '[': |
| 1221 | // We assume that reference arguments are initialized. The only way it could be otherwise |
| 1222 | // (assuming the caller was verified) is if the current method is <init>, but in that case |
| 1223 | // it's effectively considered initialized the instant we reach here (in the sense that we |
| 1224 | // can return without doing anything or call virtual methods). |
| 1225 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 1226 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 1227 | false); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 1228 | reg_line->SetRegisterType(arg_start + cur_arg, reg_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1229 | } |
| 1230 | break; |
| 1231 | case 'Z': |
| 1232 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean()); |
| 1233 | break; |
| 1234 | case 'C': |
| 1235 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char()); |
| 1236 | break; |
| 1237 | case 'B': |
| 1238 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte()); |
| 1239 | break; |
| 1240 | case 'I': |
| 1241 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer()); |
| 1242 | break; |
| 1243 | case 'S': |
| 1244 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short()); |
| 1245 | break; |
| 1246 | case 'F': |
| 1247 | reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float()); |
| 1248 | break; |
| 1249 | case 'J': |
| 1250 | case 'D': { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1251 | const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo(); |
| 1252 | const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi(); |
| 1253 | reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1254 | cur_arg++; |
| 1255 | break; |
| 1256 | } |
| 1257 | default: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1258 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" |
| 1259 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | cur_arg++; |
| 1263 | } |
| 1264 | if (cur_arg != expected_args) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1265 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args |
| 1266 | << " arguments, found " << cur_arg; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
| 1269 | const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id); |
| 1270 | // Validate return type. We don't do the type lookup; just want to make sure that it has the right |
| 1271 | // format. Only major difference from the method argument format is that 'V' is supported. |
| 1272 | bool result; |
| 1273 | if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') { |
| 1274 | result = descriptor[1] == '\0'; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1275 | } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1276 | size_t i = 0; |
| 1277 | do { |
| 1278 | i++; |
| 1279 | } while (descriptor[i] == '['); // process leading [ |
| 1280 | if (descriptor[i] == 'L') { // object array |
| 1281 | do { |
| 1282 | i++; // find closing ; |
| 1283 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1284 | result = descriptor[i] == ';'; |
| 1285 | } else { // primitive array |
| 1286 | result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0'; |
| 1287 | } |
| 1288 | } else if (descriptor[0] == 'L') { |
| 1289 | // could be more thorough here, but shouldn't be required |
| 1290 | size_t i = 0; |
| 1291 | do { |
| 1292 | i++; |
| 1293 | } while (descriptor[i] != ';' && descriptor[i] != '\0'); |
| 1294 | result = descriptor[i] == ';'; |
| 1295 | } else { |
| 1296 | result = false; |
| 1297 | } |
| 1298 | if (!result) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1299 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '" |
| 1300 | << descriptor << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1301 | } |
| 1302 | return result; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1305 | bool MethodVerifier::CodeFlowVerifyMethod() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1306 | const uint16_t* insns = code_item_->insns_; |
| 1307 | const uint32_t insns_size = code_item_->insns_size_in_code_units_; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1308 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1309 | /* Begin by marking the first instruction as "changed". */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1310 | insn_flags_[0].SetChanged(); |
| 1311 | uint32_t start_guess = 0; |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1312 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1313 | /* Continue until no instructions are marked "changed". */ |
| 1314 | while (true) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1315 | // Find the first marked one. Use "start_guess" as a way to find one quickly. |
| 1316 | uint32_t insn_idx = start_guess; |
| 1317 | for (; insn_idx < insns_size; insn_idx++) { |
| 1318 | if (insn_flags_[insn_idx].IsChanged()) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1319 | break; |
| 1320 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1321 | if (insn_idx == insns_size) { |
| 1322 | if (start_guess != 0) { |
| 1323 | /* try again, starting from the top */ |
| 1324 | start_guess = 0; |
| 1325 | continue; |
| 1326 | } else { |
| 1327 | /* all flags are clear */ |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1331 | // We carry the working set of registers from instruction to instruction. If this address can |
| 1332 | // be the target of a branch (or throw) instruction, or if we're skipping around chasing |
| 1333 | // "changed" flags, we need to load the set of registers from the table. |
| 1334 | // Because we always prefer to continue on to the next instruction, we should never have a |
| 1335 | // situation where we have a stray "changed" flag set on an instruction that isn't a branch |
| 1336 | // target. |
| 1337 | work_insn_idx_ = insn_idx; |
| 1338 | if (insn_flags_[insn_idx].IsBranchTarget()) { |
| 1339 | work_line_->CopyFromLine(reg_table_.GetLine(insn_idx)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1340 | } else { |
| 1341 | #ifndef NDEBUG |
| 1342 | /* |
| 1343 | * Sanity check: retrieve the stored register line (assuming |
| 1344 | * a full table) and make sure it actually matches. |
| 1345 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1346 | RegisterLine* register_line = reg_table_.GetLine(insn_idx); |
| 1347 | if (register_line != NULL) { |
| 1348 | if (work_line_->CompareLine(register_line) != 0) { |
| 1349 | Dump(std::cout); |
| 1350 | std::cout << info_messages_.str(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1351 | LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_) |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1352 | << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n" |
| 1353 | << " work_line=" << *work_line_ << "\n" |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 1354 | << " expected=" << *register_line; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1355 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1356 | } |
| 1357 | #endif |
| 1358 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1359 | if (!CodeFlowVerifyInstruction(&start_guess)) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1360 | std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_)); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1361 | prepend += " failed to verify: "; |
| 1362 | PrependToLastFailMessage(prepend); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1363 | return false; |
| 1364 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1365 | /* Clear "changed" and mark as visited. */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1366 | insn_flags_[insn_idx].SetVisited(); |
| 1367 | insn_flags_[insn_idx].ClearChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1368 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1369 | |
Ian Rogers | 1c849e5 | 2012-06-28 14:00:33 -0700 | [diff] [blame] | 1370 | if (gDebugVerify) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1371 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1372 | * Scan for dead code. There's nothing "evil" about dead code |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1373 | * (besides the wasted space), but it indicates a flaw somewhere |
| 1374 | * down the line, possibly in the verifier. |
| 1375 | * |
| 1376 | * If we've substituted "always throw" instructions into the stream, |
| 1377 | * we are almost certainly going to have some dead code. |
| 1378 | */ |
| 1379 | int dead_start = -1; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1380 | uint32_t insn_idx = 0; |
| 1381 | for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1382 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1383 | * Switch-statement data doesn't get "visited" by scanner. It |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1384 | * may or may not be preceded by a padding NOP (for alignment). |
| 1385 | */ |
| 1386 | if (insns[insn_idx] == Instruction::kPackedSwitchSignature || |
| 1387 | insns[insn_idx] == Instruction::kSparseSwitchSignature || |
| 1388 | insns[insn_idx] == Instruction::kArrayDataSignature || |
Elliott Hughes | 380aaa7 | 2012-07-09 14:33:15 -0700 | [diff] [blame] | 1389 | (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) && |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1390 | (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature || |
| 1391 | insns[insn_idx + 1] == Instruction::kSparseSwitchSignature || |
| 1392 | insns[insn_idx + 1] == Instruction::kArrayDataSignature))) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1393 | insn_flags_[insn_idx].SetVisited(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1396 | if (!insn_flags_[insn_idx].IsVisited()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1397 | if (dead_start < 0) |
| 1398 | dead_start = insn_idx; |
| 1399 | } else if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1400 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1401 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1402 | dead_start = -1; |
| 1403 | } |
| 1404 | } |
| 1405 | if (dead_start >= 0) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1406 | LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) |
| 1407 | << "-" << reinterpret_cast<void*>(insn_idx - 1); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1408 | } |
Ian Rogers | c9e463c | 2013-06-05 16:52:26 -0700 | [diff] [blame] | 1409 | // To dump the state of the verify after a method, do something like: |
| 1410 | // if (PrettyMethod(dex_method_idx_, *dex_file_) == |
| 1411 | // "boolean java.lang.String.equals(java.lang.Object)") { |
| 1412 | // LOG(INFO) << info_messages_.str(); |
| 1413 | // } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 1414 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1415 | return true; |
| 1416 | } |
| 1417 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1418 | bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1419 | // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about. |
| 1420 | // We want the state _before_ the instruction, for the case where the dex pc we're |
| 1421 | // interested in is itself a monitor-enter instruction (which is a likely place |
| 1422 | // for a thread to be suspended). |
| 1423 | if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1424 | 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] | 1425 | for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) { |
| 1426 | monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i)); |
| 1427 | } |
| 1428 | } |
| 1429 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * Once we finish decoding the instruction, we need to figure out where |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1432 | * we can go from here. There are three possible ways to transfer |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1433 | * control to another statement: |
| 1434 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1435 | * (1) Continue to the next instruction. Applies to all but |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1436 | * unconditional branches, method returns, and exception throws. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1437 | * (2) Branch to one or more possible locations. Applies to branches |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1438 | * and switch statements. |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1439 | * (3) Exception handlers. Applies to any instruction that can |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1440 | * throw an exception that is handled by an encompassing "try" |
| 1441 | * block. |
| 1442 | * |
| 1443 | * We can also return, in which case there is no successor instruction |
| 1444 | * from this point. |
| 1445 | * |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1446 | * The behavior can be determined from the opcode flags. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1447 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1448 | const uint16_t* insns = code_item_->insns_ + work_insn_idx_; |
| 1449 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | a75a013 | 2012-09-28 11:41:42 -0700 | [diff] [blame] | 1450 | int opcode_flags = Instruction::FlagsOf(inst->Opcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1451 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1452 | int32_t branch_target = 0; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1453 | bool just_set_result = false; |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 1454 | if (gDebugVerify) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1455 | // Generate processing back trace to debug verifier |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 1456 | LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n" |
| 1457 | << *work_line_.get() << "\n"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1458 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1459 | |
| 1460 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1461 | * Make a copy of the previous register state. If the instruction |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1462 | * can throw an exception, we will copy/merge this into the "catch" |
| 1463 | * address rather than work_line, because we don't want the result |
| 1464 | * from the "successful" code path (e.g. a check-cast that "improves" |
| 1465 | * a type) to be visible to the exception handler. |
| 1466 | */ |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 1467 | if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1468 | saved_line_->CopyFromLine(work_line_.get()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1469 | } else { |
| 1470 | #ifndef NDEBUG |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1471 | saved_line_->FillWithGarbage(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1472 | #endif |
| 1473 | } |
| 1474 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 1475 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1476 | // We need to ensure the work line is consistent while performing validation. When we spot a |
| 1477 | // peephole pattern we compute a new line for either the fallthrough instruction or the |
| 1478 | // branch target. |
| 1479 | UniquePtr<RegisterLine> branch_line; |
| 1480 | UniquePtr<RegisterLine> fallthrough_line; |
| 1481 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1482 | switch (inst->Opcode()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1483 | case Instruction::NOP: |
| 1484 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1485 | * A "pure" NOP has no effect on anything. Data tables start with |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1486 | * a signature that looks like a NOP; if we see one of these in |
| 1487 | * the course of executing code then we have a problem. |
| 1488 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1489 | if (inst->VRegA_10x() != 0) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1490 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1491 | } |
| 1492 | break; |
| 1493 | |
| 1494 | case Instruction::MOVE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1495 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr); |
| 1496 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1497 | case Instruction::MOVE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1498 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr); |
| 1499 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1500 | case Instruction::MOVE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1501 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1502 | break; |
| 1503 | case Instruction::MOVE_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1504 | work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x()); |
| 1505 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1506 | case Instruction::MOVE_WIDE_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1507 | work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x()); |
| 1508 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1509 | case Instruction::MOVE_WIDE_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1510 | work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1511 | break; |
| 1512 | case Instruction::MOVE_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1513 | work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef); |
| 1514 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1515 | case Instruction::MOVE_OBJECT_FROM16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1516 | work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef); |
| 1517 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1518 | case Instruction::MOVE_OBJECT_16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1519 | work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1520 | break; |
| 1521 | |
| 1522 | /* |
| 1523 | * The move-result instructions copy data out of a "pseudo-register" |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1524 | * with the results from the last method invocation. In practice we |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1525 | * might want to hold the result in an actual CPU register, so the |
| 1526 | * Dalvik spec requires that these only appear immediately after an |
| 1527 | * invoke or filled-new-array. |
| 1528 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1529 | * These calls invalidate the "result" register. (This is now |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1530 | * redundant with the reset done below, but it can make the debug info |
| 1531 | * easier to read in some cases.) |
| 1532 | */ |
| 1533 | case Instruction::MOVE_RESULT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1534 | work_line_->CopyResultRegister1(inst->VRegA_11x(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1535 | break; |
| 1536 | case Instruction::MOVE_RESULT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1537 | work_line_->CopyResultRegister2(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1538 | break; |
| 1539 | case Instruction::MOVE_RESULT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1540 | work_line_->CopyResultRegister1(inst->VRegA_11x(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1541 | break; |
| 1542 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1543 | case Instruction::MOVE_EXCEPTION: { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1544 | /* |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 1545 | * This statement can only appear as the first instruction in an exception handler. We verify |
| 1546 | * that as part of extracting the exception type from the catch block list. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1547 | */ |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1548 | const RegType& res_type = GetCaughtExceptionType(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1549 | work_line_->SetRegisterType(inst->VRegA_11x(), res_type); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1550 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1551 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1552 | case Instruction::RETURN_VOID: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1553 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
| 1554 | if (!GetMethodReturnType().IsConflict()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1555 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1556 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1557 | } |
| 1558 | break; |
| 1559 | case Instruction::RETURN: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1560 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1561 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1562 | const RegType& return_type = GetMethodReturnType(); |
| 1563 | if (!return_type.IsCategory1Types()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1564 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " |
| 1565 | << return_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1566 | } else { |
| 1567 | // Compilers may generate synthetic functions that write byte values into boolean fields. |
| 1568 | // 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] | 1569 | const uint32_t vregA = inst->VRegA_11x(); |
| 1570 | const RegType& src_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1571 | bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) || |
| 1572 | ((return_type.IsBoolean() || return_type.IsByte() || |
| 1573 | return_type.IsShort() || return_type.IsChar()) && |
| 1574 | src_type.IsInteger())); |
| 1575 | /* check the register contents */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1576 | bool success = |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1577 | work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1578 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1579 | AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1580 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | break; |
| 1584 | case Instruction::RETURN_WIDE: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1585 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1586 | /* check the method signature */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1587 | const RegType& return_type = GetMethodReturnType(); |
| 1588 | if (!return_type.IsCategory2Types()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1589 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1590 | } else { |
| 1591 | /* check the register contents */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1592 | const uint32_t vregA = inst->VRegA_11x(); |
| 1593 | bool success = work_line_->VerifyRegisterType(vregA, return_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1594 | if (!success) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1595 | AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1596 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | break; |
| 1600 | case Instruction::RETURN_OBJECT: |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1601 | if (!IsConstructor() || work_line_->CheckConstructorReturn()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1602 | const RegType& return_type = GetMethodReturnType(); |
| 1603 | if (!return_type.IsReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1604 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1605 | } else { |
| 1606 | /* return_type is the *expected* return type, not register value */ |
| 1607 | DCHECK(!return_type.IsZero()); |
| 1608 | DCHECK(!return_type.IsUninitializedReference()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1609 | const uint32_t vregA = inst->VRegA_11x(); |
| 1610 | const RegType& reg_type = work_line_->GetRegisterType(vregA); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1611 | // Disallow returning uninitialized values and verify that the reference in vAA is an |
| 1612 | // instance of the "return_type" |
| 1613 | if (reg_type.IsUninitializedTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1614 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" |
| 1615 | << reg_type << "'"; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1616 | } else if (!return_type.IsAssignableFrom(reg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1617 | if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) { |
| 1618 | Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type |
| 1619 | << "' or '" << reg_type << "'"; |
| 1620 | } else { |
| 1621 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type |
| 1622 | << "', but expected from declaration '" << return_type << "'"; |
| 1623 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | break; |
| 1628 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1629 | /* could be boolean, int, float, or a null reference */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1630 | case Instruction::CONST_4: { |
| 1631 | int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28; |
| 1632 | work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1633 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1634 | } |
| 1635 | case Instruction::CONST_16: { |
| 1636 | int16_t val = static_cast<int16_t>(inst->VRegB_21s()); |
| 1637 | work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 1638 | break; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1639 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1640 | case Instruction::CONST: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1641 | work_line_->SetRegisterType(inst->VRegA_31i(), |
| 1642 | reg_types_.FromCat1Const(inst->VRegB_31i(), true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1643 | break; |
| 1644 | case Instruction::CONST_HIGH16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1645 | work_line_->SetRegisterType(inst->VRegA_21h(), |
| 1646 | reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1647 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1648 | /* could be long or double; resolved upon use */ |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1649 | case Instruction::CONST_WIDE_16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1650 | int64_t val = static_cast<int16_t>(inst->VRegB_21s()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1651 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1652 | 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] | 1653 | work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1654 | break; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1655 | } |
| 1656 | case Instruction::CONST_WIDE_32: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1657 | int64_t val = static_cast<int32_t>(inst->VRegB_31i()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1658 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1659 | 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] | 1660 | work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | case Instruction::CONST_WIDE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1664 | int64_t val = inst->VRegB_51l(); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1665 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1666 | 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] | 1667 | work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1668 | break; |
| 1669 | } |
| 1670 | case Instruction::CONST_WIDE_HIGH16: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1671 | int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1672 | const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true); |
| 1673 | 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] | 1674 | work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1675 | break; |
| 1676 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1677 | case Instruction::CONST_STRING: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1678 | work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString()); |
| 1679 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1680 | case Instruction::CONST_STRING_JUMBO: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1681 | work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1682 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1683 | case Instruction::CONST_CLASS: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1684 | // Get type from instruction if unresolved then we need an access check |
| 1685 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1686 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1687 | // 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] | 1688 | work_line_->SetRegisterType(inst->VRegA_21c(), |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1689 | res_type.IsConflict() ? res_type |
| 1690 | : reg_types_.JavaLangClass(true)); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1691 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1692 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1693 | case Instruction::MONITOR_ENTER: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1694 | work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1695 | break; |
| 1696 | case Instruction::MONITOR_EXIT: |
| 1697 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1698 | * monitor-exit instructions are odd. They can throw exceptions, |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1699 | * 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] | 1700 | * pointing to the following instruction. (This behavior goes back |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1701 | * to the need to handle asynchronous exceptions, a now-deprecated |
| 1702 | * feature that Dalvik doesn't support.) |
| 1703 | * |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1704 | * In practice we don't need to worry about this. The only |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1705 | * exceptions that can be thrown from monitor-exit are for a |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1706 | * null reference and -exit without a matching -enter. If the |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1707 | * structured locking checks are working, the former would have |
| 1708 | * failed on the -enter instruction, and the latter is impossible. |
| 1709 | * |
| 1710 | * This is fortunate, because issue 3221411 prevents us from |
| 1711 | * chasing the "can throw" path when monitor verification is |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 1712 | * enabled. If we can fully verify the locking we can ignore |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1713 | * some catch blocks (which will show up as "dead" code when |
| 1714 | * we skip them here); if we can't, then the code path could be |
| 1715 | * "live" so we still need to check it. |
| 1716 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1717 | opcode_flags &= ~Instruction::kThrow; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1718 | work_line_->PopMonitor(inst->VRegA_11x()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1719 | break; |
| 1720 | |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1721 | case Instruction::CHECK_CAST: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1722 | case Instruction::INSTANCE_OF: { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1723 | /* |
| 1724 | * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This |
| 1725 | * could be a "upcast" -- not expected, so we don't try to address it.) |
| 1726 | * |
| 1727 | * 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] | 1728 | * dec_insn.vA when branching to a handler. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1729 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1730 | const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST); |
| 1731 | const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c(); |
| 1732 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1733 | if (res_type.IsConflict()) { |
| 1734 | DCHECK_NE(failures_.size(), 0U); |
| 1735 | if (!is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1736 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1737 | } |
| 1738 | break; // bad class |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 1739 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1740 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1741 | uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c(); |
| 1742 | const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1743 | if (!res_type.IsNonZeroReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1744 | if (is_checkcast) { |
| 1745 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type; |
| 1746 | } else { |
| 1747 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type; |
| 1748 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1749 | } else if (!orig_type.IsReferenceTypes()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1750 | if (is_checkcast) { |
| 1751 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg; |
| 1752 | } else { |
| 1753 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg; |
| 1754 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1755 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1756 | if (is_checkcast) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1757 | work_line_->SetRegisterType(inst->VRegA_21c(), res_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1758 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1759 | work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1760 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1761 | } |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 1762 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1763 | } |
| 1764 | case Instruction::ARRAY_LENGTH: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1765 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x()); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1766 | if (res_type.IsReferenceTypes()) { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1767 | if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1768 | 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] | 1769 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1770 | work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | break; |
| 1774 | } |
| 1775 | case Instruction::NEW_INSTANCE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1776 | const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1777 | if (res_type.IsConflict()) { |
| 1778 | DCHECK_NE(failures_.size(), 0U); |
| 1779 | break; // bad class |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1780 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1781 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 1782 | // can't create an instance of an interface or abstract class */ |
| 1783 | if (!res_type.IsInstantiableTypes()) { |
| 1784 | Fail(VERIFY_ERROR_INSTANTIATION) |
| 1785 | << "new-instance on primitive, interface or abstract class" << res_type; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1786 | // Soft failure so carry on to set register type. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1787 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 1788 | const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_); |
| 1789 | // Any registers holding previous allocations from this address that have not yet been |
| 1790 | // initialized must be marked invalid. |
| 1791 | work_line_->MarkUninitRefsAsInvalid(uninit_type); |
| 1792 | // add the new uninitialized reference to the register state |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1793 | work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1794 | break; |
| 1795 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1796 | case Instruction::NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1797 | VerifyNewArray(inst, false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1798 | break; |
| 1799 | case Instruction::FILLED_NEW_ARRAY: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1800 | VerifyNewArray(inst, true, false); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1801 | just_set_result = true; // Filled new array sets result register |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1802 | break; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1803 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1804 | VerifyNewArray(inst, true, true); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 1805 | just_set_result = true; // Filled new array range sets result register |
| 1806 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1807 | case Instruction::CMPL_FLOAT: |
| 1808 | case Instruction::CMPG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1809 | if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1810 | break; |
| 1811 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1812 | if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) { |
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; |
| 1817 | case Instruction::CMPL_DOUBLE: |
| 1818 | case Instruction::CMPG_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1819 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1820 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1821 | break; |
| 1822 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1823 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1824 | reg_types_.DoubleHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1825 | break; |
| 1826 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1827 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1828 | break; |
| 1829 | case Instruction::CMP_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1830 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1831 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1832 | break; |
| 1833 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1834 | if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1835 | reg_types_.LongHi())) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1836 | break; |
| 1837 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1838 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1839 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1840 | case Instruction::THROW: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1841 | const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x()); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 1842 | if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1843 | Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT) |
| 1844 | << "thrown class " << res_type << " not instanceof Throwable"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1845 | } |
| 1846 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1847 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1848 | case Instruction::GOTO: |
| 1849 | case Instruction::GOTO_16: |
| 1850 | case Instruction::GOTO_32: |
| 1851 | /* no effect on or use of registers */ |
| 1852 | break; |
| 1853 | |
| 1854 | case Instruction::PACKED_SWITCH: |
| 1855 | case Instruction::SPARSE_SWITCH: |
| 1856 | /* verify that vAA is an integer, or can be converted to one */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1857 | work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1858 | break; |
| 1859 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1860 | case Instruction::FILL_ARRAY_DATA: { |
| 1861 | /* Similar to the verification done for APUT */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1862 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1863 | /* array_type can be null if the reg type is Zero */ |
| 1864 | if (!array_type.IsZero()) { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1865 | if (!array_type.IsArrayTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1866 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " |
| 1867 | << array_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 1868 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 1869 | const RegType& component_type = reg_types_.GetComponentType(array_type, |
| 1870 | class_loader_->get()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 1871 | DCHECK(!component_type.IsConflict()); |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1872 | if (component_type.IsNonZeroReferenceTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1873 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type " |
| 1874 | << component_type; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1875 | } else { |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1876 | // Now verify if the element width in the table matches the element width declared in |
| 1877 | // the array |
| 1878 | const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16)); |
| 1879 | if (array_data[0] != Instruction::kArrayDataSignature) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1880 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1881 | } else { |
| 1882 | size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType()); |
| 1883 | // Since we don't compress the data in Dex, expect to see equal width of data stored |
| 1884 | // in the table and expected from the array class. |
| 1885 | if (array_data[1] != elem_width) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1886 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1] |
| 1887 | << " vs " << elem_width << ")"; |
jeffhao | 457cc51 | 2012-02-02 16:55:13 -0800 | [diff] [blame] | 1888 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1889 | } |
| 1890 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1891 | } |
| 1892 | } |
| 1893 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1894 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1895 | case Instruction::IF_EQ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1896 | case Instruction::IF_NE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1897 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1898 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1899 | bool mismatch = false; |
| 1900 | if (reg_type1.IsZero()) { // zero then integral or reference expected |
| 1901 | mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes(); |
| 1902 | } else if (reg_type1.IsReferenceTypes()) { // both references? |
| 1903 | mismatch = !reg_type2.IsReferenceTypes(); |
| 1904 | } else { // both integral? |
| 1905 | mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes(); |
| 1906 | } |
| 1907 | if (mismatch) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1908 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," |
| 1909 | << reg_type2 << ") must both be references or integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1910 | } |
| 1911 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1912 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1913 | case Instruction::IF_LT: |
| 1914 | case Instruction::IF_GE: |
| 1915 | case Instruction::IF_GT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1916 | case Instruction::IF_LE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1917 | const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t()); |
| 1918 | const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1919 | if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 1920 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << "," |
| 1921 | << reg_type2 << ") must be integral"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1922 | } |
| 1923 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1924 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1925 | case Instruction::IF_EQZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1926 | case Instruction::IF_NEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 1927 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1928 | if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 1929 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 1930 | << " unexpected as arg to if-eqz/if-nez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 1931 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1932 | |
| 1933 | // Find previous instruction - its existence is a precondition to peephole optimization. |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1934 | uint32_t instance_of_idx = 0; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1935 | if (0 != work_insn_idx_) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1936 | instance_of_idx = work_insn_idx_ - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1937 | while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1938 | instance_of_idx--; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1939 | } |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1940 | CHECK(insn_flags_[instance_of_idx].IsOpcode()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1941 | } else { |
| 1942 | break; |
| 1943 | } |
| 1944 | |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1945 | 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] | 1946 | |
| 1947 | /* Check for peep-hole pattern of: |
| 1948 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1949 | * instance-of vX, vY, T; |
| 1950 | * ifXXX vX, label ; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1951 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1952 | * label: |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1953 | * ...; |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1954 | * and sharpen the type of vY to be type T. |
| 1955 | * Note, this pattern can't be if: |
| 1956 | * - if there are other branches to this branch, |
| 1957 | * - when vX == vY. |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1958 | */ |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1959 | if (!CurrentInsnFlags()->IsBranchTarget() && |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1960 | (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) && |
| 1961 | (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) && |
| 1962 | (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) { |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1963 | // Check that the we are not attempting conversion to interface types, |
| 1964 | // which is not done because of the multiple inheritance implications. |
Jeff Hao | c642ec8 | 2013-09-04 16:11:55 -0700 | [diff] [blame] | 1965 | // Also don't change the type if it would result in an upcast. |
| 1966 | const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c()); |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1967 | const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c()); |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1968 | |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1969 | if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() && |
| 1970 | !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) { |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 1971 | RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1972 | if (inst->Opcode() == Instruction::IF_EQZ) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1973 | fallthrough_line.reset(update_line); |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 1974 | } else { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1975 | branch_line.reset(update_line); |
| 1976 | } |
| 1977 | update_line->CopyFromLine(work_line_.get()); |
| 1978 | update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type); |
| 1979 | if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) { |
| 1980 | // See if instance-of was preceded by a move-object operation, common due to the small |
| 1981 | // register encoding space of instance-of, and propagate type information to the source |
| 1982 | // of the move-object. |
| 1983 | uint32_t move_idx = instance_of_idx - 1; |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1984 | while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) { |
Ian Rogers | 9b36039 | 2013-06-06 14:45:07 -0700 | [diff] [blame] | 1985 | move_idx--; |
| 1986 | } |
| 1987 | CHECK(insn_flags_[move_idx].IsOpcode()); |
| 1988 | const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx); |
| 1989 | switch (move_inst->Opcode()) { |
| 1990 | case Instruction::MOVE_OBJECT: |
| 1991 | if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) { |
| 1992 | update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type); |
| 1993 | } |
| 1994 | break; |
| 1995 | case Instruction::MOVE_OBJECT_FROM16: |
| 1996 | if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) { |
| 1997 | update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type); |
| 1998 | } |
| 1999 | break; |
| 2000 | case Instruction::MOVE_OBJECT_16: |
| 2001 | if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) { |
| 2002 | update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type); |
| 2003 | } |
| 2004 | break; |
| 2005 | default: |
| 2006 | break; |
| 2007 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | } |
| 2011 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2012 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2013 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2014 | case Instruction::IF_LTZ: |
| 2015 | case Instruction::IF_GEZ: |
| 2016 | case Instruction::IF_GTZ: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2017 | case Instruction::IF_LEZ: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2018 | const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2019 | if (!reg_type.IsIntegralTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2020 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type |
| 2021 | << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2022 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2023 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2024 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2025 | case Instruction::AGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2026 | VerifyAGet(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2027 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2028 | case Instruction::AGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2029 | VerifyAGet(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2030 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2031 | case Instruction::AGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2032 | VerifyAGet(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2033 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2034 | case Instruction::AGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2035 | VerifyAGet(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2036 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2037 | case Instruction::AGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2038 | VerifyAGet(inst, reg_types_.Integer(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2039 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2040 | case Instruction::AGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2041 | VerifyAGet(inst, reg_types_.LongLo(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2042 | break; |
| 2043 | case Instruction::AGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2044 | VerifyAGet(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2045 | break; |
| 2046 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2047 | case Instruction::APUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2048 | VerifyAPut(inst, reg_types_.Boolean(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2049 | break; |
| 2050 | case Instruction::APUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2051 | VerifyAPut(inst, reg_types_.Byte(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2052 | break; |
| 2053 | case Instruction::APUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2054 | VerifyAPut(inst, reg_types_.Char(), true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2055 | break; |
| 2056 | case Instruction::APUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2057 | VerifyAPut(inst, reg_types_.Short(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2058 | break; |
| 2059 | case Instruction::APUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2060 | VerifyAPut(inst, reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2061 | break; |
| 2062 | case Instruction::APUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2063 | VerifyAPut(inst, reg_types_.LongLo(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2064 | break; |
| 2065 | case Instruction::APUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2066 | VerifyAPut(inst, reg_types_.JavaLangObject(false), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2067 | break; |
| 2068 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2069 | case Instruction::IGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2070 | VerifyISGet(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2071 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2072 | case Instruction::IGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2073 | VerifyISGet(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2074 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2075 | case Instruction::IGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2076 | VerifyISGet(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2077 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2078 | case Instruction::IGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2079 | VerifyISGet(inst, reg_types_.Short(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2080 | break; |
| 2081 | case Instruction::IGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2082 | VerifyISGet(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2083 | break; |
| 2084 | case Instruction::IGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2085 | VerifyISGet(inst, reg_types_.LongLo(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2086 | break; |
| 2087 | case Instruction::IGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2088 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2089 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2090 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2091 | case Instruction::IPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2092 | VerifyISPut(inst, reg_types_.Boolean(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2093 | break; |
| 2094 | case Instruction::IPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2095 | VerifyISPut(inst, reg_types_.Byte(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2096 | break; |
| 2097 | case Instruction::IPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2098 | VerifyISPut(inst, reg_types_.Char(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2099 | break; |
| 2100 | case Instruction::IPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2101 | VerifyISPut(inst, reg_types_.Short(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2102 | break; |
| 2103 | case Instruction::IPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2104 | VerifyISPut(inst, reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2105 | break; |
| 2106 | case Instruction::IPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2107 | VerifyISPut(inst, reg_types_.LongLo(), true, false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2108 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2109 | case Instruction::IPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2110 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2111 | break; |
| 2112 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2113 | case Instruction::SGET_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2114 | VerifyISGet(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2115 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2116 | case Instruction::SGET_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2117 | VerifyISGet(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2118 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2119 | case Instruction::SGET_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2120 | VerifyISGet(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2121 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2122 | case Instruction::SGET_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2123 | VerifyISGet(inst, reg_types_.Short(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2124 | break; |
| 2125 | case Instruction::SGET: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2126 | VerifyISGet(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2127 | break; |
| 2128 | case Instruction::SGET_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2129 | VerifyISGet(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2130 | break; |
| 2131 | case Instruction::SGET_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2132 | VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2133 | break; |
| 2134 | |
| 2135 | case Instruction::SPUT_BOOLEAN: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2136 | VerifyISPut(inst, reg_types_.Boolean(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2137 | break; |
| 2138 | case Instruction::SPUT_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2139 | VerifyISPut(inst, reg_types_.Byte(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2140 | break; |
| 2141 | case Instruction::SPUT_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2142 | VerifyISPut(inst, reg_types_.Char(), true, true); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2143 | break; |
| 2144 | case Instruction::SPUT_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2145 | VerifyISPut(inst, reg_types_.Short(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2146 | break; |
| 2147 | case Instruction::SPUT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2148 | VerifyISPut(inst, reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2149 | break; |
| 2150 | case Instruction::SPUT_WIDE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2151 | VerifyISPut(inst, reg_types_.LongLo(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2152 | break; |
| 2153 | case Instruction::SPUT_OBJECT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2154 | VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2155 | break; |
| 2156 | |
| 2157 | case Instruction::INVOKE_VIRTUAL: |
| 2158 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 2159 | case Instruction::INVOKE_SUPER: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2160 | case Instruction::INVOKE_SUPER_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2161 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE || |
| 2162 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
| 2163 | bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER || |
| 2164 | inst->Opcode() == Instruction::INVOKE_SUPER_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2165 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2166 | is_range, is_super); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2167 | const RegType* return_type = nullptr; |
| 2168 | if (called_method != nullptr) { |
| 2169 | MethodHelper mh(called_method); |
| 2170 | mirror::Class* return_type_class = mh.GetReturnType(); |
| 2171 | if (return_type_class != nullptr) { |
| 2172 | return_type = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 2173 | return_type_class->CannotBeAssignedFromOtherTypes()); |
| 2174 | } else { |
| 2175 | Thread* self = Thread::Current(); |
| 2176 | DCHECK(self->IsExceptionPending()); |
| 2177 | self->ClearException(); |
| 2178 | } |
| 2179 | } |
| 2180 | if (return_type == nullptr) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2181 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2182 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2183 | 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] | 2184 | const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2185 | return_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2186 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2187 | if (!return_type->IsLowHalf()) { |
| 2188 | work_line_->SetResultRegisterType(*return_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2189 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2190 | work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2191 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2192 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2193 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2194 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2195 | case Instruction::INVOKE_DIRECT: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2196 | case Instruction::INVOKE_DIRECT_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2197 | bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2198 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2199 | is_range, false); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2200 | const char* return_type_descriptor; |
| 2201 | bool is_constructor; |
| 2202 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2203 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2204 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2205 | is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0; |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2206 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2207 | return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2208 | } else { |
| 2209 | is_constructor = called_method->IsConstructor(); |
| 2210 | return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
| 2211 | } |
| 2212 | if (is_constructor) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2213 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2214 | * Some additional checks when calling a constructor. We know from the invocation arg check |
| 2215 | * that the "this" argument is an instance of called_method->klass. Now we further restrict |
| 2216 | * that to require that called_method->klass is the same as this->klass or this->super, |
| 2217 | * allowing the latter only if the "this" argument is the same as the "this" argument to |
| 2218 | * this method (which implies that we're in a constructor ourselves). |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2219 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2220 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2221 | if (this_type.IsConflict()) // failure. |
| 2222 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2223 | |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2224 | /* no null refs allowed (?) */ |
| 2225 | if (this_type.IsZero()) { |
| 2226 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref"; |
| 2227 | break; |
jeffhao | 2a8a90e | 2011-09-26 14:25:31 -0700 | [diff] [blame] | 2228 | } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2229 | |
| 2230 | /* must be in same class or in superclass */ |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2231 | // const RegType& this_super_klass = this_type.GetSuperClass(®_types_); |
| 2232 | // TODO: re-enable constructor type verification |
| 2233 | // if (this_super_klass.IsConflict()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2234 | // Unknown super class, fail so we re-check at runtime. |
Ian Rogers | 4668543 | 2012-06-03 22:26:43 -0700 | [diff] [blame] | 2235 | // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'"; |
| 2236 | // break; |
| 2237 | // } |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2238 | |
| 2239 | /* arg must be an uninitialized reference */ |
| 2240 | if (!this_type.IsUninitializedTypes()) { |
| 2241 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference " |
| 2242 | << this_type; |
| 2243 | break; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * Replace the uninitialized reference with an initialized one. We need to do this for all |
| 2248 | * registers that have the same object instance in them, not just the "this" register. |
| 2249 | */ |
| 2250 | work_line_->MarkRefsAsInitialized(this_type); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2251 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2252 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), |
| 2253 | return_type_descriptor, false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2254 | if (!return_type.IsLowHalf()) { |
| 2255 | work_line_->SetResultRegisterType(return_type); |
| 2256 | } else { |
| 2257 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2258 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2259 | just_set_result = true; |
| 2260 | break; |
| 2261 | } |
| 2262 | case Instruction::INVOKE_STATIC: |
| 2263 | case Instruction::INVOKE_STATIC_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2264 | bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2265 | mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2266 | METHOD_STATIC, |
| 2267 | is_range, |
| 2268 | false); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2269 | const char* descriptor; |
| 2270 | if (called_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2271 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2272 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2273 | 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^] | 2274 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2275 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2276 | descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2277 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2278 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2279 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2280 | if (!return_type.IsLowHalf()) { |
| 2281 | work_line_->SetResultRegisterType(return_type); |
| 2282 | } else { |
| 2283 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2284 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2285 | just_set_result = true; |
| 2286 | } |
| 2287 | break; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2288 | case Instruction::INVOKE_INTERFACE: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2289 | case Instruction::INVOKE_INTERFACE_RANGE: { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2290 | bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2291 | mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst, |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2292 | METHOD_INTERFACE, |
| 2293 | is_range, |
| 2294 | false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2295 | if (abs_method != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2296 | mirror::Class* called_interface = abs_method->GetDeclaringClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2297 | if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) { |
| 2298 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '" |
| 2299 | << PrettyMethod(abs_method) << "'"; |
| 2300 | break; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2301 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2302 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2303 | /* Get the type of the "this" arg, which should either be a sub-interface of called |
| 2304 | * interface or Object (see comments in RegType::JoinClass). |
| 2305 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2306 | const RegType& this_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2307 | if (this_type.IsZero()) { |
| 2308 | /* null pointer always passes (and always fails at runtime) */ |
| 2309 | } else { |
| 2310 | if (this_type.IsUninitializedTypes()) { |
| 2311 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object " |
| 2312 | << this_type; |
| 2313 | break; |
| 2314 | } |
| 2315 | // In the past we have tried to assert that "called_interface" is assignable |
| 2316 | // from "this_type.GetClass()", however, as we do an imprecise Join |
| 2317 | // (RegType::JoinClass) we don't have full information on what interfaces are |
| 2318 | // implemented by "this_type". For example, two classes may implement the same |
| 2319 | // interfaces and have a common parent that doesn't implement the interface. The |
| 2320 | // join will set "this_type" to the parent class and a test that this implements |
| 2321 | // the interface will incorrectly fail. |
| 2322 | } |
| 2323 | /* |
| 2324 | * We don't have an object instance, so we can't find the concrete method. However, all of |
| 2325 | * the type information is in the abstract method, so we're good. |
| 2326 | */ |
| 2327 | const char* descriptor; |
| 2328 | if (abs_method == NULL) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2329 | uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2330 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 2331 | uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_; |
| 2332 | descriptor = dex_file_->StringByTypeIdx(return_type_idx); |
| 2333 | } else { |
| 2334 | descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor(); |
| 2335 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2336 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2337 | false); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2338 | if (!return_type.IsLowHalf()) { |
| 2339 | work_line_->SetResultRegisterType(return_type); |
| 2340 | } else { |
| 2341 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2342 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2343 | just_set_result = true; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2344 | break; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2345 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2346 | case Instruction::NEG_INT: |
| 2347 | case Instruction::NOT_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2348 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2349 | break; |
| 2350 | case Instruction::NEG_LONG: |
| 2351 | case Instruction::NOT_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2352 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2353 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2354 | break; |
| 2355 | case Instruction::NEG_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2356 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2357 | break; |
| 2358 | case Instruction::NEG_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_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2361 | break; |
| 2362 | case Instruction::INT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2363 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2364 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2365 | break; |
| 2366 | case Instruction::INT_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2367 | work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2368 | break; |
| 2369 | case Instruction::INT_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_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2372 | break; |
| 2373 | case Instruction::LONG_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_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2376 | break; |
| 2377 | case Instruction::LONG_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2378 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2379 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2380 | break; |
| 2381 | case Instruction::LONG_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2382 | work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2383 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2384 | break; |
| 2385 | case Instruction::FLOAT_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2386 | work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2387 | break; |
| 2388 | case Instruction::FLOAT_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2389 | work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2390 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2391 | break; |
| 2392 | case Instruction::FLOAT_TO_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2393 | work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2394 | reg_types_.Float()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2395 | break; |
| 2396 | case Instruction::DOUBLE_TO_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2397 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2398 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2399 | break; |
| 2400 | case Instruction::DOUBLE_TO_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2401 | work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2402 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2403 | break; |
| 2404 | case Instruction::DOUBLE_TO_FLOAT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2405 | work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2406 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2407 | break; |
| 2408 | case Instruction::INT_TO_BYTE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2409 | work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2410 | break; |
| 2411 | case Instruction::INT_TO_CHAR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2412 | work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2413 | break; |
| 2414 | case Instruction::INT_TO_SHORT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2415 | work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2416 | break; |
| 2417 | |
| 2418 | case Instruction::ADD_INT: |
| 2419 | case Instruction::SUB_INT: |
| 2420 | case Instruction::MUL_INT: |
| 2421 | case Instruction::REM_INT: |
| 2422 | case Instruction::DIV_INT: |
| 2423 | case Instruction::SHL_INT: |
| 2424 | case Instruction::SHR_INT: |
| 2425 | case Instruction::USHR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2426 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2427 | reg_types_.Integer(), false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2428 | break; |
| 2429 | case Instruction::AND_INT: |
| 2430 | case Instruction::OR_INT: |
| 2431 | case Instruction::XOR_INT: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2432 | work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2433 | reg_types_.Integer(), true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2434 | break; |
| 2435 | case Instruction::ADD_LONG: |
| 2436 | case Instruction::SUB_LONG: |
| 2437 | case Instruction::MUL_LONG: |
| 2438 | case Instruction::DIV_LONG: |
| 2439 | case Instruction::REM_LONG: |
| 2440 | case Instruction::AND_LONG: |
| 2441 | case Instruction::OR_LONG: |
| 2442 | case Instruction::XOR_LONG: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2443 | work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2444 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2445 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2446 | break; |
| 2447 | case Instruction::SHL_LONG: |
| 2448 | case Instruction::SHR_LONG: |
| 2449 | case Instruction::USHR_LONG: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2450 | /* shift distance is Int, making these different from other binary operations */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2451 | work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2452 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2453 | break; |
| 2454 | case Instruction::ADD_FLOAT: |
| 2455 | case Instruction::SUB_FLOAT: |
| 2456 | case Instruction::MUL_FLOAT: |
| 2457 | case Instruction::DIV_FLOAT: |
| 2458 | case Instruction::REM_FLOAT: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2459 | work_line_->CheckBinaryOp(inst, |
| 2460 | reg_types_.Float(), |
| 2461 | reg_types_.Float(), |
| 2462 | reg_types_.Float(), |
| 2463 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2464 | break; |
| 2465 | case Instruction::ADD_DOUBLE: |
| 2466 | case Instruction::SUB_DOUBLE: |
| 2467 | case Instruction::MUL_DOUBLE: |
| 2468 | case Instruction::DIV_DOUBLE: |
| 2469 | case Instruction::REM_DOUBLE: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2470 | work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2471 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2472 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2473 | break; |
| 2474 | case Instruction::ADD_INT_2ADDR: |
| 2475 | case Instruction::SUB_INT_2ADDR: |
| 2476 | case Instruction::MUL_INT_2ADDR: |
| 2477 | case Instruction::REM_INT_2ADDR: |
| 2478 | case Instruction::SHL_INT_2ADDR: |
| 2479 | case Instruction::SHR_INT_2ADDR: |
| 2480 | case Instruction::USHR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2481 | work_line_->CheckBinaryOp2addr(inst, |
| 2482 | reg_types_.Integer(), |
| 2483 | reg_types_.Integer(), |
| 2484 | reg_types_.Integer(), |
| 2485 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2486 | break; |
| 2487 | case Instruction::AND_INT_2ADDR: |
| 2488 | case Instruction::OR_INT_2ADDR: |
| 2489 | case Instruction::XOR_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2490 | work_line_->CheckBinaryOp2addr(inst, |
| 2491 | reg_types_.Integer(), |
| 2492 | reg_types_.Integer(), |
| 2493 | reg_types_.Integer(), |
| 2494 | true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2495 | break; |
| 2496 | case Instruction::DIV_INT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2497 | work_line_->CheckBinaryOp2addr(inst, |
| 2498 | reg_types_.Integer(), |
| 2499 | reg_types_.Integer(), |
| 2500 | reg_types_.Integer(), |
| 2501 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2502 | break; |
| 2503 | case Instruction::ADD_LONG_2ADDR: |
| 2504 | case Instruction::SUB_LONG_2ADDR: |
| 2505 | case Instruction::MUL_LONG_2ADDR: |
| 2506 | case Instruction::DIV_LONG_2ADDR: |
| 2507 | case Instruction::REM_LONG_2ADDR: |
| 2508 | case Instruction::AND_LONG_2ADDR: |
| 2509 | case Instruction::OR_LONG_2ADDR: |
| 2510 | case Instruction::XOR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2511 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2512 | reg_types_.LongLo(), reg_types_.LongHi(), |
| 2513 | reg_types_.LongLo(), reg_types_.LongHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2514 | break; |
| 2515 | case Instruction::SHL_LONG_2ADDR: |
| 2516 | case Instruction::SHR_LONG_2ADDR: |
| 2517 | case Instruction::USHR_LONG_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2518 | work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2519 | reg_types_.Integer()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2520 | break; |
| 2521 | case Instruction::ADD_FLOAT_2ADDR: |
| 2522 | case Instruction::SUB_FLOAT_2ADDR: |
| 2523 | case Instruction::MUL_FLOAT_2ADDR: |
| 2524 | case Instruction::DIV_FLOAT_2ADDR: |
| 2525 | case Instruction::REM_FLOAT_2ADDR: |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 2526 | work_line_->CheckBinaryOp2addr(inst, |
| 2527 | reg_types_.Float(), |
| 2528 | reg_types_.Float(), |
| 2529 | reg_types_.Float(), |
| 2530 | false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2531 | break; |
| 2532 | case Instruction::ADD_DOUBLE_2ADDR: |
| 2533 | case Instruction::SUB_DOUBLE_2ADDR: |
| 2534 | case Instruction::MUL_DOUBLE_2ADDR: |
| 2535 | case Instruction::DIV_DOUBLE_2ADDR: |
| 2536 | case Instruction::REM_DOUBLE_2ADDR: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2537 | work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2538 | reg_types_.DoubleLo(), reg_types_.DoubleHi(), |
| 2539 | reg_types_.DoubleLo(), reg_types_.DoubleHi()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2540 | break; |
| 2541 | case Instruction::ADD_INT_LIT16: |
| 2542 | case Instruction::RSUB_INT: |
| 2543 | case Instruction::MUL_INT_LIT16: |
| 2544 | case Instruction::DIV_INT_LIT16: |
| 2545 | case Instruction::REM_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2546 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2547 | break; |
| 2548 | case Instruction::AND_INT_LIT16: |
| 2549 | case Instruction::OR_INT_LIT16: |
| 2550 | case Instruction::XOR_INT_LIT16: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2551 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2552 | break; |
| 2553 | case Instruction::ADD_INT_LIT8: |
| 2554 | case Instruction::RSUB_INT_LIT8: |
| 2555 | case Instruction::MUL_INT_LIT8: |
| 2556 | case Instruction::DIV_INT_LIT8: |
| 2557 | case Instruction::REM_INT_LIT8: |
| 2558 | case Instruction::SHL_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2559 | case Instruction::SHR_INT_LIT8: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2560 | case Instruction::USHR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2561 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2562 | break; |
| 2563 | case Instruction::AND_INT_LIT8: |
| 2564 | case Instruction::OR_INT_LIT8: |
| 2565 | case Instruction::XOR_INT_LIT8: |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2566 | work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2567 | break; |
| 2568 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2569 | // Special instructions. |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2570 | case Instruction::RETURN_VOID_BARRIER: |
Ian Rogers | 9fc16eb | 2013-07-31 14:49:16 -0700 | [diff] [blame] | 2571 | DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_); |
| 2572 | if (!IsConstructor() || IsStatic()) { |
Sebastien Hertz | cc10e0e | 2013-06-28 14:24:48 +0200 | [diff] [blame] | 2573 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected"; |
| 2574 | } |
| 2575 | break; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2576 | // Note: the following instructions encode offsets derived from class linking. |
| 2577 | // As such they use Class*/Field*/AbstractMethod* as these offsets only have |
| 2578 | // meaning if the class linking and resolution were successful. |
| 2579 | case Instruction::IGET_QUICK: |
| 2580 | VerifyIGetQuick(inst, reg_types_.Integer(), true); |
| 2581 | break; |
| 2582 | case Instruction::IGET_WIDE_QUICK: |
| 2583 | VerifyIGetQuick(inst, reg_types_.LongLo(), true); |
| 2584 | break; |
| 2585 | case Instruction::IGET_OBJECT_QUICK: |
| 2586 | VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2587 | break; |
| 2588 | case Instruction::IPUT_QUICK: |
| 2589 | VerifyIPutQuick(inst, reg_types_.Integer(), true); |
| 2590 | break; |
| 2591 | case Instruction::IPUT_WIDE_QUICK: |
| 2592 | VerifyIPutQuick(inst, reg_types_.LongLo(), true); |
| 2593 | break; |
| 2594 | case Instruction::IPUT_OBJECT_QUICK: |
| 2595 | VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false); |
| 2596 | break; |
| 2597 | case Instruction::INVOKE_VIRTUAL_QUICK: |
| 2598 | case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: { |
| 2599 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2600 | mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2601 | if (called_method != NULL) { |
| 2602 | const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2603 | const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, |
| 2604 | false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2605 | if (!return_type.IsLowHalf()) { |
| 2606 | work_line_->SetResultRegisterType(return_type); |
| 2607 | } else { |
| 2608 | work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_)); |
| 2609 | } |
| 2610 | just_set_result = true; |
| 2611 | } |
| 2612 | break; |
| 2613 | } |
| 2614 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2615 | /* These should never appear during verification. */ |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2616 | case Instruction::UNUSED_3E: |
| 2617 | case Instruction::UNUSED_3F: |
| 2618 | case Instruction::UNUSED_40: |
| 2619 | case Instruction::UNUSED_41: |
| 2620 | case Instruction::UNUSED_42: |
| 2621 | case Instruction::UNUSED_43: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2622 | case Instruction::UNUSED_79: |
| 2623 | case Instruction::UNUSED_7A: |
| 2624 | case Instruction::UNUSED_EB: |
| 2625 | case Instruction::UNUSED_EC: |
jeffhao | 9a4f003 | 2012-08-30 16:17:40 -0700 | [diff] [blame] | 2626 | case Instruction::UNUSED_ED: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2627 | case Instruction::UNUSED_EE: |
| 2628 | case Instruction::UNUSED_EF: |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 2629 | case Instruction::UNUSED_F0: |
| 2630 | case Instruction::UNUSED_F1: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2631 | case Instruction::UNUSED_F2: |
| 2632 | case Instruction::UNUSED_F3: |
| 2633 | case Instruction::UNUSED_F4: |
| 2634 | case Instruction::UNUSED_F5: |
| 2635 | case Instruction::UNUSED_F6: |
| 2636 | case Instruction::UNUSED_F7: |
| 2637 | case Instruction::UNUSED_F8: |
| 2638 | case Instruction::UNUSED_F9: |
| 2639 | case Instruction::UNUSED_FA: |
| 2640 | case Instruction::UNUSED_FB: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2641 | case Instruction::UNUSED_FC: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2642 | case Instruction::UNUSED_FD: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2643 | case Instruction::UNUSED_FE: |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2644 | case Instruction::UNUSED_FF: |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2645 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2646 | break; |
| 2647 | |
| 2648 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2649 | * DO NOT add a "default" clause here. Without it the compiler will |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2650 | * complain if an instruction is missing (which is desirable). |
| 2651 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2652 | } // end - switch (dec_insn.opcode) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2653 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2654 | if (have_pending_hard_failure_) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2655 | if (Runtime::Current()->IsCompiler()) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 2656 | /* When compiling, check that the last failure is a hard failure */ |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2657 | CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2658 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2659 | /* immediate failure, reject class */ |
| 2660 | info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_); |
| 2661 | return false; |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2662 | } else if (have_pending_runtime_throw_failure_) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 2663 | /* checking interpreter will throw, mark following code as unreachable */ |
jeffhao | faf459e | 2012-08-31 15:32:47 -0700 | [diff] [blame] | 2664 | opcode_flags = Instruction::kThrow; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2665 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2666 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2667 | * If we didn't just set the result register, clear it out. This ensures that you can only use |
| 2668 | * "move-result" immediately after the result is set. (We could check this statically, but it's |
| 2669 | * not expensive and it makes our debugging output cleaner.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2670 | */ |
| 2671 | if (!just_set_result) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2672 | work_line_->SetResultTypeToUnknown(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2675 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2676 | |
| 2677 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2678 | * Handle "branch". Tag the branch target. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2679 | * |
| 2680 | * NOTE: instructions like Instruction::EQZ provide information about the |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2681 | * 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] | 2682 | * somebody could get a reference field, check it for zero, and if the |
| 2683 | * branch is taken immediately store that register in a boolean field |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2684 | * 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] | 2685 | * that, and will reject the code. |
| 2686 | * |
| 2687 | * TODO: avoid re-fetching the branch target |
| 2688 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2689 | if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2690 | bool isConditional, selfOkay; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2691 | if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2692 | /* should never happen after static verification */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2693 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2694 | return false; |
| 2695 | } |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2696 | DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2697 | if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2698 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2699 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2700 | /* update branch target, set "changed" if appropriate */ |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2701 | if (NULL != branch_line.get()) { |
| 2702 | if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) { |
| 2703 | return false; |
| 2704 | } |
| 2705 | } else { |
| 2706 | if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) { |
| 2707 | return false; |
| 2708 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2709 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2713 | * Handle "switch". Tag all possible branch targets. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2714 | * |
| 2715 | * We've already verified that the table is structurally sound, so we |
| 2716 | * just need to walk through and tag the targets. |
| 2717 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2718 | if ((opcode_flags & Instruction::kSwitch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2719 | int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16); |
| 2720 | const uint16_t* switch_insns = insns + offset_to_switch; |
| 2721 | int switch_count = switch_insns[1]; |
| 2722 | int offset_to_targets, targ; |
| 2723 | |
| 2724 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 2725 | /* 0 = sig, 1 = count, 2/3 = first key */ |
| 2726 | offset_to_targets = 4; |
| 2727 | } else { |
| 2728 | /* 0 = sig, 1 = count, 2..count * 2 = keys */ |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2729 | DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2730 | offset_to_targets = 2 + 2 * switch_count; |
| 2731 | } |
| 2732 | |
| 2733 | /* verify each switch target */ |
| 2734 | for (targ = 0; targ < switch_count; targ++) { |
| 2735 | int offset; |
| 2736 | uint32_t abs_offset; |
| 2737 | |
| 2738 | /* offsets are 32-bit, and only partly endian-swapped */ |
| 2739 | offset = switch_insns[offset_to_targets + targ * 2] | |
| 2740 | (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2741 | abs_offset = work_insn_idx_ + offset; |
| 2742 | DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_); |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2743 | if (!CheckNotMoveException(code_item_->insns_, abs_offset)) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2744 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2745 | } |
| 2746 | if (!UpdateRegisters(abs_offset, work_line_.get())) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2747 | return false; |
| 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2752 | * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a |
| 2753 | * "try" block when they throw, control transfers out of the method.) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2754 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2755 | if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2756 | bool within_catch_all = false; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2757 | CatchHandlerIterator iterator(*code_item_, work_insn_idx_); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2758 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2759 | for (; iterator.HasNext(); iterator.Next()) { |
| 2760 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2761 | within_catch_all = true; |
| 2762 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2763 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2764 | * Merge registers into the "catch" block. We want to use the "savedRegs" rather than |
| 2765 | * "work_regs", because at runtime the exception will be thrown before the instruction |
| 2766 | * modifies any registers. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2767 | */ |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2768 | if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2769 | return false; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2770 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2774 | * If the monitor stack depth is nonzero, there must be a "catch all" handler for this |
| 2775 | * instruction. This does apply to monitor-exit because of async exception handling. |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2776 | */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2777 | if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2778 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2779 | * The state in work_line reflects the post-execution state. If the current instruction is a |
| 2780 | * 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] | 2781 | * it will do so before grabbing the lock). |
| 2782 | */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 2783 | if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2784 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2785 | << "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] | 2786 | return false; |
| 2787 | } |
| 2788 | } |
| 2789 | } |
| 2790 | |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2791 | /* Handle "continue". Tag the next consecutive instruction. |
| 2792 | * Note: Keep the code handling "continue" case below the "branch" and "switch" cases, |
| 2793 | * because it changes work_line_ when performing peephole optimization |
| 2794 | * and this change should not be used in those cases. |
| 2795 | */ |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2796 | if ((opcode_flags & Instruction::kContinue) != 0) { |
| 2797 | uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits(); |
| 2798 | if (next_insn_idx >= code_item_->insns_size_in_code_units_) { |
| 2799 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area"; |
| 2800 | return false; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2801 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2802 | // The only way to get to a move-exception instruction is to get thrown there. Make sure the |
| 2803 | // next instruction isn't one. |
| 2804 | if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) { |
| 2805 | return false; |
| 2806 | } |
| 2807 | if (NULL != fallthrough_line.get()) { |
| 2808 | // Make workline consistent with fallthrough computed from peephole optimization. |
| 2809 | work_line_->CopyFromLine(fallthrough_line.get()); |
| 2810 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 2811 | if (insn_flags_[next_insn_idx].IsReturn()) { |
| 2812 | // For returns we only care about the operand to the return, all other registers are dead. |
| 2813 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx); |
| 2814 | Instruction::Code opcode = ret_inst->Opcode(); |
| 2815 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 2816 | work_line_->MarkAllRegistersAsConflicts(); |
| 2817 | } else { |
| 2818 | if (opcode == Instruction::RETURN_WIDE) { |
| 2819 | work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 2820 | } else { |
| 2821 | work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 2822 | } |
| 2823 | } |
| 2824 | } |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 2825 | RegisterLine* next_line = reg_table_.GetLine(next_insn_idx); |
| 2826 | if (next_line != NULL) { |
| 2827 | // Merge registers into what we have for the next instruction, |
| 2828 | // and set the "changed" flag if needed. |
| 2829 | if (!UpdateRegisters(next_insn_idx, work_line_.get())) { |
| 2830 | return false; |
| 2831 | } |
| 2832 | } else { |
| 2833 | /* |
| 2834 | * We're not recording register data for the next instruction, so we don't know what the |
| 2835 | * prior state was. We have to assume that something has changed and re-evaluate it. |
| 2836 | */ |
| 2837 | insn_flags_[next_insn_idx].SetChanged(); |
| 2838 | } |
| 2839 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 2840 | |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2841 | /* 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] | 2842 | if ((opcode_flags & Instruction::kReturn) != 0) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 2843 | if (!work_line_->VerifyMonitorStackEmpty()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2844 | return false; |
| 2845 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | /* |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 2849 | * Update start_guess. Advance to the next instruction of that's |
| 2850 | * possible, otherwise use the branch target if one was found. If |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2851 | * neither of those exists we're in a return or throw; leave start_guess |
| 2852 | * alone and let the caller sort it out. |
| 2853 | */ |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2854 | if ((opcode_flags & Instruction::kContinue) != 0) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2855 | *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2856 | } else if ((opcode_flags & Instruction::kBranch) != 0) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2857 | /* we're still okay if branch_target is zero */ |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2858 | *start_guess = work_insn_idx_ + branch_target; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2861 | DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_); |
| 2862 | DCHECK(insn_flags_[*start_guess].IsOpcode()); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2863 | |
| 2864 | return true; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 2865 | } // NOLINT(readability/fn_size) |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2866 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2867 | const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2868 | const char* descriptor = dex_file_->StringByTypeIdx(class_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2869 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2870 | mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2871 | const RegType& result = |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 2872 | klass != NULL ? reg_types_.FromClass(descriptor, klass, |
| 2873 | klass->CannotBeAssignedFromOtherTypes()) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2874 | : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2875 | if (result.IsConflict()) { |
| 2876 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor |
| 2877 | << "' in " << referrer; |
| 2878 | return result; |
| 2879 | } |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2880 | if (klass == NULL && !result.IsUnresolvedTypes()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2881 | (*dex_cache_)->SetResolvedType(class_idx, result.GetClass()); |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 2882 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2883 | // Check if access is allowed. Unresolved types use xxxWithAccessCheck to |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 2884 | // check at runtime if access is allowed and so pass here. If result is |
| 2885 | // primitive, skip the access check. |
| 2886 | if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() && |
| 2887 | !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2888 | Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '" |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2889 | << referrer << "' -> '" << result << "'"; |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2890 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2891 | return result; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2894 | const RegType& MethodVerifier::GetCaughtExceptionType() { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2895 | const RegType* common_super = NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2896 | if (code_item_->tries_size_ != 0) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2897 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2898 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2899 | for (uint32_t i = 0; i < handlers_size; i++) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2900 | CatchHandlerIterator iterator(handlers_ptr); |
| 2901 | for (; iterator.HasNext(); iterator.Next()) { |
| 2902 | if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) { |
| 2903 | if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) { |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2904 | common_super = ®_types_.JavaLangThrowable(false); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2905 | } else { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2906 | const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex()); |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2907 | if (common_super == NULL) { |
| 2908 | // Unconditionally assign for the first handler. We don't assert this is a Throwable |
| 2909 | // as that is caught at runtime |
| 2910 | common_super = &exception; |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2911 | } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) { |
Jeff Hao | c26a56c | 2013-11-04 12:00:47 -0800 | [diff] [blame] | 2912 | if (exception.IsUnresolvedTypes()) { |
| 2913 | // We don't know enough about the type. Fail here and let runtime handle it. |
| 2914 | Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception; |
| 2915 | return exception; |
| 2916 | } else { |
| 2917 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception; |
| 2918 | return reg_types_.Conflict(); |
| 2919 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2920 | } else if (common_super->Equals(exception)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2921 | // odd case, but nothing to do |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2922 | } else { |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2923 | common_super = &common_super->Merge(exception, ®_types_); |
Ian Rogers | b490357 | 2012-10-11 11:52:56 -0700 | [diff] [blame] | 2924 | CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super)); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2929 | handlers_ptr = iterator.EndDataPointer(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | if (common_super == NULL) { |
| 2933 | /* no catch blocks, or no catches with classes we can find */ |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2934 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2935 | return reg_types_.Conflict(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2936 | } |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 2937 | return *common_super; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2940 | mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2941 | MethodType method_type) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2942 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2943 | const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2944 | if (klass_type.IsConflict()) { |
| 2945 | std::string append(" in attempt to access method "); |
| 2946 | append += dex_file_->GetMethodName(method_id); |
| 2947 | AppendToLastFailMessage(append); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2948 | return NULL; |
| 2949 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2950 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 2951 | return NULL; // Can't resolve Class so no more to do here |
| 2952 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2953 | mirror::Class* klass = klass_type.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2954 | const RegType& referrer = GetDeclaringClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2955 | mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2956 | if (res_method == NULL) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2957 | const char* name = dex_file_->GetMethodName(method_id); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2958 | const Signature signature = dex_file_->GetMethodSignature(method_id); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2959 | |
| 2960 | if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2961 | res_method = klass->FindDirectMethod(name, signature); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2962 | } else if (method_type == METHOD_INTERFACE) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2963 | res_method = klass->FindInterfaceMethod(name, signature); |
| 2964 | } else { |
| 2965 | res_method = klass->FindVirtualMethod(name, signature); |
| 2966 | } |
| 2967 | if (res_method != NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 2968 | (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2969 | } else { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2970 | // If a virtual or interface method wasn't found with the expected type, look in |
| 2971 | // the direct methods. This can happen when the wrong invoke type is used or when |
| 2972 | // a class has changed, and will be flagged as an error in later checks. |
| 2973 | if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) { |
| 2974 | res_method = klass->FindDirectMethod(name, signature); |
| 2975 | } |
| 2976 | if (res_method == NULL) { |
| 2977 | Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method " |
| 2978 | << PrettyDescriptor(klass) << "." << name |
| 2979 | << " " << signature; |
| 2980 | return NULL; |
| 2981 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2982 | } |
| 2983 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2984 | // Make sure calls to constructors are "direct". There are additional restrictions but we don't |
| 2985 | // enforce them here. |
| 2986 | if (res_method->IsConstructor() && method_type != METHOD_DIRECT) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2987 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor " |
| 2988 | << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 2989 | return NULL; |
| 2990 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2991 | // Disallow any calls to class initializers. |
| 2992 | if (MethodHelper(res_method).IsClassInitializer()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 2993 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer " |
| 2994 | << PrettyMethod(res_method); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2995 | return NULL; |
| 2996 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2997 | // Check if access is allowed. |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 2998 | if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 2999 | Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3000 | << " from " << referrer << ")"; |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3001 | return res_method; |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3002 | } |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3003 | // Check that invoke-virtual and invoke-super are not used on private methods of the same class. |
| 3004 | if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3005 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method " |
| 3006 | << PrettyMethod(res_method); |
jeffhao | de0d9c9 | 2012-02-27 13:58:13 -0800 | [diff] [blame] | 3007 | return NULL; |
| 3008 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3009 | // Check that interface methods match interface classes. |
| 3010 | if (klass->IsInterface() && method_type != METHOD_INTERFACE) { |
| 3011 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method) |
| 3012 | << " is in an interface class " << PrettyClass(klass); |
| 3013 | return NULL; |
| 3014 | } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) { |
| 3015 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method) |
| 3016 | << " is in a non-interface class " << PrettyClass(klass); |
| 3017 | return NULL; |
| 3018 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3019 | // See if the method type implied by the invoke instruction matches the access flags for the |
| 3020 | // target method. |
| 3021 | if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) || |
| 3022 | (method_type == METHOD_STATIC && !res_method->IsStatic()) || |
| 3023 | ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect()) |
| 3024 | ) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 3025 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method " |
| 3026 | " type of " << PrettyMethod(res_method); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3027 | return NULL; |
| 3028 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3029 | return res_method; |
| 3030 | } |
| 3031 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3032 | mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst, |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3033 | MethodType method_type, |
| 3034 | bool is_range, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3035 | bool is_super) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3036 | // Resolve the method. This could be an abstract or concrete method depending on what sort of call |
| 3037 | // we're making. |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3038 | 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] | 3039 | mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3040 | if (res_method == NULL) { // error or class is unresolved |
| 3041 | return NULL; |
| 3042 | } |
| 3043 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3044 | // If we're using invoke-super(method), make sure that the executing method's class' superclass |
| 3045 | // has a vtable entry for the target method. |
| 3046 | if (is_super) { |
| 3047 | DCHECK(method_type == METHOD_VIRTUAL); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3048 | const RegType& super = GetDeclaringClass().GetSuperClass(®_types_); |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3049 | if (super.IsUnresolvedTypes()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3050 | Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3051 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3052 | << " to super " << PrettyMethod(res_method); |
| 3053 | return NULL; |
| 3054 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3055 | mirror::Class* super_klass = super.GetClass(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3056 | if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) { |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3057 | MethodHelper mh(res_method); |
| 3058 | Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from " |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3059 | << PrettyMethod(dex_method_idx_, *dex_file_) |
jeffhao | 4d8df82 | 2012-04-24 17:09:36 -0700 | [diff] [blame] | 3060 | << " to super " << super |
| 3061 | << "." << mh.GetName() |
| 3062 | << mh.GetSignature(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3063 | return NULL; |
| 3064 | } |
| 3065 | } |
| 3066 | // 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] | 3067 | // 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] | 3068 | // definition (which doesn't have register count values). |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3069 | 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] | 3070 | /* caught by static verifier */ |
| 3071 | DCHECK(is_range || expected_args <= 5); |
| 3072 | if (expected_args > code_item_->outs_size_) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3073 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3074 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3075 | return NULL; |
| 3076 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3077 | |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3078 | /* |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3079 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3080 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3081 | * 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] | 3082 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3083 | size_t actual_args = 0; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3084 | if (!res_method->IsStatic()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3085 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3086 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3087 | return NULL; |
| 3088 | } |
| 3089 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3090 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3091 | return NULL; |
| 3092 | } |
| 3093 | if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3094 | mirror::Class* klass = res_method->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3095 | const RegType& res_method_class = |
| 3096 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3097 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3098 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3099 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS: |
| 3100 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3101 | << "' not instance of '" << res_method_class << "'"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3102 | return NULL; |
| 3103 | } |
| 3104 | } |
| 3105 | actual_args++; |
| 3106 | } |
| 3107 | /* |
| 3108 | * Process the target method's signature. This signature may or may not |
| 3109 | * have been verified, so we can't assume it's properly formed. |
| 3110 | */ |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3111 | MethodHelper mh(res_method); |
| 3112 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3113 | size_t params_size = params == NULL ? 0 : params->Size(); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3114 | uint32_t arg[5]; |
| 3115 | if (!is_range) { |
| 3116 | inst->GetArgs(arg); |
| 3117 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3118 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3119 | if (actual_args >= expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3120 | 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] | 3121 | << "'. Expected " << expected_args << " arguments, processing argument " << actual_args |
| 3122 | << " (where longs/doubles count twice)."; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3123 | return NULL; |
| 3124 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3125 | const char* descriptor = |
| 3126 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3127 | if (descriptor == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3128 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3129 | << " missing signature component"; |
| 3130 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3131 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3132 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3133 | 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] | 3134 | if (reg_type.IsIntegralTypes()) { |
| 3135 | const RegType& src_type = work_line_->GetRegisterType(get_reg); |
| 3136 | if (!src_type.IsIntegralTypes()) { |
| 3137 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type |
| 3138 | << " but expected " << reg_type; |
| 3139 | return res_method; |
| 3140 | } |
| 3141 | } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
jeffhao | b57e952 | 2012-04-26 18:08:21 -0700 | [diff] [blame] | 3142 | return res_method; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3143 | } |
| 3144 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3145 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3146 | if (actual_args != expected_args) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3147 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3148 | << " expected " << expected_args << " arguments, found " << actual_args; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3149 | return NULL; |
| 3150 | } else { |
| 3151 | return res_method; |
| 3152 | } |
| 3153 | } |
| 3154 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3155 | mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3156 | RegisterLine* reg_line, bool is_range) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3157 | DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK || |
| 3158 | inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK); |
| 3159 | const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3160 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3161 | return NULL; |
Sebastien Hertz | 8249b42 | 2013-10-29 17:50:55 +0100 | [diff] [blame] | 3162 | } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further. |
| 3163 | return NULL; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3164 | } |
| 3165 | mirror::Class* this_class = NULL; |
| 3166 | if (!actual_arg_type.IsUnresolvedTypes()) { |
| 3167 | this_class = actual_arg_type.GetClass(); |
| 3168 | } else { |
| 3169 | const std::string& descriptor(actual_arg_type.GetDescriptor()); |
| 3170 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3171 | this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3172 | if (this_class == NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3173 | Thread* self = Thread::Current(); |
| 3174 | self->ClearException(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3175 | // Look for a system class |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3176 | SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr); |
| 3177 | this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3178 | } |
| 3179 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3180 | if (this_class == NULL) { |
| 3181 | return NULL; |
| 3182 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3183 | mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3184 | CHECK(vtable != NULL); |
| 3185 | uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 3186 | CHECK(vtable_index < vtable->GetLength()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3187 | mirror::ArtMethod* res_method = vtable->Get(vtable_index); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3188 | CHECK(!Thread::Current()->IsExceptionPending()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3189 | return res_method; |
| 3190 | } |
| 3191 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3192 | mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3193 | bool is_range) { |
| 3194 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3195 | mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3196 | is_range); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3197 | if (res_method == NULL) { |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3198 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3199 | return NULL; |
| 3200 | } |
| 3201 | CHECK(!res_method->IsDirect() && !res_method->IsStatic()); |
| 3202 | |
| 3203 | // We use vAA as our expected arg count, rather than res_method->insSize, because we need to |
| 3204 | // match the call to the signature. Also, we might be calling through an abstract method |
| 3205 | // definition (which doesn't have register count values). |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3206 | const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range); |
| 3207 | if (actual_arg_type.IsConflict()) { // GetInvocationThis failed. |
| 3208 | return NULL; |
| 3209 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3210 | const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3211 | /* caught by static verifier */ |
| 3212 | DCHECK(is_range || expected_args <= 5); |
| 3213 | if (expected_args > code_item_->outs_size_) { |
| 3214 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args |
| 3215 | << ") exceeds outsSize (" << code_item_->outs_size_ << ")"; |
| 3216 | return NULL; |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Check the "this" argument, which must be an instance of the class that declared the method. |
| 3221 | * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a |
| 3222 | * rigorous check here (which is okay since we have to do it at runtime). |
| 3223 | */ |
| 3224 | if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) { |
| 3225 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized"; |
| 3226 | return NULL; |
| 3227 | } |
| 3228 | if (!actual_arg_type.IsZero()) { |
| 3229 | mirror::Class* klass = res_method->GetDeclaringClass(); |
| 3230 | const RegType& res_method_class = |
| 3231 | reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass, |
| 3232 | klass->CannotBeAssignedFromOtherTypes()); |
| 3233 | if (!res_method_class.IsAssignableFrom(actual_arg_type)) { |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 3234 | Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : |
| 3235 | VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3236 | << "' not instance of '" << res_method_class << "'"; |
| 3237 | return NULL; |
| 3238 | } |
| 3239 | } |
| 3240 | /* |
| 3241 | * Process the target method's signature. This signature may or may not |
| 3242 | * have been verified, so we can't assume it's properly formed. |
| 3243 | */ |
| 3244 | MethodHelper mh(res_method); |
| 3245 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 3246 | size_t params_size = params == NULL ? 0 : params->Size(); |
| 3247 | uint32_t arg[5]; |
| 3248 | if (!is_range) { |
| 3249 | inst->GetArgs(arg); |
| 3250 | } |
| 3251 | size_t actual_args = 1; |
| 3252 | for (size_t param_index = 0; param_index < params_size; param_index++) { |
| 3253 | if (actual_args >= expected_args) { |
| 3254 | 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] | 3255 | << "'. Expected " << expected_args |
| 3256 | << " arguments, processing argument " << actual_args |
| 3257 | << " (where longs/doubles count twice)."; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3258 | return NULL; |
| 3259 | } |
| 3260 | const char* descriptor = |
| 3261 | mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_); |
| 3262 | if (descriptor == NULL) { |
| 3263 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3264 | << " missing signature component"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3265 | return NULL; |
| 3266 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3267 | const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3268 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args]; |
| 3269 | if (!work_line_->VerifyRegisterType(get_reg, reg_type)) { |
| 3270 | return res_method; |
| 3271 | } |
| 3272 | actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1; |
| 3273 | } |
| 3274 | if (actual_args != expected_args) { |
| 3275 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method) |
| 3276 | << " expected " << expected_args << " arguments, found " << actual_args; |
| 3277 | return NULL; |
| 3278 | } else { |
| 3279 | return res_method; |
| 3280 | } |
| 3281 | } |
| 3282 | |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3283 | void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3284 | uint32_t type_idx; |
| 3285 | if (!is_filled) { |
| 3286 | DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY); |
| 3287 | type_idx = inst->VRegC_22c(); |
| 3288 | } else if (!is_range) { |
| 3289 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY); |
| 3290 | type_idx = inst->VRegB_35c(); |
| 3291 | } else { |
| 3292 | DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE); |
| 3293 | type_idx = inst->VRegB_3rc(); |
| 3294 | } |
| 3295 | const RegType& res_type = ResolveClassAndCheckAccess(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3296 | if (res_type.IsConflict()) { // bad class |
| 3297 | DCHECK_NE(failures_.size(), 0U); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3298 | } else { |
| 3299 | // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved |
| 3300 | if (!res_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3301 | 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] | 3302 | } else if (!is_filled) { |
| 3303 | /* make sure "size" register is valid type */ |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3304 | work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3305 | /* set register type to array class */ |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3306 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3307 | work_line_->SetRegisterType(inst->VRegA_22c(), precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3308 | } else { |
| 3309 | // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of |
| 3310 | // 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^] | 3311 | const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get()); |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3312 | uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 3313 | uint32_t arg[5]; |
| 3314 | if (!is_range) { |
| 3315 | inst->GetArgs(arg); |
| 3316 | } |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3317 | for (size_t ui = 0; ui < arg_count; ui++) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3318 | uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui]; |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3319 | if (!work_line_->VerifyRegisterType(get_reg, expected_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3320 | work_line_->SetResultRegisterType(reg_types_.Conflict()); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3321 | return; |
| 3322 | } |
| 3323 | } |
| 3324 | // filled-array result goes into "result" register |
Ian Rogers | 62342ec | 2013-06-11 10:26:37 -0700 | [diff] [blame] | 3325 | const RegType& precise_type = reg_types_.FromUninitialized(res_type); |
| 3326 | work_line_->SetResultRegisterType(precise_type); |
Ian Rogers | 0c4a506 | 2012-02-03 15:18:59 -0800 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3331 | void MethodVerifier::VerifyAGet(const Instruction* inst, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3332 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3333 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3334 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3335 | 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] | 3336 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3337 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3338 | if (array_type.IsZero()) { |
| 3339 | // Null array class; this code path will fail at runtime. Infer a merge-able type from the |
| 3340 | // instruction type. TODO: have a proper notion of bottom here. |
| 3341 | if (!is_primitive || insn_type.IsCategory1Types()) { |
| 3342 | // Reference or category 1 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3343 | work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3344 | } else { |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3345 | // Category 2 |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3346 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false), |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3347 | reg_types_.FromCat2ConstHi(0, false)); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3348 | } |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3349 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3350 | 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] | 3351 | } else { |
| 3352 | /* verify the class */ |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3353 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get()); |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3354 | if (!component_type.IsReferenceTypes() && !is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3355 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3356 | << " source for aget-object"; |
| 3357 | } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3358 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3359 | << " source for category 1 aget"; |
| 3360 | } else if (is_primitive && !insn_type.Equals(component_type) && |
| 3361 | !((insn_type.IsInteger() && component_type.IsFloat()) || |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3362 | (insn_type.IsLong() && component_type.IsDouble()))) { |
| 3363 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type |
| 3364 | << " incompatible with aget of type " << insn_type; |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3365 | } else { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3366 | // Use knowledge of the field type which is stronger than the type inferred from the |
| 3367 | // instruction, which can't differentiate object types and ints from floats, longs from |
| 3368 | // doubles. |
| 3369 | if (!component_type.IsLowHalf()) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3370 | work_line_->SetRegisterType(inst->VRegA_23x(), component_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3371 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3372 | work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3373 | component_type.HighHalf(®_types_)); |
| 3374 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3380 | void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type, |
| 3381 | const uint32_t vregA) { |
| 3382 | // Primitive assignability rules are weaker than regular assignability rules. |
| 3383 | bool instruction_compatible; |
| 3384 | bool value_compatible; |
| 3385 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3386 | if (target_type.IsIntegralTypes()) { |
Jeff Hao | a464748 | 2013-08-06 15:35:47 -0700 | [diff] [blame] | 3387 | instruction_compatible = target_type.Equals(insn_type); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3388 | value_compatible = value_type.IsIntegralTypes(); |
| 3389 | } else if (target_type.IsFloat()) { |
| 3390 | instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int |
| 3391 | value_compatible = value_type.IsFloatTypes(); |
| 3392 | } else if (target_type.IsLong()) { |
| 3393 | instruction_compatible = insn_type.IsLong(); |
| 3394 | value_compatible = value_type.IsLongTypes(); |
| 3395 | } else if (target_type.IsDouble()) { |
| 3396 | instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long |
| 3397 | value_compatible = value_type.IsDoubleTypes(); |
| 3398 | } else { |
| 3399 | instruction_compatible = false; // reference with primitive store |
| 3400 | value_compatible = false; // unused |
| 3401 | } |
| 3402 | if (!instruction_compatible) { |
| 3403 | // This is a global failure rather than a class change failure as the instructions and |
| 3404 | // the descriptors for the type should have been consistent within the same file at |
| 3405 | // compile time. |
| 3406 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type |
| 3407 | << "' but expected type '" << target_type << "'"; |
| 3408 | return; |
| 3409 | } |
| 3410 | if (!value_compatible) { |
| 3411 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3412 | << " of type " << value_type << " but expected " << target_type << " for put"; |
| 3413 | return; |
| 3414 | } |
| 3415 | } |
| 3416 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3417 | void MethodVerifier::VerifyAPut(const Instruction* inst, |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3418 | const RegType& insn_type, bool is_primitive) { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3419 | const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3420 | if (!index_type.IsArrayIndexTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3421 | 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] | 3422 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3423 | const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x()); |
Ian Rogers | 89310de | 2012-02-01 13:47:30 -0800 | [diff] [blame] | 3424 | if (array_type.IsZero()) { |
| 3425 | // Null array type; this code path will fail at runtime. Infer a merge-able type from the |
| 3426 | // instruction type. |
jeffhao | fc3144e | 2012-02-01 17:21:15 -0800 | [diff] [blame] | 3427 | } else if (!array_type.IsArrayTypes()) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3428 | 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] | 3429 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3430 | const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get()); |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3431 | const uint32_t vregA = inst->VRegA_23x(); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3432 | if (is_primitive) { |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3433 | VerifyPrimitivePut(component_type, insn_type, vregA); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3434 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3435 | if (!component_type.IsReferenceTypes()) { |
| 3436 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type |
| 3437 | << " source for aput-object"; |
| 3438 | } else { |
| 3439 | // The instruction agrees with the type of array, confirm the value to be stored does too |
| 3440 | // Note: we use the instruction type (rather than the component type) for aput-object as |
| 3441 | // incompatible classes will be caught at runtime as an array store exception |
Jeff Hao | fe1f7c8 | 2013-08-01 14:50:24 -0700 | [diff] [blame] | 3442 | work_line_->VerifyRegisterType(vregA, insn_type); |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3443 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | } |
| 3448 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3449 | mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3450 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3451 | // Check access to class |
| 3452 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3453 | if (klass_type.IsConflict()) { // bad class |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3454 | AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s", |
| 3455 | field_idx, dex_file_->GetFieldName(field_id), |
| 3456 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3457 | return NULL; |
| 3458 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 3459 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3460 | 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] | 3461 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3462 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3463 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3464 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3465 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3466 | VLOG(verifier) << "Unable to resolve static field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3467 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3468 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3469 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3470 | Thread::Current()->ClearException(); |
| 3471 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3472 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3473 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3474 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3475 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3476 | return NULL; |
| 3477 | } else if (!field->IsStatic()) { |
| 3478 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static"; |
| 3479 | return NULL; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3480 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3481 | return field; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3484 | mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3485 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3486 | // Check access to class |
| 3487 | const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3488 | if (klass_type.IsConflict()) { |
| 3489 | AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s", |
| 3490 | field_idx, dex_file_->GetFieldName(field_id), |
| 3491 | dex_file_->GetFieldDeclaringClassDescriptor(field_id))); |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3492 | return NULL; |
| 3493 | } |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 3494 | if (klass_type.IsUnresolvedTypes()) { |
Ian Rogers | 9004019 | 2011-12-16 08:54:29 -0800 | [diff] [blame] | 3495 | return NULL; // Can't resolve Class so no more to do here |
| 3496 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3497 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 3498 | mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_, |
| 3499 | *class_loader_); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3500 | if (field == NULL) { |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 3501 | VLOG(verifier) << "Unable to resolve instance field " << field_idx << " (" |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3502 | << dex_file_->GetFieldName(field_id) << ") in " |
| 3503 | << dex_file_->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3504 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3505 | Thread::Current()->ClearException(); |
| 3506 | return NULL; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3507 | } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(), |
| 3508 | field->GetAccessFlags())) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3509 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field) |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3510 | << " from " << GetDeclaringClass(); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3511 | return NULL; |
| 3512 | } else if (field->IsStatic()) { |
| 3513 | Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) |
| 3514 | << " to not be static"; |
| 3515 | return NULL; |
| 3516 | } else if (obj_type.IsZero()) { |
| 3517 | // Cannot infer and check type, however, access will cause null pointer exception |
| 3518 | return field; |
Ian Rogers | e1758fe | 2012-04-19 11:31:15 -0700 | [diff] [blame] | 3519 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3520 | mirror::Class* klass = field->GetDeclaringClass(); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3521 | const RegType& field_klass = |
| 3522 | reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id), |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3523 | klass, klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3524 | if (obj_type.IsUninitializedTypes() && |
| 3525 | (!IsConstructor() || GetDeclaringClass().Equals(obj_type) || |
| 3526 | !field_klass.Equals(GetDeclaringClass()))) { |
| 3527 | // Field accesses through uninitialized references are only allowable for constructors where |
| 3528 | // the field is declared in this class |
| 3529 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field) |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3530 | << " of a not fully initialized object within the context" |
| 3531 | << " of " << PrettyMethod(dex_method_idx_, *dex_file_); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3532 | return NULL; |
| 3533 | } else if (!field_klass.IsAssignableFrom(obj_type)) { |
| 3534 | // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class |
| 3535 | // of C1. For resolution to occur the declared class of the field must be compatible with |
| 3536 | // obj_type, we've discovered this wasn't so, so report the field didn't exist. |
| 3537 | Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field) |
| 3538 | << " from object of type " << obj_type; |
| 3539 | return NULL; |
| 3540 | } else { |
| 3541 | return field; |
| 3542 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3543 | } |
| 3544 | } |
| 3545 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3546 | void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type, |
| 3547 | bool is_primitive, bool is_static) { |
| 3548 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3549 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3550 | if (is_static) { |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3551 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3552 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3553 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | f4028cc | 2011-11-02 14:56:39 -0700 | [diff] [blame] | 3554 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3555 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3556 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3557 | if (field != NULL) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3558 | FieldHelper fh(field); |
| 3559 | mirror::Class* field_type_class = fh.GetType(false); |
| 3560 | if (field_type_class != nullptr) { |
| 3561 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3562 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3563 | } |
Ian Rogers | 0d60484 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3564 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3565 | if (field_type == nullptr) { |
| 3566 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3567 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3568 | field_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3569 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3570 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3571 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3572 | if (field_type->Equals(insn_type) || |
| 3573 | (field_type->IsFloat() && insn_type.IsInteger()) || |
| 3574 | (field_type->IsDouble() && insn_type.IsLong())) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3575 | // expected that read is of the correct primitive type or that int reads are reading |
| 3576 | // floats or long reads are reading doubles |
| 3577 | } else { |
| 3578 | // This is a global failure rather than a class change failure as the instructions and |
| 3579 | // the descriptors for the type should have been consistent within the same file at |
| 3580 | // compile time |
| 3581 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
| 3582 | << " to be of type '" << insn_type |
| 3583 | << "' but found type '" << field_type << "' in get"; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3584 | return; |
| 3585 | } |
| 3586 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3587 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3588 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3589 | << " to be compatible with type '" << insn_type |
| 3590 | << "' but found type '" << field_type |
| 3591 | << "' in get-object"; |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3592 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3593 | return; |
| 3594 | } |
| 3595 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3596 | if (!field_type->IsLowHalf()) { |
| 3597 | work_line_->SetRegisterType(vregA, *field_type); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3598 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3599 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3600 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3601 | } |
| 3602 | |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3603 | void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type, |
| 3604 | bool is_primitive, bool is_static) { |
| 3605 | uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3606 | mirror::ArtField* field; |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3607 | if (is_static) { |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3608 | field = GetStaticField(field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3609 | } else { |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3610 | const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c()); |
Ian Rogers | 55d249f | 2011-11-02 16:48:09 -0700 | [diff] [blame] | 3611 | field = GetInstanceField(object_type, field_idx); |
Ian Rogers | b94a27b | 2011-10-26 00:33:41 -0700 | [diff] [blame] | 3612 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3613 | const RegType* field_type = nullptr; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3614 | if (field != NULL) { |
| 3615 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3616 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
| 3617 | << " from other class " << GetDeclaringClass(); |
| 3618 | return; |
| 3619 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3620 | FieldHelper fh(field); |
| 3621 | mirror::Class* field_type_class = fh.GetType(false); |
| 3622 | if (field_type_class != nullptr) { |
| 3623 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3624 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3625 | } |
| 3626 | } |
| 3627 | if (field_type == nullptr) { |
| 3628 | const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx); |
| 3629 | const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3630 | field_type = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3631 | } |
Sebastien Hertz | 5243e91 | 2013-05-21 10:55:07 +0200 | [diff] [blame] | 3632 | const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c(); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3633 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3634 | VerifyPrimitivePut(*field_type, insn_type, vregA); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3635 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3636 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3637 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
| 3638 | << " to be compatible with type '" << insn_type |
| 3639 | << "' but found type '" << field_type |
| 3640 | << "' in put-object"; |
| 3641 | return; |
| 3642 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3643 | work_line_->VerifyRegisterType(vregA, *field_type); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3647 | // Look for an instance field with this offset. |
| 3648 | // 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] | 3649 | static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass, |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3650 | uint32_t field_offset) |
| 3651 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3652 | const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3653 | if (instance_fields != NULL) { |
| 3654 | for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3655 | mirror::ArtField* field = instance_fields->Get(i); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3656 | if (field->GetOffset().Uint32Value() == field_offset) { |
| 3657 | return field; |
| 3658 | } |
| 3659 | } |
| 3660 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3661 | // We did not find field in class: look into superclass. |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3662 | if (klass->GetSuperClass() != NULL) { |
| 3663 | return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset); |
| 3664 | } else { |
| 3665 | return NULL; |
| 3666 | } |
| 3667 | } |
| 3668 | |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3669 | // Returns the access field of a quick field access (iget/iput-quick) or NULL |
| 3670 | // if it cannot be found. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3671 | mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst, |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3672 | RegisterLine* reg_line) { |
| 3673 | DCHECK(inst->Opcode() == Instruction::IGET_QUICK || |
| 3674 | inst->Opcode() == Instruction::IGET_WIDE_QUICK || |
| 3675 | inst->Opcode() == Instruction::IGET_OBJECT_QUICK || |
| 3676 | inst->Opcode() == Instruction::IPUT_QUICK || |
| 3677 | inst->Opcode() == Instruction::IPUT_WIDE_QUICK || |
| 3678 | inst->Opcode() == Instruction::IPUT_OBJECT_QUICK); |
| 3679 | const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c()); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3680 | mirror::Class* object_class = NULL; |
| 3681 | if (!object_type.IsUnresolvedTypes()) { |
| 3682 | object_class = object_type.GetClass(); |
| 3683 | } else { |
| 3684 | // We need to resolve the class from its descriptor. |
| 3685 | const std::string& descriptor(object_type.GetDescriptor()); |
| 3686 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3687 | Thread* self = Thread::Current(); |
| 3688 | object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3689 | if (object_class == NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3690 | self->ClearException(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3691 | // Look for a system class |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3692 | SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr); |
| 3693 | object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3694 | } |
| 3695 | } |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3696 | if (object_class == NULL) { |
| 3697 | // Failed to get the Class* from reg type. |
| 3698 | LOG(WARNING) << "Failed to get Class* from " << object_type; |
| 3699 | return NULL; |
| 3700 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3701 | uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3702 | return FindInstanceFieldWithOffset(object_class, field_offset); |
| 3703 | } |
| 3704 | |
| 3705 | void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type, |
| 3706 | bool is_primitive) { |
| 3707 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3708 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3709 | if (field == NULL) { |
| 3710 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3711 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3712 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3713 | FieldHelper fh(field); |
| 3714 | mirror::Class* field_type_class = fh.GetType(false); |
| 3715 | const RegType* field_type; |
| 3716 | if (field_type_class != nullptr) { |
| 3717 | field_type = ®_types_.FromClass(fh.GetTypeDescriptor(), field_type_class, |
| 3718 | field_type_class->CannotBeAssignedFromOtherTypes()); |
| 3719 | } else { |
| 3720 | field_type = ®_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(), |
| 3721 | fh.GetTypeDescriptor(), false); |
| 3722 | } |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3723 | const uint32_t vregA = inst->VRegA_22c(); |
| 3724 | if (is_primitive) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3725 | if (field_type->Equals(insn_type) || |
| 3726 | (field_type->IsFloat() && insn_type.IsIntegralTypes()) || |
| 3727 | (field_type->IsDouble() && insn_type.IsLongTypes())) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3728 | // expected that read is of the correct primitive type or that int reads are reading |
| 3729 | // floats or long reads are reading doubles |
| 3730 | } else { |
| 3731 | // This is a global failure rather than a class change failure as the instructions and |
| 3732 | // the descriptors for the type should have been consistent within the same file at |
| 3733 | // compile time |
| 3734 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3735 | << " to be of type '" << insn_type |
| 3736 | << "' but found type '" << field_type << "' in get"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3737 | return; |
| 3738 | } |
| 3739 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3740 | if (!insn_type.IsAssignableFrom(*field_type)) { |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3741 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3742 | << " to be compatible with type '" << insn_type |
| 3743 | << "' but found type '" << field_type |
| 3744 | << "' in get-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3745 | work_line_->SetRegisterType(vregA, reg_types_.Conflict()); |
| 3746 | return; |
| 3747 | } |
| 3748 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3749 | if (!field_type->IsLowHalf()) { |
| 3750 | work_line_->SetRegisterType(vregA, *field_type); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3751 | } else { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3752 | work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(®_types_)); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type, |
| 3757 | bool is_primitive) { |
| 3758 | DCHECK(Runtime::Current()->IsStarted()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3759 | mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get()); |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3760 | if (field == NULL) { |
| 3761 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name(); |
| 3762 | return; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3763 | } |
| 3764 | const char* descriptor = FieldHelper(field).GetTypeDescriptor(); |
| 3765 | mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader(); |
| 3766 | const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false); |
| 3767 | if (field != NULL) { |
| 3768 | if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) { |
| 3769 | Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3770 | << " from other class " << GetDeclaringClass(); |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3771 | return; |
| 3772 | } |
| 3773 | } |
| 3774 | const uint32_t vregA = inst->VRegA_22c(); |
| 3775 | if (is_primitive) { |
| 3776 | // Primitive field assignability rules are weaker than regular assignability rules |
| 3777 | bool instruction_compatible; |
| 3778 | bool value_compatible; |
| 3779 | const RegType& value_type = work_line_->GetRegisterType(vregA); |
| 3780 | if (field_type.IsIntegralTypes()) { |
| 3781 | instruction_compatible = insn_type.IsIntegralTypes(); |
| 3782 | value_compatible = value_type.IsIntegralTypes(); |
| 3783 | } else if (field_type.IsFloat()) { |
| 3784 | instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int |
| 3785 | value_compatible = value_type.IsFloatTypes(); |
| 3786 | } else if (field_type.IsLong()) { |
| 3787 | instruction_compatible = insn_type.IsLong(); |
| 3788 | value_compatible = value_type.IsLongTypes(); |
| 3789 | } else if (field_type.IsDouble()) { |
| 3790 | instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long |
| 3791 | value_compatible = value_type.IsDoubleTypes(); |
| 3792 | } else { |
| 3793 | instruction_compatible = false; // reference field with primitive store |
| 3794 | value_compatible = false; // unused |
| 3795 | } |
| 3796 | if (!instruction_compatible) { |
| 3797 | // This is a global failure rather than a class change failure as the instructions and |
| 3798 | // the descriptors for the type should have been consistent within the same file at |
| 3799 | // compile time |
| 3800 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3801 | << " to be of type '" << insn_type |
| 3802 | << "' but found type '" << field_type |
| 3803 | << "' in put"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3804 | return; |
| 3805 | } |
| 3806 | if (!value_compatible) { |
| 3807 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA |
| 3808 | << " of type " << value_type |
| 3809 | << " but expected " << field_type |
| 3810 | << " for store to " << PrettyField(field) << " in put"; |
| 3811 | return; |
| 3812 | } |
| 3813 | } else { |
| 3814 | if (!insn_type.IsAssignableFrom(field_type)) { |
| 3815 | Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field) |
Sebastien Hertz | c15853b | 2013-06-25 17:36:27 +0200 | [diff] [blame] | 3816 | << " to be compatible with type '" << insn_type |
| 3817 | << "' but found type '" << field_type |
| 3818 | << "' in put-object"; |
Sebastien Hertz | 2d6ba51 | 2013-05-17 11:31:37 +0200 | [diff] [blame] | 3819 | return; |
| 3820 | } |
| 3821 | work_line_->VerifyRegisterType(vregA, field_type); |
| 3822 | } |
| 3823 | } |
| 3824 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3825 | bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3826 | if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 3827 | Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception"; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3828 | return false; |
| 3829 | } |
| 3830 | return true; |
| 3831 | } |
| 3832 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3833 | bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3834 | bool changed = true; |
| 3835 | RegisterLine* target_line = reg_table_.GetLine(next_insn); |
| 3836 | if (!insn_flags_[next_insn].IsVisitedOrChanged()) { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3837 | /* |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3838 | * We haven't processed this instruction before, and we haven't touched the registers here, so |
| 3839 | * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the |
| 3840 | * 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] | 3841 | */ |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3842 | if (!insn_flags_[next_insn].IsReturn()) { |
| 3843 | target_line->CopyFromLine(merge_line); |
| 3844 | } else { |
Jeff Hao | b24b4a7 | 2013-07-31 13:47:31 -0700 | [diff] [blame] | 3845 | // Verify that the monitor stack is empty on return. |
| 3846 | if (!merge_line->VerifyMonitorStackEmpty()) { |
| 3847 | return false; |
| 3848 | } |
Ian Rogers | b8c7859 | 2013-07-25 23:52:52 +0000 | [diff] [blame] | 3849 | // For returns we only care about the operand to the return, all other registers are dead. |
| 3850 | // Initialize them as conflicts so they don't add to GC and deoptimization information. |
| 3851 | const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn); |
| 3852 | Instruction::Code opcode = ret_inst->Opcode(); |
| 3853 | if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) { |
| 3854 | target_line->MarkAllRegistersAsConflicts(); |
| 3855 | } else { |
| 3856 | target_line->CopyFromLine(merge_line); |
| 3857 | if (opcode == Instruction::RETURN_WIDE) { |
| 3858 | target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x()); |
| 3859 | } else { |
| 3860 | target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x()); |
| 3861 | } |
| 3862 | } |
| 3863 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3864 | } else { |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3865 | UniquePtr<RegisterLine> copy(gDebugVerify ? |
Ian Rogers | d0fbd85 | 2013-09-24 18:17:04 -0700 | [diff] [blame] | 3866 | RegisterLine::Create(target_line->NumRegs(), this) : |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3867 | NULL); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 3868 | if (gDebugVerify) { |
| 3869 | copy->CopyFromLine(target_line); |
| 3870 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3871 | changed = target_line->MergeRegisters(merge_line); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3872 | if (have_pending_hard_failure_) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3873 | return false; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3874 | } |
Ian Rogers | 2c8a857 | 2011-10-24 17:11:36 -0700 | [diff] [blame] | 3875 | if (gDebugVerify && changed) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 3876 | LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]" |
Elliott Hughes | c073b07 | 2012-05-24 19:29:17 -0700 | [diff] [blame] | 3877 | << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n" |
| 3878 | << *copy.get() << " MERGE\n" |
| 3879 | << *merge_line << " ==\n" |
| 3880 | << *target_line << "\n"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3881 | } |
| 3882 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3883 | if (changed) { |
| 3884 | insn_flags_[next_insn].SetChanged(); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3885 | } |
| 3886 | return true; |
| 3887 | } |
| 3888 | |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 3889 | InstructionFlags* MethodVerifier::CurrentInsnFlags() { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3890 | return &insn_flags_[work_insn_idx_]; |
| 3891 | } |
| 3892 | |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3893 | const RegType& MethodVerifier::GetMethodReturnType() { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3894 | if (return_type_ == nullptr) { |
| 3895 | if (mirror_method_ != NULL) { |
| 3896 | MethodHelper mh(mirror_method_); |
| 3897 | mirror::Class* return_type_class = mh.GetReturnType(); |
| 3898 | if (return_type_class != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3899 | return_type_ = ®_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class, |
| 3900 | return_type_class->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3901 | } else { |
| 3902 | Thread* self = Thread::Current(); |
| 3903 | DCHECK(self->IsExceptionPending()); |
| 3904 | self->ClearException(); |
| 3905 | } |
| 3906 | } |
| 3907 | if (return_type_ == nullptr) { |
| 3908 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
| 3909 | const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id); |
| 3910 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 3911 | const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3912 | return_type_ = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3913 | } |
| 3914 | } |
| 3915 | return *return_type_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | const RegType& MethodVerifier::GetDeclaringClass() { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3919 | if (declaring_class_ == NULL) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3920 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_); |
Brian Carlstrom | 93c3396 | 2013-07-26 10:37:43 -0700 | [diff] [blame] | 3921 | const char* descriptor |
| 3922 | = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_)); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3923 | if (mirror_method_ != NULL) { |
| 3924 | mirror::Class* klass = mirror_method_->GetDeclaringClass(); |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 3925 | declaring_class_ = ®_types_.FromClass(descriptor, klass, |
| 3926 | klass->CannotBeAssignedFromOtherTypes()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3927 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3928 | declaring_class_ = ®_types_.FromDescriptor(class_loader_->get(), descriptor, false); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3929 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3930 | } |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 3931 | return *declaring_class_; |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 3932 | } |
| 3933 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 3934 | void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits, |
Ian Rogers | 6d376ae | 2013-07-23 15:12:40 -0700 | [diff] [blame] | 3935 | size_t* log2_max_gc_pc) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3936 | size_t local_gc_points = 0; |
| 3937 | size_t max_insn = 0; |
| 3938 | size_t max_ref_reg = -1; |
| 3939 | 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] | 3940 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3941 | local_gc_points++; |
| 3942 | max_insn = i; |
| 3943 | RegisterLine* line = reg_table_.GetLine(i); |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 3944 | max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3945 | } |
| 3946 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3947 | *gc_points = local_gc_points; |
| 3948 | *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1) |
| 3949 | size_t i = 0; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 3950 | while ((1U << i) <= max_insn) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 3951 | i++; |
| 3952 | } |
| 3953 | *log2_max_gc_pc = i; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3954 | } |
| 3955 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3956 | MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() { |
| 3957 | /* |
| 3958 | * Walks over the method code and adds any cast instructions in which |
| 3959 | * the type cast is implicit to a set, which is used in the code generation |
| 3960 | * to elide these casts. |
| 3961 | */ |
| 3962 | if (!failure_messages_.empty()) { |
| 3963 | return NULL; |
| 3964 | } |
| 3965 | UniquePtr<MethodSafeCastSet> mscs; |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3966 | const Instruction* inst = Instruction::At(code_item_->insns_); |
| 3967 | const Instruction* end = Instruction::At(code_item_->insns_ + |
Ian Rogers | fae370a | 2013-06-05 08:33:27 -0700 | [diff] [blame] | 3968 | code_item_->insns_size_in_code_units_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3969 | |
| 3970 | for (; inst < end; inst = inst->Next()) { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3971 | Instruction::Code code = inst->Opcode(); |
| 3972 | if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) { |
| 3973 | uint32_t dex_pc = inst->GetDexPc(code_item_->insns_); |
| 3974 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 3975 | bool is_safe_cast = false; |
| 3976 | if (code == Instruction::CHECK_CAST) { |
| 3977 | const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c())); |
| 3978 | const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c()); |
| 3979 | is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type); |
| 3980 | } else { |
| 3981 | const RegType& array_type(line->GetRegisterType(inst->VRegB_23x())); |
| 3982 | // We only know its safe to assign to an array if the array type is precise. For example, |
| 3983 | // an Object[] can have any type of object stored in it, but it may also be assigned a |
| 3984 | // String[] in which case the stores need to be of Strings. |
| 3985 | if (array_type.IsPreciseReference()) { |
| 3986 | const RegType& value_type(line->GetRegisterType(inst->VRegA_23x())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 3987 | const RegType& component_type(reg_types_.GetComponentType(array_type, |
| 3988 | class_loader_->get())); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3989 | is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type); |
| 3990 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3991 | } |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 3992 | if (is_safe_cast) { |
| 3993 | if (mscs.get() == NULL) { |
| 3994 | mscs.reset(new MethodSafeCastSet()); |
| 3995 | } |
| 3996 | mscs->insert(dex_pc); |
| 3997 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 3998 | } |
| 3999 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4000 | return mscs.release(); |
| 4001 | } |
| 4002 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4003 | MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4004 | // It is risky to rely on reg_types for sharpening in cases of soft |
| 4005 | // verification, we might end up sharpening to a wrong implementation. Just abort. |
| 4006 | if (!failure_messages_.empty()) { |
| 4007 | return NULL; |
| 4008 | } |
| 4009 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4010 | UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map; |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 4011 | const uint16_t* insns = code_item_->insns_; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4012 | const Instruction* inst = Instruction::At(insns); |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4013 | 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] | 4014 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4015 | for (; inst < end; inst = inst->Next()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4016 | bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) || |
| 4017 | (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE); |
| 4018 | bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) || |
| 4019 | (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
| 4020 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 4021 | if (!is_interface && !is_virtual) { |
Dragos Sbirlea | 29e2e7e | 2013-05-22 14:52:11 -0700 | [diff] [blame] | 4022 | continue; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4023 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4024 | // Get reg type for register holding the reference to the object that will be dispatched upon. |
| 4025 | uint32_t dex_pc = inst->GetDexPc(insns); |
| 4026 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 4027 | bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) || |
| 4028 | (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE); |
| 4029 | const RegType& |
| 4030 | 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] | 4031 | |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4032 | if (!reg_type.HasClass()) { |
| 4033 | // We will compute devirtualization information only when we know the Class of the reg type. |
| 4034 | continue; |
| 4035 | } |
| 4036 | mirror::Class* reg_class = reg_type.GetClass(); |
| 4037 | if (reg_class->IsInterface()) { |
| 4038 | // We can't devirtualize when the known type of the register is an interface. |
| 4039 | continue; |
| 4040 | } |
| 4041 | if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) { |
| 4042 | // We can't devirtualize abstract classes except on arrays of abstract classes. |
| 4043 | continue; |
| 4044 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 4045 | mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod( |
| 4046 | is_range ? inst->VRegB_3rc() : inst->VRegB_35c()); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 4047 | if (abstract_method == NULL) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4048 | // If the method is not found in the cache this means that it was never found |
| 4049 | // by ResolveMethodAndCheckAccess() called when verifying invoke_*. |
| 4050 | continue; |
| 4051 | } |
| 4052 | // Find the concrete method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4053 | mirror::ArtMethod* concrete_method = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4054 | if (is_interface) { |
| 4055 | concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method); |
| 4056 | } |
| 4057 | if (is_virtual) { |
| 4058 | concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method); |
| 4059 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4060 | if (concrete_method == NULL || concrete_method->IsAbstract()) { |
| 4061 | // 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] | 4062 | continue; |
| 4063 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4064 | if (reg_type.IsPreciseReference() || concrete_method->IsFinal() || |
| 4065 | concrete_method->GetDeclaringClass()->IsFinal()) { |
| 4066 | // If we knew exactly the class being dispatched upon, or if the target method cannot be |
| 4067 | // overridden record the target to be used in the compiler driver. |
| 4068 | if (pc_to_concrete_method_map.get() == NULL) { |
| 4069 | pc_to_concrete_method_map.reset(new PcToConcreteMethodMap()); |
| 4070 | } |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4071 | MethodReference concrete_ref( |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4072 | concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(), |
| 4073 | concrete_method->GetDexMethodIndex()); |
| 4074 | pc_to_concrete_method_map->Put(dex_pc, concrete_ref); |
| 4075 | } |
Dragos Sbirlea | 29e2e7e | 2013-05-22 14:52:11 -0700 | [diff] [blame] | 4076 | } |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4077 | return pc_to_concrete_method_map.release(); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4080 | const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4081 | size_t num_entries, ref_bitmap_bits, pc_bits; |
| 4082 | ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits); |
| 4083 | // There's a single byte to encode the size of each bitmap |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 4084 | if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4085 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4086 | 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] | 4087 | << ref_bitmap_bits << " registers"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4088 | return NULL; |
| 4089 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4090 | size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8; |
| 4091 | // There are 2 bytes to encode the number of entries |
| 4092 | if (num_entries >= 65536) { |
| 4093 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4094 | 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] | 4095 | << num_entries << " entries"; |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4096 | return NULL; |
| 4097 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4098 | size_t pc_bytes; |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4099 | RegisterMapFormat format; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 4100 | if (pc_bits <= 8) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4101 | format = kRegMapFormatCompact8; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4102 | pc_bytes = 1; |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 4103 | } else if (pc_bits <= 16) { |
jeffhao | d1f0fde | 2011-09-08 17:25:33 -0700 | [diff] [blame] | 4104 | format = kRegMapFormatCompact16; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4105 | pc_bytes = 2; |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4106 | } else { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4107 | // TODO: either a better GC map format or per method failures |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4108 | 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] | 4109 | << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)"; |
| 4110 | return NULL; |
| 4111 | } |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4112 | size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4113 | std::vector<uint8_t>* table = new std::vector<uint8_t>; |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4114 | if (table == NULL) { |
jeffhao | d5347e0 | 2012-03-22 17:25:05 -0700 | [diff] [blame] | 4115 | 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] | 4116 | return NULL; |
| 4117 | } |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 4118 | table->reserve(table_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4119 | // Write table header |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 4120 | table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) & |
| 4121 | ~DexPcToReferenceMap::kRegMapFormatMask)); |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 4122 | table->push_back(ref_bitmap_bytes & 0xFF); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4123 | table->push_back(num_entries & 0xFF); |
| 4124 | table->push_back((num_entries >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4125 | // Write table data |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4126 | 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] | 4127 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4128 | table->push_back(i & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4129 | if (pc_bytes == 2) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4130 | table->push_back((i >> 8) & 0xFF); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4131 | } |
| 4132 | RegisterLine* line = reg_table_.GetLine(i); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4133 | line->WriteReferenceBitMap(*table, ref_bitmap_bytes); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4134 | } |
| 4135 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4136 | DCHECK_EQ(table->size(), table_size); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4137 | return table; |
| 4138 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4139 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 4140 | void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4141 | // Check that for every GC point there is a map entry, there aren't entries for non-GC points, |
| 4142 | // that the table data is well formed and all references are marked (or not) in the bitmap |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 4143 | DexPcToReferenceMap map(&data[0], data.size()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4144 | size_t map_index = 0; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4145 | 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] | 4146 | const uint8_t* reg_bitmap = map.FindBitMap(i, false); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4147 | if (insn_flags_[i].IsCompileTimeInfoPoint()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4148 | CHECK_LT(map_index, map.NumEntries()); |
Ian Rogers | 46c6bb2 | 2012-09-18 13:47:36 -0700 | [diff] [blame] | 4149 | CHECK_EQ(map.GetDexPc(map_index), i); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4150 | CHECK_EQ(map.GetBitMap(map_index), reg_bitmap); |
| 4151 | map_index++; |
| 4152 | RegisterLine* line = reg_table_.GetLine(i); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 4153 | for (size_t j = 0; j < code_item_->registers_size_; j++) { |
Ian Rogers | 84fa074 | 2011-10-25 18:13:30 -0700 | [diff] [blame] | 4154 | if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4155 | CHECK_LT(j / 8, map.RegWidth()); |
| 4156 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1); |
| 4157 | } else if ((j / 8) < map.RegWidth()) { |
| 4158 | CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0); |
| 4159 | } else { |
| 4160 | // If a register doesn't contain a reference then the bitmap may be shorter than the line |
| 4161 | } |
| 4162 | } |
| 4163 | } else { |
| 4164 | CHECK(reg_bitmap == NULL); |
| 4165 | } |
| 4166 | } |
| 4167 | } |
jeffhao | a0a764a | 2011-09-16 10:43:38 -0700 | [diff] [blame] | 4168 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4169 | void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4170 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4171 | { |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4172 | WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4173 | DexGcMapTable::iterator it = dex_gc_maps_->find(ref); |
| 4174 | if (it != dex_gc_maps_->end()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4175 | delete it->second; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4176 | dex_gc_maps_->erase(it); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4177 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4178 | dex_gc_maps_->Put(ref, &gc_map); |
Brian Carlstrom | 73a15f4 | 2012-01-17 18:14:39 -0800 | [diff] [blame] | 4179 | } |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 4180 | DCHECK(GetDexGcMap(ref) != NULL); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4181 | } |
| 4182 | |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4183 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4184 | void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4185 | DCHECK(Runtime::Current()->IsCompiler()); |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4186 | WriterMutexLock mu(Thread::Current(), *safecast_map_lock_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4187 | SafeCastMap::iterator it = safecast_map_->find(ref); |
| 4188 | if (it != safecast_map_->end()) { |
| 4189 | delete it->second; |
| 4190 | safecast_map_->erase(it); |
| 4191 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4192 | safecast_map_->Put(ref, cast_set); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4193 | DCHECK(safecast_map_->find(ref) != safecast_map_->end()); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4194 | } |
| 4195 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4196 | bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4197 | DCHECK(Runtime::Current()->IsCompiler()); |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4198 | ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4199 | SafeCastMap::const_iterator it = safecast_map_->find(ref); |
| 4200 | if (it == safecast_map_->end()) { |
| 4201 | return false; |
| 4202 | } |
| 4203 | |
| 4204 | // Look up the cast address in the set of safe casts |
| 4205 | MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc); |
| 4206 | return cast_it != it->second->end(); |
| 4207 | } |
| 4208 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4209 | const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4210 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4211 | ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_); |
| 4212 | DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref); |
Ian Rogers | 0f40ac3 | 2013-08-13 22:10:30 -0700 | [diff] [blame] | 4213 | CHECK(it != dex_gc_maps_->end()) |
| 4214 | << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file); |
| 4215 | CHECK(it->second != NULL); |
| 4216 | return it->second; |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4217 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4218 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4219 | void MethodVerifier::SetDevirtMap(MethodReference ref, |
Ian Rogers | d058380 | 2013-06-01 10:51:46 -0700 | [diff] [blame] | 4220 | const PcToConcreteMethodMap* devirt_map) { |
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 | WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4223 | DevirtualizationMapTable::iterator it = devirt_maps_->find(ref); |
| 4224 | if (it != devirt_maps_->end()) { |
| 4225 | delete it->second; |
| 4226 | devirt_maps_->erase(it); |
| 4227 | } |
| 4228 | |
| 4229 | devirt_maps_->Put(ref, devirt_map); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4230 | DCHECK(devirt_maps_->find(ref) != devirt_maps_->end()); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4233 | const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref, |
Ian Rogers | e3cd2f0 | 2013-05-24 15:32:56 -0700 | [diff] [blame] | 4234 | uint32_t dex_pc) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4235 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4236 | ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_); |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4237 | DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref); |
| 4238 | if (it == devirt_maps_->end()) { |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | |
| 4242 | // 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] | 4243 | MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method |
| 4244 | = it->second->find(dex_pc); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 4245 | if (pc_to_concrete_method != it->second->end()) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4246 | return &(pc_to_concrete_method->second); |
| 4247 | } else { |
| 4248 | return NULL; |
| 4249 | } |
| 4250 | } |
| 4251 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4252 | std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) { |
| 4253 | RegisterLine* line = reg_table_.GetLine(dex_pc); |
| 4254 | std::vector<int32_t> result; |
| 4255 | for (size_t i = 0; i < line->NumRegs(); ++i) { |
| 4256 | const RegType& type = line->GetRegisterType(i); |
| 4257 | if (type.IsConstant()) { |
| 4258 | result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant); |
| 4259 | result.push_back(type.ConstantValue()); |
| 4260 | } else if (type.IsConstantLo()) { |
| 4261 | result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant); |
| 4262 | result.push_back(type.ConstantValueLo()); |
| 4263 | } else if (type.IsConstantHi()) { |
| 4264 | result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant); |
| 4265 | result.push_back(type.ConstantValueHi()); |
| 4266 | } else if (type.IsIntegralTypes()) { |
| 4267 | result.push_back(kIntVReg); |
| 4268 | result.push_back(0); |
| 4269 | } else if (type.IsFloat()) { |
| 4270 | result.push_back(kFloatVReg); |
| 4271 | result.push_back(0); |
| 4272 | } else if (type.IsLong()) { |
| 4273 | result.push_back(kLongLoVReg); |
| 4274 | result.push_back(0); |
| 4275 | result.push_back(kLongHiVReg); |
| 4276 | result.push_back(0); |
| 4277 | ++i; |
| 4278 | } else if (type.IsDouble()) { |
| 4279 | result.push_back(kDoubleLoVReg); |
| 4280 | result.push_back(0); |
| 4281 | result.push_back(kDoubleHiVReg); |
| 4282 | result.push_back(0); |
| 4283 | ++i; |
| 4284 | } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) { |
| 4285 | result.push_back(kUndefined); |
| 4286 | result.push_back(0); |
| 4287 | } else { |
Ian Rogers | 7b3ddd2 | 2013-02-21 15:19:52 -0800 | [diff] [blame] | 4288 | CHECK(type.IsNonZeroReferenceTypes()); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 4289 | result.push_back(kReferenceVReg); |
| 4290 | result.push_back(0); |
| 4291 | } |
| 4292 | } |
| 4293 | return result; |
| 4294 | } |
| 4295 | |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 4296 | bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref, |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4297 | const uint32_t access_flags) { |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 4298 | #ifdef ART_SEA_IR_MODE |
| 4299 | bool use_sea = Runtime::Current()->IsSeaIRMode(); |
| 4300 | use_sea = use_sea && (std::string::npos != PrettyMethod( |
| 4301 | method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci")); |
| 4302 | if (use_sea) return true; |
| 4303 | #endif |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4304 | // Don't compile class initializers, ever. |
| 4305 | if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) { |
| 4306 | return false; |
| 4307 | } |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 4308 | return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4309 | } |
| 4310 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4311 | ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 4312 | MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4313 | |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4314 | ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL; |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4315 | MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL; |
| 4316 | |
Ian Rogers | 637c65b | 2013-05-31 11:46:00 -0700 | [diff] [blame] | 4317 | ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4318 | MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL; |
| 4319 | |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4320 | ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4321 | MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL; |
| 4322 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4323 | void MethodVerifier::Init() { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4324 | if (Runtime::Current()->IsCompiler()) { |
| 4325 | dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock"); |
| 4326 | Thread* self = Thread::Current(); |
| 4327 | { |
| 4328 | WriterMutexLock mu(self, *dex_gc_maps_lock_); |
| 4329 | dex_gc_maps_ = new MethodVerifier::DexGcMapTable; |
| 4330 | } |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4331 | |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4332 | safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock"); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4333 | { |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4334 | WriterMutexLock mu(self, *safecast_map_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4335 | safecast_map_ = new MethodVerifier::SafeCastMap(); |
| 4336 | } |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4337 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4338 | devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock"); |
Dragos Sbirlea | 980d16b | 2013-06-04 15:01:40 -0700 | [diff] [blame] | 4339 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4340 | { |
| 4341 | WriterMutexLock mu(self, *devirt_maps_lock_); |
| 4342 | devirt_maps_ = new MethodVerifier::DevirtualizationMapTable(); |
| 4343 | } |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4344 | |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4345 | rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock"); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4346 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4347 | WriterMutexLock mu(self, *rejected_classes_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4348 | rejected_classes_ = new MethodVerifier::RejectedClassesTable; |
| 4349 | } |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4350 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4351 | art::verifier::RegTypeCache::Init(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4352 | } |
| 4353 | |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4354 | void MethodVerifier::Shutdown() { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4355 | if (Runtime::Current()->IsCompiler()) { |
| 4356 | Thread* self = Thread::Current(); |
| 4357 | { |
| 4358 | WriterMutexLock mu(self, *dex_gc_maps_lock_); |
| 4359 | STLDeleteValues(dex_gc_maps_); |
| 4360 | delete dex_gc_maps_; |
| 4361 | dex_gc_maps_ = NULL; |
| 4362 | } |
| 4363 | delete dex_gc_maps_lock_; |
| 4364 | dex_gc_maps_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4365 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4366 | { |
Sebastien Hertz | b9c37fb | 2013-08-05 17:47:40 +0200 | [diff] [blame] | 4367 | WriterMutexLock mu(self, *safecast_map_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4368 | STLDeleteValues(safecast_map_); |
| 4369 | delete safecast_map_; |
| 4370 | safecast_map_ = NULL; |
| 4371 | } |
| 4372 | delete safecast_map_lock_; |
| 4373 | safecast_map_lock_ = NULL; |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 4374 | |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4375 | { |
| 4376 | WriterMutexLock mu(self, *devirt_maps_lock_); |
| 4377 | STLDeleteValues(devirt_maps_); |
| 4378 | delete devirt_maps_; |
| 4379 | devirt_maps_ = NULL; |
| 4380 | } |
| 4381 | delete devirt_maps_lock_; |
| 4382 | devirt_maps_lock_ = NULL; |
| 4383 | |
| 4384 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4385 | WriterMutexLock mu(self, *rejected_classes_lock_); |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4386 | delete rejected_classes_; |
| 4387 | rejected_classes_ = NULL; |
| 4388 | } |
| 4389 | delete rejected_classes_lock_; |
| 4390 | rejected_classes_lock_ = NULL; |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4391 | } |
Sameer Abu Asal | 51a5fb7 | 2013-02-19 14:25:01 -0800 | [diff] [blame] | 4392 | verifier::RegTypeCache::ShutDown(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 4393 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4394 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4395 | void MethodVerifier::AddRejectedClass(ClassReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4396 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4397 | { |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4398 | WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4399 | rejected_classes_->insert(ref); |
| 4400 | } |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4401 | CHECK(IsClassRejected(ref)); |
| 4402 | } |
| 4403 | |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 4404 | bool MethodVerifier::IsClassRejected(ClassReference ref) { |
Sebastien Hertz | 4d4adb1 | 2013-07-24 16:14:19 +0200 | [diff] [blame] | 4405 | DCHECK(Runtime::Current()->IsCompiler()); |
Ian Rogers | b8a0b94 | 2013-08-20 18:09:52 -0700 | [diff] [blame] | 4406 | ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_); |
Elliott Hughes | 0a1038b | 2012-06-14 16:24:17 -0700 | [diff] [blame] | 4407 | return (rejected_classes_->find(ref) != rejected_classes_->end()); |
jeffhao | d1224c7 | 2012-02-29 13:43:08 -0800 | [diff] [blame] | 4408 | } |
| 4409 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 4410 | } // namespace verifier |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4411 | } // namespace art |