blob: c21a7a43f927b07b72f9e70bca4442211dadd3d7 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "field_helper.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogerse5877a12014-07-16 12:06:35 -070033#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070050static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070051static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070052// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070053
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070055 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070057 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070058 register_lines_.reset(new RegisterLine*[insns_size]());
59 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070060 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 Asal02c42232013-04-30 12:09:45 -070066 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070068 break;
69 case kTrackRegsBranches:
70 interesting = flags[i].IsBranchTarget();
71 break;
72 default:
73 break;
74 }
75 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070076 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
77 }
78 }
79}
80
81PcToRegisterLineTable::~PcToRegisterLineTable() {
82 for (size_t i = 0; i < size_; i++) {
83 delete register_lines_[i];
84 if (kIsDebugBuild) {
85 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
87 }
88}
89
Ian Rogersef7d42f2014-01-06 12:55:46 -080090MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070091 bool allow_soft_failures,
92 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070093 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070095 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080096 bool early_failure = false;
97 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -070098 const DexFile& dex_file = klass->GetDexFile();
99 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800100 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700101 std::string temp;
102 if (super == NULL && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800103 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 Marko2b5eaa22013-12-13 13:59:30 +0000116 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800117 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700118 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700119 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700121 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700123 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700124}
125
Ian Rogers365c1022012-06-22 15:05:28 -0700126MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700127 ConstHandle<mirror::DexCache> dex_cache,
128 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700129 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 Rogersad0b3a32012-04-16 14:50:24 -0700134 if (class_data == NULL) {
135 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700136 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700137 }
jeffhaof56197c2012-03-05 18:01:54 -0800138 ClassDataItemIterator it(*dex_file, class_data);
139 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
140 it.Next();
141 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700142 Thread* self = Thread::Current();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700143 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700144 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700145 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700146 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800147 while (it.HasNextDirectMethod()) {
148 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700149 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 Rogers8b2c0b92013-09-19 02:56:49 -0700156 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700157 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700158 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
159 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700160 if (method == NULL) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700161 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700162 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700163 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700164 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700165 StackHandleScope<1> hs(self);
166 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Brian Carlstrom93c33962013-07-26 10:37:43 -0700167 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
168 dex_file,
169 dex_cache,
170 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700172 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700173 h_method,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700174 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700175 allow_soft_failures,
176 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 if (result != kNoFailure) {
178 if (result == kHardFailure) {
179 hard_fail = true;
180 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700181 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700182 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700183 *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 Rogersad0b3a32012-04-16 14:50:24 -0700187 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800189 }
190 it.Next();
191 }
jeffhao9b0b1882012-10-01 16:51:22 -0700192 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800193 while (it.HasNextVirtualMethod()) {
194 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700195 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 Rogers8b2c0b92013-09-19 02:56:49 -0700202 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700203 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700204 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
205 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700206 if (method == NULL) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700207 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700209 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700210 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700211 StackHandleScope<1> hs(self);
212 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Brian Carlstrom93c33962013-07-26 10:37:43 -0700213 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
214 dex_file,
215 dex_cache,
216 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700217 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700218 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700219 h_method,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700221 allow_soft_failures,
222 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700223 if (result != kNoFailure) {
224 if (result == kHardFailure) {
225 hard_fail = true;
226 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700227 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700228 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700229 *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 Rogersad0b3a32012-04-16 14:50:24 -0700233 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700234 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800235 }
236 it.Next();
237 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700238 if (error_count == 0) {
239 return kNoFailure;
240 } else {
241 return hard_fail ? kHardFailure : kSoftFailure;
242 }
jeffhaof56197c2012-03-05 18:01:54 -0800243}
244
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
246 const DexFile* dex_file,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700247 ConstHandle<mirror::DexCache> dex_cache,
248 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700249 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 const DexFile::CodeItem* code_item,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700251 ConstHandle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700252 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700253 bool allow_soft_failures,
254 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700255 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700256 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700257
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700258 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700259 method_idx, method, method_access_flags, true, allow_soft_failures,
260 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700261 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 // Verification completed, however failures may be pending that didn't cause the verification
263 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700264 CHECK(!verifier.have_pending_hard_failure_);
265 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700266 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700267 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700268 << PrettyMethod(method_idx, *dex_file) << "\n");
269 }
Ian Rogersc8982582012-09-07 16:53:25 -0700270 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800271 }
272 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700273 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700274 CHECK_NE(verifier.failures_.size(), 0U);
275 CHECK(verifier.have_pending_hard_failure_);
276 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700277 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800278 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700279 std::cout << "\n" << verifier.info_messages_.str();
280 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
Ian Rogersc8982582012-09-07 16:53:25 -0700282 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800283 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700284 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 Rogersc8982582012-09-07 16:53:25 -0700290 }
291 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800292}
293
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800294void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700295 const DexFile* dex_file,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700296 ConstHandle<mirror::DexCache> dex_cache,
297 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700298 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 const DexFile::CodeItem* code_item,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700300 ConstHandle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800301 uint32_t method_access_flags) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700302 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700303 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800305 verifier.DumpFailures(os);
306 os << verifier.info_messages_.str();
307 verifier.Dump(os);
308}
309
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700310MethodVerifier::MethodVerifier(const DexFile* dex_file, ConstHandle<mirror::DexCache> dex_cache,
311 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700312 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700313 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700314 ConstHandle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700315 bool can_load_classes, bool allow_soft_failures,
316 bool need_precise_constants)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800317 : reg_types_(can_load_classes),
318 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800319 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700320 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700321 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700322 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800323 dex_file_(dex_file),
324 dex_cache_(dex_cache),
325 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700326 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800327 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700328 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700329 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700330 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700331 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700332 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800333 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800334 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700335 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200336 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700337 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200338 has_check_casts_(false),
339 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800340 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700341 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800342}
343
Mathieu Chartier590fee92013-09-13 13:46:47 -0700344MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800345 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700346 STLDeleteElements(&failure_messages_);
347}
348
Brian Carlstromea46f952013-07-30 01:26:50 -0700349void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700350 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700351 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700352 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
353 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700354 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700355 MethodVerifier verifier(m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700356 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
357 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700358 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700359 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700360 verifier.FindLocksAtDexPc();
361}
362
363void MethodVerifier::FindLocksAtDexPc() {
364 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700365 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700366
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 Carlstromea46f952013-07-30 01:26:50 -0700374mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700375 uint32_t dex_pc) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700376 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700377 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
378 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700379 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700380 MethodVerifier verifier(m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700381 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
382 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200383 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200384}
385
Brian Carlstromea46f952013-07-30 01:26:50 -0700386mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700387 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388
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 Hertzc15853b2013-06-25 17:36:27 +0200393 bool success = Verify();
394 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700395 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200396 }
397 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
398 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700399 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200400 }
401 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
402 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403}
404
Brian Carlstromea46f952013-07-30 01:26:50 -0700405mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700406 uint32_t dex_pc) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700407 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700408 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
409 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700410 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700411 MethodVerifier verifier(m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700412 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
413 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200414 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200415}
416
Brian Carlstromea46f952013-07-30 01:26:50 -0700417mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700418 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419
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 Hertzc15853b2013-06-25 17:36:27 +0200424 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 Hertz2d6ba512013-05-17 11:31:37 +0200435}
436
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700438 // If there aren't any instructions, make sure that's expected, then exit successfully.
439 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700440 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700442 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700443 } else {
444 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700445 }
jeffhaobdb76512011-09-07 11:43:16 -0700446 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700447 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
448 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700449 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
450 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700452 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700453 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800454 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700455 // 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 Rogersad0b3a32012-04-16 14:50:24 -0700461 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000462 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;
jeffhaoba5ebb92011-08-25 17:24:37 -0700468}
469
Ian Rogers776ac1f2012-04-13 23:36:36 -0700470std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 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 Rogers08f753d2012-08-24 14:35:25 -0700478 case VERIFY_ERROR_INSTANTIATION:
479 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800480 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700481 // 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 Haoa3faaf42013-09-03 19:07:00 -0700490 // 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.
jeffhaofaf459e2012-08-31 15:32:47 -0700492 have_pending_runtime_throw_failure_ = true;
493 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700494 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 // Indication that verification should be retried at runtime.
496 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700497 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700498 have_pending_hard_failure_ = true;
499 }
500 break;
jeffhaod5347e02012-03-22 17:25:05 -0700501 // 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 Rogersad0b3a32012-04-16 14:50:24 -0700503 case VERIFY_ERROR_BAD_CLASS_HARD: {
504 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700505 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000506 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800507 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700508 have_pending_hard_failure_ = true;
509 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800510 }
511 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700512 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700513 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700514 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700515 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700516 failure_messages_.push_back(failure_message);
517 return *failure_message;
518}
519
Ian Rogers576ca0c2014-06-06 15:58:22 -0700520std::ostream& MethodVerifier::LogVerifyInfo() {
521 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
522 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
523}
524
Ian Rogersad0b3a32012-04-16 14:50:24 -0700525void 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 Sayapina78480ec2014-08-15 15:52:42 +0700530 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700531 delete last_fail_message;
532}
533
534void 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 Rogers47a05882012-02-03 12:23:33 -0800539}
540
Ian Rogers776ac1f2012-04-13 23:36:36 -0700541bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 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);
jeffhaobdb76512011-09-07 11:43:16 -0700545 size_t new_instance_count = 0;
546 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700548
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700550 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700551 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;
jeffhaobdb76512011-09-07 11:43:16 -0700570 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 size_t inst_size = inst->SizeInCodeUnits();
572 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
573 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700574 inst = inst->Next();
575 }
576
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700578 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
579 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700580 return false;
581 }
582
Ian Rogersd81871c2011-10-03 13:57:23 -0700583 new_instance_count_ = new_instance_count;
584 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700585 return true;
586}
587
Ian Rogers776ac1f2012-04-13 23:36:36 -0700588bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700590 if (tries_size == 0) {
591 return true;
592 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700594 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700595
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_;
jeffhaobdb76512011-09-07 11:43:16 -0700600 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
602 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700603 return false;
604 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700606 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
607 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700608 return false;
609 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 for (uint32_t dex_pc = start; dex_pc < end;
611 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
612 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700613 }
614 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800615 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700616 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700617 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700618 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700619 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700620 CatchHandlerIterator iterator(handlers_ptr);
621 for (; iterator.HasNext(); iterator.Next()) {
622 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700624 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
625 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700626 return false;
627 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700629 // 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 Rogers0571d352011-11-03 19:51:38 -0700631 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
633 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700634 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700635 if (exception_type == NULL) {
636 DCHECK(Thread::Current()->IsExceptionPending());
637 Thread::Current()->ClearException();
638 }
639 }
jeffhaobdb76512011-09-07 11:43:16 -0700640 }
Ian Rogers0571d352011-11-03 19:51:38 -0700641 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700642 }
jeffhaobdb76512011-09-07 11:43:16 -0700643 return true;
644}
645
Ian Rogers776ac1f2012-04-13 23:36:36 -0700646bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700648
Ian Rogers0c7abda2012-09-19 13:33:42 -0700649 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700651 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700652
653 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700654 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700656 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 return false;
658 }
659 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700660 // All invoke points are marked as "Throw" points already.
661 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000662 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700663 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000664 } else if (inst->IsReturn()) {
665 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 }
667 dex_pc += inst->SizeInCodeUnits();
668 inst = inst->Next();
669 }
670 return true;
671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 bool result = true;
675 switch (inst->GetVerifyTypeArgumentA()) {
676 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700677 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 break;
679 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700680 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 break;
682 }
683 switch (inst->GetVerifyTypeArgumentB()) {
684 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700685 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700688 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700691 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 break;
693 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700694 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 break;
696 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700697 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 break;
699 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700700 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 break;
702 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700703 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 break;
705 }
706 switch (inst->GetVerifyTypeArgumentC()) {
707 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700708 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 break;
710 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700711 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 break;
713 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700714 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 break;
716 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700717 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 break;
719 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700720 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 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 Gampec3314312014-06-19 18:13:29 -0700733 case Instruction::kVerifyVarArgNonZero:
734 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700735 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700736 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 Rogers29a26482014-05-02 15:27:29 -0700741 uint32_t args[Instruction::kMaxVarArgRegs];
742 inst->GetVarArgs(args);
743 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700745 }
Andreas Gampec3314312014-06-19 18:13:29 -0700746 case Instruction::kVerifyVarArgRangeNonZero:
747 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700749 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 Rogers29a26482014-05-02 15:27:29 -0700755 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 break;
757 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 result = false;
760 break;
761 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700762 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 Rogersd81871c2011-10-03 13:57:23 -0700766 return result;
767}
768
Ian Rogers776ac1f2012-04-13 23:36:36 -0700769bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
772 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 return false;
774 }
775 return true;
776}
777
Ian Rogers776ac1f2012-04-13 23:36:36 -0700778bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
781 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 return false;
783 }
784 return true;
785}
786
Ian Rogers776ac1f2012-04-13 23:36:36 -0700787bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
790 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 return true;
794}
795
Ian Rogers776ac1f2012-04-13 23:36:36 -0700796bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
799 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 return false;
801 }
802 return true;
803}
804
Ian Rogers776ac1f2012-04-13 23:36:36 -0700805bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
808 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 return false;
810 }
811 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700812 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 return true;
818}
819
Ian Rogers776ac1f2012-04-13 23:36:36 -0700820bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
823 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 return false;
825 }
826 return true;
827}
828
Ian Rogers776ac1f2012-04-13 23:36:36 -0700829bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
832 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 return false;
834 }
835 return true;
836}
837
Ian Rogers776ac1f2012-04-13 23:36:36 -0700838bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
841 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 return false;
843 }
844 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700845 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 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 Carlstrom93c33962013-07-26 10:37:43 -0700852 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
853 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 return false;
855 } else if (bracket_count > 255) {
856 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
858 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700859 return false;
860 }
861 return true;
862}
863
Ian Rogers776ac1f2012-04-13 23:36:36 -0700864bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 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) {
jeffhaod5347e02012-03-22 17:25:05 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700876 << ", data offset " << array_data_offset
877 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 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 Rogersef7d42f2014-01-06 12:55:46 -0800883 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
885 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return false;
887 }
888 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700889 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 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) {
jeffhaod5347e02012-03-22 17:25:05 -0700893 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 Rogersd81871c2011-10-03 13:57:23 -0700897 return false;
898 }
899 return true;
900}
901
Ian Rogers776ac1f2012-04-13 23:36:36 -0700902bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 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 Carlstrom93c33962013-07-26 10:37:43 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
910 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 return false;
912 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700913 // 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 Rogersd81871c2011-10-03 13:57:23 -0700915 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
917 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 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 Carlstrom93c33962013-07-26 10:37:43 -0700922 if (abs_offset < 0 ||
923 (uint32_t) abs_offset >= insn_count ||
924 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700926 << reinterpret_cast<void*>(abs_offset) << ") at "
927 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 return false;
929 }
930 insn_flags_[abs_offset].SetBranchTarget();
931 return true;
932}
933
Ian Rogers776ac1f2012-04-13 23:36:36 -0700934bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 bool* selfOkay) {
936 const uint16_t* insns = code_item_->insns_ + cur_offset;
937 *pConditional = false;
938 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 switch (*insns & 0xff) {
940 case Instruction::GOTO:
941 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 break;
943 case Instruction::GOTO_32:
944 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700945 *selfOkay = true;
946 break;
947 case Instruction::GOTO_16:
948 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 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;
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 break;
965 default:
966 return false;
967 break;
968 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700969 return true;
970}
971
Ian Rogers776ac1f2012-04-13 23:36:36 -0700972bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700974 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700976 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 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) {
jeffhaod5347e02012-03-22 17:25:05 -0700979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700980 << ", switch offset " << switch_offset
981 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700982 return false;
983 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700986 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800987 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
989 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700990 return false;
991 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 uint32_t switch_count = switch_insns[1];
993 int32_t keys_offset, targets_offset;
994 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 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 Rogersd81871c2011-10-03 13:57:23 -07001006 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001008 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1009 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1010 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001011 return false;
1012 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001013 /* make sure the end of the switch is in range */
1014 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001015 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)
jeffhaod5347e02012-03-22 17:25:05 -07001018 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001019 return false;
1020 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001021 /* for a sparse switch, verify the keys are in ascending order */
1022 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1024 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001025 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) {
jeffhaod5347e02012-03-22 17:25:05 -07001028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1029 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001030 return false;
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 last_key = key;
1033 }
1034 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001035 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 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 Carlstrom93c33962013-07-26 10:37:43 -07001040 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 << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001047 return false;
1048 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 insn_flags_[abs_offset].SetBranchTarget();
1050 }
1051 return true;
1052}
1053
Ian Rogers776ac1f2012-04-13 23:36:36 -07001054bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001055 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 return false;
1058 }
1059 uint16_t registers_size = code_item_->registers_size_;
1060 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001061 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1063 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001066 }
1067
1068 return true;
1069}
1070
Ian Rogers776ac1f2012-04-13 23:36:36 -07001071bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 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 Carlstrom93c33962013-07-26 10:37:43 -07001076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1077 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 return false;
1079 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001080 return true;
1081}
1082
Ian Rogers776ac1f2012-04-13 23:36:36 -07001083bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 uint16_t registers_size = code_item_->registers_size_;
1085 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001086
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001088 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1089 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 }
1091 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 reg_table_.Init(kTrackCompilerInterestPoints,
1093 insn_flags_.get(),
1094 insns_size,
1095 registers_size,
1096 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001097
jeffhaobdb76512011-09-07 11:43:16 -07001098
Ian Rogersd0fbd852013-09-24 18:17:04 -07001099 work_line_.reset(RegisterLine::Create(registers_size, this));
1100 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001101
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 /* Initialize register types of method arguments. */
1103 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001104 DCHECK_NE(failures_.size(), 0U);
1105 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001106 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001107 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 /* Perform code flow verification. */
1111 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001112 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001114 }
jeffhaobdb76512011-09-07 11:43:16 -07001115 return true;
1116}
1117
Ian Rogersad0b3a32012-04-16 14:50:24 -07001118std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1119 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001120 for (size_t i = 0; i < failures_.size(); ++i) {
1121 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001122 }
1123 return os;
1124}
1125
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001126extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001128 v->Dump(std::cerr);
1129}
1130
Ian Rogers776ac1f2012-04-13 23:36:36 -07001131void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001132 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001133 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return;
jeffhaobdb76512011-09-07 11:43:16 -07001135 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 {
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 Rogersb4903572012-10-11 11:52:56 -07001142 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001143 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1144 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 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 Rogersd81871c2011-10-03 13:57:23 -07001148 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1149 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001150 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001151 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001152 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001153 const bool kDumpHexOfInstruction = false;
1154 if (kDumpHexOfInstruction) {
1155 indent_os << inst->DumpHex(5) << " ";
1156 }
1157 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001158 inst = inst->Next();
1159 }
jeffhaobdb76512011-09-07 11:43:16 -07001160}
1161
Ian Rogersd81871c2011-10-03 13:57:23 -07001162static bool IsPrimitiveDescriptor(char descriptor) {
1163 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001164 case 'I':
1165 case 'C':
1166 case 'S':
1167 case 'B':
1168 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001169 case 'F':
1170 case 'D':
1171 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001173 default:
1174 return false;
1175 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001176}
1177
Ian Rogers776ac1f2012-04-13 23:36:36 -07001178bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 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 */
jeffhaobdb76512011-09-07 11:43:16 -07001182
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001184 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001186 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 // 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 Yamauchi7da95862014-07-30 14:26:22 -07001190 RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001191 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 reg_line->SetRegisterType(arg_start + cur_arg,
1193 reg_types_.UninitializedThisArgument(declaring_class));
1194 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001195 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001196 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001198 }
1199
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001200 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001202 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001203
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) {
jeffhaod5347e02012-03-22 17:25:05 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1211 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 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 Yamauchi7da95862014-07-30 14:26:22 -07001222 RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001223 if (!reg_type.IsNonZeroReferenceTypes()) {
1224 DCHECK(HasFailures());
1225 return false;
1226 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001227 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 }
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 Gampe77cd4d62014-06-19 17:29:48 -07001250 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 Yamauchi7da95862014-07-30 14:26:22 -07001256 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 Rogers2bcb4a42012-11-08 10:39:18 -08001258 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 cur_arg++;
1260 break;
1261 }
1262 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1264 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 cur_arg++;
1268 }
1269 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1271 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 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 Carlstrom7934ac22013-07-26 10:54:15 -07001280 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 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) {
jeffhaod5347e02012-03-22 17:25:05 -07001304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1305 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 }
1307 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001308}
1309
Ian Rogers776ac1f2012-04-13 23:36:36 -07001310bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 const uint16_t* insns = code_item_->insns_;
1312 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001313
jeffhaobdb76512011-09-07 11:43:16 -07001314 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 insn_flags_[0].SetChanged();
1316 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001317
jeffhaobdb76512011-09-07 11:43:16 -07001318 /* Continue until no instructions are marked "changed". */
1319 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001320 // 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())
jeffhaobdb76512011-09-07 11:43:16 -07001324 break;
1325 }
jeffhaobdb76512011-09-07 11:43:16 -07001326 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 Rogersd81871c2011-10-03 13:57:23 -07001336 // 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 Rogersebbdd872014-07-07 23:53:08 -07001345 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001346 /*
1347 * Sanity check: retrieve the stored register line (assuming
1348 * a full table) and make sure it actually matches.
1349 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 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 Rogers2bcb4a42012-11-08 10:39:18 -08001355 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001356 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1357 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001358 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 }
jeffhaobdb76512011-09-07 11:43:16 -07001360 }
jeffhaobdb76512011-09-07 11:43:16 -07001361 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001364 prepend += " failed to verify: ";
1365 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001366 return false;
1367 }
jeffhaobdb76512011-09-07 11:43:16 -07001368 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 insn_flags_[insn_idx].SetVisited();
1370 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001371 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001372
Ian Rogers1c849e52012-06-28 14:00:33 -07001373 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001374 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001375 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001376 * (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 Rogersd81871c2011-10-03 13:57:23 -07001383 uint32_t insn_idx = 0;
1384 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001385 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001386 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001387 * 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 Hughes380aaa72012-07-09 14:33:15 -07001392 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001393 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1394 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1395 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001397 }
1398
Ian Rogersd81871c2011-10-03 13:57:23 -07001399 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001400 if (dead_start < 0)
1401 dead_start = insn_idx;
1402 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001403 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1404 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001405 dead_start = -1;
1406 }
1407 }
1408 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001409 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1410 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001411 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001412 // 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 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 }
jeffhaobdb76512011-09-07 11:43:16 -07001418 return true;
1419}
1420
Ian Rogers776ac1f2012-04-13 23:36:36 -07001421bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001422 // 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 Carlstrom7934ac22013-07-26 10:54:15 -07001427 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001428 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1429 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1430 }
1431 }
1432
jeffhaobdb76512011-09-07 11:43:16 -07001433 /*
1434 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001435 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001436 * control to another statement:
1437 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001438 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001439 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001440 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001441 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001442 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001443 * 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 Hughesadb8c672012-03-06 16:49:32 -08001449 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001450 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1452 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001453 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001454
jeffhaobdb76512011-09-07 11:43:16 -07001455 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001456 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001457 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001459 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1460 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462
1463 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001464 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001465 * 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 Rogers776ac1f2012-04-13 23:36:36 -07001470 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001472 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001474 }
1475
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001476
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001477 // 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 Rogers700a4022014-05-19 16:49:03 -07001480 std::unique_ptr<RegisterLine> branch_line;
1481 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001482
Sebastien Hertz5243e912013-05-21 10:55:07 +02001483 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001484 case Instruction::NOP:
1485 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001486 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001487 * 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 Hertz5243e912013-05-21 10:55:07 +02001490 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001492 }
1493 break;
1494
1495 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1497 break;
jeffhaobdb76512011-09-07 11:43:16 -07001498 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1500 break;
jeffhaobdb76512011-09-07 11:43:16 -07001501 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1506 break;
jeffhaobdb76512011-09-07 11:43:16 -07001507 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1509 break;
jeffhaobdb76512011-09-07 11:43:16 -07001510 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1515 break;
jeffhaobdb76512011-09-07 11:43:16 -07001516 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1518 break;
jeffhaobdb76512011-09-07 11:43:16 -07001519 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001520 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001521 break;
1522
1523 /*
1524 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001525 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001526 * 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 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001530 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001531 * 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 Hertz5243e912013-05-21 10:55:07 +02001535 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001536 break;
1537 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001539 break;
1540 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001541 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001542 break;
1543
Ian Rogersd81871c2011-10-03 13:57:23 -07001544 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001545 /*
jeffhao60f83e32012-02-13 17:16:30 -08001546 * 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.
jeffhaobdb76512011-09-07 11:43:16 -07001548 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001549 RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001551 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 }
jeffhaobdb76512011-09-07 11:43:16 -07001553 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001554 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1555 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 }
jeffhaobdb76512011-09-07 11:43:16 -07001558 }
1559 break;
1560 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001561 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001562 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001563 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1566 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 } 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 Hertz5243e912013-05-21 10:55:07 +02001570 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001571 RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 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 Rogersad0b3a32012-04-16 14:50:24 -07001577 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001579 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001580 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 }
jeffhaobdb76512011-09-07 11:43:16 -07001582 }
1583 }
1584 break;
1585 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001586 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001587 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001588 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 } else {
1592 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001593 const uint32_t vregA = inst->VRegA_11x();
1594 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001595 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 }
jeffhaobdb76512011-09-07 11:43:16 -07001598 }
1599 }
1600 break;
1601 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001602 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001603 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 } else {
1607 /* return_type is the *expected* return type, not register value */
1608 DCHECK(!return_type.IsZero());
1609 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001611 RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001612 // 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 Carlstrom93c33962013-07-26 10:37:43 -07001615 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1616 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001617 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001618 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 }
jeffhaobdb76512011-09-07 11:43:16 -07001625 }
1626 }
1627 }
1628 break;
1629
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001630 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 case Instruction::CONST_4: {
1632 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001633 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001634 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001635 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 }
1637 case Instruction::CONST_16: {
1638 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001639 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001640 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001641 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001643 case Instruction::CONST: {
1644 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001646 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001648 }
1649 case Instruction::CONST_HIGH16: {
1650 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001652 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001653 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001656 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001658 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 Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001661 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001662 }
1663 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001665 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 Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001668 break;
1669 }
1670 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 int64_t val = inst->VRegB_51l();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001672 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 Hertz5243e912013-05-21 10:55:07 +02001674 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 break;
1676 }
1677 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001679 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 Hertz5243e912013-05-21 10:55:07 +02001681 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 break;
1683 }
jeffhaobdb76512011-09-07 11:43:16 -07001684 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1686 break;
jeffhaobdb76512011-09-07 11:43:16 -07001687 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001688 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001691 // 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 Yamauchi7da95862014-07-30 14:26:22 -07001693 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001694 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001696 res_type.IsConflict() ? res_type
1697 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 }
jeffhaobdb76512011-09-07 11:43:16 -07001700 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001702 break;
1703 case Instruction::MONITOR_EXIT:
1704 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001705 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001706 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001707 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001708 * to the need to handle asynchronous exceptions, a now-deprecated
1709 * feature that Dalvik doesn't support.)
1710 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001711 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001712 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001713 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001714 * 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
jeffhaod1f0fde2011-09-08 17:25:33 -07001719 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001720 * 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 Hughesadb8c672012-03-06 16:49:32 -08001724 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001726 break;
1727
Ian Rogers28ad40d2011-10-27 15:19:26 -07001728 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001730 /*
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 Hughesadb8c672012-03-06 16:49:32 -08001735 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001736 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1738 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001739 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001740 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001741 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001742 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001743 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 Rogersad0b3a32012-04-16 14:50:24 -07001750 DCHECK_NE(failures_.size(), 0U);
1751 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001753 }
1754 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001755 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001756 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001758 RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001759 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001760 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 Rogers28ad40d2011-10-27 15:19:26 -07001765 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001766 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 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001771 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001772 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001773 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001776 }
jeffhaobdb76512011-09-07 11:43:16 -07001777 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001778 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 }
1780 case Instruction::ARRAY_LENGTH: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001781 RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001782 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001783 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001788 } else {
1789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 }
1791 break;
1792 }
1793 case Instruction::NEW_INSTANCE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001794 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001795 if (res_type.IsConflict()) {
1796 DCHECK_NE(failures_.size(), 0U);
1797 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001798 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001799 // 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 Rogers08f753d2012-08-24 14:35:25 -07001804 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001806 RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001807 // 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 Hertz5243e912013-05-21 10:55:07 +02001811 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 break;
1813 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001814 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
1817 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001819 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001821 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001822 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001823 just_set_result = true; // Filled new array range sets result register
1824 break;
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::CMPL_FLOAT:
1826 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001828 break;
1829 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001831 break;
1832 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
1835 case Instruction::CMPL_DOUBLE:
1836 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001839 break;
1840 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001842 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001843 break;
1844 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001845 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001846 break;
1847 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001848 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001849 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001850 break;
1851 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001852 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001854 break;
1855 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001857 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 case Instruction::THROW: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001859 RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001860 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001861 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1862 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001863 }
1864 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 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 Hertz5243e912013-05-21 10:55:07 +02001875 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001876 break;
1877
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 case Instruction::FILL_ARRAY_DATA: {
1879 /* Similar to the verification done for APUT */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001880 RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001881 /* array_type can be null if the reg type is Zero */
1882 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001883 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1885 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001886 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001887 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001888 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001889 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1891 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 } else {
jeffhao457cc512012-02-02 16:55:13 -08001893 // 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) {
jeffhaod5347e02012-03-22 17:25:05 -07001897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001898 } 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) {
jeffhaod5347e02012-03-22 17:25:05 -07001903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1904 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001905 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 }
1907 }
jeffhaobdb76512011-09-07 11:43:16 -07001908 }
1909 }
1910 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 }
jeffhaobdb76512011-09-07 11:43:16 -07001912 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 case Instruction::IF_NE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001914 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1915 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 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 Carlstrom93c33962013-07-26 10:37:43 -07001925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1926 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001927 }
1928 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 }
jeffhaobdb76512011-09-07 11:43:16 -07001930 case Instruction::IF_LT:
1931 case Instruction::IF_GE:
1932 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 case Instruction::IF_LE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001934 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1935 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1938 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001939 }
1940 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 }
jeffhaobdb76512011-09-07 11:43:16 -07001942 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 case Instruction::IF_NEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001944 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1947 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001948 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001949
1950 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001951 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001952 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001953 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001954 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001955 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001956 }
Ian Rogers9b360392013-06-06 14:45:07 -07001957 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001958 } else {
1959 break;
1960 }
1961
Ian Rogers9b360392013-06-06 14:45:07 -07001962 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001963
1964 /* Check for peep-hole pattern of:
1965 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001966 * instance-of vX, vY, T;
1967 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001968 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001969 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001970 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001971 * 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 Sbirlea2b87ddf2013-05-28 14:14:12 -07001975 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001976 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001977 (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 Rogersebbdd872014-07-07 23:53:08 -07001980 // 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 Yamauchi7da95862014-07-30 14:26:22 -07001990 RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
1991 RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001992
Ian Rogersebbdd872014-07-07 23:53:08 -07001993 if (!orig_type.Equals(cast_type) &&
1994 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07001995 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07001996 !cast_type.GetClass()->IsInterface() &&
1997 (orig_type.IsZero() ||
1998 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001999 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002000 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002001 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002002 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002003 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 Carlstromdf629502013-07-17 22:39:56 -07002012 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002013 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 Sbirlea2b87ddf2013-05-28 14:14:12 -07002036 }
2037 }
2038 }
2039
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::IF_LTZ:
2043 case Instruction::IF_GEZ:
2044 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 case Instruction::IF_LEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002046 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2049 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 }
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 }
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
jeffhaobdb76512011-09-07 11:43:16 -07002059 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
jeffhaobdb76512011-09-07 11:43:16 -07002062 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
2071 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
2078 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
2081 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
2084 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
jeffhaobdb76512011-09-07 11:43:16 -07002100 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
jeffhaobdb76512011-09-07 11:43:16 -07002103 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
jeffhaobdb76512011-09-07 11:43:16 -07002106 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 break;
jeffhaobdb76512011-09-07 11:43:16 -07002118
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 break;
2122 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 break;
2125 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 break;
2128 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 break;
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 break;
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 break;
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 break;
jeffhaobdb76512011-09-07 11:43:16 -07002150 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 break;
2153 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 break;
2162
2163 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 break;
2166 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 break;
2169 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 break;
2172 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184
2185 case Instruction::INVOKE_VIRTUAL:
2186 case Instruction::INVOKE_VIRTUAL_RANGE:
2187 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2190 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002191 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2192 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002193 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2194 is_super);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002195 RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002196 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002197 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 Chartiereae2fb22014-01-14 14:31:25 -08002201 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002202 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002203 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2204 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002205 return_type_class->CannotBeAssignedFromOtherTypes());
2206 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002207 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002208 self->ClearException();
2209 }
2210 }
2211 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002213 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 Rogersfc0e94b2013-09-23 23:51:32 -07002215 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002216 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002217 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002218 if (!return_type->IsLowHalf()) {
2219 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002220 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002221 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002222 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002223 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002224 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 }
jeffhaobdb76512011-09-07 11:43:16 -07002226 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002229 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002230 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002231 const char* return_type_descriptor;
2232 bool is_constructor;
Ian Rogers1ff3c982014-08-12 02:30:58 -07002233 RegType* return_type = nullptr;
Ian Rogers46685432012-06-03 22:26:43 -07002234 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002236 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002237 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002238 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 Chartierbfd9a432014-05-21 17:43:44 -07002242 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002243 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 = &reg_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 Rogers46685432012-06-03 22:26:43 -07002256 }
2257 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002258 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 * 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).
jeffhaobdb76512011-09-07 11:43:16 -07002264 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002265 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002266 if (this_type.IsConflict()) // failure.
2267 break;
jeffhaobdb76512011-09-07 11:43:16 -07002268
jeffhaob57e9522012-04-26 18:08:21 -07002269 /* no null refs allowed (?) */
2270 if (this_type.IsZero()) {
2271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2272 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002273 }
jeffhaob57e9522012-04-26 18:08:21 -07002274
2275 /* must be in same class or in superclass */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002276 // RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002277 // TODO: re-enable constructor type verification
2278 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002279 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002280 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2281 // break;
2282 // }
jeffhaob57e9522012-04-26 18:08:21 -07002283
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 Rogersad0b3a32012-04-16 14:50:24 -07002296 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002297 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002298 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2299 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002300 }
2301 if (!return_type->IsLowHalf()) {
2302 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002304 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002306 just_set_result = true;
2307 break;
2308 }
2309 case Instruction::INVOKE_STATIC:
2310 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002311 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002312 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002313 METHOD_STATIC,
2314 is_range,
2315 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002316 const char* descriptor;
2317 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002319 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 Chartier590fee92013-09-13 13:46:47 -07002321 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002322 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002323 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002324 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002325 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 if (!return_type.IsLowHalf()) {
2327 work_line_->SetResultRegisterType(return_type);
2328 } else {
2329 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2330 }
jeffhaobdb76512011-09-07 11:43:16 -07002331 just_set_result = true;
2332 }
2333 break;
jeffhaobdb76512011-09-07 11:43:16 -07002334 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002335 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002337 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002338 METHOD_INTERFACE,
2339 is_range,
2340 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002341 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002342 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002343 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 Rogers28ad40d2011-10-27 15:19:26 -07002347 }
Ian Rogers0d604842012-04-16 14:50:24 -07002348 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002349 /* 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 Yamauchi7da95862014-07-30 14:26:22 -07002352 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002353 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 Hertz5243e912013-05-21 10:55:07 +02002375 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002376 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 Chartierbfd9a432014-05-21 17:43:44 -07002380 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002381 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002382 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 if (!return_type.IsLowHalf()) {
2384 work_line_->SetResultRegisterType(return_type);
2385 } else {
2386 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2387 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002388 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 }
jeffhaobdb76512011-09-07 11:43:16 -07002391 case Instruction::NEG_INT:
2392 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::NEG_LONG:
2396 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002416 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002419 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002424 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002434 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002439 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002442 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002443 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002447 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002460 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002461 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 Hertz5243e912013-05-21 10:55:07 +02002471 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::AND_INT:
2475 case Instruction::OR_INT:
2476 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002477 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002479 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 Hertz5243e912013-05-21 10:55:07 +02002488 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.LongLo(), reg_types_.LongHi(),
2490 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::SHL_LONG:
2493 case Instruction::SHR_LONG:
2494 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002496 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002497 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 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 Carlstrom93c33962013-07-26 10:37:43 -07002504 work_line_->CheckBinaryOp(inst,
2505 reg_types_.Float(),
2506 reg_types_.Float(),
2507 reg_types_.Float(),
2508 false);
jeffhaobdb76512011-09-07 11:43:16 -07002509 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 Hertz5243e912013-05-21 10:55:07 +02002515 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002516 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2517 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002518 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 Carlstrom93c33962013-07-26 10:37:43 -07002526 work_line_->CheckBinaryOp2addr(inst,
2527 reg_types_.Integer(),
2528 reg_types_.Integer(),
2529 reg_types_.Integer(),
2530 false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::AND_INT_2ADDR:
2533 case Instruction::OR_INT_2ADDR:
2534 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002535 work_line_->CheckBinaryOp2addr(inst,
2536 reg_types_.Integer(),
2537 reg_types_.Integer(),
2538 reg_types_.Integer(),
2539 true);
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002542 work_line_->CheckBinaryOp2addr(inst,
2543 reg_types_.Integer(),
2544 reg_types_.Integer(),
2545 reg_types_.Integer(),
2546 false);
jeffhaobdb76512011-09-07 11:43:16 -07002547 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 Hertz5243e912013-05-21 10:55:07 +02002556 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 reg_types_.LongLo(), reg_types_.LongHi(),
2558 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::SHL_LONG_2ADDR:
2561 case Instruction::SHR_LONG_2ADDR:
2562 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002563 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002564 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002565 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 Carlstrom93c33962013-07-26 10:37:43 -07002571 work_line_->CheckBinaryOp2addr(inst,
2572 reg_types_.Float(),
2573 reg_types_.Float(),
2574 reg_types_.Float(),
2575 false);
jeffhaobdb76512011-09-07 11:43:16 -07002576 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 Hertz5243e912013-05-21 10:55:07 +02002582 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002583 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2584 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002585 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 Hertz5243e912013-05-21 10:55:07 +02002591 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::AND_INT_LIT16:
2594 case Instruction::OR_INT_LIT16:
2595 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002596 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002597 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:
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002606 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::AND_INT_LIT8:
2609 case Instruction::OR_INT_LIT8:
2610 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002611 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002614 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002615 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002616 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2618 }
2619 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002620 // 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 Shih37f05ef2014-07-16 18:38:08 -07002635 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 Hertz2d6ba512013-05-17 11:31:37 +02002647 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 Carlstromea46f952013-07-30 01:26:50 -07002656 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002657 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002658 const char* descriptor = called_method->GetReturnTypeDescriptor();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002659 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002660 if (!return_type.IsLowHalf()) {
2661 work_line_->SetResultRegisterType(return_type);
2662 } else {
2663 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2664 }
2665 just_set_result = true;
2666 }
2667 break;
2668 }
2669
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002671 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 Hertz2d6ba512013-05-17 11:31:37 +02002677 case Instruction::UNUSED_79:
2678 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002679 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002680 case Instruction::UNUSED_F0:
2681 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002682 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:
jeffhaobdb76512011-09-07 11:43:16 -07002692 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002693 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002694 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002695 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002697 break;
2698
2699 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002700 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002701 * complain if an instruction is missing (which is desirable).
2702 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002703 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002704
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002706 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002707 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002708 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002709 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002710 /* immediate failure, reject class */
2711 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2712 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002713 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002714 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002715 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002716 }
jeffhaobdb76512011-09-07 11:43:16 -07002717 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 * 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.)
jeffhaobdb76512011-09-07 11:43:16 -07002721 */
2722 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002724 }
2725
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002726
jeffhaobdb76512011-09-07 11:43:16 -07002727
2728 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002729 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002730 *
2731 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002732 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002733 * 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
jeffhaod1f0fde2011-09-08 17:25:33 -07002735 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002736 * that, and will reject the code.
2737 *
2738 * TODO: avoid re-fetching the branch target
2739 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002741 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002743 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002745 return false;
2746 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002747 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002748 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002749 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 }
jeffhaobdb76512011-09-07 11:43:16 -07002751 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002752 if (NULL != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002753 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002754 return false;
2755 }
2756 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002757 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002758 return false;
2759 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 }
jeffhaobdb76512011-09-07 11:43:16 -07002761 }
2762
2763 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002764 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002765 *
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 Hughesadb8c672012-03-06 16:49:32 -08002769 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002770 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 Carlstrom5b8e4c82011-09-18 01:38:59 -07002780 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002781 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 Rogersd81871c2011-10-03 13:57:23 -07002792 abs_offset = work_insn_idx_ + offset;
2793 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002794 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002795 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002796 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002797 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002798 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002799 }
jeffhaobdb76512011-09-07 11:43:16 -07002800 }
2801 }
2802
2803 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 * 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.)
jeffhaobdb76512011-09-07 11:43:16 -07002806 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002808 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002809 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002810
Andreas Gampef91baf12014-07-18 15:41:00 -07002811 // Need the linker to try and resolve the handled class to check if it's Throwable.
2812 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2813
Ian Rogers0571d352011-11-03 19:51:38 -07002814 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002815 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 Chartierbf99f772014-08-23 16:37:27 -07002820 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2821 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002822 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 Rogersd81871c2011-10-03 13:57:23 -07002832 }
jeffhaobdb76512011-09-07 11:43:16 -07002833 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 * 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.
jeffhaobdb76512011-09-07 11:43:16 -07002837 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002838 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002839 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 }
jeffhaobdb76512011-09-07 11:43:16 -07002841 }
2842
2843 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 * 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.
jeffhaobdb76512011-09-07 11:43:16 -07002846 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002847 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002848 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002849 * 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,
jeffhaobdb76512011-09-07 11:43:16 -07002851 * it will do so before grabbing the lock).
2852 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002853 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002854 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002856 return false;
2857 }
2858 }
2859 }
2860
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002861 /* 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 Rogers6d376ae2013-07-23 15:12:40 -07002866 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 Sbirlea2b87ddf2013-05-28 14:14:12 -07002871 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002872 // 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 Rogersb8c78592013-07-25 23:52:52 +00002881 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 Rogers6d376ae2013-07-23 15:12:40 -07002895 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2896 if (next_line != NULL) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002897 // 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 Rogers6d376ae2013-07-23 15:12:40 -07002901 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 Sbirlea2b87ddf2013-05-28 14:14:12 -07002911
jeffhaod1f0fde2011-09-08 17:25:33 -07002912 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002913 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002914 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 return false;
2916 }
jeffhaobdb76512011-09-07 11:43:16 -07002917 }
2918
2919 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002920 * Update start_guess. Advance to the next instruction of that's
2921 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002922 * 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 Hughesadb8c672012-03-06 16:49:32 -08002925 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002927 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002928 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002930 }
2931
Ian Rogersd81871c2011-10-03 13:57:23 -07002932 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2933 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002934
2935 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002936} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002937
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002938RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002939 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002940 RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002941 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 Rogersad0b3a32012-04-16 14:50:24 -07002945 if (result.IsConflict()) {
2946 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2947 << "' in " << referrer;
2948 return result;
2949 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002950 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002951 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002952 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002953 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002954 // 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 Rogers28ad40d2011-10-27 15:19:26 -07002958 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002959 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002960 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002961 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002962}
2963
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002964RegType& MethodVerifier::GetCaughtExceptionType() {
2965 RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002967 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002968 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2969 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002970 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 Rogersb4903572012-10-11 11:52:56 -07002974 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002976 RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002977 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002978 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 Haob878f212014-04-24 16:25:36 -07002986 } else if (common_super == nullptr) {
2987 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002988 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002989 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002991 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002992 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 }
2994 }
2995 }
2996 }
Ian Rogers0571d352011-11-03 19:51:38 -07002997 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 }
2999 }
3000 if (common_super == NULL) {
3001 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003002 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003003 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003005 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003006}
3007
Brian Carlstromea46f952013-07-30 01:26:50 -07003008mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003009 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003011 RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 if (klass_type.IsConflict()) {
3013 std::string append(" in attempt to access method ");
3014 append += dex_file_->GetMethodName(method_id);
3015 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08003016 return NULL;
3017 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003018 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003019 return NULL; // Can't resolve Class so no more to do here
3020 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003021 mirror::Class* klass = klass_type.GetClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003022 RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003023 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003025 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003026 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003027
3028 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003030 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 res_method = klass->FindInterfaceMethod(name, signature);
3032 } else {
3033 res_method = klass->FindVirtualMethod(name, signature);
3034 }
3035 if (res_method != NULL) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003036 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003038 // 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 Rogersd81871c2011-10-03 13:57:23 -07003050 }
3051 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 // 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) {
jeffhaod5347e02012-03-22 17:25:05 -07003055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3056 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 return NULL;
3058 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003059 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003060 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3062 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003063 return NULL;
3064 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003065 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003067 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003068 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003069 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003070 }
jeffhaode0d9c92012-02-27 13:58:13 -08003071 // 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) {
jeffhaod5347e02012-03-22 17:25:05 -07003073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3074 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003075 return NULL;
3076 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003077 // 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 Rogersd81871c2011-10-03 13:57:23 -07003087 // 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 Rogers2fc14272012-08-30 10:56:57 -07003093 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3094 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 return NULL;
3096 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003097 return res_method;
3098}
3099
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003100template <class T>
3101mirror::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 Yamauchi7da95862014-07-30 14:26:22 -07003129 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003130 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 Hao0d087272014-08-04 14:47:17 -07003143 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003144 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 Yamauchi7da95862014-07-30 14:26:22 -07003150 RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003151 if (res_method != nullptr) {
3152 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003153 std::string temp;
3154 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003155 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 Chartierbf99f772014-08-23 16:37:27 -07003159 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003160 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 Chartierbf99f772014-08-23 16:37:27 -07003192 RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003193 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 Yamauchi7da95862014-07-30 14:26:22 -07003196 RegType& src_type = work_line_->GetRegisterType(get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003197 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
3219void 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
3232class 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 Carlstromea46f952013-07-30 01:26:50 -07003258mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003259 MethodType method_type,
3260 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003261 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003262 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3263 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003264 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003265
Brian Carlstromea46f952013-07-30 01:26:50 -07003266 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003267 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003268 // Check what we can statically.
3269 if (!have_pending_hard_failure_) {
3270 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3271 }
3272 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003273 }
3274
Ian Rogersd81871c2011-10-03 13:57:23 -07003275 // 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 Yamauchi7da95862014-07-30 14:26:22 -07003279 RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003280 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003281 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003282 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003283 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003284 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003285 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003286 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003287 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003288 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003289 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003290 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003291 << "." << res_method->GetName()
3292 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003293 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003294 }
3295 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003296
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003297 // 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 Rogersd81871c2011-10-03 13:57:23 -07003301}
3302
Brian Carlstromea46f952013-07-30 01:26:50 -07003303mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003304 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003305 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3306 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003307 RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003308 if (!actual_arg_type.HasClass()) {
3309 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003310 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003311 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003312 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003313 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003314 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 Yang2cdbad72014-07-16 10:44:41 -07003318 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003319 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003320 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003321 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003322 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003323 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003324 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 Hertz2d6ba512013-05-17 11:31:37 +02003327 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003328 return res_method;
3329}
3330
Brian Carlstromea46f952013-07-30 01:26:50 -07003331mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003332 bool is_range) {
3333 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003334 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003335 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003336 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003338 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 Yamauchi7da95862014-07-30 14:26:22 -07003345 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003346 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3347 return NULL;
3348 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003349 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 Rogers1ff3c982014-08-12 02:30:58 -07003369 std::string temp;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003370 RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003371 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003372 klass->CannotBeAssignedFromOtherTypes());
3373 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003374 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3375 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003376 << "' 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 Chartierbfd9a432014-05-21 17:43:44 -07003384 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003385 size_t params_size = params == NULL ? 0 : params->Size();
3386 uint32_t arg[5];
3387 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003388 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003389 }
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 Carlstrom93c33962013-07-26 10:37:43 -07003394 << "'. Expected " << expected_args
3395 << " arguments, processing argument " << actual_args
3396 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003397 return NULL;
3398 }
3399 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003400 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003401 if (descriptor == NULL) {
3402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003403 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003404 return NULL;
3405 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003406 RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003407 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 Rogers62342ec2013-06-11 10:26:37 -07003422void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003423 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 Yamauchi7da95862014-07-30 14:26:22 -07003434 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003435 if (res_type.IsConflict()) { // bad class
3436 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003437 } else {
3438 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3439 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003441 } else if (!is_filled) {
3442 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003443 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003444 /* set register type to array class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003445 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003446 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003447 } 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 Chartierbf99f772014-08-23 16:37:27 -07003450 RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003451 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3452 uint32_t arg[5];
3453 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003454 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003455 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003456 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003457 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003458 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003460 return;
3461 }
3462 }
3463 // filled-array result goes into "result" register
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003464 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003465 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003466 }
3467 }
3468}
3469
Sebastien Hertz5243e912013-05-21 10:55:07 +02003470void MethodVerifier::VerifyAGet(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003471 RegType& insn_type, bool is_primitive) {
3472 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003476 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003477 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 Hertz5243e912013-05-21 10:55:07 +02003482 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003484 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003485 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003486 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003487 }
jeffhaofc3144e2012-02-01 17:21:15 -08003488 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003490 } else {
3491 /* verify the class */
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003492 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003493 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003495 << " source for aget-object";
3496 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003498 << " source for category 1 aget";
3499 } else if (is_primitive && !insn_type.Equals(component_type) &&
3500 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003501 (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 Rogers89310de2012-02-01 13:47:30 -08003504 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003505 // 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 Hertz5243e912013-05-21 10:55:07 +02003509 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003510 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003511 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003512 component_type.HighHalf(&reg_types_));
3513 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003514 }
3515 }
3516 }
3517}
3518
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003519void MethodVerifier::VerifyPrimitivePut(RegType& target_type, RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003520 const uint32_t vregA) {
3521 // Primitive assignability rules are weaker than regular assignability rules.
3522 bool instruction_compatible;
3523 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003524 RegType& value_type = work_line_->GetRegisterType(vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003525 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003526 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003527 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 Gampe376fa682014-09-07 13:06:12 -07003533 // 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 Haofe1f7c82013-08-01 14:50:24 -07003541 } else if (target_type.IsDouble()) {
3542 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003543 // 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 Haofe1f7c82013-08-01 14:50:24 -07003551 } 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 Hertz5243e912013-05-21 10:55:07 +02003570void MethodVerifier::VerifyAPut(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003571 RegType& insn_type, bool is_primitive) {
3572 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003576 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003577 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.
jeffhaofc3144e2012-02-01 17:21:15 -08003580 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003582 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003583 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003584 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003585 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003586 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003588 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 Haofe1f7c82013-08-01 14:50:24 -07003595 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003596 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 }
3598 }
3599 }
3600}
3601
Brian Carlstromea46f952013-07-30 01:26:50 -07003602mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003603 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3604 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003605 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003606 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003607 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 Rogers90040192011-12-16 08:54:29 -08003610 return NULL;
3611 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003612 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003613 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003614 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003615 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003616 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3617 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003619 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003620 << dex_file_->GetFieldName(field_id) << ") in "
3621 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 DCHECK(Thread::Current()->IsExceptionPending());
3623 Thread::Current()->ClearException();
3624 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003625 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3626 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003628 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003629 return NULL;
3630 } else if (!field->IsStatic()) {
3631 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3632 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003633 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003634 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003635}
3636
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003637mirror::ArtField* MethodVerifier::GetInstanceField(RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003638 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3639 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003640 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003641 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 Rogers90040192011-12-16 08:54:29 -08003645 return NULL;
3646 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003647 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003648 return NULL; // Can't resolve Class so no more to do here
3649 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003650 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003651 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3652 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003654 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003655 << dex_file_->GetFieldName(field_id) << ") in "
3656 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003657 DCHECK(Thread::Current()->IsExceptionPending());
3658 Thread::Current()->ClearException();
3659 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003660 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3661 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003663 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003664 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 Kyle695c5982014-08-22 15:03:07 +01003672 } 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 Rogerse1758fe2012-04-19 11:31:15 -07003677 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003678 mirror::Class* klass = field->GetDeclaringClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003679 RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003680 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003681 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003682 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 Carlstrom93c33962013-07-26 10:37:43 -07003688 << " of a not fully initialized object within the context"
3689 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003690 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 Rogersd81871c2011-10-03 13:57:23 -07003701 }
3702}
3703
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003704void MethodVerifier::VerifyISGet(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003705 bool is_primitive, bool is_static) {
3706 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003707 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003708 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003709 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003710 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003711 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003712 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003713 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003714 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003715 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003716 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 Rogersfc0e94b2013-09-23 23:51:32 -07003723 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003724 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003725 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003726 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003727 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3728 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003729 }
Ian Rogers0d604842012-04-16 14:50:24 -07003730 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003731 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 Chartierbf99f772014-08-23 16:37:27 -07003734 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003735 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003736 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003737 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003738 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003739 if (field_type->Equals(insn_type) ||
3740 (field_type->IsFloat() && insn_type.IsInteger()) ||
3741 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003742 // 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 Hertz757b3042014-03-28 14:34:28 +01003750 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003751 return;
3752 }
3753 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003754 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003755 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3756 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003757 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003758 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003759 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 return;
3761 }
3762 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003763 if (!field_type->IsLowHalf()) {
3764 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003765 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003766 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003767 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003768}
3769
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003770void MethodVerifier::VerifyISPut(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003771 bool is_primitive, bool is_static) {
3772 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003773 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003774 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003775 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003776 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003777 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003778 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003779 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003780 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003781 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 Chartier61c5ebc2014-06-05 17:42:53 -07003787 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 Rogersfc0e94b2013-09-23 23:51:32 -07003794 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003795 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003796 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003797 } else {
3798 Thread* self = Thread::Current();
3799 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3800 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003801 }
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 Chartierbf99f772014-08-23 16:37:27 -07003806 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003807 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003808 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003809 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003810 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003811 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003812 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003813 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003814 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3815 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003816 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003817 << "' in put-object";
3818 return;
3819 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003820 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003821 }
3822}
3823
Brian Carlstromea46f952013-07-30 01:26:50 -07003824mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003825 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003826 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 Yamauchi7da95862014-07-30 14:26:22 -07003832 RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003833 if (!object_type.HasClass()) {
3834 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3835 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003836 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003837 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003838 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 Hertzc15853b2013-06-25 17:36:27 +02003845}
3846
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003847void MethodVerifier::VerifyIGetQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003848 bool is_primitive) {
3849 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003850 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003851 if (field == NULL) {
3852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3853 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003854 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003855 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 Yamauchi7da95862014-07-30 14:26:22 -07003862 RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003863 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003864 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003865 field_type_class->CannotBeAssignedFromOtherTypes());
3866 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003867 Thread* self = Thread::Current();
3868 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3869 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003870 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003871 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003872 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003873 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003874 const uint32_t vregA = inst->VRegA_22c();
3875 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 if (field_type->Equals(insn_type) ||
3877 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3878 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003879 // 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 Hertzc15853b2013-06-25 17:36:27 +02003886 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003887 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003888 return;
3889 }
3890 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003891 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003892 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003893 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003894 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003895 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003896 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3897 return;
3898 }
3899 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003900 if (!field_type->IsLowHalf()) {
3901 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003902 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003903 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003904 }
3905}
3906
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003907void MethodVerifier::VerifyIPutQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003908 bool is_primitive) {
3909 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003910 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003911 if (field == NULL) {
3912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3913 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003914 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003915 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003916 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003917 RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 if (field != NULL) {
3919 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3920 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003921 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003922 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 Yamauchi7da95862014-07-30 14:26:22 -07003930 RegType& value_type = work_line_->GetRegisterType(vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003931 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 Hertzc15853b2013-06-25 17:36:27 +02003952 << " to be of type '" << insn_type
3953 << "' but found type '" << field_type
3954 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003955 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 Hertzc15853b2013-06-25 17:36:27 +02003967 << " to be compatible with type '" << insn_type
3968 << "' but found type '" << field_type
3969 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003970 return;
3971 }
3972 work_line_->VerifyRegisterType(vregA, field_type);
3973 }
3974}
3975
Ian Rogers776ac1f2012-04-13 23:36:36 -07003976bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003977 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003979 return false;
3980 }
3981 return true;
3982}
3983
Ian Rogersebbdd872014-07-07 23:53:08 -07003984bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3985 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 bool changed = true;
3987 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3988 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003989 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003990 * 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.)
jeffhaobdb76512011-09-07 11:43:16 -07003993 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003994 if (!insn_flags_[next_insn].IsReturn()) {
3995 target_line->CopyFromLine(merge_line);
3996 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003997 // Verify that the monitor stack is empty on return.
3998 if (!merge_line->VerifyMonitorStackEmpty()) {
3999 return false;
4000 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004001 // 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 }
jeffhaobdb76512011-09-07 11:43:16 -07004016 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004017 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004018 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07004019 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004020 if (gDebugVerify) {
4021 copy->CopyFromLine(target_line);
4022 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004024 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004026 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004027 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004028 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004029 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
4030 << *copy.get() << " MERGE\n"
4031 << *merge_line << " ==\n"
4032 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004033 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004034 if (update_merge_line && changed) {
4035 merge_line->CopyFromLine(target_line);
4036 }
jeffhaobdb76512011-09-07 11:43:16 -07004037 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 if (changed) {
4039 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004040 }
4041 return true;
4042}
4043
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004044InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004045 return &insn_flags_[work_insn_idx_];
4046}
4047
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004048RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004049 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004050 if (mirror_method_.Get() != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004051 Thread* self = Thread::Current();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004052 StackHandleScope<1> hs(self);
4053 mirror::Class* return_type_class =
4054 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004055 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004056 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4057 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004058 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004059 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004060 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004061 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 Chartierbf99f772014-08-23 16:37:27 -07004069 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004070 }
4071 }
4072 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004073}
4074
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004075RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004076 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004077 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004078 const char* descriptor
4079 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004080 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004081 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004082 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4083 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004084 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004085 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004086 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004087 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004088 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004089}
4090
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004091std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4092 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004093 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004094 std::vector<int32_t> result;
4095 for (size_t i = 0; i < line->NumRegs(); ++i) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004096 RegType& type = line->GetRegisterType(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004097 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 Rogers7b3ddd22013-02-21 15:19:52 -08004128 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004129 result.push_back(kReferenceVReg);
4130 result.push_back(0);
4131 }
4132 }
4133 return result;
4134}
4135
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004136RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004137 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 Hughes0a1038b2012-06-14 16:24:17 -07004164void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004165 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004166}
4167
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004168void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004169 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004170}
jeffhaod1224c72012-02-29 13:43:08 -08004171
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004172void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4173 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004174}
4175
Ian Rogersd81871c2011-10-03 13:57:23 -07004176} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004177} // namespace art