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