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