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