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