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