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