blob: fb07ba0c2d2e9d0db08d400c3f334f622e5fd5f3 [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);
Ian Rogers13735952014-10-08 12:43:28 -0700136 const uint8_t* 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
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700382static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
383 const Instruction* inst = Instruction::At(code_item->insns_);
384
385 uint32_t insns_size = code_item->insns_size_in_code_units_;
386 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
387 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
388 return true;
389 }
390
391 dex_pc += inst->SizeInCodeUnits();
392 inst = inst->Next();
393 }
394
395 return false;
396}
397
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700398void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700399 CHECK(monitor_enter_dex_pcs_ != nullptr);
400 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700401
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700402 // Quick check whether there are any monitor_enter instructions at all.
403 if (!HasMonitorEnterInstructions(code_item_)) {
404 return;
405 }
406
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700407 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
408 // verification. In practice, the phase we want relies on data structures set up by all the
409 // earlier passes, so we just run the full method verification and bail out early when we've
410 // got what we wanted.
411 Verify();
412}
413
Brian Carlstromea46f952013-07-30 01:26:50 -0700414mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700415 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700416 Thread* self = Thread::Current();
417 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700418 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
419 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700420 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700421 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700422 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
423 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200424 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425}
426
Brian Carlstromea46f952013-07-30 01:26:50 -0700427mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700428 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200429
430 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
431 // verification. In practice, the phase we want relies on data structures set up by all the
432 // earlier passes, so we just run the full method verification and bail out early when we've
433 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200434 bool success = Verify();
435 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700436 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200437 }
438 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700440 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200441 }
442 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
443 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200444}
445
Brian Carlstromea46f952013-07-30 01:26:50 -0700446mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700447 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700448 Thread* self = Thread::Current();
449 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700450 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
451 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700452 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700454 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
455 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200456 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200457}
458
Brian Carlstromea46f952013-07-30 01:26:50 -0700459mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700460 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200461
462 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
463 // verification. In practice, the phase we want relies on data structures set up by all the
464 // earlier passes, so we just run the full method verification and bail out early when we've
465 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200466 bool success = Verify();
467 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200469 }
470 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700471 if (register_line == nullptr) {
472 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200473 }
474 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
475 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
476 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200477}
478
Ian Rogersad0b3a32012-04-16 14:50:24 -0700479bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700481 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700484 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700485 } else {
486 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700487 }
jeffhaobdb76512011-09-07 11:43:16 -0700488 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
490 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
492 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700493 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700494 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800496 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 // Run through the instructions and see if the width checks out.
498 bool result = ComputeWidthsAndCountOps();
499 // Flag instructions guarded by a "try" block and check exception handlers.
500 result = result && ScanTryCatchBlocks();
501 // Perform static instruction verification.
502 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700503 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000504 result = result && VerifyCodeFlow();
505 // Compute information for compiler.
506 if (result && Runtime::Current()->IsCompiler()) {
507 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
508 }
509 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700510}
511
Ian Rogers776ac1f2012-04-13 23:36:36 -0700512std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700513 switch (error) {
514 case VERIFY_ERROR_NO_CLASS:
515 case VERIFY_ERROR_NO_FIELD:
516 case VERIFY_ERROR_NO_METHOD:
517 case VERIFY_ERROR_ACCESS_CLASS:
518 case VERIFY_ERROR_ACCESS_FIELD:
519 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700520 case VERIFY_ERROR_INSTANTIATION:
521 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800522 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700523 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
524 // class change and instantiation errors into soft verification errors so that we re-verify
525 // at runtime. We may fail to find or to agree on access because of not yet available class
526 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
527 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
528 // paths" that dynamically perform the verification and cause the behavior to be that akin
529 // to an interpreter.
530 error = VERIFY_ERROR_BAD_CLASS_SOFT;
531 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700532 // If we fail again at runtime, mark that this instruction would throw and force this
533 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700534 have_pending_runtime_throw_failure_ = true;
535 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700536 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537 // Indication that verification should be retried at runtime.
538 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700539 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700540 have_pending_hard_failure_ = true;
541 }
542 break;
jeffhaod5347e02012-03-22 17:25:05 -0700543 // Hard verification failures at compile time will still fail at runtime, so the class is
544 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700545 case VERIFY_ERROR_BAD_CLASS_HARD: {
546 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700547 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000548 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800549 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700550 have_pending_hard_failure_ = true;
551 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800552 }
553 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700554 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700555 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700556 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700557 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558 failure_messages_.push_back(failure_message);
559 return *failure_message;
560}
561
Ian Rogers576ca0c2014-06-06 15:58:22 -0700562std::ostream& MethodVerifier::LogVerifyInfo() {
563 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
564 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
565}
566
Ian Rogersad0b3a32012-04-16 14:50:24 -0700567void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
568 size_t failure_num = failure_messages_.size();
569 DCHECK_NE(failure_num, 0U);
570 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
571 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700572 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700573 delete last_fail_message;
574}
575
576void MethodVerifier::AppendToLastFailMessage(std::string append) {
577 size_t failure_num = failure_messages_.size();
578 DCHECK_NE(failure_num, 0U);
579 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
580 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800581}
582
Ian Rogers776ac1f2012-04-13 23:36:36 -0700583bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 const uint16_t* insns = code_item_->insns_;
585 size_t insns_size = code_item_->insns_size_in_code_units_;
586 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700587 size_t new_instance_count = 0;
588 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700590
Ian Rogersd81871c2011-10-03 13:57:23 -0700591 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700592 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700593 switch (opcode) {
594 case Instruction::APUT_OBJECT:
595 case Instruction::CHECK_CAST:
596 has_check_casts_ = true;
597 break;
598 case Instruction::INVOKE_VIRTUAL:
599 case Instruction::INVOKE_VIRTUAL_RANGE:
600 case Instruction::INVOKE_INTERFACE:
601 case Instruction::INVOKE_INTERFACE_RANGE:
602 has_virtual_or_interface_invokes_ = true;
603 break;
604 case Instruction::MONITOR_ENTER:
605 monitor_enter_count++;
606 break;
607 case Instruction::NEW_INSTANCE:
608 new_instance_count++;
609 break;
610 default:
611 break;
jeffhaobdb76512011-09-07 11:43:16 -0700612 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700613 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700614 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700616 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700617 }
618
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
621 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700622 return false;
623 }
624
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 new_instance_count_ = new_instance_count;
626 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700627 return true;
628}
629
Ian Rogers776ac1f2012-04-13 23:36:36 -0700630bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700632 if (tries_size == 0) {
633 return true;
634 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700636 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700637
638 for (uint32_t idx = 0; idx < tries_size; idx++) {
639 const DexFile::TryItem* try_item = &tries[idx];
640 uint32_t start = try_item->start_addr_;
641 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700642 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
644 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700645 return false;
646 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
649 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700650 return false;
651 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700652 uint32_t dex_pc = start;
653 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
654 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700656 size_t insn_size = inst->SizeInCodeUnits();
657 dex_pc += insn_size;
658 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700659 }
660 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800661 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700662 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700663 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700664 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700665 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700666 CatchHandlerIterator iterator(handlers_ptr);
667 for (; iterator.HasNext(); iterator.Next()) {
668 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700670 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
671 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700672 return false;
673 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100674 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
675 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
676 << "exception handler begins with move-result* (" << dex_pc << ")";
677 return false;
678 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700680 // Ensure exception types are resolved so that they don't need resolution to be delivered,
681 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700682 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
684 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700685 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700686 if (exception_type == nullptr) {
687 DCHECK(self_->IsExceptionPending());
688 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700689 }
690 }
jeffhaobdb76512011-09-07 11:43:16 -0700691 }
Ian Rogers0571d352011-11-03 19:51:38 -0700692 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700693 }
jeffhaobdb76512011-09-07 11:43:16 -0700694 return true;
695}
696
Ian Rogers776ac1f2012-04-13 23:36:36 -0700697bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700699
Ian Rogers0c7abda2012-09-19 13:33:42 -0700700 /* 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 -0700701 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700702 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700703
704 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700705 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700707 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 return false;
709 }
710 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700711 // All invoke points are marked as "Throw" points already.
712 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000713 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700714 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000715 } else if (inst->IsReturn()) {
716 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 }
718 dex_pc += inst->SizeInCodeUnits();
719 inst = inst->Next();
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 bool result = true;
726 switch (inst->GetVerifyTypeArgumentA()) {
727 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700728 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 break;
730 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700731 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 break;
733 }
734 switch (inst->GetVerifyTypeArgumentB()) {
735 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700736 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 break;
738 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700739 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 break;
741 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700742 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 break;
744 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700745 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 break;
747 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700748 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 break;
750 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700751 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 break;
753 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700754 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 break;
756 }
757 switch (inst->GetVerifyTypeArgumentC()) {
758 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700759 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 break;
761 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700762 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 break;
764 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700765 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 break;
767 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700768 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 break;
770 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700771 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 break;
773 }
774 switch (inst->GetVerifyExtraFlags()) {
775 case Instruction::kVerifyArrayData:
776 result = result && CheckArrayData(code_offset);
777 break;
778 case Instruction::kVerifyBranchTarget:
779 result = result && CheckBranchTarget(code_offset);
780 break;
781 case Instruction::kVerifySwitchTargets:
782 result = result && CheckSwitchTargets(code_offset);
783 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700784 case Instruction::kVerifyVarArgNonZero:
785 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700786 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700787 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
789 "non-range invoke";
790 return false;
791 }
Ian Rogers29a26482014-05-02 15:27:29 -0700792 uint32_t args[Instruction::kMaxVarArgRegs];
793 inst->GetVarArgs(args);
794 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700796 }
Andreas Gampec3314312014-06-19 18:13:29 -0700797 case Instruction::kVerifyVarArgRangeNonZero:
798 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700800 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
801 inst->VRegA() <= 0) {
802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
803 "range invoke";
804 return false;
805 }
Ian Rogers29a26482014-05-02 15:27:29 -0700806 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 break;
808 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 result = false;
811 break;
812 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700813 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
815 result = false;
816 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 return result;
818}
819
Ian Rogers7b078e82014-09-10 14:44:24 -0700820inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
823 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 return false;
825 }
826 return true;
827}
828
Ian Rogers7b078e82014-09-10 14:44:24 -0700829inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
832 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 return false;
834 }
835 return true;
836}
837
Ian Rogers7b078e82014-09-10 14:44:24 -0700838inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
841 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 return false;
843 }
844 return true;
845}
846
Ian Rogers7b078e82014-09-10 14:44:24 -0700847inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
850 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 return false;
852 }
853 return true;
854}
855
Ian Rogers7b078e82014-09-10 14:44:24 -0700856inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
859 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 return false;
861 }
862 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700863 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 return false;
867 }
868 return true;
869}
870
Ian Rogers7b078e82014-09-10 14:44:24 -0700871inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
874 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 return true;
878}
879
Ian Rogers7b078e82014-09-10 14:44:24 -0700880inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700881 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
883 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 return false;
885 }
886 return true;
887}
888
Ian Rogers776ac1f2012-04-13 23:36:36 -0700889bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
892 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 return false;
894 }
895 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700896 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 const char* cp = descriptor;
898 while (*cp++ == '[') {
899 bracket_count++;
900 }
901 if (bracket_count == 0) {
902 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
904 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 return false;
906 } else if (bracket_count > 255) {
907 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
909 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 return false;
911 }
912 return true;
913}
914
Ian Rogers776ac1f2012-04-13 23:36:36 -0700915bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
917 const uint16_t* insns = code_item_->insns_ + cur_offset;
918 const uint16_t* array_data;
919 int32_t array_data_offset;
920
921 DCHECK_LT(cur_offset, insn_count);
922 /* make sure the start of the array data table is in range */
923 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
924 if ((int32_t) cur_offset + array_data_offset < 0 ||
925 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700927 << ", data offset " << array_data_offset
928 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
931 /* offset to array data table is a relative branch-style offset */
932 array_data = insns + array_data_offset;
933 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800934 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
936 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 return false;
938 }
939 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700940 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
942 /* make sure the end of the switch is in range */
943 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
945 << ", data offset " << array_data_offset << ", end "
946 << cur_offset + array_data_offset + table_size
947 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 return false;
949 }
950 return true;
951}
952
Ian Rogers776ac1f2012-04-13 23:36:36 -0700953bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 int32_t offset;
955 bool isConditional, selfOkay;
956 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
957 return false;
958 }
959 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
961 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 return false;
963 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700964 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
965 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
968 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 return false;
970 }
971 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
972 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700973 if (abs_offset < 0 ||
974 (uint32_t) abs_offset >= insn_count ||
975 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700977 << reinterpret_cast<void*>(abs_offset) << ") at "
978 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 return false;
980 }
981 insn_flags_[abs_offset].SetBranchTarget();
982 return true;
983}
984
Ian Rogers776ac1f2012-04-13 23:36:36 -0700985bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 bool* selfOkay) {
987 const uint16_t* insns = code_item_->insns_ + cur_offset;
988 *pConditional = false;
989 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700990 switch (*insns & 0xff) {
991 case Instruction::GOTO:
992 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700993 break;
994 case Instruction::GOTO_32:
995 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 *selfOkay = true;
997 break;
998 case Instruction::GOTO_16:
999 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001000 break;
1001 case Instruction::IF_EQ:
1002 case Instruction::IF_NE:
1003 case Instruction::IF_LT:
1004 case Instruction::IF_GE:
1005 case Instruction::IF_GT:
1006 case Instruction::IF_LE:
1007 case Instruction::IF_EQZ:
1008 case Instruction::IF_NEZ:
1009 case Instruction::IF_LTZ:
1010 case Instruction::IF_GEZ:
1011 case Instruction::IF_GTZ:
1012 case Instruction::IF_LEZ:
1013 *pOffset = (int16_t) insns[1];
1014 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 break;
1016 default:
1017 return false;
1018 break;
1019 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001020 return true;
1021}
1022
Ian Rogers776ac1f2012-04-13 23:36:36 -07001023bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001025 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001027 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1029 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 << ", switch offset " << switch_offset
1032 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001033 return false;
1034 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001035 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001037 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001038 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1040 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001041 return false;
1042 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 uint32_t switch_count = switch_insns[1];
1044 int32_t keys_offset, targets_offset;
1045 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1047 /* 0=sig, 1=count, 2/3=firstKey */
1048 targets_offset = 4;
1049 keys_offset = -1;
1050 expected_signature = Instruction::kPackedSwitchSignature;
1051 } else {
1052 /* 0=sig, 1=count, 2..count*2 = keys */
1053 keys_offset = 2;
1054 targets_offset = 2 + 2 * switch_count;
1055 expected_signature = Instruction::kSparseSwitchSignature;
1056 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001058 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001059 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1060 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1061 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001062 return false;
1063 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001064 /* make sure the end of the switch is in range */
1065 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1067 << ", switch offset " << switch_offset
1068 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001069 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001070 return false;
1071 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001072 /* for a sparse switch, verify the keys are in ascending order */
1073 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1075 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001076 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1077 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1078 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1080 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 return false;
1082 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001083 last_key = key;
1084 }
1085 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001086 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 for (uint32_t targ = 0; targ < switch_count; targ++) {
1088 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1089 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1090 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 if (abs_offset < 0 ||
1092 abs_offset >= (int32_t) insn_count ||
1093 !insn_flags_[abs_offset].IsOpcode()) {
1094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1095 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1096 << reinterpret_cast<void*>(cur_offset)
1097 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001098 return false;
1099 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 insn_flags_[abs_offset].SetBranchTarget();
1101 }
1102 return true;
1103}
1104
Ian Rogers776ac1f2012-04-13 23:36:36 -07001105bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001106 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 uint16_t registers_size = code_item_->registers_size_;
1111 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001112 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1114 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 return false;
1116 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 }
1118
1119 return true;
1120}
1121
Ian Rogers776ac1f2012-04-13 23:36:36 -07001122bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 uint16_t registers_size = code_item_->registers_size_;
1124 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1125 // integer overflow when adding them here.
1126 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1128 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 return false;
1130 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001131 return true;
1132}
1133
Ian Rogers776ac1f2012-04-13 23:36:36 -07001134bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 uint16_t registers_size = code_item_->registers_size_;
1136 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001137
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001139 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1140 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 }
1142 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001143 reg_table_.Init(kTrackCompilerInterestPoints,
1144 insn_flags_.get(),
1145 insns_size,
1146 registers_size,
1147 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001148
jeffhaobdb76512011-09-07 11:43:16 -07001149
Ian Rogersd0fbd852013-09-24 18:17:04 -07001150 work_line_.reset(RegisterLine::Create(registers_size, this));
1151 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001152
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 /* Initialize register types of method arguments. */
1154 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001155 DCHECK_NE(failures_.size(), 0U);
1156 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001158 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 return false;
1160 }
1161 /* Perform code flow verification. */
1162 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001163 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001165 }
jeffhaobdb76512011-09-07 11:43:16 -07001166 return true;
1167}
1168
Ian Rogersad0b3a32012-04-16 14:50:24 -07001169std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1170 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001171 for (size_t i = 0; i < failures_.size(); ++i) {
1172 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001173 }
1174 return os;
1175}
1176
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001177extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001179 v->Dump(std::cerr);
1180}
1181
Ian Rogers776ac1f2012-04-13 23:36:36 -07001182void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001183 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001184 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return;
jeffhaobdb76512011-09-07 11:43:16 -07001186 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001187 {
1188 os << "Register Types:\n";
1189 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1190 std::ostream indent_os(&indent_filter);
1191 reg_types_.Dump(indent_os);
1192 }
Ian Rogersb4903572012-10-11 11:52:56 -07001193 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001194 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1195 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 const Instruction* inst = Instruction::At(code_item_->insns_);
1197 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001198 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001200 if (reg_line != nullptr) {
1201 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001202 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001203 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 const bool kDumpHexOfInstruction = false;
1205 if (kDumpHexOfInstruction) {
1206 indent_os << inst->DumpHex(5) << " ";
1207 }
1208 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001209 inst = inst->Next();
1210 }
jeffhaobdb76512011-09-07 11:43:16 -07001211}
1212
Ian Rogersd81871c2011-10-03 13:57:23 -07001213static bool IsPrimitiveDescriptor(char descriptor) {
1214 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001215 case 'I':
1216 case 'C':
1217 case 'S':
1218 case 'B':
1219 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001220 case 'F':
1221 case 'D':
1222 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001224 default:
1225 return false;
1226 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001227}
1228
Ian Rogers776ac1f2012-04-13 23:36:36 -07001229bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 RegisterLine* reg_line = reg_table_.GetLine(0);
1231 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1232 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001233
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001235 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001237 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1239 // argument as uninitialized. This restricts field access until the superclass constructor is
1240 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001241 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001242 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001243 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 reg_types_.UninitializedThisArgument(declaring_class));
1245 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001246 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001247 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001249 }
1250
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001251 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001252 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001253 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001254
1255 for (; iterator.HasNext(); iterator.Next()) {
1256 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001257 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 LOG(FATAL) << "Null descriptor";
1259 }
1260 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1262 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 return false;
1264 }
1265 switch (descriptor[0]) {
1266 case 'L':
1267 case '[':
1268 // We assume that reference arguments are initialized. The only way it could be otherwise
1269 // (assuming the caller was verified) is if the current method is <init>, but in that case
1270 // it's effectively considered initialized the instant we reach here (in the sense that we
1271 // can return without doing anything or call virtual methods).
1272 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001273 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001274 if (!reg_type.IsNonZeroReferenceTypes()) {
1275 DCHECK(HasFailures());
1276 return false;
1277 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001278 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 }
1280 break;
1281 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001282 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 break;
1284 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001285 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 break;
1287 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001288 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 break;
1290 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001291 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 break;
1293 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001294 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 break;
1296 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001297 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 break;
1299 case 'J':
1300 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001301 if (cur_arg + 1 >= expected_args) {
1302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1303 << " args, found more (" << descriptor << ")";
1304 return false;
1305 }
1306
Ian Rogers7b078e82014-09-10 14:44:24 -07001307 const RegType* lo_half;
1308 const RegType* hi_half;
1309 if (descriptor[0] == 'J') {
1310 lo_half = &reg_types_.LongLo();
1311 hi_half = &reg_types_.LongHi();
1312 } else {
1313 lo_half = &reg_types_.DoubleLo();
1314 hi_half = &reg_types_.DoubleHi();
1315 }
1316 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 cur_arg++;
1318 break;
1319 }
1320 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1322 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 return false;
1324 }
1325 cur_arg++;
1326 }
1327 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1329 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 return false;
1331 }
1332 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1333 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1334 // format. Only major difference from the method argument format is that 'V' is supported.
1335 bool result;
1336 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1337 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001338 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 size_t i = 0;
1340 do {
1341 i++;
1342 } while (descriptor[i] == '['); // process leading [
1343 if (descriptor[i] == 'L') { // object array
1344 do {
1345 i++; // find closing ;
1346 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1347 result = descriptor[i] == ';';
1348 } else { // primitive array
1349 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1350 }
1351 } else if (descriptor[0] == 'L') {
1352 // could be more thorough here, but shouldn't be required
1353 size_t i = 0;
1354 do {
1355 i++;
1356 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1357 result = descriptor[i] == ';';
1358 } else {
1359 result = false;
1360 }
1361 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1363 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 }
1365 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001366}
1367
Ian Rogers776ac1f2012-04-13 23:36:36 -07001368bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 const uint16_t* insns = code_item_->insns_;
1370 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001371
jeffhaobdb76512011-09-07 11:43:16 -07001372 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 insn_flags_[0].SetChanged();
1374 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001375
jeffhaobdb76512011-09-07 11:43:16 -07001376 /* Continue until no instructions are marked "changed". */
1377 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001378 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1380 uint32_t insn_idx = start_guess;
1381 for (; insn_idx < insns_size; insn_idx++) {
1382 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001383 break;
1384 }
jeffhaobdb76512011-09-07 11:43:16 -07001385 if (insn_idx == insns_size) {
1386 if (start_guess != 0) {
1387 /* try again, starting from the top */
1388 start_guess = 0;
1389 continue;
1390 } else {
1391 /* all flags are clear */
1392 break;
1393 }
1394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 // We carry the working set of registers from instruction to instruction. If this address can
1396 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1397 // "changed" flags, we need to load the set of registers from the table.
1398 // Because we always prefer to continue on to the next instruction, we should never have a
1399 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1400 // target.
1401 work_insn_idx_ = insn_idx;
1402 if (insn_flags_[insn_idx].IsBranchTarget()) {
1403 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001404 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001405 /*
1406 * Sanity check: retrieve the stored register line (assuming
1407 * a full table) and make sure it actually matches.
1408 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001410 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 if (work_line_->CompareLine(register_line) != 0) {
1412 Dump(std::cout);
1413 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001414 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001415 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001416 << " work_line=" << work_line_->Dump(this) << "\n"
1417 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 }
jeffhaobdb76512011-09-07 11:43:16 -07001419 }
jeffhaobdb76512011-09-07 11:43:16 -07001420 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001422 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001423 prepend += " failed to verify: ";
1424 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001425 return false;
1426 }
jeffhaobdb76512011-09-07 11:43:16 -07001427 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 insn_flags_[insn_idx].SetVisited();
1429 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001430 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001431
Ian Rogers1c849e52012-06-28 14:00:33 -07001432 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001433 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001434 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001435 * (besides the wasted space), but it indicates a flaw somewhere
1436 * down the line, possibly in the verifier.
1437 *
1438 * If we've substituted "always throw" instructions into the stream,
1439 * we are almost certainly going to have some dead code.
1440 */
1441 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001443 for (; insn_idx < insns_size;
1444 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001445 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001446 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001447 * may or may not be preceded by a padding NOP (for alignment).
1448 */
1449 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1450 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1451 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001452 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001453 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1454 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1455 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001457 }
1458
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001460 if (dead_start < 0)
1461 dead_start = insn_idx;
1462 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001463 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1464 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001465 dead_start = -1;
1466 }
1467 }
1468 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001469 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1470 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001471 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001472 // To dump the state of the verify after a method, do something like:
1473 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1474 // "boolean java.lang.String.equals(java.lang.Object)") {
1475 // LOG(INFO) << info_messages_.str();
1476 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001477 }
jeffhaobdb76512011-09-07 11:43:16 -07001478 return true;
1479}
1480
Ian Rogers776ac1f2012-04-13 23:36:36 -07001481bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001482 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1483 // We want the state _before_ the instruction, for the case where the dex pc we're
1484 // interested in is itself a monitor-enter instruction (which is a likely place
1485 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001486 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001487 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001488 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1489 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1490 }
1491 }
1492
jeffhaobdb76512011-09-07 11:43:16 -07001493 /*
1494 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001495 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001496 * control to another statement:
1497 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001498 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001499 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001500 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001501 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001502 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001503 * throw an exception that is handled by an encompassing "try"
1504 * block.
1505 *
1506 * We can also return, in which case there is no successor instruction
1507 * from this point.
1508 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001509 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001510 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1512 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001513 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001514
jeffhaobdb76512011-09-07 11:43:16 -07001515 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001516 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001517 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001519 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001520 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 }
jeffhaobdb76512011-09-07 11:43:16 -07001522
1523 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001524 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001525 * can throw an exception, we will copy/merge this into the "catch"
1526 * address rather than work_line, because we don't want the result
1527 * from the "successful" code path (e.g. a check-cast that "improves"
1528 * a type) to be visible to the exception handler.
1529 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001530 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001532 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
1535
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001536
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001537 // We need to ensure the work line is consistent while performing validation. When we spot a
1538 // peephole pattern we compute a new line for either the fallthrough instruction or the
1539 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001540 std::unique_ptr<RegisterLine> branch_line;
1541 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001542
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001544 case Instruction::NOP:
1545 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001546 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001547 * a signature that looks like a NOP; if we see one of these in
1548 * the course of executing code then we have a problem.
1549 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001550 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001552 }
1553 break;
1554
1555 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001556 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 break;
jeffhaobdb76512011-09-07 11:43:16 -07001558 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001559 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 break;
jeffhaobdb76512011-09-07 11:43:16 -07001561 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001562 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001563 break;
1564 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001565 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001566 break;
jeffhaobdb76512011-09-07 11:43:16 -07001567 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001568 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 break;
jeffhaobdb76512011-09-07 11:43:16 -07001570 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001571 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001572 break;
1573 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001574 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 break;
jeffhaobdb76512011-09-07 11:43:16 -07001576 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001577 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 break;
jeffhaobdb76512011-09-07 11:43:16 -07001579 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001580 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001581 break;
1582
1583 /*
1584 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001585 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001586 * might want to hold the result in an actual CPU register, so the
1587 * Dalvik spec requires that these only appear immediately after an
1588 * invoke or filled-new-array.
1589 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001590 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001591 * redundant with the reset done below, but it can make the debug info
1592 * easier to read in some cases.)
1593 */
1594 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001596 break;
1597 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001598 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001599 break;
1600 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001601 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001602 break;
1603
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001605 /*
jeffhao60f83e32012-02-13 17:16:30 -08001606 * This statement can only appear as the first instruction in an exception handler. We verify
1607 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001608 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001609 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001610 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001611 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 }
jeffhaobdb76512011-09-07 11:43:16 -07001613 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001614 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001615 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 }
jeffhaobdb76512011-09-07 11:43:16 -07001618 }
1619 break;
1620 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001621 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001622 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001623 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1626 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 } else {
1628 // Compilers may generate synthetic functions that write byte values into boolean fields.
1629 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001631 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1633 ((return_type.IsBoolean() || return_type.IsByte() ||
1634 return_type.IsShort() || return_type.IsChar()) &&
1635 src_type.IsInteger()));
1636 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001637 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001638 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001639 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 }
jeffhaobdb76512011-09-07 11:43:16 -07001642 }
1643 }
1644 break;
1645 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001647 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001648 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 } else {
1652 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001654 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001655 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 }
jeffhaobdb76512011-09-07 11:43:16 -07001658 }
1659 }
1660 break;
1661 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001662 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001663 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 } else {
1667 /* return_type is the *expected* return type, not register value */
1668 DCHECK(!return_type.IsZero());
1669 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001671 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001672 // Disallow returning uninitialized values and verify that the reference in vAA is an
1673 // instance of the "return_type"
1674 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001675 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1676 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001677 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001678 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1679 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1680 << "' or '" << reg_type << "'";
1681 } else {
1682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1683 << "', but expected from declaration '" << return_type << "'";
1684 }
jeffhaobdb76512011-09-07 11:43:16 -07001685 }
1686 }
1687 }
1688 break;
1689
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001690 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 case Instruction::CONST_4: {
1692 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001693 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001694 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001695 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 }
1697 case Instruction::CONST_16: {
1698 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001699 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001700 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001701 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001703 case Instruction::CONST: {
1704 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001705 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001706 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001707 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001708 }
1709 case Instruction::CONST_HIGH16: {
1710 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001711 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001712 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
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_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001722 }
1723 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001725 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1726 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001727 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001728 break;
1729 }
1730 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001732 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1733 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001734 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 break;
1736 }
1737 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001739 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1740 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001741 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001742 break;
1743 }
jeffhaobdb76512011-09-07 11:43:16 -07001744 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 break;
jeffhaobdb76512011-09-07 11:43:16 -07001747 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001748 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001749 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001751 // Get type from instruction if unresolved then we need an access check
1752 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001753 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001754 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001756 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001757 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 }
jeffhaobdb76512011-09-07 11:43:16 -07001760 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
1763 case Instruction::MONITOR_EXIT:
1764 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001765 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001766 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001767 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001768 * to the need to handle asynchronous exceptions, a now-deprecated
1769 * feature that Dalvik doesn't support.)
1770 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001771 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001772 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001773 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001774 * structured locking checks are working, the former would have
1775 * failed on the -enter instruction, and the latter is impossible.
1776 *
1777 * This is fortunate, because issue 3221411 prevents us from
1778 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001779 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001780 * some catch blocks (which will show up as "dead" code when
1781 * we skip them here); if we can't, then the code path could be
1782 * "live" so we still need to check it.
1783 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001784 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001785 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001786 break;
1787
Ian Rogers28ad40d2011-10-27 15:19:26 -07001788 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001790 /*
1791 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1792 * could be a "upcast" -- not expected, so we don't try to address it.)
1793 *
1794 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001795 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001796 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1798 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001799 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001800 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001801 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001802 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001803 if (klass != nullptr && klass->IsPrimitive()) {
1804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1805 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1806 << GetDeclaringClass();
1807 break;
1808 }
1809
Ian Rogersad0b3a32012-04-16 14:50:24 -07001810 DCHECK_NE(failures_.size(), 0U);
1811 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001812 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001813 }
1814 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001815 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001816 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001818 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001819 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 if (is_checkcast) {
1821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1822 } else {
1823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1824 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001825 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 if (is_checkcast) {
1827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1828 } else {
1829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1830 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001831 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001832 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001835 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001836 }
jeffhaobdb76512011-09-07 11:43:16 -07001837 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001838 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 }
1840 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001842 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001843 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001845 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001846 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001848 } else {
1849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 }
1851 break;
1852 }
1853 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001854 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001855 if (res_type.IsConflict()) {
1856 DCHECK_NE(failures_.size(), 0U);
1857 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001858 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001859 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1860 // can't create an instance of an interface or abstract class */
1861 if (!res_type.IsInstantiableTypes()) {
1862 Fail(VERIFY_ERROR_INSTANTIATION)
1863 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001864 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001866 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001867 // Any registers holding previous allocations from this address that have not yet been
1868 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001870 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 break;
1873 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001874 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001875 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001876 break;
1877 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001878 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001879 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001881 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001882 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001883 just_set_result = true; // Filled new array range sets result register
1884 break;
jeffhaobdb76512011-09-07 11:43:16 -07001885 case Instruction::CMPL_FLOAT:
1886 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001887 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001888 break;
1889 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001891 break;
1892 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001894 break;
1895 case Instruction::CMPL_DOUBLE:
1896 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001898 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001899 break;
1900 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001901 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001902 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001903 break;
1904 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001905 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001906 break;
1907 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001910 break;
1911 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001912 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001914 break;
1915 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001916 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001917 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001919 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001920 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001921 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1922 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001923 }
1924 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 }
jeffhaobdb76512011-09-07 11:43:16 -07001926 case Instruction::GOTO:
1927 case Instruction::GOTO_16:
1928 case Instruction::GOTO_32:
1929 /* no effect on or use of registers */
1930 break;
1931
1932 case Instruction::PACKED_SWITCH:
1933 case Instruction::SPARSE_SWITCH:
1934 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001935 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001936 break;
1937
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 case Instruction::FILL_ARRAY_DATA: {
1939 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001940 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001941 /* array_type can be null if the reg type is Zero */
1942 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001943 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1945 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001946 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001947 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001948 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001949 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1951 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001952 } else {
jeffhao457cc512012-02-02 16:55:13 -08001953 // Now verify if the element width in the table matches the element width declared in
1954 // the array
1955 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1956 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001958 } else {
1959 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1960 // Since we don't compress the data in Dex, expect to see equal width of data stored
1961 // in the table and expected from the array class.
1962 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1964 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001965 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 }
1967 }
jeffhaobdb76512011-09-07 11:43:16 -07001968 }
1969 }
1970 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 }
jeffhaobdb76512011-09-07 11:43:16 -07001972 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1975 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 bool mismatch = false;
1977 if (reg_type1.IsZero()) { // zero then integral or reference expected
1978 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1979 } else if (reg_type1.IsReferenceTypes()) { // both references?
1980 mismatch = !reg_type2.IsReferenceTypes();
1981 } else { // both integral?
1982 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1983 }
1984 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1986 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001987 }
1988 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::IF_LT:
1991 case Instruction::IF_GE:
1992 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001994 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1995 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1998 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001999 }
2000 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002004 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2007 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002009
2010 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002011 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002012 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002013 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002014 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002015 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002016 }
Ian Rogers9b360392013-06-06 14:45:07 -07002017 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002018 } else {
2019 break;
2020 }
2021
Ian Rogers9b360392013-06-06 14:45:07 -07002022 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002023
2024 /* Check for peep-hole pattern of:
2025 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002026 * instance-of vX, vY, T;
2027 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002028 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002029 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002030 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002031 * and sharpen the type of vY to be type T.
2032 * Note, this pattern can't be if:
2033 * - if there are other branches to this branch,
2034 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002035 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002036 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002037 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2038 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2039 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002040 // Check the type of the instance-of is different than that of registers type, as if they
2041 // are the same there is no work to be done here. Check that the conversion is not to or
2042 // from an unresolved type as type information is imprecise. If the instance-of is to an
2043 // interface then ignore the type information as interfaces can only be treated as Objects
2044 // and we don't want to disallow field and other operations on the object. If the value
2045 // being instance-of checked against is known null (zero) then allow the optimization as
2046 // we didn't have type information. If the merge of the instance-of type with the original
2047 // type is assignable to the original then allow optimization. This check is performed to
2048 // ensure that subsequent merges don't lose type information - such as becoming an
2049 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002050 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002051 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002052
Ian Rogersebbdd872014-07-07 23:53:08 -07002053 if (!orig_type.Equals(cast_type) &&
2054 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002055 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002056 !cast_type.GetClass()->IsInterface() &&
2057 (orig_type.IsZero() ||
2058 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002059 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002060 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002061 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002062 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002063 branch_line.reset(update_line);
2064 }
2065 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002067 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2068 // See if instance-of was preceded by a move-object operation, common due to the small
2069 // register encoding space of instance-of, and propagate type information to the source
2070 // of the move-object.
2071 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002072 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002073 move_idx--;
2074 }
2075 CHECK(insn_flags_[move_idx].IsOpcode());
2076 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2077 switch (move_inst->Opcode()) {
2078 case Instruction::MOVE_OBJECT:
2079 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002081 }
2082 break;
2083 case Instruction::MOVE_OBJECT_FROM16:
2084 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002085 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002086 }
2087 break;
2088 case Instruction::MOVE_OBJECT_16:
2089 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002090 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002091 }
2092 break;
2093 default:
2094 break;
2095 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002096 }
2097 }
2098 }
2099
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 }
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::IF_LTZ:
2103 case Instruction::IF_GEZ:
2104 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2109 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 }
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 }
jeffhaobdb76512011-09-07 11:43:16 -07002113 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 break;
jeffhaobdb76512011-09-07 11:43:16 -07002116 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 break;
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 break;
jeffhaobdb76512011-09-07 11:43:16 -07002122 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 break;
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002130 break;
2131 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 break;
2138 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
2141 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 break;
2144 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156
jeffhaobdb76512011-09-07 11:43:16 -07002157 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 break;
jeffhaobdb76512011-09-07 11:43:16 -07002160 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 break;
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 break;
jeffhaobdb76512011-09-07 11:43:16 -07002166 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 break;
2169 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 break;
jeffhaobdb76512011-09-07 11:43:16 -07002178
Ian Rogersd81871c2011-10-03 13:57:23 -07002179 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002181 break;
2182 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002184 break;
2185 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002187 break;
2188 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002190 break;
2191 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002192 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
2194 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002195 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 break;
jeffhaobdb76512011-09-07 11:43:16 -07002197 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002199 break;
2200
jeffhaobdb76512011-09-07 11:43:16 -07002201 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002203 break;
jeffhaobdb76512011-09-07 11:43:16 -07002204 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002205 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002206 break;
jeffhaobdb76512011-09-07 11:43:16 -07002207 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002208 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002209 break;
jeffhaobdb76512011-09-07 11:43:16 -07002210 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002211 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 break;
2213 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002214 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
2216 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 break;
2222
2223 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 break;
2226 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002228 break;
2229 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002231 break;
2232 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
2235 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002240 break;
2241 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244
2245 case Instruction::INVOKE_VIRTUAL:
2246 case Instruction::INVOKE_VIRTUAL_RANGE:
2247 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2250 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002251 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2252 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002253 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2254 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002255 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002256 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002258 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2259 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002260 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002261 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002262 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2263 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002264 return_type_class->CannotBeAssignedFromOtherTypes());
2265 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002266 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2267 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002268 }
2269 }
2270 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002272 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2273 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002274 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002275 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002276 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002277 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002278 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002279 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002280 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002283 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002286 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002287 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002288 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002289 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002290 const char* return_type_descriptor;
2291 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002292 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002293 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002295 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002296 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002297 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2298 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2299 } else {
2300 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002301 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002302 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002303 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2304 MethodHelper mh(h_called_method);
2305 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2306 if (return_type_class != nullptr) {
2307 return_type = &reg_types_.FromClass(return_type_descriptor,
2308 return_type_class,
2309 return_type_class->CannotBeAssignedFromOtherTypes());
2310 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002311 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2312 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002313 }
Ian Rogers46685432012-06-03 22:26:43 -07002314 }
2315 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002316 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 * Some additional checks when calling a constructor. We know from the invocation arg check
2318 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2319 * that to require that called_method->klass is the same as this->klass or this->super,
2320 * allowing the latter only if the "this" argument is the same as the "this" argument to
2321 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002322 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002323 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002324 if (this_type.IsConflict()) // failure.
2325 break;
jeffhaobdb76512011-09-07 11:43:16 -07002326
jeffhaob57e9522012-04-26 18:08:21 -07002327 /* no null refs allowed (?) */
2328 if (this_type.IsZero()) {
2329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2330 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002331 }
jeffhaob57e9522012-04-26 18:08:21 -07002332
2333 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002334 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002335 // TODO: re-enable constructor type verification
2336 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002337 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002338 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2339 // break;
2340 // }
jeffhaob57e9522012-04-26 18:08:21 -07002341
2342 /* arg must be an uninitialized reference */
2343 if (!this_type.IsUninitializedTypes()) {
2344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2345 << this_type;
2346 break;
2347 }
2348
2349 /*
2350 * Replace the uninitialized reference with an initialized one. We need to do this for all
2351 * registers that have the same object instance in them, not just the "this" register.
2352 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002353 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002354 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002355 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002356 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2357 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002358 }
2359 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002362 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002364 just_set_result = true;
2365 break;
2366 }
2367 case Instruction::INVOKE_STATIC:
2368 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002369 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002370 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002371 METHOD_STATIC,
2372 is_range,
2373 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002374 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002375 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002377 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2378 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002379 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002380 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002381 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002382 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002383 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002385 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 } else {
2387 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 just_set_result = true;
2390 }
2391 break;
jeffhaobdb76512011-09-07 11:43:16 -07002392 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002395 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002396 METHOD_INTERFACE,
2397 is_range,
2398 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002399 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002400 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002401 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2402 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2403 << PrettyMethod(abs_method) << "'";
2404 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002405 }
Ian Rogers0d604842012-04-16 14:50:24 -07002406 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002407 /* Get the type of the "this" arg, which should either be a sub-interface of called
2408 * interface or Object (see comments in RegType::JoinClass).
2409 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002411 if (this_type.IsZero()) {
2412 /* null pointer always passes (and always fails at runtime) */
2413 } else {
2414 if (this_type.IsUninitializedTypes()) {
2415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2416 << this_type;
2417 break;
2418 }
2419 // In the past we have tried to assert that "called_interface" is assignable
2420 // from "this_type.GetClass()", however, as we do an imprecise Join
2421 // (RegType::JoinClass) we don't have full information on what interfaces are
2422 // implemented by "this_type". For example, two classes may implement the same
2423 // interfaces and have a common parent that doesn't implement the interface. The
2424 // join will set "this_type" to the parent class and a test that this implements
2425 // the interface will incorrectly fail.
2426 }
2427 /*
2428 * We don't have an object instance, so we can't find the concrete method. However, all of
2429 * the type information is in the abstract method, so we're good.
2430 */
2431 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002433 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002434 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2435 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2436 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2437 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002438 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002439 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002440 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002441 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002442 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002443 } else {
2444 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002446 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 case Instruction::NEG_INT:
2450 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::NEG_LONG:
2454 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002466 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002467 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002474 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002477 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002481 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002482 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002485 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002493 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002496 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002497 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002500 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002501 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002504 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002505 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002509 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002512 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002515 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002516 break;
2517 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002518 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002519 break;
2520
2521 case Instruction::ADD_INT:
2522 case Instruction::SUB_INT:
2523 case Instruction::MUL_INT:
2524 case Instruction::REM_INT:
2525 case Instruction::DIV_INT:
2526 case Instruction::SHL_INT:
2527 case Instruction::SHR_INT:
2528 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002529 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002530 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::AND_INT:
2533 case Instruction::OR_INT:
2534 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002535 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002536 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::ADD_LONG:
2539 case Instruction::SUB_LONG:
2540 case Instruction::MUL_LONG:
2541 case Instruction::DIV_LONG:
2542 case Instruction::REM_LONG:
2543 case Instruction::AND_LONG:
2544 case Instruction::OR_LONG:
2545 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002546 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002547 reg_types_.LongLo(), reg_types_.LongHi(),
2548 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550 case Instruction::SHL_LONG:
2551 case Instruction::SHR_LONG:
2552 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002555 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::ADD_FLOAT:
2558 case Instruction::SUB_FLOAT:
2559 case Instruction::MUL_FLOAT:
2560 case Instruction::DIV_FLOAT:
2561 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002562 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2563 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
2565 case Instruction::ADD_DOUBLE:
2566 case Instruction::SUB_DOUBLE:
2567 case Instruction::MUL_DOUBLE:
2568 case Instruction::DIV_DOUBLE:
2569 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002570 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002571 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2572 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
2574 case Instruction::ADD_INT_2ADDR:
2575 case Instruction::SUB_INT_2ADDR:
2576 case Instruction::MUL_INT_2ADDR:
2577 case Instruction::REM_INT_2ADDR:
2578 case Instruction::SHL_INT_2ADDR:
2579 case Instruction::SHR_INT_2ADDR:
2580 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2582 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::AND_INT_2ADDR:
2585 case Instruction::OR_INT_2ADDR:
2586 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2588 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2592 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::ADD_LONG_2ADDR:
2595 case Instruction::SUB_LONG_2ADDR:
2596 case Instruction::MUL_LONG_2ADDR:
2597 case Instruction::DIV_LONG_2ADDR:
2598 case Instruction::REM_LONG_2ADDR:
2599 case Instruction::AND_LONG_2ADDR:
2600 case Instruction::OR_LONG_2ADDR:
2601 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002603 reg_types_.LongLo(), reg_types_.LongHi(),
2604 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002605 break;
2606 case Instruction::SHL_LONG_2ADDR:
2607 case Instruction::SHR_LONG_2ADDR:
2608 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002610 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::ADD_FLOAT_2ADDR:
2613 case Instruction::SUB_FLOAT_2ADDR:
2614 case Instruction::MUL_FLOAT_2ADDR:
2615 case Instruction::DIV_FLOAT_2ADDR:
2616 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002617 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2618 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002619 break;
2620 case Instruction::ADD_DOUBLE_2ADDR:
2621 case Instruction::SUB_DOUBLE_2ADDR:
2622 case Instruction::MUL_DOUBLE_2ADDR:
2623 case Instruction::DIV_DOUBLE_2ADDR:
2624 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002626 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2627 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002628 break;
2629 case Instruction::ADD_INT_LIT16:
2630 case Instruction::RSUB_INT:
2631 case Instruction::MUL_INT_LIT16:
2632 case Instruction::DIV_INT_LIT16:
2633 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002634 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2635 true);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::AND_INT_LIT16:
2638 case Instruction::OR_INT_LIT16:
2639 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002640 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2641 true);
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643 case Instruction::ADD_INT_LIT8:
2644 case Instruction::RSUB_INT_LIT8:
2645 case Instruction::MUL_INT_LIT8:
2646 case Instruction::DIV_INT_LIT8:
2647 case Instruction::REM_INT_LIT8:
2648 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002649 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002650 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002651 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2652 false);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::AND_INT_LIT8:
2655 case Instruction::OR_INT_LIT8:
2656 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002657 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2658 false);
jeffhaobdb76512011-09-07 11:43:16 -07002659 break;
2660
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002661 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002662 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002663 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2665 }
2666 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002667 // Note: the following instructions encode offsets derived from class linking.
2668 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2669 // meaning if the class linking and resolution were successful.
2670 case Instruction::IGET_QUICK:
2671 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2672 break;
2673 case Instruction::IGET_WIDE_QUICK:
2674 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2675 break;
2676 case Instruction::IGET_OBJECT_QUICK:
2677 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2678 break;
2679 case Instruction::IPUT_QUICK:
2680 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2681 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002682 case Instruction::IPUT_BOOLEAN_QUICK:
2683 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2684 break;
2685 case Instruction::IPUT_BYTE_QUICK:
2686 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2687 break;
2688 case Instruction::IPUT_CHAR_QUICK:
2689 VerifyIPutQuick(inst, reg_types_.Char(), true);
2690 break;
2691 case Instruction::IPUT_SHORT_QUICK:
2692 VerifyIPutQuick(inst, reg_types_.Short(), true);
2693 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002694 case Instruction::IPUT_WIDE_QUICK:
2695 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2696 break;
2697 case Instruction::IPUT_OBJECT_QUICK:
2698 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2699 break;
2700 case Instruction::INVOKE_VIRTUAL_QUICK:
2701 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2702 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002703 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002705 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002706 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002707 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002709 } else {
2710 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2711 }
2712 just_set_result = true;
2713 }
2714 break;
2715 }
2716
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002718 case Instruction::UNUSED_3E:
2719 case Instruction::UNUSED_3F:
2720 case Instruction::UNUSED_40:
2721 case Instruction::UNUSED_41:
2722 case Instruction::UNUSED_42:
2723 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002724 case Instruction::UNUSED_79:
2725 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002727 case Instruction::UNUSED_F0:
2728 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::UNUSED_F2:
2730 case Instruction::UNUSED_F3:
2731 case Instruction::UNUSED_F4:
2732 case Instruction::UNUSED_F5:
2733 case Instruction::UNUSED_F6:
2734 case Instruction::UNUSED_F7:
2735 case Instruction::UNUSED_F8:
2736 case Instruction::UNUSED_F9:
2737 case Instruction::UNUSED_FA:
2738 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002739 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002740 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002741 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002742 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002744 break;
2745
2746 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002747 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002748 * complain if an instruction is missing (which is desirable).
2749 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002750 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002751
Ian Rogersad0b3a32012-04-16 14:50:24 -07002752 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002753 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002754 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002755 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002756 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002757 /* immediate failure, reject class */
2758 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2759 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002760 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002761 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002762 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002763 }
jeffhaobdb76512011-09-07 11:43:16 -07002764 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002765 * If we didn't just set the result register, clear it out. This ensures that you can only use
2766 * "move-result" immediately after the result is set. (We could check this statically, but it's
2767 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002768 */
2769 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002770 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002771 }
2772
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002773
jeffhaobdb76512011-09-07 11:43:16 -07002774
2775 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002776 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002777 *
2778 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002779 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002780 * somebody could get a reference field, check it for zero, and if the
2781 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002782 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002783 * that, and will reject the code.
2784 *
2785 * TODO: avoid re-fetching the branch target
2786 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002787 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002788 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002790 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002792 return false;
2793 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002794 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002795 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002796 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 }
jeffhaobdb76512011-09-07 11:43:16 -07002798 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002799 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002800 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002801 return false;
2802 }
2803 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002804 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002805 return false;
2806 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 }
jeffhaobdb76512011-09-07 11:43:16 -07002808 }
2809
2810 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002811 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002812 *
2813 * We've already verified that the table is structurally sound, so we
2814 * just need to walk through and tag the targets.
2815 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002816 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002817 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2818 const uint16_t* switch_insns = insns + offset_to_switch;
2819 int switch_count = switch_insns[1];
2820 int offset_to_targets, targ;
2821
2822 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2823 /* 0 = sig, 1 = count, 2/3 = first key */
2824 offset_to_targets = 4;
2825 } else {
2826 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002827 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002828 offset_to_targets = 2 + 2 * switch_count;
2829 }
2830
2831 /* verify each switch target */
2832 for (targ = 0; targ < switch_count; targ++) {
2833 int offset;
2834 uint32_t abs_offset;
2835
2836 /* offsets are 32-bit, and only partly endian-swapped */
2837 offset = switch_insns[offset_to_targets + targ * 2] |
2838 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 abs_offset = work_insn_idx_ + offset;
2840 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002841 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002842 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002844 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002845 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002846 }
jeffhaobdb76512011-09-07 11:43:16 -07002847 }
2848 }
2849
2850 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2852 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002853 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002854 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002855 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002856 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002857
Andreas Gampef91baf12014-07-18 15:41:00 -07002858 // Need the linker to try and resolve the handled class to check if it's Throwable.
2859 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2860
Ian Rogers0571d352011-11-03 19:51:38 -07002861 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002862 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2863 if (handler_type_idx == DexFile::kDexNoIndex16) {
2864 has_catch_all_handler = true;
2865 } else {
2866 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002867 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2868 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002869 if (klass != nullptr) {
2870 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2871 has_catch_all_handler = true;
2872 }
2873 } else {
2874 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002875 DCHECK(self_->IsExceptionPending());
2876 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002877 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 }
jeffhaobdb76512011-09-07 11:43:16 -07002879 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2881 * "work_regs", because at runtime the exception will be thrown before the instruction
2882 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002883 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002884 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002885 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002886 }
jeffhaobdb76512011-09-07 11:43:16 -07002887 }
2888
2889 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002890 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2891 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002892 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002893 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002894 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 * The state in work_line reflects the post-execution state. If the current instruction is a
2896 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002897 * it will do so before grabbing the lock).
2898 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002899 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002900 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002902 return false;
2903 }
2904 }
2905 }
2906
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002907 /* Handle "continue". Tag the next consecutive instruction.
2908 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2909 * because it changes work_line_ when performing peephole optimization
2910 * and this change should not be used in those cases.
2911 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002912 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002913 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2914 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002915 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2917 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002918 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002919 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2920 // next instruction isn't one.
2921 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2922 return false;
2923 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002924 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002925 // Make workline consistent with fallthrough computed from peephole optimization.
2926 work_line_->CopyFromLine(fallthrough_line.get());
2927 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002928 if (insn_flags_[next_insn_idx].IsReturn()) {
2929 // For returns we only care about the operand to the return, all other registers are dead.
2930 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2931 Instruction::Code opcode = ret_inst->Opcode();
2932 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002933 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002934 } else {
2935 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002937 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002938 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002939 }
2940 }
2941 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002942 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002943 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002944 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2945 // needed. If the merge changes the state of the registers then the work line will be
2946 // updated.
2947 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002948 return false;
2949 }
2950 } else {
2951 /*
2952 * We're not recording register data for the next instruction, so we don't know what the
2953 * prior state was. We have to assume that something has changed and re-evaluate it.
2954 */
2955 insn_flags_[next_insn_idx].SetChanged();
2956 }
2957 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002958
jeffhaod1f0fde2011-09-08 17:25:33 -07002959 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002960 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002961 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 return false;
2963 }
jeffhaobdb76512011-09-07 11:43:16 -07002964 }
2965
2966 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002967 * Update start_guess. Advance to the next instruction of that's
2968 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002969 * neither of those exists we're in a return or throw; leave start_guess
2970 * alone and let the caller sort it out.
2971 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002972 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2974 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002975 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002976 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002977 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002978 }
2979
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2981 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002982
2983 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002984} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002985
Ian Rogersd8f69b02014-09-10 21:43:52 +00002986const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002987 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002988 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002989 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002991 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2992 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002993 if (result.IsConflict()) {
2994 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2995 << "' in " << referrer;
2996 return result;
2997 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002999 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003000 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003001 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003002 // check at runtime if access is allowed and so pass here. If result is
3003 // primitive, skip the access check.
3004 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3005 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003006 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003008 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003010}
3011
Ian Rogersd8f69b02014-09-10 21:43:52 +00003012const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003015 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3017 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003018 CatchHandlerIterator iterator(handlers_ptr);
3019 for (; iterator.HasNext(); iterator.Next()) {
3020 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3021 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003022 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003024 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003025 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003026 if (exception.IsUnresolvedTypes()) {
3027 // We don't know enough about the type. Fail here and let runtime handle it.
3028 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3029 return exception;
3030 } else {
3031 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3032 return reg_types_.Conflict();
3033 }
Jeff Haob878f212014-04-24 16:25:36 -07003034 } else if (common_super == nullptr) {
3035 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003036 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003037 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003039 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07003040 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 }
3042 }
3043 }
3044 }
Ian Rogers0571d352011-11-03 19:51:38 -07003045 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 }
3047 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003050 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003051 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003052 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003053 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003054}
3055
Brian Carlstromea46f952013-07-30 01:26:50 -07003056mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003057 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003059 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003060 if (klass_type.IsConflict()) {
3061 std::string append(" in attempt to access method ");
3062 append += dex_file_->GetMethodName(method_id);
3063 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003064 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003065 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003066 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003068 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003069 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003070 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003071 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003072 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003073 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003074 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003075
3076 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003078 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 res_method = klass->FindInterfaceMethod(name, signature);
3080 } else {
3081 res_method = klass->FindVirtualMethod(name, signature);
3082 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003083 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003084 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003086 // If a virtual or interface method wasn't found with the expected type, look in
3087 // the direct methods. This can happen when the wrong invoke type is used or when
3088 // a class has changed, and will be flagged as an error in later checks.
3089 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3090 res_method = klass->FindDirectMethod(name, signature);
3091 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003093 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3094 << PrettyDescriptor(klass) << "." << name
3095 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003096 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003097 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 }
3099 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3101 // enforce them here.
3102 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3104 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003105 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003106 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003107 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003108 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3110 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003112 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003113 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003114 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003115 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003116 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003117 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003118 }
jeffhaode0d9c92012-02-27 13:58:13 -08003119 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3120 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3122 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003123 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003124 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003125 // Check that interface methods match interface classes.
3126 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3127 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3128 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003130 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3131 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3132 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003133 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003134 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003135 // See if the method type implied by the invoke instruction matches the access flags for the
3136 // target method.
3137 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3138 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3139 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3140 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003141 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3142 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003145 return res_method;
3146}
3147
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003148template <class T>
3149mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3150 MethodType method_type,
3151 bool is_range,
3152 mirror::ArtMethod* res_method) {
3153 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3154 // match the call to the signature. Also, we might be calling through an abstract method
3155 // definition (which doesn't have register count values).
3156 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3157 /* caught by static verifier */
3158 DCHECK(is_range || expected_args <= 5);
3159 if (expected_args > code_item_->outs_size_) {
3160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3161 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3162 return nullptr;
3163 }
3164
3165 uint32_t arg[5];
3166 if (!is_range) {
3167 inst->GetVarArgs(arg);
3168 }
3169 uint32_t sig_registers = 0;
3170
3171 /*
3172 * Check the "this" argument, which must be an instance of the class that declared the method.
3173 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3174 * rigorous check here (which is okay since we have to do it at runtime).
3175 */
3176 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003177 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003178 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3179 CHECK(have_pending_hard_failure_);
3180 return nullptr;
3181 }
3182 if (actual_arg_type.IsUninitializedReference()) {
3183 if (res_method) {
3184 if (!res_method->IsConstructor()) {
3185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3186 return nullptr;
3187 }
3188 } else {
3189 // Check whether the name of the called method is "<init>"
3190 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003191 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3193 return nullptr;
3194 }
3195 }
3196 }
3197 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003198 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003199 if (res_method != nullptr) {
3200 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003201 std::string temp;
3202 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003203 klass->CannotBeAssignedFromOtherTypes());
3204 } else {
3205 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3206 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003207 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003208 dex_file_->StringByTypeIdx(class_idx),
3209 false);
3210 }
3211 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3212 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3213 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3214 << "' not instance of '" << *res_method_class << "'";
3215 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3216 // the compiler.
3217 if (have_pending_hard_failure_) {
3218 return nullptr;
3219 }
3220 }
3221 }
3222 sig_registers = 1;
3223 }
3224
3225 for ( ; it->HasNext(); it->Next()) {
3226 if (sig_registers >= expected_args) {
3227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3228 " arguments, found " << sig_registers << " or more.";
3229 return nullptr;
3230 }
3231
3232 const char* param_descriptor = it->GetDescriptor();
3233
3234 if (param_descriptor == nullptr) {
3235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3236 "component";
3237 return nullptr;
3238 }
3239
Ian Rogersd8f69b02014-09-10 21:43:52 +00003240 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003241 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3242 arg[sig_registers];
3243 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003244 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003245 if (!src_type.IsIntegralTypes()) {
3246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3247 << " but expected " << reg_type;
3248 return res_method;
3249 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003250 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003251 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3252 // compiler.
3253 if (have_pending_hard_failure_) {
3254 return res_method;
3255 }
3256 }
3257 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3258 }
3259 if (expected_args != sig_registers) {
3260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3261 " arguments, found " << sig_registers;
3262 return nullptr;
3263 }
3264 return res_method;
3265}
3266
3267void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3268 MethodType method_type,
3269 bool is_range) {
3270 // As the method may not have been resolved, make this static check against what we expect.
3271 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3272 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3273 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3274 DexFileParameterIterator it(*dex_file_,
3275 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3276 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3277 nullptr);
3278}
3279
3280class MethodParamListDescriptorIterator {
3281 public:
3282 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3283 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3284 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3285 }
3286
3287 bool HasNext() {
3288 return pos_ < params_size_;
3289 }
3290
3291 void Next() {
3292 ++pos_;
3293 }
3294
3295 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3296 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3297 }
3298
3299 private:
3300 mirror::ArtMethod* res_method_;
3301 size_t pos_;
3302 const DexFile::TypeList* params_;
3303 const size_t params_size_;
3304};
3305
Brian Carlstromea46f952013-07-30 01:26:50 -07003306mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003307 MethodType method_type,
3308 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003309 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003310 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3311 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003312 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003313
Brian Carlstromea46f952013-07-30 01:26:50 -07003314 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003315 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003316 // Check what we can statically.
3317 if (!have_pending_hard_failure_) {
3318 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3319 }
3320 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003321 }
3322
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3324 // has a vtable entry for the target method.
3325 if (is_super) {
3326 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003327 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003328 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003329 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003330 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003331 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003332 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003333 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003334 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003335 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003336 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003337 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003338 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003339 << "." << res_method->GetName()
3340 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003341 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003342 }
3343 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003344
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003345 // Process the target method's signature. This signature may or may not
3346 MethodParamListDescriptorIterator it(res_method);
3347 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3348 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003349}
3350
Brian Carlstromea46f952013-07-30 01:26:50 -07003351mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003352 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003353 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3354 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003355 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003356 if (!actual_arg_type.HasClass()) {
3357 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003358 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003359 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003360 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003361 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003362 if (klass->IsInterface()) {
3363 // Derive Object.class from Class.class.getSuperclass().
3364 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3365 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003366 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003367 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003368 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003369 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003370 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003371 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003372 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
Andreas Gampe5ca20542014-09-15 19:02:30 -07003373 << PrettyDescriptor(klass) << " in method "
3374 << PrettyMethod(dex_method_idx_, *dex_file_, true);
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003375 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Ian Rogers7b078e82014-09-10 14:44:24 -07003376 CHECK(!self_->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003377 return res_method;
3378}
3379
Brian Carlstromea46f952013-07-30 01:26:50 -07003380mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003381 bool is_range) {
3382 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003383 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003384 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003385 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003387 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388 }
3389 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3390
3391 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3392 // match the call to the signature. Also, we might be calling through an abstract method
3393 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003394 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003395 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003396 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003397 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003398 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3399 /* caught by static verifier */
3400 DCHECK(is_range || expected_args <= 5);
3401 if (expected_args > code_item_->outs_size_) {
3402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3403 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003404 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003405 }
3406
3407 /*
3408 * Check the "this" argument, which must be an instance of the class that declared the method.
3409 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3410 * rigorous check here (which is okay since we have to do it at runtime).
3411 */
3412 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003414 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003415 }
3416 if (!actual_arg_type.IsZero()) {
3417 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003418 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003419 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003420 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003421 klass->CannotBeAssignedFromOtherTypes());
3422 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003423 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3424 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003425 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003427 }
3428 }
3429 /*
3430 * Process the target method's signature. This signature may or may not
3431 * have been verified, so we can't assume it's properly formed.
3432 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003433 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003434 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003435 uint32_t arg[5];
3436 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003437 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003438 }
3439 size_t actual_args = 1;
3440 for (size_t param_index = 0; param_index < params_size; param_index++) {
3441 if (actual_args >= expected_args) {
3442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003443 << "'. Expected " << expected_args
3444 << " arguments, processing argument " << actual_args
3445 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003446 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003447 }
3448 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003449 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003452 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003453 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003454 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003455 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003456 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003457 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003458 return res_method;
3459 }
3460 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3461 }
3462 if (actual_args != expected_args) {
3463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3464 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003465 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003466 } else {
3467 return res_method;
3468 }
3469}
3470
Ian Rogers62342ec2013-06-11 10:26:37 -07003471void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003472 uint32_t type_idx;
3473 if (!is_filled) {
3474 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3475 type_idx = inst->VRegC_22c();
3476 } else if (!is_range) {
3477 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3478 type_idx = inst->VRegB_35c();
3479 } else {
3480 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3481 type_idx = inst->VRegB_3rc();
3482 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003483 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003484 if (res_type.IsConflict()) { // bad class
3485 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003486 } else {
3487 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3488 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003490 } else if (!is_filled) {
3491 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003492 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003493 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003494 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003495 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003496 } else {
3497 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3498 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003499 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003500 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3501 uint32_t arg[5];
3502 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003503 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003505 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003506 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003507 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3508 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003509 return;
3510 }
3511 }
3512 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003513 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003514 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003515 }
3516 }
3517}
3518
Sebastien Hertz5243e912013-05-21 10:55:07 +02003519void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003520 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003521 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003525 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003526 if (array_type.IsZero()) {
3527 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3528 // instruction type. TODO: have a proper notion of bottom here.
3529 if (!is_primitive || insn_type.IsCategory1Types()) {
3530 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003531 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003532 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003533 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003534 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3535 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003536 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003537 }
jeffhaofc3144e2012-02-01 17:21:15 -08003538 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003540 } else {
3541 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003542 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003543 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003545 << " source for aget-object";
3546 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003548 << " source for category 1 aget";
3549 } else if (is_primitive && !insn_type.Equals(component_type) &&
3550 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003551 (insn_type.IsLong() && component_type.IsDouble()))) {
3552 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3553 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003554 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003555 // Use knowledge of the field type which is stronger than the type inferred from the
3556 // instruction, which can't differentiate object types and ints from floats, longs from
3557 // doubles.
3558 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003559 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003560 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003562 component_type.HighHalf(&reg_types_));
3563 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003564 }
3565 }
3566 }
3567}
3568
Ian Rogersd8f69b02014-09-10 21:43:52 +00003569void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003570 const uint32_t vregA) {
3571 // Primitive assignability rules are weaker than regular assignability rules.
3572 bool instruction_compatible;
3573 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003574 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003575 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003576 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003577 value_compatible = value_type.IsIntegralTypes();
3578 } else if (target_type.IsFloat()) {
3579 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3580 value_compatible = value_type.IsFloatTypes();
3581 } else if (target_type.IsLong()) {
3582 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003583 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3584 // as target_type depends on the resolved type of the field.
3585 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003586 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003587 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3588 } else {
3589 value_compatible = false;
3590 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003591 } else if (target_type.IsDouble()) {
3592 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003593 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3594 // as target_type depends on the resolved type of the field.
3595 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003596 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003597 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3598 } else {
3599 value_compatible = false;
3600 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003601 } else {
3602 instruction_compatible = false; // reference with primitive store
3603 value_compatible = false; // unused
3604 }
3605 if (!instruction_compatible) {
3606 // This is a global failure rather than a class change failure as the instructions and
3607 // the descriptors for the type should have been consistent within the same file at
3608 // compile time.
3609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3610 << "' but expected type '" << target_type << "'";
3611 return;
3612 }
3613 if (!value_compatible) {
3614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3615 << " of type " << value_type << " but expected " << target_type << " for put";
3616 return;
3617 }
3618}
3619
Sebastien Hertz5243e912013-05-21 10:55:07 +02003620void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003621 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003622 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003626 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003627 if (array_type.IsZero()) {
3628 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3629 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003630 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003632 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003633 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003634 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003635 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003636 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003637 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003638 if (!component_type.IsReferenceTypes()) {
3639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3640 << " source for aput-object";
3641 } else {
3642 // The instruction agrees with the type of array, confirm the value to be stored does too
3643 // Note: we use the instruction type (rather than the component type) for aput-object as
3644 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003646 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 }
3648 }
3649 }
3650}
3651
Brian Carlstromea46f952013-07-30 01:26:50 -07003652mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003653 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3654 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003655 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003656 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003657 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3658 field_idx, dex_file_->GetFieldName(field_id),
3659 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003660 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003661 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003662 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003663 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003664 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003665 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003666 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3667 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003668 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003669 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003670 << dex_file_->GetFieldName(field_id) << ") in "
3671 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003672 DCHECK(self_->IsExceptionPending());
3673 self_->ClearException();
3674 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003675 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3676 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003677 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003678 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003679 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003680 } else if (!field->IsStatic()) {
3681 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003682 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003684 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003685}
3686
Ian Rogersd8f69b02014-09-10 21:43:52 +00003687mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003688 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3689 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003690 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003691 if (klass_type.IsConflict()) {
3692 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3693 field_idx, dex_file_->GetFieldName(field_id),
3694 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003696 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003697 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003698 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003699 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003700 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003701 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3702 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003703 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003704 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003705 << dex_file_->GetFieldName(field_id) << ") in "
3706 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003707 DCHECK(self_->IsExceptionPending());
3708 self_->ClearException();
3709 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003710 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3711 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003713 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003714 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 } else if (field->IsStatic()) {
3716 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3717 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003718 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003719 } else if (obj_type.IsZero()) {
3720 // Cannot infer and check type, however, access will cause null pointer exception
3721 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003722 } else if (!obj_type.IsReferenceTypes()) {
3723 // Trying to read a field from something that isn't a reference
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3725 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003727 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003728 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003729 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003730 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003731 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003732 if (obj_type.IsUninitializedTypes() &&
3733 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3734 !field_klass.Equals(GetDeclaringClass()))) {
3735 // Field accesses through uninitialized references are only allowable for constructors where
3736 // the field is declared in this class
3737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003738 << " of a not fully initialized object within the context"
3739 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003740 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003741 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3742 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3743 // of C1. For resolution to occur the declared class of the field must be compatible with
3744 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3745 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3746 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003748 } else {
3749 return field;
3750 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 }
3752}
3753
Ian Rogersd8f69b02014-09-10 21:43:52 +00003754void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003755 bool is_primitive, bool is_static) {
3756 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003757 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003758 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003759 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003760 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003761 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003762 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003763 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003764 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003766 mirror::Class* field_type_class;
3767 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003768 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003769 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3770 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3771 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003772 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003773 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003774 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003775 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3777 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003778 }
Ian Rogers0d604842012-04-16 14:50:24 -07003779 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003780 if (field_type == nullptr) {
3781 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3782 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003783 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003784 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003785 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003786 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003787 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003788 if (field_type->Equals(insn_type) ||
3789 (field_type->IsFloat() && insn_type.IsInteger()) ||
3790 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003791 // expected that read is of the correct primitive type or that int reads are reading
3792 // floats or long reads are reading doubles
3793 } else {
3794 // This is a global failure rather than a class change failure as the instructions and
3795 // the descriptors for the type should have been consistent within the same file at
3796 // compile time
3797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3798 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003799 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003800 return;
3801 }
3802 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003803 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003804 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3805 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003806 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003807 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003808 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003809 return;
3810 }
3811 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003812 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003813 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003814 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003815 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003816 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003817}
3818
Ian Rogersd8f69b02014-09-10 21:43:52 +00003819void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003820 bool is_primitive, bool is_static) {
3821 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003822 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003823 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003824 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003825 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003826 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003827 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003828 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003829 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003830 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3832 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3833 << " from other class " << GetDeclaringClass();
3834 return;
3835 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003836 mirror::Class* field_type_class;
3837 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003838 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003839 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3840 FieldHelper fh(h_field);
3841 field_type_class = fh.GetType(can_load_classes_);
3842 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003843 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003844 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003845 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003846 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3848 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003849 }
3850 }
3851 if (field_type == nullptr) {
3852 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3853 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003854 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003855 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003856 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003857 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003858 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003859 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003860 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003861 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003862 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3863 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003864 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003865 << "' in put-object";
3866 return;
3867 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003869 }
3870}
3871
Brian Carlstromea46f952013-07-30 01:26:50 -07003872mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003873 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003874 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3875 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3876 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3877 inst->Opcode() == Instruction::IPUT_QUICK ||
3878 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
Hiroshi Yamauchi5f09be92014-09-26 10:43:59 -07003879 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK ||
3880 inst->Opcode() == Instruction::IPUT_BOOLEAN_QUICK ||
3881 inst->Opcode() == Instruction::IPUT_BYTE_QUICK ||
3882 inst->Opcode() == Instruction::IPUT_CHAR_QUICK ||
3883 inst->Opcode() == Instruction::IPUT_SHORT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003885 if (!object_type.HasClass()) {
3886 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3887 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003888 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003889 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003890 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3891 field_offset);
3892 if (f == nullptr) {
3893 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3894 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3895 }
3896 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003897}
3898
Ian Rogersd8f69b02014-09-10 21:43:52 +00003899void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003900 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003901 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003902 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003903 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3905 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003906 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003907 mirror::Class* field_type_class;
3908 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003909 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003910 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3911 FieldHelper fh(h_field);
3912 field_type_class = fh.GetType(can_load_classes_);
3913 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003914 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003915 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003916 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003917 field_type_class->CannotBeAssignedFromOtherTypes());
3918 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003919 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3920 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003921 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003922 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003923 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003924 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003925 const uint32_t vregA = inst->VRegA_22c();
3926 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003927 if (field_type->Equals(insn_type) ||
3928 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3929 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003930 // expected that read is of the correct primitive type or that int reads are reading
3931 // floats or long reads are reading doubles
3932 } else {
3933 // This is a global failure rather than a class change failure as the instructions and
3934 // the descriptors for the type should have been consistent within the same file at
3935 // compile time
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003937 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003938 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003939 return;
3940 }
3941 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003942 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003943 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003944 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003945 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003946 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003947 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003948 return;
3949 }
3950 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003951 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003953 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003954 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003955 }
3956}
3957
Ian Rogersd8f69b02014-09-10 21:43:52 +00003958void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003959 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003960 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003961 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3964 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003965 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003966 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003967 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003968 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003970 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3971 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003972 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003973 return;
3974 }
3975 }
3976 const uint32_t vregA = inst->VRegA_22c();
3977 if (is_primitive) {
3978 // Primitive field assignability rules are weaker than regular assignability rules
3979 bool instruction_compatible;
3980 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003981 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003982 if (field_type.IsIntegralTypes()) {
3983 instruction_compatible = insn_type.IsIntegralTypes();
3984 value_compatible = value_type.IsIntegralTypes();
3985 } else if (field_type.IsFloat()) {
3986 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3987 value_compatible = value_type.IsFloatTypes();
3988 } else if (field_type.IsLong()) {
3989 instruction_compatible = insn_type.IsLong();
3990 value_compatible = value_type.IsLongTypes();
3991 } else if (field_type.IsDouble()) {
3992 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3993 value_compatible = value_type.IsDoubleTypes();
3994 } else {
3995 instruction_compatible = false; // reference field with primitive store
3996 value_compatible = false; // unused
3997 }
3998 if (!instruction_compatible) {
3999 // This is a global failure rather than a class change failure as the instructions and
4000 // the descriptors for the type should have been consistent within the same file at
4001 // compile time
4002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004003 << " to be of type '" << insn_type
4004 << "' but found type '" << field_type
4005 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 return;
4007 }
4008 if (!value_compatible) {
4009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4010 << " of type " << value_type
4011 << " but expected " << field_type
4012 << " for store to " << PrettyField(field) << " in put";
4013 return;
4014 }
4015 } else {
4016 if (!insn_type.IsAssignableFrom(field_type)) {
4017 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004018 << " to be compatible with type '" << insn_type
4019 << "' but found type '" << field_type
4020 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004021 return;
4022 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004023 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004024 }
4025}
4026
Ian Rogers776ac1f2012-04-13 23:36:36 -07004027bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004028 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004030 return false;
4031 }
4032 return true;
4033}
4034
Stephen Kyle9bc61992014-09-22 13:53:15 +01004035bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4036 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4037 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4039 return false;
4040 }
4041 return true;
4042}
4043
4044bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4045 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4046}
4047
Ian Rogersebbdd872014-07-07 23:53:08 -07004048bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4049 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004050 bool changed = true;
4051 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4052 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004053 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004054 * We haven't processed this instruction before, and we haven't touched the registers here, so
4055 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4056 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004057 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004058 if (!insn_flags_[next_insn].IsReturn()) {
4059 target_line->CopyFromLine(merge_line);
4060 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004061 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004062 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004063 return false;
4064 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004065 // For returns we only care about the operand to the return, all other registers are dead.
4066 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4067 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4068 Instruction::Code opcode = ret_inst->Opcode();
4069 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004070 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004071 } else {
4072 target_line->CopyFromLine(merge_line);
4073 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004074 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004075 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004076 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004077 }
4078 }
4079 }
jeffhaobdb76512011-09-07 11:43:16 -07004080 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004081 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004082 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004083 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004084 if (gDebugVerify) {
4085 copy->CopyFromLine(target_line);
4086 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004087 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004088 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004089 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004090 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004091 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004092 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004093 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004094 << copy->Dump(this) << " MERGE\n"
4095 << merge_line->Dump(this) << " ==\n"
4096 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004097 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004098 if (update_merge_line && changed) {
4099 merge_line->CopyFromLine(target_line);
4100 }
jeffhaobdb76512011-09-07 11:43:16 -07004101 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004102 if (changed) {
4103 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004104 }
4105 return true;
4106}
4107
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004108InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004109 return &insn_flags_[work_insn_idx_];
4110}
4111
Ian Rogersd8f69b02014-09-10 21:43:52 +00004112const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004113 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004114 if (mirror_method_.Get() != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 StackHandleScope<1> hs(self_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004116 mirror::Class* return_type_class =
4117 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004118 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004119 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4120 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004121 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004122 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004123 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4124 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004125 }
4126 }
4127 if (return_type_ == nullptr) {
4128 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4129 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4130 uint16_t return_type_idx = proto_id.return_type_idx_;
4131 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004132 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004133 }
4134 }
4135 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004136}
4137
Ian Rogersd8f69b02014-09-10 21:43:52 +00004138const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004139 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004140 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004141 const char* descriptor
4142 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004143 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004144 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004145 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4146 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004147 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004148 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004149 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004150 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004151 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004152}
4153
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004154std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4155 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004156 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004157 std::vector<int32_t> result;
4158 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004159 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004160 if (type.IsConstant()) {
4161 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004162 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4163 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004164 } else if (type.IsConstantLo()) {
4165 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004166 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4167 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004168 } else if (type.IsConstantHi()) {
4169 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4171 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004172 } else if (type.IsIntegralTypes()) {
4173 result.push_back(kIntVReg);
4174 result.push_back(0);
4175 } else if (type.IsFloat()) {
4176 result.push_back(kFloatVReg);
4177 result.push_back(0);
4178 } else if (type.IsLong()) {
4179 result.push_back(kLongLoVReg);
4180 result.push_back(0);
4181 result.push_back(kLongHiVReg);
4182 result.push_back(0);
4183 ++i;
4184 } else if (type.IsDouble()) {
4185 result.push_back(kDoubleLoVReg);
4186 result.push_back(0);
4187 result.push_back(kDoubleHiVReg);
4188 result.push_back(0);
4189 ++i;
4190 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4191 result.push_back(kUndefined);
4192 result.push_back(0);
4193 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004194 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004195 result.push_back(kReferenceVReg);
4196 result.push_back(0);
4197 }
4198 }
4199 return result;
4200}
4201
Ian Rogersd8f69b02014-09-10 21:43:52 +00004202const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004203 if (precise) {
4204 // Precise constant type.
4205 return reg_types_.FromCat1Const(value, true);
4206 } else {
4207 // Imprecise constant type.
4208 if (value < -32768) {
4209 return reg_types_.IntConstant();
4210 } else if (value < -128) {
4211 return reg_types_.ShortConstant();
4212 } else if (value < 0) {
4213 return reg_types_.ByteConstant();
4214 } else if (value == 0) {
4215 return reg_types_.Zero();
4216 } else if (value == 1) {
4217 return reg_types_.One();
4218 } else if (value < 128) {
4219 return reg_types_.PosByteConstant();
4220 } else if (value < 32768) {
4221 return reg_types_.PosShortConstant();
4222 } else if (value < 65536) {
4223 return reg_types_.CharConstant();
4224 } else {
4225 return reg_types_.IntConstant();
4226 }
4227 }
4228}
4229
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004230void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004231 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004232}
4233
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004234void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004235 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004236}
jeffhaod1224c72012-02-29 13:43:08 -08004237
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004238void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4239 RegTypeCache::VisitStaticRoots(callback, arg);
4240}
4241
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004242void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4243 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004244}
4245
Ian Rogersd81871c2011-10-03 13:57:23 -07004246} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004247} // namespace art