blob: 521a2ddc4b052559059954f61bcc9656f0c62ad4 [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 Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080046#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
48namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070049namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070051static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070052static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070053// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070054
Ian Rogers7b3ddd22013-02-21 15:19:52 -080055void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070056 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070057 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070058 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070059 register_lines_.reset(new RegisterLine*[insns_size]());
60 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070061 for (uint32_t i = 0; i < insns_size; i++) {
62 bool interesting = false;
63 switch (mode) {
64 case kTrackRegsAll:
65 interesting = flags[i].IsOpcode();
66 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070067 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070068 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070069 break;
70 case kTrackRegsBranches:
71 interesting = flags[i].IsBranchTarget();
72 break;
73 default:
74 break;
75 }
76 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070077 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
78 }
79 }
80}
81
82PcToRegisterLineTable::~PcToRegisterLineTable() {
83 for (size_t i = 0; i < size_; i++) {
84 delete register_lines_[i];
85 if (kIsDebugBuild) {
86 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070087 }
88 }
89}
90
Ian Rogers7b078e82014-09-10 14:44:24 -070091MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
92 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070093 bool allow_soft_failures,
94 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070095 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070096 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070097 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080098 bool early_failure = false;
99 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700100 const DexFile& dex_file = klass->GetDexFile();
101 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800102 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700103 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700104 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800105 early_failure = true;
106 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700107 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800108 early_failure = true;
109 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700110 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800111 early_failure = true;
112 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
113 }
114 if (early_failure) {
115 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
116 if (Runtime::Current()->IsCompiler()) {
117 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000118 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800119 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700120 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700121 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700122 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700123 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700125 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700126}
127
Ian Rogers7b078e82014-09-10 14:44:24 -0700128MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
129 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700130 Handle<mirror::DexCache> dex_cache,
131 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700132 const DexFile::ClassDef* class_def,
133 bool allow_soft_failures,
134 std::string* error) {
135 DCHECK(class_def != nullptr);
136 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700138 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700139 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 }
jeffhaof56197c2012-03-05 18:01:54 -0800141 ClassDataItemIterator it(*dex_file, class_data);
142 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
143 it.Next();
144 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700145 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700147 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700148 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800149 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800151 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700152 if (method_idx == previous_direct_method_idx) {
153 // smali can create dex files with two encoded_methods sharing the same method_idx
154 // http://code.google.com/p/smali/issues/detail?id=119
155 it.Next();
156 continue;
157 }
158 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700159 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700161 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
162 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700164 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700165 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700166 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700167 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700168 StackHandleScope<1> hs(self);
169 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 MethodVerifier::FailureKind result = VerifyMethod(self,
171 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700172 dex_file,
173 dex_cache,
174 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700176 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700177 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700178 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700179 allow_soft_failures,
180 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 if (result != kNoFailure) {
182 if (result == kHardFailure) {
183 hard_fail = true;
184 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700185 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700186 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700187 *error = "Verifier rejected class ";
188 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
189 *error += " due to bad method ";
190 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800193 }
194 it.Next();
195 }
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_virtual_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700208 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700209 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
210 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700211 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700212 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 StackHandleScope<1> hs(self);
217 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700225 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700226 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700227 allow_soft_failures,
228 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700244 if (error_count == 0) {
245 return kNoFailure;
246 } else {
247 return hard_fail ? kHardFailure : kSoftFailure;
248 }
jeffhaof56197c2012-03-05 18:01:54 -0800249}
250
Ian Rogers7b078e82014-09-10 14:44:24 -0700251MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700253 Handle<mirror::DexCache> dex_cache,
254 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700257 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700258 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700259 bool allow_soft_failures,
260 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700261 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700262 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700263
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700265 method_idx, method, method_access_flags, true, allow_soft_failures,
266 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700267 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700268 // Verification completed, however failures may be pending that didn't cause the verification
269 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700270 CHECK(!verifier.have_pending_hard_failure_);
271 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700272 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700273 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700274 << PrettyMethod(method_idx, *dex_file) << "\n");
275 }
Ian Rogersc8982582012-09-07 16:53:25 -0700276 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800277 }
278 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700280 CHECK_NE(verifier.failures_.size(), 0U);
281 CHECK(verifier.have_pending_hard_failure_);
282 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700283 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800284 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700285 std::cout << "\n" << verifier.info_messages_.str();
286 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800287 }
Ian Rogersc8982582012-09-07 16:53:25 -0700288 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800289 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700290 if (kTimeVerifyMethod) {
291 uint64_t duration_ns = NanoTime() - start_ns;
292 if (duration_ns > MsToNs(100)) {
293 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
294 << " took " << PrettyDuration(duration_ns);
295 }
Ian Rogersc8982582012-09-07 16:53:25 -0700296 }
297 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800298}
299
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700300MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700301 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700302 Handle<mirror::DexCache> dex_cache,
303 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700306 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800307 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700308 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
309 class_def, code_item, dex_method_idx, method,
310 method_access_flags, true, true, true, true);
311 verifier->Verify();
312 verifier->DumpFailures(os);
313 os << verifier->info_messages_.str();
314 verifier->Dump(os);
315
316 return verifier;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800317}
318
Ian Rogers7b078e82014-09-10 14:44:24 -0700319MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700320 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
321 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700322 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700323 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700324 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700326 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700327 : self_(self),
328 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800329 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800330 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700331 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700332 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700333 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800334 dex_file_(dex_file),
335 dex_cache_(dex_cache),
336 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700337 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800338 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700339 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700341 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700342 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700343 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800344 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800345 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700346 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200347 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200349 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700350 has_virtual_or_interface_invokes_(false),
351 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800352 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700353 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800354}
355
Mathieu Chartier590fee92013-09-13 13:46:47 -0700356MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800357 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358 STLDeleteElements(&failure_messages_);
359}
360
Brian Carlstromea46f952013-07-30 01:26:50 -0700361void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700362 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700363 Thread* self = Thread::Current();
364 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700365 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
366 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700367 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700369 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
370 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700371 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700372 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700373 verifier.FindLocksAtDexPc();
374}
375
376void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700377 CHECK(monitor_enter_dex_pcs_ != nullptr);
378 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700379
380 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
381 // verification. In practice, the phase we want relies on data structures set up by all the
382 // earlier passes, so we just run the full method verification and bail out early when we've
383 // got what we wanted.
384 Verify();
385}
386
Brian Carlstromea46f952013-07-30 01:26:50 -0700387mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700388 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700389 Thread* self = Thread::Current();
390 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700391 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
392 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700393 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700394 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700395 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
396 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200397 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398}
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700401 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200402
403 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
404 // verification. In practice, the phase we want relies on data structures set up by all the
405 // earlier passes, so we just run the full method verification and bail out early when we've
406 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200407 bool success = Verify();
408 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700409 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200410 }
411 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700412 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700413 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200414 }
415 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
416 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200417}
418
Brian Carlstromea46f952013-07-30 01:26:50 -0700419mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700420 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700421 Thread* self = Thread::Current();
422 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700423 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
424 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700425 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700426 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700427 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
428 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200429 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430}
431
Brian Carlstromea46f952013-07-30 01:26:50 -0700432mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700433 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200434
435 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
436 // verification. In practice, the phase we want relies on data structures set up by all the
437 // earlier passes, so we just run the full method verification and bail out early when we've
438 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200439 bool success = Verify();
440 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700441 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200442 }
443 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700444 if (register_line == nullptr) {
445 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200446 }
447 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
448 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
449 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200450}
451
Ian Rogersad0b3a32012-04-16 14:50:24 -0700452bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700453 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700454 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700457 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 } else {
459 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700460 }
jeffhaobdb76512011-09-07 11:43:16 -0700461 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
463 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
465 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800469 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700470 // Run through the instructions and see if the width checks out.
471 bool result = ComputeWidthsAndCountOps();
472 // Flag instructions guarded by a "try" block and check exception handlers.
473 result = result && ScanTryCatchBlocks();
474 // Perform static instruction verification.
475 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700476 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000477 result = result && VerifyCodeFlow();
478 // Compute information for compiler.
479 if (result && Runtime::Current()->IsCompiler()) {
480 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
481 }
482 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700483}
484
Ian Rogers776ac1f2012-04-13 23:36:36 -0700485std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 switch (error) {
487 case VERIFY_ERROR_NO_CLASS:
488 case VERIFY_ERROR_NO_FIELD:
489 case VERIFY_ERROR_NO_METHOD:
490 case VERIFY_ERROR_ACCESS_CLASS:
491 case VERIFY_ERROR_ACCESS_FIELD:
492 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700493 case VERIFY_ERROR_INSTANTIATION:
494 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800495 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700496 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
497 // class change and instantiation errors into soft verification errors so that we re-verify
498 // at runtime. We may fail to find or to agree on access because of not yet available class
499 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
500 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
501 // paths" that dynamically perform the verification and cause the behavior to be that akin
502 // to an interpreter.
503 error = VERIFY_ERROR_BAD_CLASS_SOFT;
504 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700505 // If we fail again at runtime, mark that this instruction would throw and force this
506 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700507 have_pending_runtime_throw_failure_ = true;
508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700509 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700510 // Indication that verification should be retried at runtime.
511 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700512 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700513 have_pending_hard_failure_ = true;
514 }
515 break;
jeffhaod5347e02012-03-22 17:25:05 -0700516 // Hard verification failures at compile time will still fail at runtime, so the class is
517 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700518 case VERIFY_ERROR_BAD_CLASS_HARD: {
519 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700520 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000521 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800522 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700523 have_pending_hard_failure_ = true;
524 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800525 }
526 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700527 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700528 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700530 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700531 failure_messages_.push_back(failure_message);
532 return *failure_message;
533}
534
Ian Rogers576ca0c2014-06-06 15:58:22 -0700535std::ostream& MethodVerifier::LogVerifyInfo() {
536 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
537 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
538}
539
Ian Rogersad0b3a32012-04-16 14:50:24 -0700540void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
541 size_t failure_num = failure_messages_.size();
542 DCHECK_NE(failure_num, 0U);
543 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
544 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700545 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700546 delete last_fail_message;
547}
548
549void MethodVerifier::AppendToLastFailMessage(std::string append) {
550 size_t failure_num = failure_messages_.size();
551 DCHECK_NE(failure_num, 0U);
552 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
553 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800554}
555
Ian Rogers776ac1f2012-04-13 23:36:36 -0700556bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700557 const uint16_t* insns = code_item_->insns_;
558 size_t insns_size = code_item_->insns_size_in_code_units_;
559 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700560 size_t new_instance_count = 0;
561 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700563
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700565 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700566 switch (opcode) {
567 case Instruction::APUT_OBJECT:
568 case Instruction::CHECK_CAST:
569 has_check_casts_ = true;
570 break;
571 case Instruction::INVOKE_VIRTUAL:
572 case Instruction::INVOKE_VIRTUAL_RANGE:
573 case Instruction::INVOKE_INTERFACE:
574 case Instruction::INVOKE_INTERFACE_RANGE:
575 has_virtual_or_interface_invokes_ = true;
576 break;
577 case Instruction::MONITOR_ENTER:
578 monitor_enter_count++;
579 break;
580 case Instruction::NEW_INSTANCE:
581 new_instance_count++;
582 break;
583 default:
584 break;
jeffhaobdb76512011-09-07 11:43:16 -0700585 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700587 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700589 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
591
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
594 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700595 return false;
596 }
597
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 new_instance_count_ = new_instance_count;
599 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700600 return true;
601}
602
Ian Rogers776ac1f2012-04-13 23:36:36 -0700603bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700605 if (tries_size == 0) {
606 return true;
607 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700609 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700610
611 for (uint32_t idx = 0; idx < tries_size; idx++) {
612 const DexFile::TryItem* try_item = &tries[idx];
613 uint32_t start = try_item->start_addr_;
614 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700615 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
617 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700618 return false;
619 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700621 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
622 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700623 return false;
624 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700625 uint32_t dex_pc = start;
626 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
627 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700628 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700629 size_t insn_size = inst->SizeInCodeUnits();
630 dex_pc += insn_size;
631 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700632 }
633 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800634 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700635 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700636 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700637 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700638 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700639 CatchHandlerIterator iterator(handlers_ptr);
640 for (; iterator.HasNext(); iterator.Next()) {
641 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700643 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
644 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700645 return false;
646 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700648 // Ensure exception types are resolved so that they don't need resolution to be delivered,
649 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700650 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
652 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700653 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700654 if (exception_type == nullptr) {
655 DCHECK(self_->IsExceptionPending());
656 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700657 }
658 }
jeffhaobdb76512011-09-07 11:43:16 -0700659 }
Ian Rogers0571d352011-11-03 19:51:38 -0700660 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700661 }
jeffhaobdb76512011-09-07 11:43:16 -0700662 return true;
663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700667
Ian Rogers0c7abda2012-09-19 13:33:42 -0700668 /* 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 -0700669 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700670 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700671
672 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700673 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700675 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 return false;
677 }
678 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700679 // All invoke points are marked as "Throw" points already.
680 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000681 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700682 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000683 } else if (inst->IsReturn()) {
684 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 }
686 dex_pc += inst->SizeInCodeUnits();
687 inst = inst->Next();
688 }
689 return true;
690}
691
Ian Rogers776ac1f2012-04-13 23:36:36 -0700692bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 bool result = true;
694 switch (inst->GetVerifyTypeArgumentA()) {
695 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700696 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700699 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 }
702 switch (inst->GetVerifyTypeArgumentB()) {
703 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700704 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 break;
706 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700707 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 break;
709 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700710 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 break;
712 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700713 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 break;
715 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700716 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 break;
718 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700719 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 break;
721 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700722 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 break;
724 }
725 switch (inst->GetVerifyTypeArgumentC()) {
726 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700727 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 break;
729 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700730 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 break;
732 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700733 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 break;
735 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700736 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 break;
738 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700739 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 break;
741 }
742 switch (inst->GetVerifyExtraFlags()) {
743 case Instruction::kVerifyArrayData:
744 result = result && CheckArrayData(code_offset);
745 break;
746 case Instruction::kVerifyBranchTarget:
747 result = result && CheckBranchTarget(code_offset);
748 break;
749 case Instruction::kVerifySwitchTargets:
750 result = result && CheckSwitchTargets(code_offset);
751 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700752 case Instruction::kVerifyVarArgNonZero:
753 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700754 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700755 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
757 "non-range invoke";
758 return false;
759 }
Ian Rogers29a26482014-05-02 15:27:29 -0700760 uint32_t args[Instruction::kMaxVarArgRegs];
761 inst->GetVarArgs(args);
762 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700764 }
Andreas Gampec3314312014-06-19 18:13:29 -0700765 case Instruction::kVerifyVarArgRangeNonZero:
766 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700768 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
769 inst->VRegA() <= 0) {
770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
771 "range invoke";
772 return false;
773 }
Ian Rogers29a26482014-05-02 15:27:29 -0700774 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 break;
776 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 result = false;
779 break;
780 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700781 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
783 result = false;
784 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 return result;
786}
787
Ian Rogers7b078e82014-09-10 14:44:24 -0700788inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
791 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700792 return false;
793 }
794 return true;
795}
796
Ian Rogers7b078e82014-09-10 14:44:24 -0700797inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
800 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 return false;
802 }
803 return true;
804}
805
Ian Rogers7b078e82014-09-10 14:44:24 -0700806inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
809 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 return false;
811 }
812 return true;
813}
814
Ian Rogers7b078e82014-09-10 14:44:24 -0700815inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
818 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 return false;
820 }
821 return true;
822}
823
Ian Rogers7b078e82014-09-10 14:44:24 -0700824inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
827 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 return false;
829 }
830 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700831 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
835 }
836 return true;
837}
838
Ian Rogers7b078e82014-09-10 14:44:24 -0700839inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
842 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 return false;
844 }
845 return true;
846}
847
Ian Rogers7b078e82014-09-10 14:44:24 -0700848inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
851 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 return false;
853 }
854 return true;
855}
856
Ian Rogers776ac1f2012-04-13 23:36:36 -0700857bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
860 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 return false;
862 }
863 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700864 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 const char* cp = descriptor;
866 while (*cp++ == '[') {
867 bracket_count++;
868 }
869 if (bracket_count == 0) {
870 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700871 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
872 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700873 return false;
874 } else if (bracket_count > 255) {
875 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700876 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
877 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 return false;
879 }
880 return true;
881}
882
Ian Rogers776ac1f2012-04-13 23:36:36 -0700883bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
885 const uint16_t* insns = code_item_->insns_ + cur_offset;
886 const uint16_t* array_data;
887 int32_t array_data_offset;
888
889 DCHECK_LT(cur_offset, insn_count);
890 /* make sure the start of the array data table is in range */
891 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
892 if ((int32_t) cur_offset + array_data_offset < 0 ||
893 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700895 << ", data offset " << array_data_offset
896 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 return false;
898 }
899 /* offset to array data table is a relative branch-style offset */
900 array_data = insns + array_data_offset;
901 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800902 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
904 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 return false;
906 }
907 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700908 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
910 /* make sure the end of the switch is in range */
911 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
913 << ", data offset " << array_data_offset << ", end "
914 << cur_offset + array_data_offset + table_size
915 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 return true;
919}
920
Ian Rogers776ac1f2012-04-13 23:36:36 -0700921bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 int32_t offset;
923 bool isConditional, selfOkay;
924 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
925 return false;
926 }
927 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
929 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 return false;
931 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700932 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
933 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
936 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 }
939 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
940 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700941 if (abs_offset < 0 ||
942 (uint32_t) abs_offset >= insn_count ||
943 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700945 << reinterpret_cast<void*>(abs_offset) << ") at "
946 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 insn_flags_[abs_offset].SetBranchTarget();
950 return true;
951}
952
Ian Rogers776ac1f2012-04-13 23:36:36 -0700953bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 bool* selfOkay) {
955 const uint16_t* insns = code_item_->insns_ + cur_offset;
956 *pConditional = false;
957 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 switch (*insns & 0xff) {
959 case Instruction::GOTO:
960 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700961 break;
962 case Instruction::GOTO_32:
963 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 *selfOkay = true;
965 break;
966 case Instruction::GOTO_16:
967 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700968 break;
969 case Instruction::IF_EQ:
970 case Instruction::IF_NE:
971 case Instruction::IF_LT:
972 case Instruction::IF_GE:
973 case Instruction::IF_GT:
974 case Instruction::IF_LE:
975 case Instruction::IF_EQZ:
976 case Instruction::IF_NEZ:
977 case Instruction::IF_LTZ:
978 case Instruction::IF_GEZ:
979 case Instruction::IF_GTZ:
980 case Instruction::IF_LEZ:
981 *pOffset = (int16_t) insns[1];
982 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 break;
984 default:
985 return false;
986 break;
987 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700988 return true;
989}
990
Ian Rogers776ac1f2012-04-13 23:36:36 -0700991bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700993 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
997 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999 << ", switch offset " << switch_offset
1000 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001001 return false;
1002 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001003 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001006 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1008 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 return false;
1010 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 uint32_t switch_count = switch_insns[1];
1012 int32_t keys_offset, targets_offset;
1013 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001014 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1015 /* 0=sig, 1=count, 2/3=firstKey */
1016 targets_offset = 4;
1017 keys_offset = -1;
1018 expected_signature = Instruction::kPackedSwitchSignature;
1019 } else {
1020 /* 0=sig, 1=count, 2..count*2 = keys */
1021 keys_offset = 2;
1022 targets_offset = 2 + 2 * switch_count;
1023 expected_signature = Instruction::kSparseSwitchSignature;
1024 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001026 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001027 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1028 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1029 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001030 return false;
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 /* make sure the end of the switch is in range */
1033 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1035 << ", switch offset " << switch_offset
1036 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001037 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001038 return false;
1039 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001040 /* for a sparse switch, verify the keys are in ascending order */
1041 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001042 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1043 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001044 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1045 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1046 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1048 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 return false;
1050 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001051 last_key = key;
1052 }
1053 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001054 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 for (uint32_t targ = 0; targ < switch_count; targ++) {
1056 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1057 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1058 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001059 if (abs_offset < 0 ||
1060 abs_offset >= (int32_t) insn_count ||
1061 !insn_flags_[abs_offset].IsOpcode()) {
1062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1063 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1064 << reinterpret_cast<void*>(cur_offset)
1065 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001066 return false;
1067 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001068 insn_flags_[abs_offset].SetBranchTarget();
1069 }
1070 return true;
1071}
1072
Ian Rogers776ac1f2012-04-13 23:36:36 -07001073bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001074 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 return false;
1077 }
1078 uint16_t registers_size = code_item_->registers_size_;
1079 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001080 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1082 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 return false;
1084 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001085 }
1086
1087 return true;
1088}
1089
Ian Rogers776ac1f2012-04-13 23:36:36 -07001090bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 uint16_t registers_size = code_item_->registers_size_;
1092 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1093 // integer overflow when adding them here.
1094 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1096 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001097 return false;
1098 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001099 return true;
1100}
1101
Ian Rogers776ac1f2012-04-13 23:36:36 -07001102bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 uint16_t registers_size = code_item_->registers_size_;
1104 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001105
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001107 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1108 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 }
1110 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001111 reg_table_.Init(kTrackCompilerInterestPoints,
1112 insn_flags_.get(),
1113 insns_size,
1114 registers_size,
1115 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001116
jeffhaobdb76512011-09-07 11:43:16 -07001117
Ian Rogersd0fbd852013-09-24 18:17:04 -07001118 work_line_.reset(RegisterLine::Create(registers_size, this));
1119 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001120
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 /* Initialize register types of method arguments. */
1122 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001123 DCHECK_NE(failures_.size(), 0U);
1124 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001126 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 return false;
1128 }
1129 /* Perform code flow verification. */
1130 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001131 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001133 }
jeffhaobdb76512011-09-07 11:43:16 -07001134 return true;
1135}
1136
Ian Rogersad0b3a32012-04-16 14:50:24 -07001137std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1138 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001139 for (size_t i = 0; i < failures_.size(); ++i) {
1140 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001141 }
1142 return os;
1143}
1144
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001145extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001147 v->Dump(std::cerr);
1148}
1149
Ian Rogers776ac1f2012-04-13 23:36:36 -07001150void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001151 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001152 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 return;
jeffhaobdb76512011-09-07 11:43:16 -07001154 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001155 {
1156 os << "Register Types:\n";
1157 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1158 std::ostream indent_os(&indent_filter);
1159 reg_types_.Dump(indent_os);
1160 }
Ian Rogersb4903572012-10-11 11:52:56 -07001161 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001162 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1163 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 const Instruction* inst = Instruction::At(code_item_->insns_);
1165 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001166 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001168 if (reg_line != nullptr) {
1169 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001170 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001171 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001172 const bool kDumpHexOfInstruction = false;
1173 if (kDumpHexOfInstruction) {
1174 indent_os << inst->DumpHex(5) << " ";
1175 }
1176 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001177 inst = inst->Next();
1178 }
jeffhaobdb76512011-09-07 11:43:16 -07001179}
1180
Ian Rogersd81871c2011-10-03 13:57:23 -07001181static bool IsPrimitiveDescriptor(char descriptor) {
1182 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001183 case 'I':
1184 case 'C':
1185 case 'S':
1186 case 'B':
1187 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001188 case 'F':
1189 case 'D':
1190 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001192 default:
1193 return false;
1194 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001195}
1196
Ian Rogers776ac1f2012-04-13 23:36:36 -07001197bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 RegisterLine* reg_line = reg_table_.GetLine(0);
1199 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1200 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001201
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001203 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001205 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1207 // argument as uninitialized. This restricts field access until the superclass constructor is
1208 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001209 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001210 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001211 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 reg_types_.UninitializedThisArgument(declaring_class));
1213 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001214 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001215 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001217 }
1218
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001219 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001220 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001221 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001222
1223 for (; iterator.HasNext(); iterator.Next()) {
1224 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001225 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 LOG(FATAL) << "Null descriptor";
1227 }
1228 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001229 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1230 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 return false;
1232 }
1233 switch (descriptor[0]) {
1234 case 'L':
1235 case '[':
1236 // We assume that reference arguments are initialized. The only way it could be otherwise
1237 // (assuming the caller was verified) is if the current method is <init>, but in that case
1238 // it's effectively considered initialized the instant we reach here (in the sense that we
1239 // can return without doing anything or call virtual methods).
1240 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001241 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001242 if (!reg_type.IsNonZeroReferenceTypes()) {
1243 DCHECK(HasFailures());
1244 return false;
1245 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001246 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 }
1248 break;
1249 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001250 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 break;
1252 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001253 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 break;
1255 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001256 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 break;
1258 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001259 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 break;
1261 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001262 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 break;
1264 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001265 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 break;
1267 case 'J':
1268 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001269 if (cur_arg + 1 >= expected_args) {
1270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1271 << " args, found more (" << descriptor << ")";
1272 return false;
1273 }
1274
Ian Rogers7b078e82014-09-10 14:44:24 -07001275 const RegType* lo_half;
1276 const RegType* hi_half;
1277 if (descriptor[0] == 'J') {
1278 lo_half = &reg_types_.LongLo();
1279 hi_half = &reg_types_.LongHi();
1280 } else {
1281 lo_half = &reg_types_.DoubleLo();
1282 hi_half = &reg_types_.DoubleHi();
1283 }
1284 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 cur_arg++;
1286 break;
1287 }
1288 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1290 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 return false;
1292 }
1293 cur_arg++;
1294 }
1295 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1297 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 return false;
1299 }
1300 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1301 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1302 // format. Only major difference from the method argument format is that 'V' is supported.
1303 bool result;
1304 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1305 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001306 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 size_t i = 0;
1308 do {
1309 i++;
1310 } while (descriptor[i] == '['); // process leading [
1311 if (descriptor[i] == 'L') { // object array
1312 do {
1313 i++; // find closing ;
1314 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1315 result = descriptor[i] == ';';
1316 } else { // primitive array
1317 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1318 }
1319 } else if (descriptor[0] == 'L') {
1320 // could be more thorough here, but shouldn't be required
1321 size_t i = 0;
1322 do {
1323 i++;
1324 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1325 result = descriptor[i] == ';';
1326 } else {
1327 result = false;
1328 }
1329 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1331 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 }
1333 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001334}
1335
Ian Rogers776ac1f2012-04-13 23:36:36 -07001336bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 const uint16_t* insns = code_item_->insns_;
1338 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001339
jeffhaobdb76512011-09-07 11:43:16 -07001340 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 insn_flags_[0].SetChanged();
1342 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001343
jeffhaobdb76512011-09-07 11:43:16 -07001344 /* Continue until no instructions are marked "changed". */
1345 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001346 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1348 uint32_t insn_idx = start_guess;
1349 for (; insn_idx < insns_size; insn_idx++) {
1350 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001351 break;
1352 }
jeffhaobdb76512011-09-07 11:43:16 -07001353 if (insn_idx == insns_size) {
1354 if (start_guess != 0) {
1355 /* try again, starting from the top */
1356 start_guess = 0;
1357 continue;
1358 } else {
1359 /* all flags are clear */
1360 break;
1361 }
1362 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 // We carry the working set of registers from instruction to instruction. If this address can
1364 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1365 // "changed" flags, we need to load the set of registers from the table.
1366 // Because we always prefer to continue on to the next instruction, we should never have a
1367 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1368 // target.
1369 work_insn_idx_ = insn_idx;
1370 if (insn_flags_[insn_idx].IsBranchTarget()) {
1371 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001372 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001373 /*
1374 * Sanity check: retrieve the stored register line (assuming
1375 * a full table) and make sure it actually matches.
1376 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001378 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 if (work_line_->CompareLine(register_line) != 0) {
1380 Dump(std::cout);
1381 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001382 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001383 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001384 << " work_line=" << work_line_->Dump(this) << "\n"
1385 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 }
jeffhaobdb76512011-09-07 11:43:16 -07001387 }
jeffhaobdb76512011-09-07 11:43:16 -07001388 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001390 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001391 prepend += " failed to verify: ";
1392 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 return false;
1394 }
jeffhaobdb76512011-09-07 11:43:16 -07001395 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 insn_flags_[insn_idx].SetVisited();
1397 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001398 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001399
Ian Rogers1c849e52012-06-28 14:00:33 -07001400 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001401 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001402 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001403 * (besides the wasted space), but it indicates a flaw somewhere
1404 * down the line, possibly in the verifier.
1405 *
1406 * If we've substituted "always throw" instructions into the stream,
1407 * we are almost certainly going to have some dead code.
1408 */
1409 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001411 for (; insn_idx < insns_size;
1412 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001413 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001415 * may or may not be preceded by a padding NOP (for alignment).
1416 */
1417 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1418 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1419 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001420 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001421 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1422 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1423 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001425 }
1426
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001428 if (dead_start < 0)
1429 dead_start = insn_idx;
1430 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001431 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1432 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001433 dead_start = -1;
1434 }
1435 }
1436 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001437 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1438 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001439 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001440 // To dump the state of the verify after a method, do something like:
1441 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1442 // "boolean java.lang.String.equals(java.lang.Object)") {
1443 // LOG(INFO) << info_messages_.str();
1444 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 }
jeffhaobdb76512011-09-07 11:43:16 -07001446 return true;
1447}
1448
Ian Rogers776ac1f2012-04-13 23:36:36 -07001449bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001450 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1451 // We want the state _before_ the instruction, for the case where the dex pc we're
1452 // interested in is itself a monitor-enter instruction (which is a likely place
1453 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001454 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001455 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001456 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1457 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1458 }
1459 }
1460
jeffhaobdb76512011-09-07 11:43:16 -07001461 /*
1462 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001463 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001464 * control to another statement:
1465 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001466 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001467 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001468 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001469 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001470 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001471 * throw an exception that is handled by an encompassing "try"
1472 * block.
1473 *
1474 * We can also return, in which case there is no successor instruction
1475 * from this point.
1476 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001477 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001478 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001479 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1480 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001481 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001482
jeffhaobdb76512011-09-07 11:43:16 -07001483 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001484 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001485 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001487 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001488 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 }
jeffhaobdb76512011-09-07 11:43:16 -07001490
1491 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001492 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001493 * can throw an exception, we will copy/merge this into the "catch"
1494 * address rather than work_line, because we don't want the result
1495 * from the "successful" code path (e.g. a check-cast that "improves"
1496 * a type) to be visible to the exception handler.
1497 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001498 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001500 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001502 }
1503
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001504
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001505 // We need to ensure the work line is consistent while performing validation. When we spot a
1506 // peephole pattern we compute a new line for either the fallthrough instruction or the
1507 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001508 std::unique_ptr<RegisterLine> branch_line;
1509 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001510
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001512 case Instruction::NOP:
1513 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001514 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001515 * a signature that looks like a NOP; if we see one of these in
1516 * the course of executing code then we have a problem.
1517 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001518 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001520 }
1521 break;
1522
1523 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001524 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001525 break;
jeffhaobdb76512011-09-07 11:43:16 -07001526 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001527 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 break;
jeffhaobdb76512011-09-07 11:43:16 -07001529 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001530 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001531 break;
1532 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001533 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 break;
jeffhaobdb76512011-09-07 11:43:16 -07001535 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001536 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001537 break;
jeffhaobdb76512011-09-07 11:43:16 -07001538 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001539 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001540 break;
1541 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001542 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 break;
jeffhaobdb76512011-09-07 11:43:16 -07001544 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001545 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001546 break;
jeffhaobdb76512011-09-07 11:43:16 -07001547 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001548 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001549 break;
1550
1551 /*
1552 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001553 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001554 * might want to hold the result in an actual CPU register, so the
1555 * Dalvik spec requires that these only appear immediately after an
1556 * invoke or filled-new-array.
1557 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001558 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001559 * redundant with the reset done below, but it can make the debug info
1560 * easier to read in some cases.)
1561 */
1562 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001563 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001564 break;
1565 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001566 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001567 break;
1568 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001569 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001570 break;
1571
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001573 /*
jeffhao60f83e32012-02-13 17:16:30 -08001574 * This statement can only appear as the first instruction in an exception handler. We verify
1575 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001576 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001577 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001578 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001579 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001582 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001583 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 }
jeffhaobdb76512011-09-07 11:43:16 -07001586 }
1587 break;
1588 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001589 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001590 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001591 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1594 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 } else {
1596 // Compilers may generate synthetic functions that write byte values into boolean fields.
1597 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001598 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001599 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1601 ((return_type.IsBoolean() || return_type.IsByte() ||
1602 return_type.IsShort() || return_type.IsChar()) &&
1603 src_type.IsInteger()));
1604 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001605 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001606 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001607 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001608 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 }
jeffhaobdb76512011-09-07 11:43:16 -07001610 }
1611 }
1612 break;
1613 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001614 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001615 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001616 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001619 } else {
1620 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001622 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001623 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 }
jeffhaobdb76512011-09-07 11:43:16 -07001626 }
1627 }
1628 break;
1629 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001630 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001631 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001634 } else {
1635 /* return_type is the *expected* return type, not register value */
1636 DCHECK(!return_type.IsZero());
1637 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001639 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001640 // Disallow returning uninitialized values and verify that the reference in vAA is an
1641 // instance of the "return_type"
1642 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001643 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1644 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001645 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001646 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1647 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1648 << "' or '" << reg_type << "'";
1649 } else {
1650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1651 << "', but expected from declaration '" << return_type << "'";
1652 }
jeffhaobdb76512011-09-07 11:43:16 -07001653 }
1654 }
1655 }
1656 break;
1657
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001658 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 case Instruction::CONST_4: {
1660 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001662 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001663 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 }
1665 case Instruction::CONST_16: {
1666 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001668 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001669 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001671 case Instruction::CONST: {
1672 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001674 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001675 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001676 }
1677 case Instruction::CONST_HIGH16: {
1678 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001680 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001681 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001682 }
jeffhaobdb76512011-09-07 11:43:16 -07001683 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001686 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1687 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001690 }
1691 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001693 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1694 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001695 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001696 break;
1697 }
1698 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001700 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1701 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001702 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 break;
1704 }
1705 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001707 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1708 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001709 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001710 break;
1711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001713 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001714 break;
jeffhaobdb76512011-09-07 11:43:16 -07001715 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001716 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001717 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 // Get type from instruction if unresolved then we need an access check
1720 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001721 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001722 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001723 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001724 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001725 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001726 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 }
jeffhaobdb76512011-09-07 11:43:16 -07001728 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001729 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001730 break;
1731 case Instruction::MONITOR_EXIT:
1732 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001733 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001734 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001735 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001736 * to the need to handle asynchronous exceptions, a now-deprecated
1737 * feature that Dalvik doesn't support.)
1738 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001739 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001740 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001741 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001742 * structured locking checks are working, the former would have
1743 * failed on the -enter instruction, and the latter is impossible.
1744 *
1745 * This is fortunate, because issue 3221411 prevents us from
1746 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001747 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001748 * some catch blocks (which will show up as "dead" code when
1749 * we skip them here); if we can't, then the code path could be
1750 * "live" so we still need to check it.
1751 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001752 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
1755
Ian Rogers28ad40d2011-10-27 15:19:26 -07001756 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001758 /*
1759 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1760 * could be a "upcast" -- not expected, so we don't try to address it.)
1761 *
1762 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001763 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001764 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1766 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001767 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001768 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001769 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001770 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001771 if (klass != nullptr && klass->IsPrimitive()) {
1772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1773 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1774 << GetDeclaringClass();
1775 break;
1776 }
1777
Ian Rogersad0b3a32012-04-16 14:50:24 -07001778 DCHECK_NE(failures_.size(), 0U);
1779 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001780 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001781 }
1782 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001783 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001784 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001785 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001786 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001787 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 if (is_checkcast) {
1789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1790 } else {
1791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1792 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001793 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 if (is_checkcast) {
1795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1796 } else {
1797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1798 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001799 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001800 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001801 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001803 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001804 }
jeffhaobdb76512011-09-07 11:43:16 -07001805 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001806 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 }
1808 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001810 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001811 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001814 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001816 } else {
1817 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 }
1819 break;
1820 }
1821 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001822 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001823 if (res_type.IsConflict()) {
1824 DCHECK_NE(failures_.size(), 0U);
1825 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001826 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001827 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1828 // can't create an instance of an interface or abstract class */
1829 if (!res_type.IsInstantiableTypes()) {
1830 Fail(VERIFY_ERROR_INSTANTIATION)
1831 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001832 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001834 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001835 // Any registers holding previous allocations from this address that have not yet been
1836 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001837 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001838 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 break;
1841 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001842 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001843 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
1845 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001846 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001847 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001848 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001849 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001851 just_set_result = true; // Filled new array range sets result register
1852 break;
jeffhaobdb76512011-09-07 11:43:16 -07001853 case Instruction::CMPL_FLOAT:
1854 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001855 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001856 break;
1857 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001858 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001859 break;
1860 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001862 break;
1863 case Instruction::CMPL_DOUBLE:
1864 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001867 break;
1868 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001870 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001871 break;
1872 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001873 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001878 break;
1879 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001882 break;
1883 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001884 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001885 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001888 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001889 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1890 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001891 }
1892 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 }
jeffhaobdb76512011-09-07 11:43:16 -07001894 case Instruction::GOTO:
1895 case Instruction::GOTO_16:
1896 case Instruction::GOTO_32:
1897 /* no effect on or use of registers */
1898 break;
1899
1900 case Instruction::PACKED_SWITCH:
1901 case Instruction::SPARSE_SWITCH:
1902 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001903 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001904 break;
1905
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 case Instruction::FILL_ARRAY_DATA: {
1907 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001909 /* array_type can be null if the reg type is Zero */
1910 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001911 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1913 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001914 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001915 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001916 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001917 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1919 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 } else {
jeffhao457cc512012-02-02 16:55:13 -08001921 // Now verify if the element width in the table matches the element width declared in
1922 // the array
1923 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1924 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001926 } else {
1927 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1928 // Since we don't compress the data in Dex, expect to see equal width of data stored
1929 // in the table and expected from the array class.
1930 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1932 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001933 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 }
1935 }
jeffhaobdb76512011-09-07 11:43:16 -07001936 }
1937 }
1938 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1943 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 bool mismatch = false;
1945 if (reg_type1.IsZero()) { // zero then integral or reference expected
1946 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1947 } else if (reg_type1.IsReferenceTypes()) { // both references?
1948 mismatch = !reg_type2.IsReferenceTypes();
1949 } else { // both integral?
1950 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1951 }
1952 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1954 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001955 }
1956 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 }
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::IF_LT:
1959 case Instruction::IF_GE:
1960 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1963 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1966 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001967 }
1968 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 }
jeffhaobdb76512011-09-07 11:43:16 -07001970 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001972 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1975 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001977
1978 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001979 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001980 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001981 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001982 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001983 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001984 }
Ian Rogers9b360392013-06-06 14:45:07 -07001985 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001986 } else {
1987 break;
1988 }
1989
Ian Rogers9b360392013-06-06 14:45:07 -07001990 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001991
1992 /* Check for peep-hole pattern of:
1993 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001994 * instance-of vX, vY, T;
1995 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001996 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001997 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001998 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001999 * and sharpen the type of vY to be type T.
2000 * Note, this pattern can't be if:
2001 * - if there are other branches to this branch,
2002 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002003 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002004 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002005 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2006 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2007 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002008 // Check the type of the instance-of is different than that of registers type, as if they
2009 // are the same there is no work to be done here. Check that the conversion is not to or
2010 // from an unresolved type as type information is imprecise. If the instance-of is to an
2011 // interface then ignore the type information as interfaces can only be treated as Objects
2012 // and we don't want to disallow field and other operations on the object. If the value
2013 // being instance-of checked against is known null (zero) then allow the optimization as
2014 // we didn't have type information. If the merge of the instance-of type with the original
2015 // type is assignable to the original then allow optimization. This check is performed to
2016 // ensure that subsequent merges don't lose type information - such as becoming an
2017 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002019 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002020
Ian Rogersebbdd872014-07-07 23:53:08 -07002021 if (!orig_type.Equals(cast_type) &&
2022 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002023 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002024 !cast_type.GetClass()->IsInterface() &&
2025 (orig_type.IsZero() ||
2026 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002027 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002028 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002029 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002030 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002031 branch_line.reset(update_line);
2032 }
2033 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002034 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002035 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2036 // See if instance-of was preceded by a move-object operation, common due to the small
2037 // register encoding space of instance-of, and propagate type information to the source
2038 // of the move-object.
2039 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002040 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002041 move_idx--;
2042 }
2043 CHECK(insn_flags_[move_idx].IsOpcode());
2044 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2045 switch (move_inst->Opcode()) {
2046 case Instruction::MOVE_OBJECT:
2047 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002049 }
2050 break;
2051 case Instruction::MOVE_OBJECT_FROM16:
2052 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002053 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002054 }
2055 break;
2056 case Instruction::MOVE_OBJECT_16:
2057 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002058 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002059 }
2060 break;
2061 default:
2062 break;
2063 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002064 }
2065 }
2066 }
2067
jeffhaobdb76512011-09-07 11:43:16 -07002068 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 }
jeffhaobdb76512011-09-07 11:43:16 -07002070 case Instruction::IF_LTZ:
2071 case Instruction::IF_GEZ:
2072 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002074 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002076 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2077 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 }
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 }
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
2099 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124
jeffhaobdb76512011-09-07 11:43:16 -07002125 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 break;
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 break;
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
jeffhaobdb76512011-09-07 11:43:16 -07002134 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 break;
2137 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
2140 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
2143 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 break;
jeffhaobdb76512011-09-07 11:43:16 -07002146
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 break;
2150 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002152 break;
2153 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 break;
2156 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002158 break;
2159 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
2162 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 break;
jeffhaobdb76512011-09-07 11:43:16 -07002165 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168
jeffhaobdb76512011-09-07 11:43:16 -07002169 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 break;
jeffhaobdb76512011-09-07 11:43:16 -07002172 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 break;
jeffhaobdb76512011-09-07 11:43:16 -07002175 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 break;
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 break;
2181 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002183 break;
2184 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002186 break;
2187 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 break;
2190
2191 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002192 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002193 break;
2194 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 break;
2197 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 break;
2200 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002205 break;
2206 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
2209 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212
2213 case Instruction::INVOKE_VIRTUAL:
2214 case Instruction::INVOKE_VIRTUAL_RANGE:
2215 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2218 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002219 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2220 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002221 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2222 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002223 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002224 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002225 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002226 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2227 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002228 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002229 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002230 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2231 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002232 return_type_class->CannotBeAssignedFromOtherTypes());
2233 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002234 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2235 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002236 }
2237 }
2238 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002240 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2241 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002242 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002243 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002244 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002245 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002246 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002248 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002250 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 }
jeffhaobdb76512011-09-07 11:43:16 -07002253 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002256 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002257 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002258 const char* return_type_descriptor;
2259 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002260 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002261 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002263 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002264 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002265 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2266 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2267 } else {
2268 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002269 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002270 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002271 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2272 MethodHelper mh(h_called_method);
2273 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2274 if (return_type_class != nullptr) {
2275 return_type = &reg_types_.FromClass(return_type_descriptor,
2276 return_type_class,
2277 return_type_class->CannotBeAssignedFromOtherTypes());
2278 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002279 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2280 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002281 }
Ian Rogers46685432012-06-03 22:26:43 -07002282 }
2283 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002284 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 * Some additional checks when calling a constructor. We know from the invocation arg check
2286 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2287 * that to require that called_method->klass is the same as this->klass or this->super,
2288 * allowing the latter only if the "this" argument is the same as the "this" argument to
2289 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002290 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002291 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002292 if (this_type.IsConflict()) // failure.
2293 break;
jeffhaobdb76512011-09-07 11:43:16 -07002294
jeffhaob57e9522012-04-26 18:08:21 -07002295 /* no null refs allowed (?) */
2296 if (this_type.IsZero()) {
2297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2298 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002299 }
jeffhaob57e9522012-04-26 18:08:21 -07002300
2301 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002302 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002303 // TODO: re-enable constructor type verification
2304 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002305 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002306 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2307 // break;
2308 // }
jeffhaob57e9522012-04-26 18:08:21 -07002309
2310 /* arg must be an uninitialized reference */
2311 if (!this_type.IsUninitializedTypes()) {
2312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2313 << this_type;
2314 break;
2315 }
2316
2317 /*
2318 * Replace the uninitialized reference with an initialized one. We need to do this for all
2319 * registers that have the same object instance in them, not just the "this" register.
2320 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002321 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002322 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002323 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002324 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2325 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002326 }
2327 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002328 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002330 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002332 just_set_result = true;
2333 break;
2334 }
2335 case Instruction::INVOKE_STATIC:
2336 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002338 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002339 METHOD_STATIC,
2340 is_range,
2341 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002342 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002343 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002345 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2346 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002347 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002348 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002349 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002350 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002351 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002353 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 } else {
2355 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2356 }
jeffhaobdb76512011-09-07 11:43:16 -07002357 just_set_result = true;
2358 }
2359 break;
jeffhaobdb76512011-09-07 11:43:16 -07002360 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002363 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002364 METHOD_INTERFACE,
2365 is_range,
2366 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002367 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002368 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002369 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2370 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2371 << PrettyMethod(abs_method) << "'";
2372 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002373 }
Ian Rogers0d604842012-04-16 14:50:24 -07002374 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 /* Get the type of the "this" arg, which should either be a sub-interface of called
2376 * interface or Object (see comments in RegType::JoinClass).
2377 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002378 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002379 if (this_type.IsZero()) {
2380 /* null pointer always passes (and always fails at runtime) */
2381 } else {
2382 if (this_type.IsUninitializedTypes()) {
2383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2384 << this_type;
2385 break;
2386 }
2387 // In the past we have tried to assert that "called_interface" is assignable
2388 // from "this_type.GetClass()", however, as we do an imprecise Join
2389 // (RegType::JoinClass) we don't have full information on what interfaces are
2390 // implemented by "this_type". For example, two classes may implement the same
2391 // interfaces and have a common parent that doesn't implement the interface. The
2392 // join will set "this_type" to the parent class and a test that this implements
2393 // the interface will incorrectly fail.
2394 }
2395 /*
2396 * We don't have an object instance, so we can't find the concrete method. However, all of
2397 * the type information is in the abstract method, so we're good.
2398 */
2399 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002400 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002402 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2403 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2404 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2405 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002406 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002407 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002408 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002411 } else {
2412 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2413 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002414 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 case Instruction::NEG_INT:
2418 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::NEG_LONG:
2422 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002427 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002430 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002431 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002438 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002442 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002445 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002446 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002450 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002451 break;
2452 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002454 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002460 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002461 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002464 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002469 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002472 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002473 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002476 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002477 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002480 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002486 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488
2489 case Instruction::ADD_INT:
2490 case Instruction::SUB_INT:
2491 case Instruction::MUL_INT:
2492 case Instruction::REM_INT:
2493 case Instruction::DIV_INT:
2494 case Instruction::SHL_INT:
2495 case Instruction::SHR_INT:
2496 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002498 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::AND_INT:
2501 case Instruction::OR_INT:
2502 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002503 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002504 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::ADD_LONG:
2507 case Instruction::SUB_LONG:
2508 case Instruction::MUL_LONG:
2509 case Instruction::DIV_LONG:
2510 case Instruction::REM_LONG:
2511 case Instruction::AND_LONG:
2512 case Instruction::OR_LONG:
2513 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002515 reg_types_.LongLo(), reg_types_.LongHi(),
2516 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::SHL_LONG:
2519 case Instruction::SHR_LONG:
2520 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002522 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002523 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::ADD_FLOAT:
2526 case Instruction::SUB_FLOAT:
2527 case Instruction::MUL_FLOAT:
2528 case Instruction::DIV_FLOAT:
2529 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002530 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2531 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::ADD_DOUBLE:
2534 case Instruction::SUB_DOUBLE:
2535 case Instruction::MUL_DOUBLE:
2536 case Instruction::DIV_DOUBLE:
2537 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2540 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002541 break;
2542 case Instruction::ADD_INT_2ADDR:
2543 case Instruction::SUB_INT_2ADDR:
2544 case Instruction::MUL_INT_2ADDR:
2545 case Instruction::REM_INT_2ADDR:
2546 case Instruction::SHL_INT_2ADDR:
2547 case Instruction::SHR_INT_2ADDR:
2548 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2550 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::AND_INT_2ADDR:
2553 case Instruction::OR_INT_2ADDR:
2554 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2556 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2560 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002561 break;
2562 case Instruction::ADD_LONG_2ADDR:
2563 case Instruction::SUB_LONG_2ADDR:
2564 case Instruction::MUL_LONG_2ADDR:
2565 case Instruction::DIV_LONG_2ADDR:
2566 case Instruction::REM_LONG_2ADDR:
2567 case Instruction::AND_LONG_2ADDR:
2568 case Instruction::OR_LONG_2ADDR:
2569 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002570 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002571 reg_types_.LongLo(), reg_types_.LongHi(),
2572 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
2574 case Instruction::SHL_LONG_2ADDR:
2575 case Instruction::SHR_LONG_2ADDR:
2576 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002577 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002578 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::ADD_FLOAT_2ADDR:
2581 case Instruction::SUB_FLOAT_2ADDR:
2582 case Instruction::MUL_FLOAT_2ADDR:
2583 case Instruction::DIV_FLOAT_2ADDR:
2584 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2586 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::ADD_DOUBLE_2ADDR:
2589 case Instruction::SUB_DOUBLE_2ADDR:
2590 case Instruction::MUL_DOUBLE_2ADDR:
2591 case Instruction::DIV_DOUBLE_2ADDR:
2592 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002594 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2595 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::ADD_INT_LIT16:
2598 case Instruction::RSUB_INT:
2599 case Instruction::MUL_INT_LIT16:
2600 case Instruction::DIV_INT_LIT16:
2601 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2603 true);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::AND_INT_LIT16:
2606 case Instruction::OR_INT_LIT16:
2607 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2609 true);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::ADD_INT_LIT8:
2612 case Instruction::RSUB_INT_LIT8:
2613 case Instruction::MUL_INT_LIT8:
2614 case Instruction::DIV_INT_LIT8:
2615 case Instruction::REM_INT_LIT8:
2616 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002617 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002618 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2620 false);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::AND_INT_LIT8:
2623 case Instruction::OR_INT_LIT8:
2624 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2626 false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002629 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002630 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002631 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2633 }
2634 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002635 // Note: the following instructions encode offsets derived from class linking.
2636 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2637 // meaning if the class linking and resolution were successful.
2638 case Instruction::IGET_QUICK:
2639 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2640 break;
2641 case Instruction::IGET_WIDE_QUICK:
2642 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2643 break;
2644 case Instruction::IGET_OBJECT_QUICK:
2645 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2646 break;
2647 case Instruction::IPUT_QUICK:
2648 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2649 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002650 case Instruction::IPUT_BOOLEAN_QUICK:
2651 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2652 break;
2653 case Instruction::IPUT_BYTE_QUICK:
2654 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2655 break;
2656 case Instruction::IPUT_CHAR_QUICK:
2657 VerifyIPutQuick(inst, reg_types_.Char(), true);
2658 break;
2659 case Instruction::IPUT_SHORT_QUICK:
2660 VerifyIPutQuick(inst, reg_types_.Short(), true);
2661 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002662 case Instruction::IPUT_WIDE_QUICK:
2663 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2664 break;
2665 case Instruction::IPUT_OBJECT_QUICK:
2666 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2667 break;
2668 case Instruction::INVOKE_VIRTUAL_QUICK:
2669 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2670 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002671 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002673 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002674 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002675 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002676 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002677 } else {
2678 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2679 }
2680 just_set_result = true;
2681 }
2682 break;
2683 }
2684
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002686 case Instruction::UNUSED_3E:
2687 case Instruction::UNUSED_3F:
2688 case Instruction::UNUSED_40:
2689 case Instruction::UNUSED_41:
2690 case Instruction::UNUSED_42:
2691 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002692 case Instruction::UNUSED_79:
2693 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002694 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002695 case Instruction::UNUSED_F0:
2696 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002697 case Instruction::UNUSED_F2:
2698 case Instruction::UNUSED_F3:
2699 case Instruction::UNUSED_F4:
2700 case Instruction::UNUSED_F5:
2701 case Instruction::UNUSED_F6:
2702 case Instruction::UNUSED_F7:
2703 case Instruction::UNUSED_F8:
2704 case Instruction::UNUSED_F9:
2705 case Instruction::UNUSED_FA:
2706 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002707 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002708 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002710 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713
2714 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002715 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002716 * complain if an instruction is missing (which is desirable).
2717 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002718 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002719
Ian Rogersad0b3a32012-04-16 14:50:24 -07002720 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002721 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002722 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002723 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002724 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002725 /* immediate failure, reject class */
2726 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2727 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002728 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002729 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002730 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002731 }
jeffhaobdb76512011-09-07 11:43:16 -07002732 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 * If we didn't just set the result register, clear it out. This ensures that you can only use
2734 * "move-result" immediately after the result is set. (We could check this statically, but it's
2735 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002736 */
2737 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002738 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002739 }
2740
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002741
jeffhaobdb76512011-09-07 11:43:16 -07002742
2743 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002744 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002745 *
2746 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002747 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002748 * somebody could get a reference field, check it for zero, and if the
2749 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002750 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002751 * that, and will reject the code.
2752 *
2753 * TODO: avoid re-fetching the branch target
2754 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002756 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002758 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002760 return false;
2761 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002762 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002763 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002764 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 }
jeffhaobdb76512011-09-07 11:43:16 -07002766 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002767 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002768 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002769 return false;
2770 }
2771 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002772 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002773 return false;
2774 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002775 }
jeffhaobdb76512011-09-07 11:43:16 -07002776 }
2777
2778 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002779 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002780 *
2781 * We've already verified that the table is structurally sound, so we
2782 * just need to walk through and tag the targets.
2783 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002784 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002785 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2786 const uint16_t* switch_insns = insns + offset_to_switch;
2787 int switch_count = switch_insns[1];
2788 int offset_to_targets, targ;
2789
2790 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2791 /* 0 = sig, 1 = count, 2/3 = first key */
2792 offset_to_targets = 4;
2793 } else {
2794 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002795 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002796 offset_to_targets = 2 + 2 * switch_count;
2797 }
2798
2799 /* verify each switch target */
2800 for (targ = 0; targ < switch_count; targ++) {
2801 int offset;
2802 uint32_t abs_offset;
2803
2804 /* offsets are 32-bit, and only partly endian-swapped */
2805 offset = switch_insns[offset_to_targets + targ * 2] |
2806 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 abs_offset = work_insn_idx_ + offset;
2808 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002809 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002810 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002812 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002813 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002814 }
jeffhaobdb76512011-09-07 11:43:16 -07002815 }
2816 }
2817
2818 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2820 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002821 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002822 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002823 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002824 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002825
Andreas Gampef91baf12014-07-18 15:41:00 -07002826 // Need the linker to try and resolve the handled class to check if it's Throwable.
2827 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2828
Ian Rogers0571d352011-11-03 19:51:38 -07002829 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002830 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2831 if (handler_type_idx == DexFile::kDexNoIndex16) {
2832 has_catch_all_handler = true;
2833 } else {
2834 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002835 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2836 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002837 if (klass != nullptr) {
2838 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2839 has_catch_all_handler = true;
2840 }
2841 } else {
2842 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002843 DCHECK(self_->IsExceptionPending());
2844 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 }
jeffhaobdb76512011-09-07 11:43:16 -07002847 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2849 * "work_regs", because at runtime the exception will be thrown before the instruction
2850 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002851 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002852 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002853 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 }
jeffhaobdb76512011-09-07 11:43:16 -07002855 }
2856
2857 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2859 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002860 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002861 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002862 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 * The state in work_line reflects the post-execution state. If the current instruction is a
2864 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002865 * it will do so before grabbing the lock).
2866 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002867 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002868 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002870 return false;
2871 }
2872 }
2873 }
2874
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002875 /* Handle "continue". Tag the next consecutive instruction.
2876 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2877 * because it changes work_line_ when performing peephole optimization
2878 * and this change should not be used in those cases.
2879 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002880 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2882 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002883 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2885 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002886 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002887 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2888 // next instruction isn't one.
2889 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2890 return false;
2891 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002892 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002893 // Make workline consistent with fallthrough computed from peephole optimization.
2894 work_line_->CopyFromLine(fallthrough_line.get());
2895 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002896 if (insn_flags_[next_insn_idx].IsReturn()) {
2897 // For returns we only care about the operand to the return, all other registers are dead.
2898 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2899 Instruction::Code opcode = ret_inst->Opcode();
2900 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002901 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002902 } else {
2903 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002905 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002907 }
2908 }
2909 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002910 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002911 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002912 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2913 // needed. If the merge changes the state of the registers then the work line will be
2914 // updated.
2915 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002916 return false;
2917 }
2918 } else {
2919 /*
2920 * We're not recording register data for the next instruction, so we don't know what the
2921 * prior state was. We have to assume that something has changed and re-evaluate it.
2922 */
2923 insn_flags_[next_insn_idx].SetChanged();
2924 }
2925 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002926
jeffhaod1f0fde2011-09-08 17:25:33 -07002927 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002928 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 return false;
2931 }
jeffhaobdb76512011-09-07 11:43:16 -07002932 }
2933
2934 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002935 * Update start_guess. Advance to the next instruction of that's
2936 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002937 * neither of those exists we're in a return or throw; leave start_guess
2938 * alone and let the caller sort it out.
2939 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002940 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2942 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002943 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002944 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002946 }
2947
Ian Rogersd81871c2011-10-03 13:57:23 -07002948 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2949 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002950
2951 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002952} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002953
Ian Rogersd8f69b02014-09-10 21:43:52 +00002954const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002955 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002956 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002957 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002958 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002959 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2960 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002961 if (result.IsConflict()) {
2962 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2963 << "' in " << referrer;
2964 return result;
2965 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002966 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002967 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002968 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002969 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002970 // check at runtime if access is allowed and so pass here. If result is
2971 // primitive, skip the access check.
2972 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2973 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002974 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002975 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002976 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002978}
2979
Ian Rogersd8f69b02014-09-10 21:43:52 +00002980const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002983 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2985 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002986 CatchHandlerIterator iterator(handlers_ptr);
2987 for (; iterator.HasNext(); iterator.Next()) {
2988 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2989 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002990 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002992 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002993 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002994 if (exception.IsUnresolvedTypes()) {
2995 // We don't know enough about the type. Fail here and let runtime handle it.
2996 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2997 return exception;
2998 } else {
2999 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3000 return reg_types_.Conflict();
3001 }
Jeff Haob878f212014-04-24 16:25:36 -07003002 } else if (common_super == nullptr) {
3003 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003004 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003005 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003007 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07003008 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 }
3010 }
3011 }
3012 }
Ian Rogers0571d352011-11-03 19:51:38 -07003013 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 }
3015 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003016 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003018 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003019 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003021 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022}
3023
Brian Carlstromea46f952013-07-30 01:26:50 -07003024mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003025 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003026 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003027 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 if (klass_type.IsConflict()) {
3029 std::string append(" in attempt to access method ");
3030 append += dex_file_->GetMethodName(method_id);
3031 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003033 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003034 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003035 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003036 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003037 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003038 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003039 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003041 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003042 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003043
3044 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003045 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003046 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 res_method = klass->FindInterfaceMethod(name, signature);
3048 } else {
3049 res_method = klass->FindVirtualMethod(name, signature);
3050 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003052 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003054 // If a virtual or interface method wasn't found with the expected type, look in
3055 // the direct methods. This can happen when the wrong invoke type is used or when
3056 // a class has changed, and will be flagged as an error in later checks.
3057 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3058 res_method = klass->FindDirectMethod(name, signature);
3059 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003061 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3062 << PrettyDescriptor(klass) << "." << name
3063 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003064 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003065 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003066 }
3067 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3069 // enforce them here.
3070 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3072 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003075 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003076 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3078 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003080 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003081 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003082 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003083 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003084 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003085 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003086 }
jeffhaode0d9c92012-02-27 13:58:13 -08003087 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3088 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3090 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003091 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003092 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003093 // Check that interface methods match interface classes.
3094 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3095 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3096 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003098 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3099 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3100 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003101 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003102 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 // See if the method type implied by the invoke instruction matches the access flags for the
3104 // target method.
3105 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3106 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3107 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3108 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003109 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3110 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003112 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003113 return res_method;
3114}
3115
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003116template <class T>
3117mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3118 MethodType method_type,
3119 bool is_range,
3120 mirror::ArtMethod* res_method) {
3121 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3122 // match the call to the signature. Also, we might be calling through an abstract method
3123 // definition (which doesn't have register count values).
3124 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3125 /* caught by static verifier */
3126 DCHECK(is_range || expected_args <= 5);
3127 if (expected_args > code_item_->outs_size_) {
3128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3129 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3130 return nullptr;
3131 }
3132
3133 uint32_t arg[5];
3134 if (!is_range) {
3135 inst->GetVarArgs(arg);
3136 }
3137 uint32_t sig_registers = 0;
3138
3139 /*
3140 * Check the "this" argument, which must be an instance of the class that declared the method.
3141 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3142 * rigorous check here (which is okay since we have to do it at runtime).
3143 */
3144 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003145 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003146 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3147 CHECK(have_pending_hard_failure_);
3148 return nullptr;
3149 }
3150 if (actual_arg_type.IsUninitializedReference()) {
3151 if (res_method) {
3152 if (!res_method->IsConstructor()) {
3153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3154 return nullptr;
3155 }
3156 } else {
3157 // Check whether the name of the called method is "<init>"
3158 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003159 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3161 return nullptr;
3162 }
3163 }
3164 }
3165 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003166 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003167 if (res_method != nullptr) {
3168 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003169 std::string temp;
3170 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003171 klass->CannotBeAssignedFromOtherTypes());
3172 } else {
3173 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3174 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003175 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003176 dex_file_->StringByTypeIdx(class_idx),
3177 false);
3178 }
3179 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3180 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3181 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3182 << "' not instance of '" << *res_method_class << "'";
3183 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3184 // the compiler.
3185 if (have_pending_hard_failure_) {
3186 return nullptr;
3187 }
3188 }
3189 }
3190 sig_registers = 1;
3191 }
3192
3193 for ( ; it->HasNext(); it->Next()) {
3194 if (sig_registers >= expected_args) {
3195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3196 " arguments, found " << sig_registers << " or more.";
3197 return nullptr;
3198 }
3199
3200 const char* param_descriptor = it->GetDescriptor();
3201
3202 if (param_descriptor == nullptr) {
3203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3204 "component";
3205 return nullptr;
3206 }
3207
Ian Rogersd8f69b02014-09-10 21:43:52 +00003208 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003209 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3210 arg[sig_registers];
3211 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003212 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003213 if (!src_type.IsIntegralTypes()) {
3214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3215 << " but expected " << reg_type;
3216 return res_method;
3217 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003219 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3220 // compiler.
3221 if (have_pending_hard_failure_) {
3222 return res_method;
3223 }
3224 }
3225 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3226 }
3227 if (expected_args != sig_registers) {
3228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3229 " arguments, found " << sig_registers;
3230 return nullptr;
3231 }
3232 return res_method;
3233}
3234
3235void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3236 MethodType method_type,
3237 bool is_range) {
3238 // As the method may not have been resolved, make this static check against what we expect.
3239 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3240 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3241 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3242 DexFileParameterIterator it(*dex_file_,
3243 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3244 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3245 nullptr);
3246}
3247
3248class MethodParamListDescriptorIterator {
3249 public:
3250 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3251 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3252 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3253 }
3254
3255 bool HasNext() {
3256 return pos_ < params_size_;
3257 }
3258
3259 void Next() {
3260 ++pos_;
3261 }
3262
3263 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3264 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3265 }
3266
3267 private:
3268 mirror::ArtMethod* res_method_;
3269 size_t pos_;
3270 const DexFile::TypeList* params_;
3271 const size_t params_size_;
3272};
3273
Brian Carlstromea46f952013-07-30 01:26:50 -07003274mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003275 MethodType method_type,
3276 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003277 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003278 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3279 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003280 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003281
Brian Carlstromea46f952013-07-30 01:26:50 -07003282 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003284 // Check what we can statically.
3285 if (!have_pending_hard_failure_) {
3286 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3287 }
3288 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003289 }
3290
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3292 // has a vtable entry for the target method.
3293 if (is_super) {
3294 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003295 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003296 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003297 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003298 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003299 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003300 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003301 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003302 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003303 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003304 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003306 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003307 << "." << res_method->GetName()
3308 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003309 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 }
3311 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003312
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003313 // Process the target method's signature. This signature may or may not
3314 MethodParamListDescriptorIterator it(res_method);
3315 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3316 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003317}
3318
Brian Carlstromea46f952013-07-30 01:26:50 -07003319mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003320 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003321 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3322 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003323 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003324 if (!actual_arg_type.HasClass()) {
3325 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003326 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003327 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003328 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003329 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003330 if (klass->IsInterface()) {
3331 // Derive Object.class from Class.class.getSuperclass().
3332 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3333 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003334 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003335 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003336 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003337 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003338 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003339 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003340 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3341 << PrettyDescriptor(klass);
3342 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Ian Rogers7b078e82014-09-10 14:44:24 -07003343 CHECK(!self_->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003344 return res_method;
3345}
3346
Brian Carlstromea46f952013-07-30 01:26:50 -07003347mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003348 bool is_range) {
3349 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003350 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003351 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003352 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003354 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003355 }
3356 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3357
3358 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3359 // match the call to the signature. Also, we might be calling through an abstract method
3360 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003361 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003362 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003363 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003364 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003365 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3366 /* caught by static verifier */
3367 DCHECK(is_range || expected_args <= 5);
3368 if (expected_args > code_item_->outs_size_) {
3369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3370 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003371 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003372 }
3373
3374 /*
3375 * Check the "this" argument, which must be an instance of the class that declared the method.
3376 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3377 * rigorous check here (which is okay since we have to do it at runtime).
3378 */
3379 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003381 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003382 }
3383 if (!actual_arg_type.IsZero()) {
3384 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003385 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003386 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003387 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388 klass->CannotBeAssignedFromOtherTypes());
3389 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003390 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3391 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003392 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003393 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003394 }
3395 }
3396 /*
3397 * Process the target method's signature. This signature may or may not
3398 * have been verified, so we can't assume it's properly formed.
3399 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003400 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003401 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003402 uint32_t arg[5];
3403 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003404 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 }
3406 size_t actual_args = 1;
3407 for (size_t param_index = 0; param_index < params_size; param_index++) {
3408 if (actual_args >= expected_args) {
3409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003410 << "'. Expected " << expected_args
3411 << " arguments, processing argument " << actual_args
3412 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003414 }
3415 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003416 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003417 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003418 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003419 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003420 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003421 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003422 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003423 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003425 return res_method;
3426 }
3427 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3428 }
3429 if (actual_args != expected_args) {
3430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3431 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003432 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003433 } else {
3434 return res_method;
3435 }
3436}
3437
Ian Rogers62342ec2013-06-11 10:26:37 -07003438void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003439 uint32_t type_idx;
3440 if (!is_filled) {
3441 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3442 type_idx = inst->VRegC_22c();
3443 } else if (!is_range) {
3444 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3445 type_idx = inst->VRegB_35c();
3446 } else {
3447 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3448 type_idx = inst->VRegB_3rc();
3449 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003450 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003451 if (res_type.IsConflict()) { // bad class
3452 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003453 } else {
3454 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3455 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003457 } else if (!is_filled) {
3458 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003459 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003460 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003461 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003462 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003463 } else {
3464 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3465 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003466 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003467 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3468 uint32_t arg[5];
3469 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003470 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003471 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003472 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003473 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003474 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3475 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003476 return;
3477 }
3478 }
3479 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003480 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003481 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003482 }
3483 }
3484}
3485
Sebastien Hertz5243e912013-05-21 10:55:07 +02003486void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003487 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003488 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003492 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003493 if (array_type.IsZero()) {
3494 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3495 // instruction type. TODO: have a proper notion of bottom here.
3496 if (!is_primitive || insn_type.IsCategory1Types()) {
3497 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003498 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003500 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003501 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3502 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003503 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003504 }
jeffhaofc3144e2012-02-01 17:21:15 -08003505 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003507 } else {
3508 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003509 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003510 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003512 << " source for aget-object";
3513 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003515 << " source for category 1 aget";
3516 } else if (is_primitive && !insn_type.Equals(component_type) &&
3517 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003518 (insn_type.IsLong() && component_type.IsDouble()))) {
3519 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3520 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003521 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003522 // Use knowledge of the field type which is stronger than the type inferred from the
3523 // instruction, which can't differentiate object types and ints from floats, longs from
3524 // doubles.
3525 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003527 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003528 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003529 component_type.HighHalf(&reg_types_));
3530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 }
3532 }
3533 }
3534}
3535
Ian Rogersd8f69b02014-09-10 21:43:52 +00003536void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003537 const uint32_t vregA) {
3538 // Primitive assignability rules are weaker than regular assignability rules.
3539 bool instruction_compatible;
3540 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003541 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003542 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003543 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003544 value_compatible = value_type.IsIntegralTypes();
3545 } else if (target_type.IsFloat()) {
3546 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3547 value_compatible = value_type.IsFloatTypes();
3548 } else if (target_type.IsLong()) {
3549 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003550 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3551 // as target_type depends on the resolved type of the field.
3552 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003553 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003554 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3555 } else {
3556 value_compatible = false;
3557 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003558 } else if (target_type.IsDouble()) {
3559 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003560 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3561 // as target_type depends on the resolved type of the field.
3562 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003563 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003564 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3565 } else {
3566 value_compatible = false;
3567 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003568 } else {
3569 instruction_compatible = false; // reference with primitive store
3570 value_compatible = false; // unused
3571 }
3572 if (!instruction_compatible) {
3573 // This is a global failure rather than a class change failure as the instructions and
3574 // the descriptors for the type should have been consistent within the same file at
3575 // compile time.
3576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3577 << "' but expected type '" << target_type << "'";
3578 return;
3579 }
3580 if (!value_compatible) {
3581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3582 << " of type " << value_type << " but expected " << target_type << " for put";
3583 return;
3584 }
3585}
3586
Sebastien Hertz5243e912013-05-21 10:55:07 +02003587void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003588 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003589 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003590 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003592 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003594 if (array_type.IsZero()) {
3595 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3596 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003597 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003599 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003600 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003601 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003602 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003603 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003605 if (!component_type.IsReferenceTypes()) {
3606 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3607 << " source for aput-object";
3608 } else {
3609 // The instruction agrees with the type of array, confirm the value to be stored does too
3610 // Note: we use the instruction type (rather than the component type) for aput-object as
3611 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003612 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003613 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 }
3615 }
3616 }
3617}
3618
Brian Carlstromea46f952013-07-30 01:26:50 -07003619mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003620 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3621 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003622 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003623 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003624 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3625 field_idx, dex_file_->GetFieldName(field_id),
3626 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003627 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003628 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003629 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003630 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003631 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003632 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003633 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3634 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003636 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003637 << dex_file_->GetFieldName(field_id) << ") in "
3638 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003639 DCHECK(self_->IsExceptionPending());
3640 self_->ClearException();
3641 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003642 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3643 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003645 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003646 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 } else if (!field->IsStatic()) {
3648 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003649 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003651 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003652}
3653
Ian Rogersd8f69b02014-09-10 21:43:52 +00003654mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003655 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3656 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003657 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003658 if (klass_type.IsConflict()) {
3659 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3660 field_idx, dex_file_->GetFieldName(field_id),
3661 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003662 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003663 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003664 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003665 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003666 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003667 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003668 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3669 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003670 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003671 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003672 << dex_file_->GetFieldName(field_id) << ") in "
3673 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003674 DCHECK(self_->IsExceptionPending());
3675 self_->ClearException();
3676 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003677 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3678 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003680 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 } else if (field->IsStatic()) {
3683 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3684 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003686 } else if (obj_type.IsZero()) {
3687 // Cannot infer and check type, however, access will cause null pointer exception
3688 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003689 } else if (!obj_type.IsReferenceTypes()) {
3690 // Trying to read a field from something that isn't a reference
3691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3692 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003694 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003695 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003696 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003697 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003698 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003699 if (obj_type.IsUninitializedTypes() &&
3700 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3701 !field_klass.Equals(GetDeclaringClass()))) {
3702 // Field accesses through uninitialized references are only allowable for constructors where
3703 // the field is declared in this class
3704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003705 << " of a not fully initialized object within the context"
3706 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003707 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003708 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3709 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3710 // of C1. For resolution to occur the declared class of the field must be compatible with
3711 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3712 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3713 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003715 } else {
3716 return field;
3717 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003718 }
3719}
3720
Ian Rogersd8f69b02014-09-10 21:43:52 +00003721void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003722 bool is_primitive, bool is_static) {
3723 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003724 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003725 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003726 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003727 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003728 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003729 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003730 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003731 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003732 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003733 mirror::Class* field_type_class;
3734 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003736 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3737 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3738 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003739 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003740 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003741 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003742 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3744 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003745 }
Ian Rogers0d604842012-04-16 14:50:24 -07003746 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003747 if (field_type == nullptr) {
3748 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3749 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003750 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003751 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003752 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003753 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003754 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003755 if (field_type->Equals(insn_type) ||
3756 (field_type->IsFloat() && insn_type.IsInteger()) ||
3757 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003758 // expected that read is of the correct primitive type or that int reads are reading
3759 // floats or long reads are reading doubles
3760 } else {
3761 // This is a global failure rather than a class change failure as the instructions and
3762 // the descriptors for the type should have been consistent within the same file at
3763 // compile time
3764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3765 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003766 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 return;
3768 }
3769 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003770 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003771 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3772 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003773 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003774 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003775 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003776 return;
3777 }
3778 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003779 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003780 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003781 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003782 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003783 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003784}
3785
Ian Rogersd8f69b02014-09-10 21:43:52 +00003786void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003787 bool is_primitive, bool is_static) {
3788 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003789 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003790 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003791 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003792 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003794 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003795 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003796 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003797 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003798 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3799 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3800 << " from other class " << GetDeclaringClass();
3801 return;
3802 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003803 mirror::Class* field_type_class;
3804 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003805 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003806 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3807 FieldHelper fh(h_field);
3808 field_type_class = fh.GetType(can_load_classes_);
3809 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003810 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003811 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003812 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003813 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003814 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3815 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003816 }
3817 }
3818 if (field_type == nullptr) {
3819 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3820 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003821 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003822 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003823 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003824 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003826 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003827 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003828 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003829 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3830 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003831 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003832 << "' in put-object";
3833 return;
3834 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 }
3837}
3838
Brian Carlstromea46f952013-07-30 01:26:50 -07003839mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003840 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003841 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3842 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3843 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3844 inst->Opcode() == Instruction::IPUT_QUICK ||
3845 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3846 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003848 if (!object_type.HasClass()) {
3849 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3850 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003851 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003852 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003853 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3854 field_offset);
3855 if (f == nullptr) {
3856 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3857 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3858 }
3859 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003860}
3861
Ian Rogersd8f69b02014-09-10 21:43:52 +00003862void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003863 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003864 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003865 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003866 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3868 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003869 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003870 mirror::Class* field_type_class;
3871 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003873 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3874 FieldHelper fh(h_field);
3875 field_type_class = fh.GetType(can_load_classes_);
3876 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003877 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003878 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003879 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003880 field_type_class->CannotBeAssignedFromOtherTypes());
3881 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003882 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3883 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003884 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003885 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003886 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003887 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003888 const uint32_t vregA = inst->VRegA_22c();
3889 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003890 if (field_type->Equals(insn_type) ||
3891 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3892 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003893 // expected that read is of the correct primitive type or that int reads are reading
3894 // floats or long reads are reading doubles
3895 } else {
3896 // This is a global failure rather than a class change failure as the instructions and
3897 // the descriptors for the type should have been consistent within the same file at
3898 // compile time
3899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003900 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003901 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003902 return;
3903 }
3904 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003905 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003906 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003907 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003908 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003909 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003910 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003911 return;
3912 }
3913 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003914 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003915 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003916 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003917 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 }
3919}
3920
Ian Rogersd8f69b02014-09-10 21:43:52 +00003921void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003922 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003923 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003924 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003925 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3927 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003928 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003929 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003930 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003931 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003933 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3934 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003935 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003936 return;
3937 }
3938 }
3939 const uint32_t vregA = inst->VRegA_22c();
3940 if (is_primitive) {
3941 // Primitive field assignability rules are weaker than regular assignability rules
3942 bool instruction_compatible;
3943 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003944 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003945 if (field_type.IsIntegralTypes()) {
3946 instruction_compatible = insn_type.IsIntegralTypes();
3947 value_compatible = value_type.IsIntegralTypes();
3948 } else if (field_type.IsFloat()) {
3949 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3950 value_compatible = value_type.IsFloatTypes();
3951 } else if (field_type.IsLong()) {
3952 instruction_compatible = insn_type.IsLong();
3953 value_compatible = value_type.IsLongTypes();
3954 } else if (field_type.IsDouble()) {
3955 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3956 value_compatible = value_type.IsDoubleTypes();
3957 } else {
3958 instruction_compatible = false; // reference field with primitive store
3959 value_compatible = false; // unused
3960 }
3961 if (!instruction_compatible) {
3962 // This is a global failure rather than a class change failure as the instructions and
3963 // the descriptors for the type should have been consistent within the same file at
3964 // compile time
3965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003966 << " to be of type '" << insn_type
3967 << "' but found type '" << field_type
3968 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003969 return;
3970 }
3971 if (!value_compatible) {
3972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3973 << " of type " << value_type
3974 << " but expected " << field_type
3975 << " for store to " << PrettyField(field) << " in put";
3976 return;
3977 }
3978 } else {
3979 if (!insn_type.IsAssignableFrom(field_type)) {
3980 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003981 << " to be compatible with type '" << insn_type
3982 << "' but found type '" << field_type
3983 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003984 return;
3985 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003987 }
3988}
3989
Ian Rogers776ac1f2012-04-13 23:36:36 -07003990bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 return false;
3994 }
3995 return true;
3996}
3997
Ian Rogersebbdd872014-07-07 23:53:08 -07003998bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3999 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004000 bool changed = true;
4001 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4002 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004003 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 * We haven't processed this instruction before, and we haven't touched the registers here, so
4005 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4006 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004007 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004008 if (!insn_flags_[next_insn].IsReturn()) {
4009 target_line->CopyFromLine(merge_line);
4010 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004011 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004013 return false;
4014 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004015 // For returns we only care about the operand to the return, all other registers are dead.
4016 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4017 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4018 Instruction::Code opcode = ret_inst->Opcode();
4019 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004020 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004021 } else {
4022 target_line->CopyFromLine(merge_line);
4023 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004024 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004025 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004026 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004027 }
4028 }
4029 }
jeffhaobdb76512011-09-07 11:43:16 -07004030 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004031 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004032 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004033 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004034 if (gDebugVerify) {
4035 copy->CopyFromLine(target_line);
4036 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004037 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004038 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004039 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004040 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004041 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004042 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004043 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004044 << copy->Dump(this) << " MERGE\n"
4045 << merge_line->Dump(this) << " ==\n"
4046 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004047 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004048 if (update_merge_line && changed) {
4049 merge_line->CopyFromLine(target_line);
4050 }
jeffhaobdb76512011-09-07 11:43:16 -07004051 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004052 if (changed) {
4053 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004054 }
4055 return true;
4056}
4057
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004058InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004059 return &insn_flags_[work_insn_idx_];
4060}
4061
Ian Rogersd8f69b02014-09-10 21:43:52 +00004062const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004063 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004064 if (mirror_method_.Get() != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004065 StackHandleScope<1> hs(self_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004066 mirror::Class* return_type_class =
4067 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004068 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004069 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4070 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004071 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004072 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004073 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4074 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004075 }
4076 }
4077 if (return_type_ == nullptr) {
4078 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4079 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4080 uint16_t return_type_idx = proto_id.return_type_idx_;
4081 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004082 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004083 }
4084 }
4085 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004086}
4087
Ian Rogersd8f69b02014-09-10 21:43:52 +00004088const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004089 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004090 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004091 const char* descriptor
4092 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004093 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004094 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004095 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4096 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004097 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004098 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004099 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004100 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004101 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004102}
4103
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004104std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4105 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004106 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004107 std::vector<int32_t> result;
4108 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004109 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004110 if (type.IsConstant()) {
4111 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004112 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4113 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004114 } else if (type.IsConstantLo()) {
4115 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4117 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004118 } else if (type.IsConstantHi()) {
4119 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004120 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4121 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004122 } else if (type.IsIntegralTypes()) {
4123 result.push_back(kIntVReg);
4124 result.push_back(0);
4125 } else if (type.IsFloat()) {
4126 result.push_back(kFloatVReg);
4127 result.push_back(0);
4128 } else if (type.IsLong()) {
4129 result.push_back(kLongLoVReg);
4130 result.push_back(0);
4131 result.push_back(kLongHiVReg);
4132 result.push_back(0);
4133 ++i;
4134 } else if (type.IsDouble()) {
4135 result.push_back(kDoubleLoVReg);
4136 result.push_back(0);
4137 result.push_back(kDoubleHiVReg);
4138 result.push_back(0);
4139 ++i;
4140 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4141 result.push_back(kUndefined);
4142 result.push_back(0);
4143 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004144 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004145 result.push_back(kReferenceVReg);
4146 result.push_back(0);
4147 }
4148 }
4149 return result;
4150}
4151
Ian Rogersd8f69b02014-09-10 21:43:52 +00004152const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004153 if (precise) {
4154 // Precise constant type.
4155 return reg_types_.FromCat1Const(value, true);
4156 } else {
4157 // Imprecise constant type.
4158 if (value < -32768) {
4159 return reg_types_.IntConstant();
4160 } else if (value < -128) {
4161 return reg_types_.ShortConstant();
4162 } else if (value < 0) {
4163 return reg_types_.ByteConstant();
4164 } else if (value == 0) {
4165 return reg_types_.Zero();
4166 } else if (value == 1) {
4167 return reg_types_.One();
4168 } else if (value < 128) {
4169 return reg_types_.PosByteConstant();
4170 } else if (value < 32768) {
4171 return reg_types_.PosShortConstant();
4172 } else if (value < 65536) {
4173 return reg_types_.CharConstant();
4174 } else {
4175 return reg_types_.IntConstant();
4176 }
4177 }
4178}
4179
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004180void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004181 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004182}
4183
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004184void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004185 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004186}
jeffhaod1224c72012-02-29 13:43:08 -08004187
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004188void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4189 RegTypeCache::VisitStaticRoots(callback, arg);
4190}
4191
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004192void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4193 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004194}
4195
Ian Rogersd81871c2011-10-03 13:57:23 -07004196} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004197} // namespace art