blob: 467390662fefbc3ad29ffc78ed658d95a6a56508 [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();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700314 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
315 // and querying any info is dangerous/can abort.
316 if (verifier->have_pending_hard_failure_) {
317 delete verifier;
318 return nullptr;
319 } else {
320 verifier->Dump(os);
321 return verifier;
322 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800323}
324
Ian Rogers7b078e82014-09-10 14:44:24 -0700325MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700326 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
327 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700328 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700329 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700330 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700332 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700333 : self_(self),
334 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800335 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800336 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700337 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700338 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700339 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800340 dex_file_(dex_file),
341 dex_cache_(dex_cache),
342 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700343 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800344 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700345 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700346 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700347 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700348 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700349 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800350 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800351 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700352 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200353 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700354 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200355 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700356 has_virtual_or_interface_invokes_(false),
357 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800358 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700359 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Mathieu Chartier590fee92013-09-13 13:46:47 -0700362MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800363 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 STLDeleteElements(&failure_messages_);
365}
366
Brian Carlstromea46f952013-07-30 01:26:50 -0700367void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700368 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700369 Thread* self = Thread::Current();
370 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
372 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700373 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700374 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700375 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
376 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700377 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700378 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700379 verifier.FindLocksAtDexPc();
380}
381
382void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700383 CHECK(monitor_enter_dex_pcs_ != nullptr);
384 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700385
386 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
387 // verification. In practice, the phase we want relies on data structures set up by all the
388 // earlier passes, so we just run the full method verification and bail out early when we've
389 // got what we wanted.
390 Verify();
391}
392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700394 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 Thread* self = Thread::Current();
396 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700397 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
398 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700399 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700400 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700401 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
402 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200403 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200404}
405
Brian Carlstromea46f952013-07-30 01:26:50 -0700406mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700407 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200408
409 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
410 // verification. In practice, the phase we want relies on data structures set up by all the
411 // earlier passes, so we just run the full method verification and bail out early when we've
412 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200413 bool success = Verify();
414 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700415 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200416 }
417 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700418 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700419 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200420 }
421 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
422 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423}
424
Brian Carlstromea46f952013-07-30 01:26:50 -0700425mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700427 Thread* self = Thread::Current();
428 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700429 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
430 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700431 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700432 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700433 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
434 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200435 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200436}
437
Brian Carlstromea46f952013-07-30 01:26:50 -0700438mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200440
441 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
442 // verification. In practice, the phase we want relies on data structures set up by all the
443 // earlier passes, so we just run the full method verification and bail out early when we've
444 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200445 bool success = Verify();
446 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700447 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200448 }
449 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700450 if (register_line == nullptr) {
451 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200452 }
453 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
454 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
455 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200456}
457
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700460 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700463 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700464 } else {
465 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700466 }
jeffhaobdb76512011-09-07 11:43:16 -0700467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
469 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
471 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700472 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700473 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800475 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700476 // Run through the instructions and see if the width checks out.
477 bool result = ComputeWidthsAndCountOps();
478 // Flag instructions guarded by a "try" block and check exception handlers.
479 result = result && ScanTryCatchBlocks();
480 // Perform static instruction verification.
481 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000483 result = result && VerifyCodeFlow();
484 // Compute information for compiler.
485 if (result && Runtime::Current()->IsCompiler()) {
486 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
487 }
488 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700489}
490
Ian Rogers776ac1f2012-04-13 23:36:36 -0700491std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 switch (error) {
493 case VERIFY_ERROR_NO_CLASS:
494 case VERIFY_ERROR_NO_FIELD:
495 case VERIFY_ERROR_NO_METHOD:
496 case VERIFY_ERROR_ACCESS_CLASS:
497 case VERIFY_ERROR_ACCESS_FIELD:
498 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700499 case VERIFY_ERROR_INSTANTIATION:
500 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800501 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700502 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
503 // class change and instantiation errors into soft verification errors so that we re-verify
504 // at runtime. We may fail to find or to agree on access because of not yet available class
505 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
506 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
507 // paths" that dynamically perform the verification and cause the behavior to be that akin
508 // to an interpreter.
509 error = VERIFY_ERROR_BAD_CLASS_SOFT;
510 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700511 // If we fail again at runtime, mark that this instruction would throw and force this
512 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700513 have_pending_runtime_throw_failure_ = true;
514 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700515 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700516 // Indication that verification should be retried at runtime.
517 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700518 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519 have_pending_hard_failure_ = true;
520 }
521 break;
jeffhaod5347e02012-03-22 17:25:05 -0700522 // Hard verification failures at compile time will still fail at runtime, so the class is
523 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700524 case VERIFY_ERROR_BAD_CLASS_HARD: {
525 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700526 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000527 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800528 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 have_pending_hard_failure_ = true;
530 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800531 }
532 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700533 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700534 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700535 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700536 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537 failure_messages_.push_back(failure_message);
538 return *failure_message;
539}
540
Ian Rogers576ca0c2014-06-06 15:58:22 -0700541std::ostream& MethodVerifier::LogVerifyInfo() {
542 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
543 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
544}
545
Ian Rogersad0b3a32012-04-16 14:50:24 -0700546void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
547 size_t failure_num = failure_messages_.size();
548 DCHECK_NE(failure_num, 0U);
549 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
550 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700551 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552 delete last_fail_message;
553}
554
555void MethodVerifier::AppendToLastFailMessage(std::string append) {
556 size_t failure_num = failure_messages_.size();
557 DCHECK_NE(failure_num, 0U);
558 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
559 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800560}
561
Ian Rogers776ac1f2012-04-13 23:36:36 -0700562bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 const uint16_t* insns = code_item_->insns_;
564 size_t insns_size = code_item_->insns_size_in_code_units_;
565 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700566 size_t new_instance_count = 0;
567 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700569
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700571 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700572 switch (opcode) {
573 case Instruction::APUT_OBJECT:
574 case Instruction::CHECK_CAST:
575 has_check_casts_ = true;
576 break;
577 case Instruction::INVOKE_VIRTUAL:
578 case Instruction::INVOKE_VIRTUAL_RANGE:
579 case Instruction::INVOKE_INTERFACE:
580 case Instruction::INVOKE_INTERFACE_RANGE:
581 has_virtual_or_interface_invokes_ = true;
582 break;
583 case Instruction::MONITOR_ENTER:
584 monitor_enter_count++;
585 break;
586 case Instruction::NEW_INSTANCE:
587 new_instance_count++;
588 break;
589 default:
590 break;
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700593 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700595 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700596 }
597
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
600 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700601 return false;
602 }
603
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 new_instance_count_ = new_instance_count;
605 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700606 return true;
607}
608
Ian Rogers776ac1f2012-04-13 23:36:36 -0700609bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700611 if (tries_size == 0) {
612 return true;
613 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700615 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700616
617 for (uint32_t idx = 0; idx < tries_size; idx++) {
618 const DexFile::TryItem* try_item = &tries[idx];
619 uint32_t start = try_item->start_addr_;
620 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700621 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
623 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700624 return false;
625 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700627 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
628 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700629 return false;
630 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 uint32_t dex_pc = start;
632 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
633 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700635 size_t insn_size = inst->SizeInCodeUnits();
636 dex_pc += insn_size;
637 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700638 }
639 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800640 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700641 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700642 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700643 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700644 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700645 CatchHandlerIterator iterator(handlers_ptr);
646 for (; iterator.HasNext(); iterator.Next()) {
647 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
650 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700651 return false;
652 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100653 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
654 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
655 << "exception handler begins with move-result* (" << dex_pc << ")";
656 return false;
657 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700659 // Ensure exception types are resolved so that they don't need resolution to be delivered,
660 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700661 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800662 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
663 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700664 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700665 if (exception_type == nullptr) {
666 DCHECK(self_->IsExceptionPending());
667 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700668 }
669 }
jeffhaobdb76512011-09-07 11:43:16 -0700670 }
Ian Rogers0571d352011-11-03 19:51:38 -0700671 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700672 }
jeffhaobdb76512011-09-07 11:43:16 -0700673 return true;
674}
675
Ian Rogers776ac1f2012-04-13 23:36:36 -0700676bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700678
Ian Rogers0c7abda2012-09-19 13:33:42 -0700679 /* 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 -0700680 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700681 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700682
683 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700684 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700686 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 return false;
688 }
689 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700690 // All invoke points are marked as "Throw" points already.
691 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000692 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700693 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000694 } else if (inst->IsReturn()) {
695 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 }
697 dex_pc += inst->SizeInCodeUnits();
698 inst = inst->Next();
699 }
700 return true;
701}
702
Ian Rogers776ac1f2012-04-13 23:36:36 -0700703bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 bool result = true;
705 switch (inst->GetVerifyTypeArgumentA()) {
706 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700707 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 break;
709 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700710 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 break;
712 }
713 switch (inst->GetVerifyTypeArgumentB()) {
714 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700715 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 break;
717 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700718 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 break;
720 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700721 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 break;
723 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700724 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 break;
726 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700727 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 break;
729 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700730 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 break;
732 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700733 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 break;
735 }
736 switch (inst->GetVerifyTypeArgumentC()) {
737 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700738 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 break;
740 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700741 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 break;
743 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700744 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 break;
746 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700747 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 break;
749 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700750 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 break;
752 }
753 switch (inst->GetVerifyExtraFlags()) {
754 case Instruction::kVerifyArrayData:
755 result = result && CheckArrayData(code_offset);
756 break;
757 case Instruction::kVerifyBranchTarget:
758 result = result && CheckBranchTarget(code_offset);
759 break;
760 case Instruction::kVerifySwitchTargets:
761 result = result && CheckSwitchTargets(code_offset);
762 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700763 case Instruction::kVerifyVarArgNonZero:
764 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700765 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700766 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
768 "non-range invoke";
769 return false;
770 }
Ian Rogers29a26482014-05-02 15:27:29 -0700771 uint32_t args[Instruction::kMaxVarArgRegs];
772 inst->GetVarArgs(args);
773 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700775 }
Andreas Gampec3314312014-06-19 18:13:29 -0700776 case Instruction::kVerifyVarArgRangeNonZero:
777 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700779 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
780 inst->VRegA() <= 0) {
781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
782 "range invoke";
783 return false;
784 }
Ian Rogers29a26482014-05-02 15:27:29 -0700785 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 break;
787 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 result = false;
790 break;
791 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700792 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
794 result = false;
795 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 return result;
797}
798
Ian Rogers7b078e82014-09-10 14:44:24 -0700799inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
802 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 return false;
804 }
805 return true;
806}
807
Ian Rogers7b078e82014-09-10 14:44:24 -0700808inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
811 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 }
814 return true;
815}
816
Ian Rogers7b078e82014-09-10 14:44:24 -0700817inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
820 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 return false;
822 }
823 return true;
824}
825
Ian Rogers7b078e82014-09-10 14:44:24 -0700826inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
829 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
832 return true;
833}
834
Ian Rogers7b078e82014-09-10 14:44:24 -0700835inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
838 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 return false;
840 }
841 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700842 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 return false;
846 }
847 return true;
848}
849
Ian Rogers7b078e82014-09-10 14:44:24 -0700850inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700852 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
853 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 return false;
855 }
856 return true;
857}
858
Ian Rogers7b078e82014-09-10 14:44:24 -0700859inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
862 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 return false;
864 }
865 return true;
866}
867
Ian Rogers776ac1f2012-04-13 23:36:36 -0700868bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
871 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 return false;
873 }
874 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700875 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 const char* cp = descriptor;
877 while (*cp++ == '[') {
878 bracket_count++;
879 }
880 if (bracket_count == 0) {
881 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
883 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 } else if (bracket_count > 255) {
886 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
888 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 return false;
890 }
891 return true;
892}
893
Ian Rogers776ac1f2012-04-13 23:36:36 -0700894bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
896 const uint16_t* insns = code_item_->insns_ + cur_offset;
897 const uint16_t* array_data;
898 int32_t array_data_offset;
899
900 DCHECK_LT(cur_offset, insn_count);
901 /* make sure the start of the array data table is in range */
902 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
903 if ((int32_t) cur_offset + array_data_offset < 0 ||
904 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700906 << ", data offset " << array_data_offset
907 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 return false;
909 }
910 /* offset to array data table is a relative branch-style offset */
911 array_data = insns + array_data_offset;
912 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800913 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
915 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700919 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700920 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
921 /* make sure the end of the switch is in range */
922 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
924 << ", data offset " << array_data_offset << ", end "
925 << cur_offset + array_data_offset + table_size
926 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 return false;
928 }
929 return true;
930}
931
Ian Rogers776ac1f2012-04-13 23:36:36 -0700932bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 int32_t offset;
934 bool isConditional, selfOkay;
935 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
936 return false;
937 }
938 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
940 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 return false;
942 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700943 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
944 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
947 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return false;
949 }
950 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
951 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700952 if (abs_offset < 0 ||
953 (uint32_t) abs_offset >= insn_count ||
954 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700956 << reinterpret_cast<void*>(abs_offset) << ") at "
957 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 return false;
959 }
960 insn_flags_[abs_offset].SetBranchTarget();
961 return true;
962}
963
Ian Rogers776ac1f2012-04-13 23:36:36 -0700964bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 bool* selfOkay) {
966 const uint16_t* insns = code_item_->insns_ + cur_offset;
967 *pConditional = false;
968 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700969 switch (*insns & 0xff) {
970 case Instruction::GOTO:
971 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 break;
973 case Instruction::GOTO_32:
974 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700975 *selfOkay = true;
976 break;
977 case Instruction::GOTO_16:
978 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 break;
980 case Instruction::IF_EQ:
981 case Instruction::IF_NE:
982 case Instruction::IF_LT:
983 case Instruction::IF_GE:
984 case Instruction::IF_GT:
985 case Instruction::IF_LE:
986 case Instruction::IF_EQZ:
987 case Instruction::IF_NEZ:
988 case Instruction::IF_LTZ:
989 case Instruction::IF_GEZ:
990 case Instruction::IF_GTZ:
991 case Instruction::IF_LEZ:
992 *pOffset = (int16_t) insns[1];
993 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700994 break;
995 default:
996 return false;
997 break;
998 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700999 return true;
1000}
1001
Ian Rogers776ac1f2012-04-13 23:36:36 -07001002bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001004 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001006 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1008 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001010 << ", switch offset " << switch_offset
1011 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001012 return false;
1013 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001014 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001015 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001016 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001017 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1019 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001020 return false;
1021 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 uint32_t switch_count = switch_insns[1];
1023 int32_t keys_offset, targets_offset;
1024 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001025 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1026 /* 0=sig, 1=count, 2/3=firstKey */
1027 targets_offset = 4;
1028 keys_offset = -1;
1029 expected_signature = Instruction::kPackedSwitchSignature;
1030 } else {
1031 /* 0=sig, 1=count, 2..count*2 = keys */
1032 keys_offset = 2;
1033 targets_offset = 2 + 2 * switch_count;
1034 expected_signature = Instruction::kSparseSwitchSignature;
1035 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001037 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001038 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1039 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1040 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001041 return false;
1042 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001043 /* make sure the end of the switch is in range */
1044 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1046 << ", switch offset " << switch_offset
1047 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001048 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001049 return false;
1050 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001051 /* for a sparse switch, verify the keys are in ascending order */
1052 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1054 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001055 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1056 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1057 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1059 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001060 return false;
1061 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001062 last_key = key;
1063 }
1064 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001065 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 for (uint32_t targ = 0; targ < switch_count; targ++) {
1067 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1068 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1069 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001070 if (abs_offset < 0 ||
1071 abs_offset >= (int32_t) insn_count ||
1072 !insn_flags_[abs_offset].IsOpcode()) {
1073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1074 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1075 << reinterpret_cast<void*>(cur_offset)
1076 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001077 return false;
1078 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 insn_flags_[abs_offset].SetBranchTarget();
1080 }
1081 return true;
1082}
1083
Ian Rogers776ac1f2012-04-13 23:36:36 -07001084bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001085 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 return false;
1088 }
1089 uint16_t registers_size = code_item_->registers_size_;
1090 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001091 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1093 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 return false;
1095 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001096 }
1097
1098 return true;
1099}
1100
Ian Rogers776ac1f2012-04-13 23:36:36 -07001101bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 uint16_t registers_size = code_item_->registers_size_;
1103 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1104 // integer overflow when adding them here.
1105 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1107 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001108 return false;
1109 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001110 return true;
1111}
1112
Ian Rogers776ac1f2012-04-13 23:36:36 -07001113bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 uint16_t registers_size = code_item_->registers_size_;
1115 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001116
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001118 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1119 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 }
1121 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001122 reg_table_.Init(kTrackCompilerInterestPoints,
1123 insn_flags_.get(),
1124 insns_size,
1125 registers_size,
1126 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001127
jeffhaobdb76512011-09-07 11:43:16 -07001128
Ian Rogersd0fbd852013-09-24 18:17:04 -07001129 work_line_.reset(RegisterLine::Create(registers_size, this));
1130 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001131
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 /* Initialize register types of method arguments. */
1133 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001134 DCHECK_NE(failures_.size(), 0U);
1135 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001136 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001137 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return false;
1139 }
1140 /* Perform code flow verification. */
1141 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001142 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001144 }
jeffhaobdb76512011-09-07 11:43:16 -07001145 return true;
1146}
1147
Ian Rogersad0b3a32012-04-16 14:50:24 -07001148std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1149 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001150 for (size_t i = 0; i < failures_.size(); ++i) {
1151 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001152 }
1153 return os;
1154}
1155
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001156extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001158 v->Dump(std::cerr);
1159}
1160
Ian Rogers776ac1f2012-04-13 23:36:36 -07001161void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001162 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001163 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return;
jeffhaobdb76512011-09-07 11:43:16 -07001165 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001166 {
1167 os << "Register Types:\n";
1168 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1169 std::ostream indent_os(&indent_filter);
1170 reg_types_.Dump(indent_os);
1171 }
Ian Rogersb4903572012-10-11 11:52:56 -07001172 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001173 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1174 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 const Instruction* inst = Instruction::At(code_item_->insns_);
1176 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001177 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001179 if (reg_line != nullptr) {
1180 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001181 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001182 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001183 const bool kDumpHexOfInstruction = false;
1184 if (kDumpHexOfInstruction) {
1185 indent_os << inst->DumpHex(5) << " ";
1186 }
1187 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001188 inst = inst->Next();
1189 }
jeffhaobdb76512011-09-07 11:43:16 -07001190}
1191
Ian Rogersd81871c2011-10-03 13:57:23 -07001192static bool IsPrimitiveDescriptor(char descriptor) {
1193 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001194 case 'I':
1195 case 'C':
1196 case 'S':
1197 case 'B':
1198 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001199 case 'F':
1200 case 'D':
1201 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001203 default:
1204 return false;
1205 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001206}
1207
Ian Rogers776ac1f2012-04-13 23:36:36 -07001208bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 RegisterLine* reg_line = reg_table_.GetLine(0);
1210 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1211 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001212
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001214 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001216 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1218 // argument as uninitialized. This restricts field access until the superclass constructor is
1219 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001220 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001221 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001222 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 reg_types_.UninitializedThisArgument(declaring_class));
1224 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001225 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001226 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001228 }
1229
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001230 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001231 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001232 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001233
1234 for (; iterator.HasNext(); iterator.Next()) {
1235 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001236 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 LOG(FATAL) << "Null descriptor";
1238 }
1239 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1241 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
1244 switch (descriptor[0]) {
1245 case 'L':
1246 case '[':
1247 // We assume that reference arguments are initialized. The only way it could be otherwise
1248 // (assuming the caller was verified) is if the current method is <init>, but in that case
1249 // it's effectively considered initialized the instant we reach here (in the sense that we
1250 // can return without doing anything or call virtual methods).
1251 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001252 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001253 if (!reg_type.IsNonZeroReferenceTypes()) {
1254 DCHECK(HasFailures());
1255 return false;
1256 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001257 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 }
1259 break;
1260 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001261 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 break;
1263 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001264 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 break;
1266 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001267 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 break;
1269 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001270 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 break;
1272 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001273 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 break;
1275 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001276 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 break;
1278 case 'J':
1279 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001280 if (cur_arg + 1 >= expected_args) {
1281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1282 << " args, found more (" << descriptor << ")";
1283 return false;
1284 }
1285
Ian Rogers7b078e82014-09-10 14:44:24 -07001286 const RegType* lo_half;
1287 const RegType* hi_half;
1288 if (descriptor[0] == 'J') {
1289 lo_half = &reg_types_.LongLo();
1290 hi_half = &reg_types_.LongHi();
1291 } else {
1292 lo_half = &reg_types_.DoubleLo();
1293 hi_half = &reg_types_.DoubleHi();
1294 }
1295 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 cur_arg++;
1297 break;
1298 }
1299 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1301 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 return false;
1303 }
1304 cur_arg++;
1305 }
1306 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1308 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 return false;
1310 }
1311 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1312 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1313 // format. Only major difference from the method argument format is that 'V' is supported.
1314 bool result;
1315 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1316 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001317 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 size_t i = 0;
1319 do {
1320 i++;
1321 } while (descriptor[i] == '['); // process leading [
1322 if (descriptor[i] == 'L') { // object array
1323 do {
1324 i++; // find closing ;
1325 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1326 result = descriptor[i] == ';';
1327 } else { // primitive array
1328 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1329 }
1330 } else if (descriptor[0] == 'L') {
1331 // could be more thorough here, but shouldn't be required
1332 size_t i = 0;
1333 do {
1334 i++;
1335 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1336 result = descriptor[i] == ';';
1337 } else {
1338 result = false;
1339 }
1340 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1342 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 }
1344 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001345}
1346
Ian Rogers776ac1f2012-04-13 23:36:36 -07001347bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 const uint16_t* insns = code_item_->insns_;
1349 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001350
jeffhaobdb76512011-09-07 11:43:16 -07001351 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 insn_flags_[0].SetChanged();
1353 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001354
jeffhaobdb76512011-09-07 11:43:16 -07001355 /* Continue until no instructions are marked "changed". */
1356 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001357 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1359 uint32_t insn_idx = start_guess;
1360 for (; insn_idx < insns_size; insn_idx++) {
1361 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001362 break;
1363 }
jeffhaobdb76512011-09-07 11:43:16 -07001364 if (insn_idx == insns_size) {
1365 if (start_guess != 0) {
1366 /* try again, starting from the top */
1367 start_guess = 0;
1368 continue;
1369 } else {
1370 /* all flags are clear */
1371 break;
1372 }
1373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 // We carry the working set of registers from instruction to instruction. If this address can
1375 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1376 // "changed" flags, we need to load the set of registers from the table.
1377 // Because we always prefer to continue on to the next instruction, we should never have a
1378 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1379 // target.
1380 work_insn_idx_ = insn_idx;
1381 if (insn_flags_[insn_idx].IsBranchTarget()) {
1382 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001383 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001384 /*
1385 * Sanity check: retrieve the stored register line (assuming
1386 * a full table) and make sure it actually matches.
1387 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001389 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 if (work_line_->CompareLine(register_line) != 0) {
1391 Dump(std::cout);
1392 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001393 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001394 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001395 << " work_line=" << work_line_->Dump(this) << "\n"
1396 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 }
jeffhaobdb76512011-09-07 11:43:16 -07001398 }
jeffhaobdb76512011-09-07 11:43:16 -07001399 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001401 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001402 prepend += " failed to verify: ";
1403 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001404 return false;
1405 }
jeffhaobdb76512011-09-07 11:43:16 -07001406 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 insn_flags_[insn_idx].SetVisited();
1408 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001409 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001410
Ian Rogers1c849e52012-06-28 14:00:33 -07001411 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001412 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001413 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001414 * (besides the wasted space), but it indicates a flaw somewhere
1415 * down the line, possibly in the verifier.
1416 *
1417 * If we've substituted "always throw" instructions into the stream,
1418 * we are almost certainly going to have some dead code.
1419 */
1420 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001422 for (; insn_idx < insns_size;
1423 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001424 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001425 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001426 * may or may not be preceded by a padding NOP (for alignment).
1427 */
1428 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1429 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1430 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001431 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001432 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1433 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1434 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001436 }
1437
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001439 if (dead_start < 0)
1440 dead_start = insn_idx;
1441 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001442 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1443 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001444 dead_start = -1;
1445 }
1446 }
1447 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001448 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1449 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001450 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001451 // To dump the state of the verify after a method, do something like:
1452 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1453 // "boolean java.lang.String.equals(java.lang.Object)") {
1454 // LOG(INFO) << info_messages_.str();
1455 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001456 }
jeffhaobdb76512011-09-07 11:43:16 -07001457 return true;
1458}
1459
Ian Rogers776ac1f2012-04-13 23:36:36 -07001460bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001461 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1462 // We want the state _before_ the instruction, for the case where the dex pc we're
1463 // interested in is itself a monitor-enter instruction (which is a likely place
1464 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001465 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001466 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001467 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1468 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1469 }
1470 }
1471
jeffhaobdb76512011-09-07 11:43:16 -07001472 /*
1473 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001474 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001475 * control to another statement:
1476 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001477 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001478 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001479 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001480 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001481 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001482 * throw an exception that is handled by an encompassing "try"
1483 * block.
1484 *
1485 * We can also return, in which case there is no successor instruction
1486 * from this point.
1487 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001488 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001489 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1491 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001492 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001493
jeffhaobdb76512011-09-07 11:43:16 -07001494 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001495 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001496 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001498 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001499 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001500 }
jeffhaobdb76512011-09-07 11:43:16 -07001501
1502 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001504 * can throw an exception, we will copy/merge this into the "catch"
1505 * address rather than work_line, because we don't want the result
1506 * from the "successful" code path (e.g. a check-cast that "improves"
1507 * a type) to be visible to the exception handler.
1508 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001509 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001510 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001511 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
1514
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001515
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001516 // We need to ensure the work line is consistent while performing validation. When we spot a
1517 // peephole pattern we compute a new line for either the fallthrough instruction or the
1518 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001519 std::unique_ptr<RegisterLine> branch_line;
1520 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001521
Sebastien Hertz5243e912013-05-21 10:55:07 +02001522 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001523 case Instruction::NOP:
1524 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001525 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001526 * a signature that looks like a NOP; if we see one of these in
1527 * the course of executing code then we have a problem.
1528 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001529 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001531 }
1532 break;
1533
1534 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001535 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001536 break;
jeffhaobdb76512011-09-07 11:43:16 -07001537 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001538 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 break;
jeffhaobdb76512011-09-07 11:43:16 -07001540 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001541 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001542 break;
1543 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001544 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001545 break;
jeffhaobdb76512011-09-07 11:43:16 -07001546 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001547 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 break;
jeffhaobdb76512011-09-07 11:43:16 -07001549 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001550 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001551 break;
1552 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001553 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 break;
jeffhaobdb76512011-09-07 11:43:16 -07001555 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001556 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 break;
jeffhaobdb76512011-09-07 11:43:16 -07001558 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001559 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001560 break;
1561
1562 /*
1563 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001564 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001565 * might want to hold the result in an actual CPU register, so the
1566 * Dalvik spec requires that these only appear immediately after an
1567 * invoke or filled-new-array.
1568 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001569 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001570 * redundant with the reset done below, but it can make the debug info
1571 * easier to read in some cases.)
1572 */
1573 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001574 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001575 break;
1576 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001578 break;
1579 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001580 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
1582
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001584 /*
jeffhao60f83e32012-02-13 17:16:30 -08001585 * This statement can only appear as the first instruction in an exception handler. We verify
1586 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001587 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001588 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001589 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001590 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 }
jeffhaobdb76512011-09-07 11:43:16 -07001592 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001593 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001594 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 }
jeffhaobdb76512011-09-07 11:43:16 -07001597 }
1598 break;
1599 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001600 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001601 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001602 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1605 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 } else {
1607 // Compilers may generate synthetic functions that write byte values into boolean fields.
1608 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001610 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001611 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1612 ((return_type.IsBoolean() || return_type.IsByte() ||
1613 return_type.IsShort() || return_type.IsChar()) &&
1614 src_type.IsInteger()));
1615 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001616 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001617 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001618 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 }
jeffhaobdb76512011-09-07 11:43:16 -07001621 }
1622 }
1623 break;
1624 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001625 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001626 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001627 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 } else {
1631 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001633 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001634 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 }
jeffhaobdb76512011-09-07 11:43:16 -07001637 }
1638 }
1639 break;
1640 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001642 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 } else {
1646 /* return_type is the *expected* return type, not register value */
1647 DCHECK(!return_type.IsZero());
1648 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001650 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001651 // Disallow returning uninitialized values and verify that the reference in vAA is an
1652 // instance of the "return_type"
1653 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001654 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1655 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001656 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001657 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1658 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1659 << "' or '" << reg_type << "'";
1660 } else {
1661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1662 << "', but expected from declaration '" << return_type << "'";
1663 }
jeffhaobdb76512011-09-07 11:43:16 -07001664 }
1665 }
1666 }
1667 break;
1668
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001669 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 case Instruction::CONST_4: {
1671 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001672 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001673 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001674 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 }
1676 case Instruction::CONST_16: {
1677 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001678 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001679 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001680 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001682 case Instruction::CONST: {
1683 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001684 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001685 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001687 }
1688 case Instruction::CONST_HIGH16: {
1689 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001690 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001691 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001695 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001697 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1698 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001700 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 }
1702 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001704 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1705 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001706 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001707 break;
1708 }
1709 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001711 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1712 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001713 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 break;
1715 }
1716 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001718 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1719 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 break;
1722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001724 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 break;
jeffhaobdb76512011-09-07 11:43:16 -07001726 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001730 // Get type from instruction if unresolved then we need an access check
1731 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001732 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001733 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001734 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001735 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001736 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001737 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001740 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001741 break;
1742 case Instruction::MONITOR_EXIT:
1743 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001744 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001745 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001746 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001747 * to the need to handle asynchronous exceptions, a now-deprecated
1748 * feature that Dalvik doesn't support.)
1749 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001750 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001751 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001752 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001753 * structured locking checks are working, the former would have
1754 * failed on the -enter instruction, and the latter is impossible.
1755 *
1756 * This is fortunate, because issue 3221411 prevents us from
1757 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001758 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001759 * some catch blocks (which will show up as "dead" code when
1760 * we skip them here); if we can't, then the code path could be
1761 * "live" so we still need to check it.
1762 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001763 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001764 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001765 break;
1766
Ian Rogers28ad40d2011-10-27 15:19:26 -07001767 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001769 /*
1770 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1771 * could be a "upcast" -- not expected, so we don't try to address it.)
1772 *
1773 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001774 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001775 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1777 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001778 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001779 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001780 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001781 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001782 if (klass != nullptr && klass->IsPrimitive()) {
1783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1784 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1785 << GetDeclaringClass();
1786 break;
1787 }
1788
Ian Rogersad0b3a32012-04-16 14:50:24 -07001789 DCHECK_NE(failures_.size(), 0U);
1790 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001791 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001792 }
1793 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001794 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001795 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001797 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001798 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 if (is_checkcast) {
1800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1801 } else {
1802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1803 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001804 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 if (is_checkcast) {
1806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1807 } else {
1808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1809 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001810 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001811 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001812 work_line_->SetRegisterType(this, inst->VRegA_21c(), 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_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001817 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001818 }
1819 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001821 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001822 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001825 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001827 } else {
1828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 }
1830 break;
1831 }
1832 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001833 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001834 if (res_type.IsConflict()) {
1835 DCHECK_NE(failures_.size(), 0U);
1836 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001837 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001838 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1839 // can't create an instance of an interface or abstract class */
1840 if (!res_type.IsInstantiableTypes()) {
1841 Fail(VERIFY_ERROR_INSTANTIATION)
1842 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001843 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001844 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001845 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001846 // Any registers holding previous allocations from this address that have not yet been
1847 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001849 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001850 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001851 break;
1852 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001853 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001854 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
1856 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001858 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001859 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001860 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001861 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001862 just_set_result = true; // Filled new array range sets result register
1863 break;
jeffhaobdb76512011-09-07 11:43:16 -07001864 case Instruction::CMPL_FLOAT:
1865 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001866 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001867 break;
1868 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001870 break;
1871 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001872 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874 case Instruction::CMPL_DOUBLE:
1875 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 reg_types_.DoubleHi())) {
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_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001881 reg_types_.DoubleHi())) {
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;
1886 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001888 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001889 break;
1890 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001891 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001892 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001893 break;
1894 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001895 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001896 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001898 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001899 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001900 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1901 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001902 }
1903 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 }
jeffhaobdb76512011-09-07 11:43:16 -07001905 case Instruction::GOTO:
1906 case Instruction::GOTO_16:
1907 case Instruction::GOTO_32:
1908 /* no effect on or use of registers */
1909 break;
1910
1911 case Instruction::PACKED_SWITCH:
1912 case Instruction::SPARSE_SWITCH:
1913 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001915 break;
1916
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 case Instruction::FILL_ARRAY_DATA: {
1918 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001920 /* array_type can be null if the reg type is Zero */
1921 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001922 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1924 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001925 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001926 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001927 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001928 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1930 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 } else {
jeffhao457cc512012-02-02 16:55:13 -08001932 // Now verify if the element width in the table matches the element width declared in
1933 // the array
1934 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1935 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001937 } else {
1938 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1939 // Since we don't compress the data in Dex, expect to see equal width of data stored
1940 // in the table and expected from the array class.
1941 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1943 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001944 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
1946 }
jeffhaobdb76512011-09-07 11:43:16 -07001947 }
1948 }
1949 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 }
jeffhaobdb76512011-09-07 11:43:16 -07001951 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1954 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 bool mismatch = false;
1956 if (reg_type1.IsZero()) { // zero then integral or reference expected
1957 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1958 } else if (reg_type1.IsReferenceTypes()) { // both references?
1959 mismatch = !reg_type2.IsReferenceTypes();
1960 } else { // both integral?
1961 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1962 }
1963 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1965 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001966 }
1967 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 case Instruction::IF_LT:
1970 case Instruction::IF_GE:
1971 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001973 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1974 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1977 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001978 }
1979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1986 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001988
1989 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001990 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001991 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001992 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001993 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001994 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001995 }
Ian Rogers9b360392013-06-06 14:45:07 -07001996 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001997 } else {
1998 break;
1999 }
2000
Ian Rogers9b360392013-06-06 14:45:07 -07002001 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002002
2003 /* Check for peep-hole pattern of:
2004 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002005 * instance-of vX, vY, T;
2006 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002007 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002008 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002009 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002010 * and sharpen the type of vY to be type T.
2011 * Note, this pattern can't be if:
2012 * - if there are other branches to this branch,
2013 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002014 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002015 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002016 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2017 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2018 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002019 // Check the type of the instance-of is different than that of registers type, as if they
2020 // are the same there is no work to be done here. Check that the conversion is not to or
2021 // from an unresolved type as type information is imprecise. If the instance-of is to an
2022 // interface then ignore the type information as interfaces can only be treated as Objects
2023 // and we don't want to disallow field and other operations on the object. If the value
2024 // being instance-of checked against is known null (zero) then allow the optimization as
2025 // we didn't have type information. If the merge of the instance-of type with the original
2026 // type is assignable to the original then allow optimization. This check is performed to
2027 // ensure that subsequent merges don't lose type information - such as becoming an
2028 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002029 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002030 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002031
Ian Rogersebbdd872014-07-07 23:53:08 -07002032 if (!orig_type.Equals(cast_type) &&
2033 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002034 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002035 !cast_type.GetClass()->IsInterface() &&
2036 (orig_type.IsZero() ||
2037 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002038 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002039 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002040 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002041 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002042 branch_line.reset(update_line);
2043 }
2044 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002046 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2047 // See if instance-of was preceded by a move-object operation, common due to the small
2048 // register encoding space of instance-of, and propagate type information to the source
2049 // of the move-object.
2050 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002051 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002052 move_idx--;
2053 }
2054 CHECK(insn_flags_[move_idx].IsOpcode());
2055 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2056 switch (move_inst->Opcode()) {
2057 case Instruction::MOVE_OBJECT:
2058 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002060 }
2061 break;
2062 case Instruction::MOVE_OBJECT_FROM16:
2063 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002065 }
2066 break;
2067 case Instruction::MOVE_OBJECT_16:
2068 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002070 }
2071 break;
2072 default:
2073 break;
2074 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002075 }
2076 }
2077 }
2078
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::IF_LTZ:
2082 case Instruction::IF_GEZ:
2083 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2088 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 }
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 }
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
jeffhaobdb76512011-09-07 11:43:16 -07002101 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 break;
2110 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 break;
2117 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 break;
2120 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 break;
2123 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002130 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
2135
jeffhaobdb76512011-09-07 11:43:16 -07002136 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 break;
jeffhaobdb76512011-09-07 11:43:16 -07002139 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 break;
jeffhaobdb76512011-09-07 11:43:16 -07002145 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 break;
2148 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
2151 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 break;
jeffhaobdb76512011-09-07 11:43:16 -07002157
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 break;
2161 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 break;
2164 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 break;
2167 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002175 break;
jeffhaobdb76512011-09-07 11:43:16 -07002176 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179
jeffhaobdb76512011-09-07 11:43:16 -07002180 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 break;
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 break;
jeffhaobdb76512011-09-07 11:43:16 -07002186 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002188 break;
jeffhaobdb76512011-09-07 11:43:16 -07002189 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 break;
2192 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002194 break;
2195 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002196 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002197 break;
2198 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 break;
2201
2202 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002204 break;
2205 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 break;
2208 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002210 break;
2211 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002212 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
2214 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002215 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002216 break;
2217 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002219 break;
2220 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
2223
2224 case Instruction::INVOKE_VIRTUAL:
2225 case Instruction::INVOKE_VIRTUAL_RANGE:
2226 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2229 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002230 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2231 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002232 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2233 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002234 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002235 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002236 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002237 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2238 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002239 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002240 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002241 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2242 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002243 return_type_class->CannotBeAssignedFromOtherTypes());
2244 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002245 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2246 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002247 }
2248 }
2249 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002251 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2252 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002253 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002254 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002255 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002256 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002258 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002259 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002261 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 }
jeffhaobdb76512011-09-07 11:43:16 -07002264 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002267 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002268 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002269 const char* return_type_descriptor;
2270 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002271 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002272 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002274 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002275 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002276 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2277 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2278 } else {
2279 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002280 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002281 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002282 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2283 MethodHelper mh(h_called_method);
2284 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2285 if (return_type_class != nullptr) {
2286 return_type = &reg_types_.FromClass(return_type_descriptor,
2287 return_type_class,
2288 return_type_class->CannotBeAssignedFromOtherTypes());
2289 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002290 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2291 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002292 }
Ian Rogers46685432012-06-03 22:26:43 -07002293 }
2294 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002295 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 * Some additional checks when calling a constructor. We know from the invocation arg check
2297 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2298 * that to require that called_method->klass is the same as this->klass or this->super,
2299 * allowing the latter only if the "this" argument is the same as the "this" argument to
2300 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002301 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002302 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002303 if (this_type.IsConflict()) // failure.
2304 break;
jeffhaobdb76512011-09-07 11:43:16 -07002305
jeffhaob57e9522012-04-26 18:08:21 -07002306 /* no null refs allowed (?) */
2307 if (this_type.IsZero()) {
2308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2309 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002310 }
jeffhaob57e9522012-04-26 18:08:21 -07002311
2312 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002313 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002314 // TODO: re-enable constructor type verification
2315 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002316 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002317 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2318 // break;
2319 // }
jeffhaob57e9522012-04-26 18:08:21 -07002320
2321 /* arg must be an uninitialized reference */
2322 if (!this_type.IsUninitializedTypes()) {
2323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2324 << this_type;
2325 break;
2326 }
2327
2328 /*
2329 * Replace the uninitialized reference with an initialized one. We need to do this for all
2330 * registers that have the same object instance in them, not just the "this" register.
2331 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002332 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002333 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002334 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002335 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2336 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002337 }
2338 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002339 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002340 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002341 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002343 just_set_result = true;
2344 break;
2345 }
2346 case Instruction::INVOKE_STATIC:
2347 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002349 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002350 METHOD_STATIC,
2351 is_range,
2352 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002353 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002356 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2357 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002358 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002359 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002360 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002361 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002362 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002364 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 } else {
2366 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2367 }
jeffhaobdb76512011-09-07 11:43:16 -07002368 just_set_result = true;
2369 }
2370 break;
jeffhaobdb76512011-09-07 11:43:16 -07002371 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002374 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002375 METHOD_INTERFACE,
2376 is_range,
2377 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002378 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002379 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2381 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2382 << PrettyMethod(abs_method) << "'";
2383 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002384 }
Ian Rogers0d604842012-04-16 14:50:24 -07002385 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002386 /* Get the type of the "this" arg, which should either be a sub-interface of called
2387 * interface or Object (see comments in RegType::JoinClass).
2388 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002389 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002390 if (this_type.IsZero()) {
2391 /* null pointer always passes (and always fails at runtime) */
2392 } else {
2393 if (this_type.IsUninitializedTypes()) {
2394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2395 << this_type;
2396 break;
2397 }
2398 // In the past we have tried to assert that "called_interface" is assignable
2399 // from "this_type.GetClass()", however, as we do an imprecise Join
2400 // (RegType::JoinClass) we don't have full information on what interfaces are
2401 // implemented by "this_type". For example, two classes may implement the same
2402 // interfaces and have a common parent that doesn't implement the interface. The
2403 // join will set "this_type" to the parent class and a test that this implements
2404 // the interface will incorrectly fail.
2405 }
2406 /*
2407 * We don't have an object instance, so we can't find the concrete method. However, all of
2408 * the type information is in the abstract method, so we're good.
2409 */
2410 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002411 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002413 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2414 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2415 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2416 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002417 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002418 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002419 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002421 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002422 } else {
2423 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2424 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002425 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 }
jeffhaobdb76512011-09-07 11:43:16 -07002428 case Instruction::NEG_INT:
2429 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002430 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002431 break;
2432 case Instruction::NEG_LONG:
2433 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002438 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002442 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002445 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002446 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002452 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002453 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
2455 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002456 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002457 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002460 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002461 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002464 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002475 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002476 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002477 break;
2478 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002479 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002481 break;
2482 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002485 break;
2486 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002487 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002488 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002489 break;
2490 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002491 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002492 break;
2493 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002494 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002497 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499
2500 case Instruction::ADD_INT:
2501 case Instruction::SUB_INT:
2502 case Instruction::MUL_INT:
2503 case Instruction::REM_INT:
2504 case Instruction::DIV_INT:
2505 case Instruction::SHL_INT:
2506 case Instruction::SHR_INT:
2507 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002509 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::AND_INT:
2512 case Instruction::OR_INT:
2513 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002514 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002515 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002516 break;
2517 case Instruction::ADD_LONG:
2518 case Instruction::SUB_LONG:
2519 case Instruction::MUL_LONG:
2520 case Instruction::DIV_LONG:
2521 case Instruction::REM_LONG:
2522 case Instruction::AND_LONG:
2523 case Instruction::OR_LONG:
2524 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002525 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002526 reg_types_.LongLo(), reg_types_.LongHi(),
2527 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::SHL_LONG:
2530 case Instruction::SHR_LONG:
2531 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002532 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002534 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
2536 case Instruction::ADD_FLOAT:
2537 case Instruction::SUB_FLOAT:
2538 case Instruction::MUL_FLOAT:
2539 case Instruction::DIV_FLOAT:
2540 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2542 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::ADD_DOUBLE:
2545 case Instruction::SUB_DOUBLE:
2546 case Instruction::MUL_DOUBLE:
2547 case Instruction::DIV_DOUBLE:
2548 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002550 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2551 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::ADD_INT_2ADDR:
2554 case Instruction::SUB_INT_2ADDR:
2555 case Instruction::MUL_INT_2ADDR:
2556 case Instruction::REM_INT_2ADDR:
2557 case Instruction::SHL_INT_2ADDR:
2558 case Instruction::SHR_INT_2ADDR:
2559 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002560 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2561 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::AND_INT_2ADDR:
2564 case Instruction::OR_INT_2ADDR:
2565 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2567 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002570 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2571 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::ADD_LONG_2ADDR:
2574 case Instruction::SUB_LONG_2ADDR:
2575 case Instruction::MUL_LONG_2ADDR:
2576 case Instruction::DIV_LONG_2ADDR:
2577 case Instruction::REM_LONG_2ADDR:
2578 case Instruction::AND_LONG_2ADDR:
2579 case Instruction::OR_LONG_2ADDR:
2580 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002582 reg_types_.LongLo(), reg_types_.LongHi(),
2583 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::SHL_LONG_2ADDR:
2586 case Instruction::SHR_LONG_2ADDR:
2587 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002588 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002589 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591 case Instruction::ADD_FLOAT_2ADDR:
2592 case Instruction::SUB_FLOAT_2ADDR:
2593 case Instruction::MUL_FLOAT_2ADDR:
2594 case Instruction::DIV_FLOAT_2ADDR:
2595 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2597 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::ADD_DOUBLE_2ADDR:
2600 case Instruction::SUB_DOUBLE_2ADDR:
2601 case Instruction::MUL_DOUBLE_2ADDR:
2602 case Instruction::DIV_DOUBLE_2ADDR:
2603 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2606 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::ADD_INT_LIT16:
2609 case Instruction::RSUB_INT:
2610 case Instruction::MUL_INT_LIT16:
2611 case Instruction::DIV_INT_LIT16:
2612 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2614 true);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::AND_INT_LIT16:
2617 case Instruction::OR_INT_LIT16:
2618 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2620 true);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::ADD_INT_LIT8:
2623 case Instruction::RSUB_INT_LIT8:
2624 case Instruction::MUL_INT_LIT8:
2625 case Instruction::DIV_INT_LIT8:
2626 case Instruction::REM_INT_LIT8:
2627 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002628 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002629 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002630 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2631 false);
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::AND_INT_LIT8:
2634 case Instruction::OR_INT_LIT8:
2635 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002636 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2637 false);
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002640 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002641 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002642 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2644 }
2645 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002646 // Note: the following instructions encode offsets derived from class linking.
2647 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2648 // meaning if the class linking and resolution were successful.
2649 case Instruction::IGET_QUICK:
2650 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2651 break;
2652 case Instruction::IGET_WIDE_QUICK:
2653 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2654 break;
2655 case Instruction::IGET_OBJECT_QUICK:
2656 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2657 break;
2658 case Instruction::IPUT_QUICK:
2659 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2660 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002661 case Instruction::IPUT_BOOLEAN_QUICK:
2662 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2663 break;
2664 case Instruction::IPUT_BYTE_QUICK:
2665 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2666 break;
2667 case Instruction::IPUT_CHAR_QUICK:
2668 VerifyIPutQuick(inst, reg_types_.Char(), true);
2669 break;
2670 case Instruction::IPUT_SHORT_QUICK:
2671 VerifyIPutQuick(inst, reg_types_.Short(), true);
2672 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002673 case Instruction::IPUT_WIDE_QUICK:
2674 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2675 break;
2676 case Instruction::IPUT_OBJECT_QUICK:
2677 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2678 break;
2679 case Instruction::INVOKE_VIRTUAL_QUICK:
2680 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2681 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002682 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002683 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002684 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002685 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002686 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002688 } else {
2689 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2690 }
2691 just_set_result = true;
2692 }
2693 break;
2694 }
2695
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002697 case Instruction::UNUSED_3E:
2698 case Instruction::UNUSED_3F:
2699 case Instruction::UNUSED_40:
2700 case Instruction::UNUSED_41:
2701 case Instruction::UNUSED_42:
2702 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002703 case Instruction::UNUSED_79:
2704 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002705 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002706 case Instruction::UNUSED_F0:
2707 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002708 case Instruction::UNUSED_F2:
2709 case Instruction::UNUSED_F3:
2710 case Instruction::UNUSED_F4:
2711 case Instruction::UNUSED_F5:
2712 case Instruction::UNUSED_F6:
2713 case Instruction::UNUSED_F7:
2714 case Instruction::UNUSED_F8:
2715 case Instruction::UNUSED_F9:
2716 case Instruction::UNUSED_FA:
2717 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002718 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002719 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002720 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002721 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724
2725 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002726 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002727 * complain if an instruction is missing (which is desirable).
2728 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002729 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002730
Ian Rogersad0b3a32012-04-16 14:50:24 -07002731 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002732 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002733 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002734 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002735 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002736 /* immediate failure, reject class */
2737 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2738 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002739 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002740 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002741 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002742 }
jeffhaobdb76512011-09-07 11:43:16 -07002743 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 * If we didn't just set the result register, clear it out. This ensures that you can only use
2745 * "move-result" immediately after the result is set. (We could check this statically, but it's
2746 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002747 */
2748 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002749 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002750 }
2751
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002752
jeffhaobdb76512011-09-07 11:43:16 -07002753
2754 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002755 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002756 *
2757 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002758 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002759 * somebody could get a reference field, check it for zero, and if the
2760 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002761 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002762 * that, and will reject the code.
2763 *
2764 * TODO: avoid re-fetching the branch target
2765 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002766 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002767 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002769 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002771 return false;
2772 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002773 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002774 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002775 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002776 }
jeffhaobdb76512011-09-07 11:43:16 -07002777 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002779 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002780 return false;
2781 }
2782 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002783 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002784 return false;
2785 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 }
jeffhaobdb76512011-09-07 11:43:16 -07002787 }
2788
2789 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002790 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002791 *
2792 * We've already verified that the table is structurally sound, so we
2793 * just need to walk through and tag the targets.
2794 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002795 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002796 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2797 const uint16_t* switch_insns = insns + offset_to_switch;
2798 int switch_count = switch_insns[1];
2799 int offset_to_targets, targ;
2800
2801 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2802 /* 0 = sig, 1 = count, 2/3 = first key */
2803 offset_to_targets = 4;
2804 } else {
2805 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002806 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002807 offset_to_targets = 2 + 2 * switch_count;
2808 }
2809
2810 /* verify each switch target */
2811 for (targ = 0; targ < switch_count; targ++) {
2812 int offset;
2813 uint32_t abs_offset;
2814
2815 /* offsets are 32-bit, and only partly endian-swapped */
2816 offset = switch_insns[offset_to_targets + targ * 2] |
2817 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 abs_offset = work_insn_idx_ + offset;
2819 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002820 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002821 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002823 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002824 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002825 }
jeffhaobdb76512011-09-07 11:43:16 -07002826 }
2827 }
2828
2829 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2831 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002832 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002833 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002834 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002835 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002836
Andreas Gampef91baf12014-07-18 15:41:00 -07002837 // Need the linker to try and resolve the handled class to check if it's Throwable.
2838 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2839
Ian Rogers0571d352011-11-03 19:51:38 -07002840 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002841 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2842 if (handler_type_idx == DexFile::kDexNoIndex16) {
2843 has_catch_all_handler = true;
2844 } else {
2845 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002846 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2847 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002848 if (klass != nullptr) {
2849 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2850 has_catch_all_handler = true;
2851 }
2852 } else {
2853 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002854 DCHECK(self_->IsExceptionPending());
2855 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002856 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002857 }
jeffhaobdb76512011-09-07 11:43:16 -07002858 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2860 * "work_regs", because at runtime the exception will be thrown before the instruction
2861 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002862 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002863 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002864 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 }
jeffhaobdb76512011-09-07 11:43:16 -07002866 }
2867
2868 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2870 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002871 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002872 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002873 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 * The state in work_line reflects the post-execution state. If the current instruction is a
2875 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002876 * it will do so before grabbing the lock).
2877 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002878 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002879 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002881 return false;
2882 }
2883 }
2884 }
2885
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002886 /* Handle "continue". Tag the next consecutive instruction.
2887 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2888 * because it changes work_line_ when performing peephole optimization
2889 * and this change should not be used in those cases.
2890 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002891 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002892 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2893 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002894 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2896 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002897 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002898 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2899 // next instruction isn't one.
2900 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2901 return false;
2902 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002903 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002904 // Make workline consistent with fallthrough computed from peephole optimization.
2905 work_line_->CopyFromLine(fallthrough_line.get());
2906 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002907 if (insn_flags_[next_insn_idx].IsReturn()) {
2908 // For returns we only care about the operand to the return, all other registers are dead.
2909 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2910 Instruction::Code opcode = ret_inst->Opcode();
2911 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002913 } else {
2914 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002915 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002916 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002917 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002918 }
2919 }
2920 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002921 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002922 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002923 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2924 // needed. If the merge changes the state of the registers then the work line will be
2925 // updated.
2926 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002927 return false;
2928 }
2929 } else {
2930 /*
2931 * We're not recording register data for the next instruction, so we don't know what the
2932 * prior state was. We have to assume that something has changed and re-evaluate it.
2933 */
2934 insn_flags_[next_insn_idx].SetChanged();
2935 }
2936 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002937
jeffhaod1f0fde2011-09-08 17:25:33 -07002938 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002939 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002940 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 return false;
2942 }
jeffhaobdb76512011-09-07 11:43:16 -07002943 }
2944
2945 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002946 * Update start_guess. Advance to the next instruction of that's
2947 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002948 * neither of those exists we're in a return or throw; leave start_guess
2949 * alone and let the caller sort it out.
2950 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002951 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002952 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2953 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002954 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002955 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002957 }
2958
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2960 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002961
2962 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002963} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002964
Ian Rogersd8f69b02014-09-10 21:43:52 +00002965const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002966 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002967 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002968 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002969 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002970 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2971 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002972 if (result.IsConflict()) {
2973 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2974 << "' in " << referrer;
2975 return result;
2976 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002977 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002978 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002979 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002980 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002981 // check at runtime if access is allowed and so pass here. If result is
2982 // primitive, skip the access check.
2983 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2984 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002985 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002987 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002988 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002989}
2990
Ian Rogersd8f69b02014-09-10 21:43:52 +00002991const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002994 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2996 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002997 CatchHandlerIterator iterator(handlers_ptr);
2998 for (; iterator.HasNext(); iterator.Next()) {
2999 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3000 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003001 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003003 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003004 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003005 if (exception.IsUnresolvedTypes()) {
3006 // We don't know enough about the type. Fail here and let runtime handle it.
3007 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3008 return exception;
3009 } else {
3010 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3011 return reg_types_.Conflict();
3012 }
Jeff Haob878f212014-04-24 16:25:36 -07003013 } else if (common_super == nullptr) {
3014 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003015 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003016 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003018 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07003019 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 }
3021 }
3022 }
3023 }
Ian Rogers0571d352011-11-03 19:51:38 -07003024 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 }
3026 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003027 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003029 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003032 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003033}
3034
Brian Carlstromea46f952013-07-30 01:26:50 -07003035mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003036 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003038 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003039 if (klass_type.IsConflict()) {
3040 std::string append(" in attempt to access method ");
3041 append += dex_file_->GetMethodName(method_id);
3042 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003044 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003045 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003047 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003048 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003049 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003050 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003052 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003053 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003054
3055 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003057 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003058 res_method = klass->FindInterfaceMethod(name, signature);
3059 } else {
3060 res_method = klass->FindVirtualMethod(name, signature);
3061 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003063 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003065 // If a virtual or interface method wasn't found with the expected type, look in
3066 // the direct methods. This can happen when the wrong invoke type is used or when
3067 // a class has changed, and will be flagged as an error in later checks.
3068 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3069 res_method = klass->FindDirectMethod(name, signature);
3070 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003072 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3073 << PrettyDescriptor(klass) << "." << name
3074 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 }
3078 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3080 // enforce them here.
3081 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3083 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003086 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003087 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3089 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003091 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003092 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003093 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003094 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003095 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003096 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003097 }
jeffhaode0d9c92012-02-27 13:58:13 -08003098 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3099 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3101 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003102 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003103 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003104 // Check that interface methods match interface classes.
3105 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3106 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3107 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003109 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3110 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3111 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003112 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003113 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 // See if the method type implied by the invoke instruction matches the access flags for the
3115 // target method.
3116 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3117 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3118 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3119 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003120 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3121 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003122 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003124 return res_method;
3125}
3126
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003127template <class T>
3128mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3129 MethodType method_type,
3130 bool is_range,
3131 mirror::ArtMethod* res_method) {
3132 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3133 // match the call to the signature. Also, we might be calling through an abstract method
3134 // definition (which doesn't have register count values).
3135 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3136 /* caught by static verifier */
3137 DCHECK(is_range || expected_args <= 5);
3138 if (expected_args > code_item_->outs_size_) {
3139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3140 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3141 return nullptr;
3142 }
3143
3144 uint32_t arg[5];
3145 if (!is_range) {
3146 inst->GetVarArgs(arg);
3147 }
3148 uint32_t sig_registers = 0;
3149
3150 /*
3151 * Check the "this" argument, which must be an instance of the class that declared the method.
3152 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3153 * rigorous check here (which is okay since we have to do it at runtime).
3154 */
3155 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003156 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003157 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3158 CHECK(have_pending_hard_failure_);
3159 return nullptr;
3160 }
3161 if (actual_arg_type.IsUninitializedReference()) {
3162 if (res_method) {
3163 if (!res_method->IsConstructor()) {
3164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3165 return nullptr;
3166 }
3167 } else {
3168 // Check whether the name of the called method is "<init>"
3169 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003170 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3172 return nullptr;
3173 }
3174 }
3175 }
3176 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003177 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003178 if (res_method != nullptr) {
3179 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003180 std::string temp;
3181 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003182 klass->CannotBeAssignedFromOtherTypes());
3183 } else {
3184 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3185 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003186 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003187 dex_file_->StringByTypeIdx(class_idx),
3188 false);
3189 }
3190 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3191 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3192 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3193 << "' not instance of '" << *res_method_class << "'";
3194 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3195 // the compiler.
3196 if (have_pending_hard_failure_) {
3197 return nullptr;
3198 }
3199 }
3200 }
3201 sig_registers = 1;
3202 }
3203
3204 for ( ; it->HasNext(); it->Next()) {
3205 if (sig_registers >= expected_args) {
3206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3207 " arguments, found " << sig_registers << " or more.";
3208 return nullptr;
3209 }
3210
3211 const char* param_descriptor = it->GetDescriptor();
3212
3213 if (param_descriptor == nullptr) {
3214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3215 "component";
3216 return nullptr;
3217 }
3218
Ian Rogersd8f69b02014-09-10 21:43:52 +00003219 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003220 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3221 arg[sig_registers];
3222 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003223 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003224 if (!src_type.IsIntegralTypes()) {
3225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3226 << " but expected " << reg_type;
3227 return res_method;
3228 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003229 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003230 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3231 // compiler.
3232 if (have_pending_hard_failure_) {
3233 return res_method;
3234 }
3235 }
3236 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3237 }
3238 if (expected_args != sig_registers) {
3239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3240 " arguments, found " << sig_registers;
3241 return nullptr;
3242 }
3243 return res_method;
3244}
3245
3246void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3247 MethodType method_type,
3248 bool is_range) {
3249 // As the method may not have been resolved, make this static check against what we expect.
3250 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3251 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3252 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3253 DexFileParameterIterator it(*dex_file_,
3254 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3255 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3256 nullptr);
3257}
3258
3259class MethodParamListDescriptorIterator {
3260 public:
3261 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3262 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3263 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3264 }
3265
3266 bool HasNext() {
3267 return pos_ < params_size_;
3268 }
3269
3270 void Next() {
3271 ++pos_;
3272 }
3273
3274 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3275 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3276 }
3277
3278 private:
3279 mirror::ArtMethod* res_method_;
3280 size_t pos_;
3281 const DexFile::TypeList* params_;
3282 const size_t params_size_;
3283};
3284
Brian Carlstromea46f952013-07-30 01:26:50 -07003285mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286 MethodType method_type,
3287 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003288 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003289 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3290 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003292
Brian Carlstromea46f952013-07-30 01:26:50 -07003293 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003294 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003295 // Check what we can statically.
3296 if (!have_pending_hard_failure_) {
3297 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3298 }
3299 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003300 }
3301
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3303 // has a vtable entry for the target method.
3304 if (is_super) {
3305 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003306 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003307 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003308 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003310 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003311 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003312 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003313 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003314 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003315 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003316 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003317 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003318 << "." << res_method->GetName()
3319 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003320 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 }
3322 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003323
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003324 // Process the target method's signature. This signature may or may not
3325 MethodParamListDescriptorIterator it(res_method);
3326 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3327 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003328}
3329
Brian Carlstromea46f952013-07-30 01:26:50 -07003330mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003331 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003332 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3333 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003334 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003335 if (!actual_arg_type.HasClass()) {
3336 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003337 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003338 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003339 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003340 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003341 if (klass->IsInterface()) {
3342 // Derive Object.class from Class.class.getSuperclass().
3343 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3344 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003345 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003346 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003347 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003348 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003349 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003350 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003351 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3352 << PrettyDescriptor(klass);
3353 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Ian Rogers7b078e82014-09-10 14:44:24 -07003354 CHECK(!self_->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003355 return res_method;
3356}
3357
Brian Carlstromea46f952013-07-30 01:26:50 -07003358mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003359 bool is_range) {
3360 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003361 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003362 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003363 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003365 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003366 }
3367 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3368
3369 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3370 // match the call to the signature. Also, we might be calling through an abstract method
3371 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003372 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003373 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003374 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003375 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003376 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3377 /* caught by static verifier */
3378 DCHECK(is_range || expected_args <= 5);
3379 if (expected_args > code_item_->outs_size_) {
3380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3381 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003382 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003383 }
3384
3385 /*
3386 * Check the "this" argument, which must be an instance of the class that declared the method.
3387 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3388 * rigorous check here (which is okay since we have to do it at runtime).
3389 */
3390 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003392 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003393 }
3394 if (!actual_arg_type.IsZero()) {
3395 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003396 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003397 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003398 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003399 klass->CannotBeAssignedFromOtherTypes());
3400 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003401 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3402 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003403 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 }
3406 }
3407 /*
3408 * Process the target method's signature. This signature may or may not
3409 * have been verified, so we can't assume it's properly formed.
3410 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003411 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003412 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003413 uint32_t arg[5];
3414 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003415 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003416 }
3417 size_t actual_args = 1;
3418 for (size_t param_index = 0; param_index < params_size; param_index++) {
3419 if (actual_args >= expected_args) {
3420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003421 << "'. Expected " << expected_args
3422 << " arguments, processing argument " << actual_args
3423 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003424 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003425 }
3426 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003427 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003430 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003432 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003433 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003434 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003435 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003436 return res_method;
3437 }
3438 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3439 }
3440 if (actual_args != expected_args) {
3441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3442 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003443 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003444 } else {
3445 return res_method;
3446 }
3447}
3448
Ian Rogers62342ec2013-06-11 10:26:37 -07003449void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003450 uint32_t type_idx;
3451 if (!is_filled) {
3452 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3453 type_idx = inst->VRegC_22c();
3454 } else if (!is_range) {
3455 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3456 type_idx = inst->VRegB_35c();
3457 } else {
3458 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3459 type_idx = inst->VRegB_3rc();
3460 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003461 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003462 if (res_type.IsConflict()) { // bad class
3463 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003464 } else {
3465 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3466 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003468 } else if (!is_filled) {
3469 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003470 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003471 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003472 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003473 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003474 } else {
3475 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3476 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003477 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003478 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3479 uint32_t arg[5];
3480 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003481 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003482 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003483 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003484 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003485 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3486 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003487 return;
3488 }
3489 }
3490 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003491 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003492 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003493 }
3494 }
3495}
3496
Sebastien Hertz5243e912013-05-21 10:55:07 +02003497void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003498 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003499 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003501 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003502 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003503 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003504 if (array_type.IsZero()) {
3505 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3506 // instruction type. TODO: have a proper notion of bottom here.
3507 if (!is_primitive || insn_type.IsCategory1Types()) {
3508 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003509 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003510 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003511 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003512 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3513 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003514 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003515 }
jeffhaofc3144e2012-02-01 17:21:15 -08003516 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003518 } else {
3519 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003520 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003521 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003523 << " source for aget-object";
3524 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003526 << " source for category 1 aget";
3527 } else if (is_primitive && !insn_type.Equals(component_type) &&
3528 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003529 (insn_type.IsLong() && component_type.IsDouble()))) {
3530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3531 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003532 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003533 // Use knowledge of the field type which is stronger than the type inferred from the
3534 // instruction, which can't differentiate object types and ints from floats, longs from
3535 // doubles.
3536 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003537 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003538 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003540 component_type.HighHalf(&reg_types_));
3541 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 }
3543 }
3544 }
3545}
3546
Ian Rogersd8f69b02014-09-10 21:43:52 +00003547void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003548 const uint32_t vregA) {
3549 // Primitive assignability rules are weaker than regular assignability rules.
3550 bool instruction_compatible;
3551 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003552 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003553 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003554 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003555 value_compatible = value_type.IsIntegralTypes();
3556 } else if (target_type.IsFloat()) {
3557 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3558 value_compatible = value_type.IsFloatTypes();
3559 } else if (target_type.IsLong()) {
3560 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003561 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3562 // as target_type depends on the resolved type of the field.
3563 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003564 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003565 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3566 } else {
3567 value_compatible = false;
3568 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003569 } else if (target_type.IsDouble()) {
3570 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003571 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3572 // as target_type depends on the resolved type of the field.
3573 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003574 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003575 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3576 } else {
3577 value_compatible = false;
3578 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003579 } else {
3580 instruction_compatible = false; // reference with primitive store
3581 value_compatible = false; // unused
3582 }
3583 if (!instruction_compatible) {
3584 // This is a global failure rather than a class change failure as the instructions and
3585 // the descriptors for the type should have been consistent within the same file at
3586 // compile time.
3587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3588 << "' but expected type '" << target_type << "'";
3589 return;
3590 }
3591 if (!value_compatible) {
3592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3593 << " of type " << value_type << " but expected " << target_type << " for put";
3594 return;
3595 }
3596}
3597
Sebastien Hertz5243e912013-05-21 10:55:07 +02003598void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003599 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003600 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003605 if (array_type.IsZero()) {
3606 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3607 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003608 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003610 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003611 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003612 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003613 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003614 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003616 if (!component_type.IsReferenceTypes()) {
3617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3618 << " source for aput-object";
3619 } else {
3620 // The instruction agrees with the type of array, confirm the value to be stored does too
3621 // Note: we use the instruction type (rather than the component type) for aput-object as
3622 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003624 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 }
3626 }
3627 }
3628}
3629
Brian Carlstromea46f952013-07-30 01:26:50 -07003630mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003631 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3632 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003633 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003634 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003635 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3636 field_idx, dex_file_->GetFieldName(field_id),
3637 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003639 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003640 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003641 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003642 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003643 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003644 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3645 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003646 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003647 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003648 << dex_file_->GetFieldName(field_id) << ") in "
3649 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003650 DCHECK(self_->IsExceptionPending());
3651 self_->ClearException();
3652 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003653 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3654 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003656 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003657 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 } else if (!field->IsStatic()) {
3659 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003661 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003662 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003663}
3664
Ian Rogersd8f69b02014-09-10 21:43:52 +00003665mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003666 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3667 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003668 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003669 if (klass_type.IsConflict()) {
3670 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3671 field_idx, dex_file_->GetFieldName(field_id),
3672 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003674 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003675 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003676 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003677 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003678 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003679 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3680 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003682 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003683 << dex_file_->GetFieldName(field_id) << ") in "
3684 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 DCHECK(self_->IsExceptionPending());
3686 self_->ClearException();
3687 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003688 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3689 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003690 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003691 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 } else if (field->IsStatic()) {
3694 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3695 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003696 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 } else if (obj_type.IsZero()) {
3698 // Cannot infer and check type, however, access will cause null pointer exception
3699 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003700 } else if (!obj_type.IsReferenceTypes()) {
3701 // Trying to read a field from something that isn't a reference
3702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3703 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003705 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003706 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003707 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003708 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003709 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003710 if (obj_type.IsUninitializedTypes() &&
3711 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3712 !field_klass.Equals(GetDeclaringClass()))) {
3713 // Field accesses through uninitialized references are only allowable for constructors where
3714 // the field is declared in this class
3715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003716 << " of a not fully initialized object within the context"
3717 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003719 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3720 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3721 // of C1. For resolution to occur the declared class of the field must be compatible with
3722 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3723 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3724 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003725 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003726 } else {
3727 return field;
3728 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003729 }
3730}
3731
Ian Rogersd8f69b02014-09-10 21:43:52 +00003732void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003733 bool is_primitive, bool is_static) {
3734 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003735 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003736 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003737 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003738 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003739 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003740 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003741 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003742 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003743 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003744 mirror::Class* field_type_class;
3745 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003746 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003747 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3748 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3749 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003750 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003751 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003752 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003753 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003754 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3755 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003756 }
Ian Rogers0d604842012-04-16 14:50:24 -07003757 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003758 if (field_type == nullptr) {
3759 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3760 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003761 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003762 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003763 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003764 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003765 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003766 if (field_type->Equals(insn_type) ||
3767 (field_type->IsFloat() && insn_type.IsInteger()) ||
3768 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003769 // expected that read is of the correct primitive type or that int reads are reading
3770 // floats or long reads are reading doubles
3771 } else {
3772 // This is a global failure rather than a class change failure as the instructions and
3773 // the descriptors for the type should have been consistent within the same file at
3774 // compile time
3775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3776 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003777 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003778 return;
3779 }
3780 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003781 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003782 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3783 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003784 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003785 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003787 return;
3788 }
3789 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003790 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003791 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003792 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003794 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003795}
3796
Ian Rogersd8f69b02014-09-10 21:43:52 +00003797void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003798 bool is_primitive, bool is_static) {
3799 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003800 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003801 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003802 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003803 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003804 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003805 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003806 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003807 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003808 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003809 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3810 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3811 << " from other class " << GetDeclaringClass();
3812 return;
3813 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003814 mirror::Class* field_type_class;
3815 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003816 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003817 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3818 FieldHelper fh(h_field);
3819 field_type_class = fh.GetType(can_load_classes_);
3820 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003821 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003822 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003823 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003824 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003825 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3826 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003827 }
3828 }
3829 if (field_type == nullptr) {
3830 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3831 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003832 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003833 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003834 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003835 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003836 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003837 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003838 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003839 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003840 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3841 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003842 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003843 << "' in put-object";
3844 return;
3845 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003847 }
3848}
3849
Brian Carlstromea46f952013-07-30 01:26:50 -07003850mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003851 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003852 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3853 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3854 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3855 inst->Opcode() == Instruction::IPUT_QUICK ||
3856 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003857 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3858 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3859 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3860 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3861 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003863 if (!object_type.HasClass()) {
3864 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3865 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003866 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003867 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003868 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3869 field_offset);
3870 if (f == nullptr) {
3871 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3872 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3873 }
3874 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003875}
3876
Ian Rogersd8f69b02014-09-10 21:43:52 +00003877void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003878 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003879 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003880 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3883 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003884 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003885 mirror::Class* field_type_class;
3886 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003888 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3889 FieldHelper fh(h_field);
3890 field_type_class = fh.GetType(can_load_classes_);
3891 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003892 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003893 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003894 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003895 field_type_class->CannotBeAssignedFromOtherTypes());
3896 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003897 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3898 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003899 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003900 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003901 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003902 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003903 const uint32_t vregA = inst->VRegA_22c();
3904 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003905 if (field_type->Equals(insn_type) ||
3906 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3907 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003908 // expected that read is of the correct primitive type or that int reads are reading
3909 // floats or long reads are reading doubles
3910 } else {
3911 // This is a global failure rather than a class change failure as the instructions and
3912 // the descriptors for the type should have been consistent within the same file at
3913 // compile time
3914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003915 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003916 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003917 return;
3918 }
3919 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003920 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003921 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003922 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003923 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003924 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003925 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003926 return;
3927 }
3928 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003929 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003930 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003931 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003932 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003933 }
3934}
3935
Ian Rogersd8f69b02014-09-10 21:43:52 +00003936void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003937 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003938 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003939 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003940 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3942 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003943 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003944 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003945 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003946 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003947 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003948 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3949 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003950 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003951 return;
3952 }
3953 }
3954 const uint32_t vregA = inst->VRegA_22c();
3955 if (is_primitive) {
3956 // Primitive field assignability rules are weaker than regular assignability rules
3957 bool instruction_compatible;
3958 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003959 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003960 if (field_type.IsIntegralTypes()) {
3961 instruction_compatible = insn_type.IsIntegralTypes();
3962 value_compatible = value_type.IsIntegralTypes();
3963 } else if (field_type.IsFloat()) {
3964 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3965 value_compatible = value_type.IsFloatTypes();
3966 } else if (field_type.IsLong()) {
3967 instruction_compatible = insn_type.IsLong();
3968 value_compatible = value_type.IsLongTypes();
3969 } else if (field_type.IsDouble()) {
3970 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3971 value_compatible = value_type.IsDoubleTypes();
3972 } else {
3973 instruction_compatible = false; // reference field with primitive store
3974 value_compatible = false; // unused
3975 }
3976 if (!instruction_compatible) {
3977 // This is a global failure rather than a class change failure as the instructions and
3978 // the descriptors for the type should have been consistent within the same file at
3979 // compile time
3980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003981 << " to be of type '" << insn_type
3982 << "' but found type '" << field_type
3983 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003984 return;
3985 }
3986 if (!value_compatible) {
3987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3988 << " of type " << value_type
3989 << " but expected " << field_type
3990 << " for store to " << PrettyField(field) << " in put";
3991 return;
3992 }
3993 } else {
3994 if (!insn_type.IsAssignableFrom(field_type)) {
3995 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003996 << " to be compatible with type '" << insn_type
3997 << "' but found type '" << field_type
3998 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003999 return;
4000 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 }
4003}
4004
Ian Rogers776ac1f2012-04-13 23:36:36 -07004005bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004008 return false;
4009 }
4010 return true;
4011}
4012
Stephen Kyle9bc61992014-09-22 13:53:15 +01004013bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4014 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4015 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4017 return false;
4018 }
4019 return true;
4020}
4021
4022bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4023 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4024}
4025
Ian Rogersebbdd872014-07-07 23:53:08 -07004026bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4027 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 bool changed = true;
4029 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4030 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004031 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004032 * We haven't processed this instruction before, and we haven't touched the registers here, so
4033 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4034 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004035 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004036 if (!insn_flags_[next_insn].IsReturn()) {
4037 target_line->CopyFromLine(merge_line);
4038 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004039 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004041 return false;
4042 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004043 // For returns we only care about the operand to the return, all other registers are dead.
4044 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4045 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4046 Instruction::Code opcode = ret_inst->Opcode();
4047 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004048 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004049 } else {
4050 target_line->CopyFromLine(merge_line);
4051 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004052 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004053 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004054 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004055 }
4056 }
4057 }
jeffhaobdb76512011-09-07 11:43:16 -07004058 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004059 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004060 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004062 if (gDebugVerify) {
4063 copy->CopyFromLine(target_line);
4064 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004065 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004066 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004067 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004068 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004069 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004070 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004071 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004072 << copy->Dump(this) << " MERGE\n"
4073 << merge_line->Dump(this) << " ==\n"
4074 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004075 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004076 if (update_merge_line && changed) {
4077 merge_line->CopyFromLine(target_line);
4078 }
jeffhaobdb76512011-09-07 11:43:16 -07004079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004080 if (changed) {
4081 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004082 }
4083 return true;
4084}
4085
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004086InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004087 return &insn_flags_[work_insn_idx_];
4088}
4089
Ian Rogersd8f69b02014-09-10 21:43:52 +00004090const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004091 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004092 if (mirror_method_.Get() != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004093 StackHandleScope<1> hs(self_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004094 mirror::Class* return_type_class =
4095 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004096 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004097 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4098 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004099 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004100 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004101 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4102 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004103 }
4104 }
4105 if (return_type_ == nullptr) {
4106 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4107 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4108 uint16_t return_type_idx = proto_id.return_type_idx_;
4109 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004110 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004111 }
4112 }
4113 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004114}
4115
Ian Rogersd8f69b02014-09-10 21:43:52 +00004116const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004117 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004118 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004119 const char* descriptor
4120 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004121 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004122 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004123 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4124 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004125 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004126 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004127 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004128 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004129 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004130}
4131
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004132std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4133 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004134 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004135 std::vector<int32_t> result;
4136 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004137 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004138 if (type.IsConstant()) {
4139 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004140 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4141 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004142 } else if (type.IsConstantLo()) {
4143 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004144 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4145 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004146 } else if (type.IsConstantHi()) {
4147 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004148 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4149 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004150 } else if (type.IsIntegralTypes()) {
4151 result.push_back(kIntVReg);
4152 result.push_back(0);
4153 } else if (type.IsFloat()) {
4154 result.push_back(kFloatVReg);
4155 result.push_back(0);
4156 } else if (type.IsLong()) {
4157 result.push_back(kLongLoVReg);
4158 result.push_back(0);
4159 result.push_back(kLongHiVReg);
4160 result.push_back(0);
4161 ++i;
4162 } else if (type.IsDouble()) {
4163 result.push_back(kDoubleLoVReg);
4164 result.push_back(0);
4165 result.push_back(kDoubleHiVReg);
4166 result.push_back(0);
4167 ++i;
4168 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4169 result.push_back(kUndefined);
4170 result.push_back(0);
4171 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004172 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004173 result.push_back(kReferenceVReg);
4174 result.push_back(0);
4175 }
4176 }
4177 return result;
4178}
4179
Ian Rogersd8f69b02014-09-10 21:43:52 +00004180const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004181 if (precise) {
4182 // Precise constant type.
4183 return reg_types_.FromCat1Const(value, true);
4184 } else {
4185 // Imprecise constant type.
4186 if (value < -32768) {
4187 return reg_types_.IntConstant();
4188 } else if (value < -128) {
4189 return reg_types_.ShortConstant();
4190 } else if (value < 0) {
4191 return reg_types_.ByteConstant();
4192 } else if (value == 0) {
4193 return reg_types_.Zero();
4194 } else if (value == 1) {
4195 return reg_types_.One();
4196 } else if (value < 128) {
4197 return reg_types_.PosByteConstant();
4198 } else if (value < 32768) {
4199 return reg_types_.PosShortConstant();
4200 } else if (value < 65536) {
4201 return reg_types_.CharConstant();
4202 } else {
4203 return reg_types_.IntConstant();
4204 }
4205 }
4206}
4207
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004208void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004209 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004210}
4211
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004212void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004213 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004214}
jeffhaod1224c72012-02-29 13:43:08 -08004215
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004216void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4217 RegTypeCache::VisitStaticRoots(callback, arg);
4218}
4219
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004220void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4221 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004222}
4223
Ian Rogersd81871c2011-10-03 13:57:23 -07004224} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004225} // namespace art