blob: f28d4883b23c57f4275f4e429754012e27390d58 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "field_helper.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogerse5877a12014-07-16 12:06:35 -070033#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080046#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
48namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070049namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070051static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070052static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070053// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070054
Ian Rogers7b3ddd22013-02-21 15:19:52 -080055void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070056 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070057 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070058 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070059 register_lines_.reset(new RegisterLine*[insns_size]());
60 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070061 for (uint32_t i = 0; i < insns_size; i++) {
62 bool interesting = false;
63 switch (mode) {
64 case kTrackRegsAll:
65 interesting = flags[i].IsOpcode();
66 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070067 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070068 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070069 break;
70 case kTrackRegsBranches:
71 interesting = flags[i].IsBranchTarget();
72 break;
73 default:
74 break;
75 }
76 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070077 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
78 }
79 }
80}
81
82PcToRegisterLineTable::~PcToRegisterLineTable() {
83 for (size_t i = 0; i < size_; i++) {
84 delete register_lines_[i];
85 if (kIsDebugBuild) {
86 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070087 }
88 }
89}
90
Ian Rogers7b078e82014-09-10 14:44:24 -070091MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
92 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070093 bool allow_soft_failures,
94 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070095 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070096 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070097 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080098 bool early_failure = false;
99 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700100 const DexFile& dex_file = klass->GetDexFile();
101 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800102 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700103 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700104 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800105 early_failure = true;
106 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700107 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800108 early_failure = true;
109 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700110 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800111 early_failure = true;
112 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
113 }
114 if (early_failure) {
115 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
116 if (Runtime::Current()->IsCompiler()) {
117 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000118 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800119 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700120 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700121 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700122 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700123 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700125 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700126}
127
Ian Rogers7b078e82014-09-10 14:44:24 -0700128MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
129 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700130 Handle<mirror::DexCache> dex_cache,
131 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700132 const DexFile::ClassDef* class_def,
133 bool allow_soft_failures,
134 std::string* error) {
135 DCHECK(class_def != nullptr);
136 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700138 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700139 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 }
jeffhaof56197c2012-03-05 18:01:54 -0800141 ClassDataItemIterator it(*dex_file, class_data);
142 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
143 it.Next();
144 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700145 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700146 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700147 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700148 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800149 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800151 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700152 if (method_idx == previous_direct_method_idx) {
153 // smali can create dex files with two encoded_methods sharing the same method_idx
154 // http://code.google.com/p/smali/issues/detail?id=119
155 it.Next();
156 continue;
157 }
158 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700159 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700161 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
162 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700164 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700165 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700166 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700167 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700168 StackHandleScope<1> hs(self);
169 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 MethodVerifier::FailureKind result = VerifyMethod(self,
171 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700172 dex_file,
173 dex_cache,
174 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700176 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700177 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700178 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700179 allow_soft_failures,
180 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 if (result != kNoFailure) {
182 if (result == kHardFailure) {
183 hard_fail = true;
184 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700185 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700186 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700187 *error = "Verifier rejected class ";
188 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
189 *error += " due to bad method ";
190 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800193 }
194 it.Next();
195 }
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_virtual_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700208 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700209 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
210 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700211 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700212 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 StackHandleScope<1> hs(self);
217 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700225 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700226 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700227 allow_soft_failures,
228 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700244 if (error_count == 0) {
245 return kNoFailure;
246 } else {
247 return hard_fail ? kHardFailure : kSoftFailure;
248 }
jeffhaof56197c2012-03-05 18:01:54 -0800249}
250
Ian Rogers7b078e82014-09-10 14:44:24 -0700251MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700253 Handle<mirror::DexCache> dex_cache,
254 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700257 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700258 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700259 bool allow_soft_failures,
260 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700261 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700262 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700263
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700265 method_idx, method, method_access_flags, true, allow_soft_failures,
266 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700267 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700268 // Verification completed, however failures may be pending that didn't cause the verification
269 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700270 CHECK(!verifier.have_pending_hard_failure_);
271 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700272 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700273 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700274 << PrettyMethod(method_idx, *dex_file) << "\n");
275 }
Ian Rogersc8982582012-09-07 16:53:25 -0700276 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800277 }
278 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700280 CHECK_NE(verifier.failures_.size(), 0U);
281 CHECK(verifier.have_pending_hard_failure_);
282 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700283 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800284 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700285 std::cout << "\n" << verifier.info_messages_.str();
286 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800287 }
Ian Rogersc8982582012-09-07 16:53:25 -0700288 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800289 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700290 if (kTimeVerifyMethod) {
291 uint64_t duration_ns = NanoTime() - start_ns;
292 if (duration_ns > MsToNs(100)) {
293 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
294 << " took " << PrettyDuration(duration_ns);
295 }
Ian Rogersc8982582012-09-07 16:53:25 -0700296 }
297 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800298}
299
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700300MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700301 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700302 Handle<mirror::DexCache> dex_cache,
303 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700306 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800307 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700308 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
309 class_def, code_item, dex_method_idx, method,
310 method_access_flags, true, true, true, true);
311 verifier->Verify();
312 verifier->DumpFailures(os);
313 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700314 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
315 // and querying any info is dangerous/can abort.
316 if (verifier->have_pending_hard_failure_) {
317 delete verifier;
318 return nullptr;
319 } else {
320 verifier->Dump(os);
321 return verifier;
322 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800323}
324
Ian Rogers7b078e82014-09-10 14:44:24 -0700325MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700326 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
327 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700328 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700329 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700330 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 bool can_load_classes, bool allow_soft_failures,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700332 bool need_precise_constants, bool verify_to_dump)
Ian Rogers7b078e82014-09-10 14:44:24 -0700333 : self_(self),
334 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800335 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800336 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700337 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700338 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700339 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800340 dex_file_(dex_file),
341 dex_cache_(dex_cache),
342 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700343 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800344 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700345 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700346 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700347 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700348 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700349 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800350 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800351 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700352 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200353 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700354 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200355 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700356 has_virtual_or_interface_invokes_(false),
357 verify_to_dump_(verify_to_dump) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800358 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700359 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Mathieu Chartier590fee92013-09-13 13:46:47 -0700362MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800363 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700364 STLDeleteElements(&failure_messages_);
365}
366
Brian Carlstromea46f952013-07-30 01:26:50 -0700367void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700368 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700369 Thread* self = Thread::Current();
370 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
372 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700373 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700374 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700375 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
376 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700377 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700378 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700379 verifier.FindLocksAtDexPc();
380}
381
382void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700383 CHECK(monitor_enter_dex_pcs_ != nullptr);
384 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700385
386 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
387 // verification. In practice, the phase we want relies on data structures set up by all the
388 // earlier passes, so we just run the full method verification and bail out early when we've
389 // got what we wanted.
390 Verify();
391}
392
Brian Carlstromea46f952013-07-30 01:26:50 -0700393mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700394 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 Thread* self = Thread::Current();
396 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700397 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
398 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700399 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700400 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700401 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
402 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200403 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200404}
405
Brian Carlstromea46f952013-07-30 01:26:50 -0700406mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700407 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200408
409 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
410 // verification. In practice, the phase we want relies on data structures set up by all the
411 // earlier passes, so we just run the full method verification and bail out early when we've
412 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200413 bool success = Verify();
414 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700415 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200416 }
417 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700418 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700419 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200420 }
421 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
422 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423}
424
Brian Carlstromea46f952013-07-30 01:26:50 -0700425mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700427 Thread* self = Thread::Current();
428 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700429 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
430 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700431 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700432 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700433 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
434 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200435 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200436}
437
Brian Carlstromea46f952013-07-30 01:26:50 -0700438mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700439 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200440
441 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
442 // verification. In practice, the phase we want relies on data structures set up by all the
443 // earlier passes, so we just run the full method verification and bail out early when we've
444 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200445 bool success = Verify();
446 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700447 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200448 }
449 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700450 if (register_line == nullptr) {
451 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200452 }
453 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
454 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
455 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200456}
457
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700459 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700460 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700463 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700464 } else {
465 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700466 }
jeffhaobdb76512011-09-07 11:43:16 -0700467 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
469 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
471 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700472 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700473 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800475 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700476 // Run through the instructions and see if the width checks out.
477 bool result = ComputeWidthsAndCountOps();
478 // Flag instructions guarded by a "try" block and check exception handlers.
479 result = result && ScanTryCatchBlocks();
480 // Perform static instruction verification.
481 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000483 result = result && VerifyCodeFlow();
484 // Compute information for compiler.
485 if (result && Runtime::Current()->IsCompiler()) {
486 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
487 }
488 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700489}
490
Ian Rogers776ac1f2012-04-13 23:36:36 -0700491std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 switch (error) {
493 case VERIFY_ERROR_NO_CLASS:
494 case VERIFY_ERROR_NO_FIELD:
495 case VERIFY_ERROR_NO_METHOD:
496 case VERIFY_ERROR_ACCESS_CLASS:
497 case VERIFY_ERROR_ACCESS_FIELD:
498 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700499 case VERIFY_ERROR_INSTANTIATION:
500 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800501 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700502 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
503 // class change and instantiation errors into soft verification errors so that we re-verify
504 // at runtime. We may fail to find or to agree on access because of not yet available class
505 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
506 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
507 // paths" that dynamically perform the verification and cause the behavior to be that akin
508 // to an interpreter.
509 error = VERIFY_ERROR_BAD_CLASS_SOFT;
510 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700511 // If we fail again at runtime, mark that this instruction would throw and force this
512 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700513 have_pending_runtime_throw_failure_ = true;
514 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700515 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700516 // Indication that verification should be retried at runtime.
517 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700518 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519 have_pending_hard_failure_ = true;
520 }
521 break;
jeffhaod5347e02012-03-22 17:25:05 -0700522 // Hard verification failures at compile time will still fail at runtime, so the class is
523 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700524 case VERIFY_ERROR_BAD_CLASS_HARD: {
525 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700526 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000527 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800528 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 have_pending_hard_failure_ = true;
530 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800531 }
532 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700533 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700534 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700535 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700536 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537 failure_messages_.push_back(failure_message);
538 return *failure_message;
539}
540
Ian Rogers576ca0c2014-06-06 15:58:22 -0700541std::ostream& MethodVerifier::LogVerifyInfo() {
542 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
543 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
544}
545
Ian Rogersad0b3a32012-04-16 14:50:24 -0700546void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
547 size_t failure_num = failure_messages_.size();
548 DCHECK_NE(failure_num, 0U);
549 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
550 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700551 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700552 delete last_fail_message;
553}
554
555void MethodVerifier::AppendToLastFailMessage(std::string append) {
556 size_t failure_num = failure_messages_.size();
557 DCHECK_NE(failure_num, 0U);
558 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
559 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800560}
561
Ian Rogers776ac1f2012-04-13 23:36:36 -0700562bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 const uint16_t* insns = code_item_->insns_;
564 size_t insns_size = code_item_->insns_size_in_code_units_;
565 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700566 size_t new_instance_count = 0;
567 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700569
Ian Rogersd81871c2011-10-03 13:57:23 -0700570 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700571 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700572 switch (opcode) {
573 case Instruction::APUT_OBJECT:
574 case Instruction::CHECK_CAST:
575 has_check_casts_ = true;
576 break;
577 case Instruction::INVOKE_VIRTUAL:
578 case Instruction::INVOKE_VIRTUAL_RANGE:
579 case Instruction::INVOKE_INTERFACE:
580 case Instruction::INVOKE_INTERFACE_RANGE:
581 has_virtual_or_interface_invokes_ = true;
582 break;
583 case Instruction::MONITOR_ENTER:
584 monitor_enter_count++;
585 break;
586 case Instruction::NEW_INSTANCE:
587 new_instance_count++;
588 break;
589 default:
590 break;
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700593 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700595 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700596 }
597
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
600 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700601 return false;
602 }
603
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 new_instance_count_ = new_instance_count;
605 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700606 return true;
607}
608
Ian Rogers776ac1f2012-04-13 23:36:36 -0700609bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700611 if (tries_size == 0) {
612 return true;
613 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700615 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700616
617 for (uint32_t idx = 0; idx < tries_size; idx++) {
618 const DexFile::TryItem* try_item = &tries[idx];
619 uint32_t start = try_item->start_addr_;
620 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700621 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
623 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700624 return false;
625 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700627 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
628 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700629 return false;
630 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 uint32_t dex_pc = start;
632 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
633 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700635 size_t insn_size = inst->SizeInCodeUnits();
636 dex_pc += insn_size;
637 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700638 }
639 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800640 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700641 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700642 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700643 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700644 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700645 CatchHandlerIterator iterator(handlers_ptr);
646 for (; iterator.HasNext(); iterator.Next()) {
647 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700649 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
650 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700651 return false;
652 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700654 // Ensure exception types are resolved so that they don't need resolution to be delivered,
655 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700656 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
658 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700659 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 if (exception_type == nullptr) {
661 DCHECK(self_->IsExceptionPending());
662 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700663 }
664 }
jeffhaobdb76512011-09-07 11:43:16 -0700665 }
Ian Rogers0571d352011-11-03 19:51:38 -0700666 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700667 }
jeffhaobdb76512011-09-07 11:43:16 -0700668 return true;
669}
670
Ian Rogers776ac1f2012-04-13 23:36:36 -0700671bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700673
Ian Rogers0c7abda2012-09-19 13:33:42 -0700674 /* 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 -0700675 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700676 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700677
678 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700679 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700681 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 return false;
683 }
684 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700685 // All invoke points are marked as "Throw" points already.
686 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000687 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700688 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000689 } else if (inst->IsReturn()) {
690 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 }
692 dex_pc += inst->SizeInCodeUnits();
693 inst = inst->Next();
694 }
695 return true;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 bool result = true;
700 switch (inst->GetVerifyTypeArgumentA()) {
701 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700702 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 break;
704 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700705 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 break;
707 }
708 switch (inst->GetVerifyTypeArgumentB()) {
709 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700710 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 break;
712 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700713 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 break;
715 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700716 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 break;
718 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700719 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 break;
721 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700722 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 break;
724 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700725 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 break;
727 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700728 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 break;
730 }
731 switch (inst->GetVerifyTypeArgumentC()) {
732 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700733 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 break;
735 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700736 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 break;
738 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700739 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 break;
741 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700742 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 break;
744 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700745 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 break;
747 }
748 switch (inst->GetVerifyExtraFlags()) {
749 case Instruction::kVerifyArrayData:
750 result = result && CheckArrayData(code_offset);
751 break;
752 case Instruction::kVerifyBranchTarget:
753 result = result && CheckBranchTarget(code_offset);
754 break;
755 case Instruction::kVerifySwitchTargets:
756 result = result && CheckSwitchTargets(code_offset);
757 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700758 case Instruction::kVerifyVarArgNonZero:
759 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700760 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700761 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
763 "non-range invoke";
764 return false;
765 }
Ian Rogers29a26482014-05-02 15:27:29 -0700766 uint32_t args[Instruction::kMaxVarArgRegs];
767 inst->GetVarArgs(args);
768 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700770 }
Andreas Gampec3314312014-06-19 18:13:29 -0700771 case Instruction::kVerifyVarArgRangeNonZero:
772 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700774 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
775 inst->VRegA() <= 0) {
776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
777 "range invoke";
778 return false;
779 }
Ian Rogers29a26482014-05-02 15:27:29 -0700780 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 break;
782 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700783 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 result = false;
785 break;
786 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700787 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
789 result = false;
790 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return result;
792}
793
Ian Rogers7b078e82014-09-10 14:44:24 -0700794inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
797 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 return true;
801}
802
Ian Rogers7b078e82014-09-10 14:44:24 -0700803inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
806 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 return true;
810}
811
Ian Rogers7b078e82014-09-10 14:44:24 -0700812inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
815 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return false;
817 }
818 return true;
819}
820
Ian Rogers7b078e82014-09-10 14:44:24 -0700821inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
824 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 return false;
826 }
827 return true;
828}
829
Ian Rogers7b078e82014-09-10 14:44:24 -0700830inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
833 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
835 }
836 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700837 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700838 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 return false;
841 }
842 return true;
843}
844
Ian Rogers7b078e82014-09-10 14:44:24 -0700845inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
848 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 return false;
850 }
851 return true;
852}
853
Ian Rogers7b078e82014-09-10 14:44:24 -0700854inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
857 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 return false;
859 }
860 return true;
861}
862
Ian Rogers776ac1f2012-04-13 23:36:36 -0700863bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
866 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
869 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700870 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 const char* cp = descriptor;
872 while (*cp++ == '[') {
873 bracket_count++;
874 }
875 if (bracket_count == 0) {
876 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700877 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
878 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 return false;
880 } else if (bracket_count > 255) {
881 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
883 << "can't new-array class '" << descriptor << "' (exceeds limit)";
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::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
891 const uint16_t* insns = code_item_->insns_ + cur_offset;
892 const uint16_t* array_data;
893 int32_t array_data_offset;
894
895 DCHECK_LT(cur_offset, insn_count);
896 /* make sure the start of the array data table is in range */
897 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
898 if ((int32_t) cur_offset + array_data_offset < 0 ||
899 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700901 << ", data offset " << array_data_offset
902 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 return false;
904 }
905 /* offset to array data table is a relative branch-style offset */
906 array_data = insns + array_data_offset;
907 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800908 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
910 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 return false;
912 }
913 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700914 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
916 /* make sure the end of the switch is in range */
917 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
919 << ", data offset " << array_data_offset << ", end "
920 << cur_offset + array_data_offset + table_size
921 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 return false;
923 }
924 return true;
925}
926
Ian Rogers776ac1f2012-04-13 23:36:36 -0700927bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 int32_t offset;
929 bool isConditional, selfOkay;
930 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
931 return false;
932 }
933 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
935 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700938 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
939 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700940 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
942 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 return false;
944 }
945 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
946 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700947 if (abs_offset < 0 ||
948 (uint32_t) abs_offset >= insn_count ||
949 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700951 << reinterpret_cast<void*>(abs_offset) << ") at "
952 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700953 return false;
954 }
955 insn_flags_[abs_offset].SetBranchTarget();
956 return true;
957}
958
Ian Rogers776ac1f2012-04-13 23:36:36 -0700959bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 bool* selfOkay) {
961 const uint16_t* insns = code_item_->insns_ + cur_offset;
962 *pConditional = false;
963 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 switch (*insns & 0xff) {
965 case Instruction::GOTO:
966 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700967 break;
968 case Instruction::GOTO_32:
969 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 *selfOkay = true;
971 break;
972 case Instruction::GOTO_16:
973 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700974 break;
975 case Instruction::IF_EQ:
976 case Instruction::IF_NE:
977 case Instruction::IF_LT:
978 case Instruction::IF_GE:
979 case Instruction::IF_GT:
980 case Instruction::IF_LE:
981 case Instruction::IF_EQZ:
982 case Instruction::IF_NEZ:
983 case Instruction::IF_LTZ:
984 case Instruction::IF_GEZ:
985 case Instruction::IF_GTZ:
986 case Instruction::IF_LEZ:
987 *pOffset = (int16_t) insns[1];
988 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700989 break;
990 default:
991 return false;
992 break;
993 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700994 return true;
995}
996
Ian Rogers776ac1f2012-04-13 23:36:36 -0700997bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700999 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001001 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1003 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001005 << ", switch offset " << switch_offset
1006 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 return false;
1008 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001011 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001012 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1014 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 return false;
1016 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 uint32_t switch_count = switch_insns[1];
1018 int32_t keys_offset, targets_offset;
1019 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001020 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1021 /* 0=sig, 1=count, 2/3=firstKey */
1022 targets_offset = 4;
1023 keys_offset = -1;
1024 expected_signature = Instruction::kPackedSwitchSignature;
1025 } else {
1026 /* 0=sig, 1=count, 2..count*2 = keys */
1027 keys_offset = 2;
1028 targets_offset = 2 + 2 * switch_count;
1029 expected_signature = Instruction::kSparseSwitchSignature;
1030 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001033 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1034 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1035 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 return false;
1037 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001038 /* make sure the end of the switch is in range */
1039 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1041 << ", switch offset " << switch_offset
1042 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001043 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001044 return false;
1045 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001046 /* for a sparse switch, verify the keys are in ascending order */
1047 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1049 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1051 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1052 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1054 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001055 return false;
1056 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001057 last_key = key;
1058 }
1059 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001060 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 for (uint32_t targ = 0; targ < switch_count; targ++) {
1062 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1063 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1064 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001065 if (abs_offset < 0 ||
1066 abs_offset >= (int32_t) insn_count ||
1067 !insn_flags_[abs_offset].IsOpcode()) {
1068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1069 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1070 << reinterpret_cast<void*>(cur_offset)
1071 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001072 return false;
1073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 insn_flags_[abs_offset].SetBranchTarget();
1075 }
1076 return true;
1077}
1078
Ian Rogers776ac1f2012-04-13 23:36:36 -07001079bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001080 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return false;
1083 }
1084 uint16_t registers_size = code_item_->registers_size_;
1085 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001086 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1088 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 return false;
1090 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001091 }
1092
1093 return true;
1094}
1095
Ian Rogers776ac1f2012-04-13 23:36:36 -07001096bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 uint16_t registers_size = code_item_->registers_size_;
1098 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1099 // integer overflow when adding them here.
1100 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1102 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001103 return false;
1104 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001105 return true;
1106}
1107
Ian Rogers776ac1f2012-04-13 23:36:36 -07001108bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 uint16_t registers_size = code_item_->registers_size_;
1110 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001111
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001113 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1114 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 }
1116 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001117 reg_table_.Init(kTrackCompilerInterestPoints,
1118 insn_flags_.get(),
1119 insns_size,
1120 registers_size,
1121 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001122
jeffhaobdb76512011-09-07 11:43:16 -07001123
Ian Rogersd0fbd852013-09-24 18:17:04 -07001124 work_line_.reset(RegisterLine::Create(registers_size, this));
1125 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001126
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 /* Initialize register types of method arguments. */
1128 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001129 DCHECK_NE(failures_.size(), 0U);
1130 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001131 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001132 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 return false;
1134 }
1135 /* Perform code flow verification. */
1136 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001137 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001139 }
jeffhaobdb76512011-09-07 11:43:16 -07001140 return true;
1141}
1142
Ian Rogersad0b3a32012-04-16 14:50:24 -07001143std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1144 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001145 for (size_t i = 0; i < failures_.size(); ++i) {
1146 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001147 }
1148 return os;
1149}
1150
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001151extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001153 v->Dump(std::cerr);
1154}
1155
Ian Rogers776ac1f2012-04-13 23:36:36 -07001156void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001157 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001158 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 return;
jeffhaobdb76512011-09-07 11:43:16 -07001160 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001161 {
1162 os << "Register Types:\n";
1163 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1164 std::ostream indent_os(&indent_filter);
1165 reg_types_.Dump(indent_os);
1166 }
Ian Rogersb4903572012-10-11 11:52:56 -07001167 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001168 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1169 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 const Instruction* inst = Instruction::At(code_item_->insns_);
1171 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001172 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001174 if (reg_line != nullptr) {
1175 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001176 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001177 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001178 const bool kDumpHexOfInstruction = false;
1179 if (kDumpHexOfInstruction) {
1180 indent_os << inst->DumpHex(5) << " ";
1181 }
1182 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001183 inst = inst->Next();
1184 }
jeffhaobdb76512011-09-07 11:43:16 -07001185}
1186
Ian Rogersd81871c2011-10-03 13:57:23 -07001187static bool IsPrimitiveDescriptor(char descriptor) {
1188 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001189 case 'I':
1190 case 'C':
1191 case 'S':
1192 case 'B':
1193 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001194 case 'F':
1195 case 'D':
1196 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001198 default:
1199 return false;
1200 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001201}
1202
Ian Rogers776ac1f2012-04-13 23:36:36 -07001203bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 RegisterLine* reg_line = reg_table_.GetLine(0);
1205 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1206 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001207
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001209 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001211 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1213 // argument as uninitialized. This restricts field access until the superclass constructor is
1214 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001215 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001216 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001217 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 reg_types_.UninitializedThisArgument(declaring_class));
1219 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001220 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001221 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001223 }
1224
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001225 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001226 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001227 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001228
1229 for (; iterator.HasNext(); iterator.Next()) {
1230 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001231 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 LOG(FATAL) << "Null descriptor";
1233 }
1234 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1236 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
1239 switch (descriptor[0]) {
1240 case 'L':
1241 case '[':
1242 // We assume that reference arguments are initialized. The only way it could be otherwise
1243 // (assuming the caller was verified) is if the current method is <init>, but in that case
1244 // it's effectively considered initialized the instant we reach here (in the sense that we
1245 // can return without doing anything or call virtual methods).
1246 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001247 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001248 if (!reg_type.IsNonZeroReferenceTypes()) {
1249 DCHECK(HasFailures());
1250 return false;
1251 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001252 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 }
1254 break;
1255 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001256 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 break;
1258 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001259 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 break;
1261 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001262 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 break;
1264 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001265 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 break;
1267 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001268 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 break;
1270 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001271 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 break;
1273 case 'J':
1274 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001275 if (cur_arg + 1 >= expected_args) {
1276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1277 << " args, found more (" << descriptor << ")";
1278 return false;
1279 }
1280
Ian Rogers7b078e82014-09-10 14:44:24 -07001281 const RegType* lo_half;
1282 const RegType* hi_half;
1283 if (descriptor[0] == 'J') {
1284 lo_half = &reg_types_.LongLo();
1285 hi_half = &reg_types_.LongHi();
1286 } else {
1287 lo_half = &reg_types_.DoubleLo();
1288 hi_half = &reg_types_.DoubleHi();
1289 }
1290 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 cur_arg++;
1292 break;
1293 }
1294 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1296 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 return false;
1298 }
1299 cur_arg++;
1300 }
1301 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1303 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return false;
1305 }
1306 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1307 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1308 // format. Only major difference from the method argument format is that 'V' is supported.
1309 bool result;
1310 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1311 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001312 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 size_t i = 0;
1314 do {
1315 i++;
1316 } while (descriptor[i] == '['); // process leading [
1317 if (descriptor[i] == 'L') { // object array
1318 do {
1319 i++; // find closing ;
1320 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1321 result = descriptor[i] == ';';
1322 } else { // primitive array
1323 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1324 }
1325 } else if (descriptor[0] == 'L') {
1326 // could be more thorough here, but shouldn't be required
1327 size_t i = 0;
1328 do {
1329 i++;
1330 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1331 result = descriptor[i] == ';';
1332 } else {
1333 result = false;
1334 }
1335 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1337 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 }
1339 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001340}
1341
Ian Rogers776ac1f2012-04-13 23:36:36 -07001342bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 const uint16_t* insns = code_item_->insns_;
1344 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001345
jeffhaobdb76512011-09-07 11:43:16 -07001346 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 insn_flags_[0].SetChanged();
1348 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001349
jeffhaobdb76512011-09-07 11:43:16 -07001350 /* Continue until no instructions are marked "changed". */
1351 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001352 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1354 uint32_t insn_idx = start_guess;
1355 for (; insn_idx < insns_size; insn_idx++) {
1356 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001357 break;
1358 }
jeffhaobdb76512011-09-07 11:43:16 -07001359 if (insn_idx == insns_size) {
1360 if (start_guess != 0) {
1361 /* try again, starting from the top */
1362 start_guess = 0;
1363 continue;
1364 } else {
1365 /* all flags are clear */
1366 break;
1367 }
1368 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 // We carry the working set of registers from instruction to instruction. If this address can
1370 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1371 // "changed" flags, we need to load the set of registers from the table.
1372 // Because we always prefer to continue on to the next instruction, we should never have a
1373 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1374 // target.
1375 work_insn_idx_ = insn_idx;
1376 if (insn_flags_[insn_idx].IsBranchTarget()) {
1377 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001378 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001379 /*
1380 * Sanity check: retrieve the stored register line (assuming
1381 * a full table) and make sure it actually matches.
1382 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001384 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 if (work_line_->CompareLine(register_line) != 0) {
1386 Dump(std::cout);
1387 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001388 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001389 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001390 << " work_line=" << work_line_->Dump(this) << "\n"
1391 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 }
jeffhaobdb76512011-09-07 11:43:16 -07001393 }
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001397 prepend += " failed to verify: ";
1398 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 return false;
1400 }
jeffhaobdb76512011-09-07 11:43:16 -07001401 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 insn_flags_[insn_idx].SetVisited();
1403 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001404 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001405
Ian Rogers1c849e52012-06-28 14:00:33 -07001406 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001407 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001408 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001409 * (besides the wasted space), but it indicates a flaw somewhere
1410 * down the line, possibly in the verifier.
1411 *
1412 * If we've substituted "always throw" instructions into the stream,
1413 * we are almost certainly going to have some dead code.
1414 */
1415 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001417 for (; insn_idx < insns_size;
1418 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001419 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001420 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001421 * may or may not be preceded by a padding NOP (for alignment).
1422 */
1423 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1424 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1425 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001426 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001427 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1428 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1429 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001431 }
1432
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001434 if (dead_start < 0)
1435 dead_start = insn_idx;
1436 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001437 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1438 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001439 dead_start = -1;
1440 }
1441 }
1442 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001443 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1444 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001445 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001446 // To dump the state of the verify after a method, do something like:
1447 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1448 // "boolean java.lang.String.equals(java.lang.Object)") {
1449 // LOG(INFO) << info_messages_.str();
1450 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001451 }
jeffhaobdb76512011-09-07 11:43:16 -07001452 return true;
1453}
1454
Ian Rogers776ac1f2012-04-13 23:36:36 -07001455bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001456 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1457 // We want the state _before_ the instruction, for the case where the dex pc we're
1458 // interested in is itself a monitor-enter instruction (which is a likely place
1459 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001460 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001461 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001462 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1463 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1464 }
1465 }
1466
jeffhaobdb76512011-09-07 11:43:16 -07001467 /*
1468 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001469 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001470 * control to another statement:
1471 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001472 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001473 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001474 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001475 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001476 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001477 * throw an exception that is handled by an encompassing "try"
1478 * block.
1479 *
1480 * We can also return, in which case there is no successor instruction
1481 * from this point.
1482 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001483 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001484 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1486 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001487 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001488
jeffhaobdb76512011-09-07 11:43:16 -07001489 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001490 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001491 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001493 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001494 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 }
jeffhaobdb76512011-09-07 11:43:16 -07001496
1497 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001498 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001499 * can throw an exception, we will copy/merge this into the "catch"
1500 * address rather than work_line, because we don't want the result
1501 * from the "successful" code path (e.g. a check-cast that "improves"
1502 * a type) to be visible to the exception handler.
1503 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001504 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001506 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001508 }
1509
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001510
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001511 // We need to ensure the work line is consistent while performing validation. When we spot a
1512 // peephole pattern we compute a new line for either the fallthrough instruction or the
1513 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001514 std::unique_ptr<RegisterLine> branch_line;
1515 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001516
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001518 case Instruction::NOP:
1519 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001520 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001521 * a signature that looks like a NOP; if we see one of these in
1522 * the course of executing code then we have a problem.
1523 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001526 }
1527 break;
1528
1529 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001530 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 break;
jeffhaobdb76512011-09-07 11:43:16 -07001532 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001533 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 break;
jeffhaobdb76512011-09-07 11:43:16 -07001535 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001536 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
1538 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001539 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 break;
jeffhaobdb76512011-09-07 11:43:16 -07001541 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001542 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 break;
jeffhaobdb76512011-09-07 11:43:16 -07001544 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001545 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001546 break;
1547 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001548 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001549 break;
jeffhaobdb76512011-09-07 11:43:16 -07001550 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001551 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001552 break;
jeffhaobdb76512011-09-07 11:43:16 -07001553 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001554 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001555 break;
1556
1557 /*
1558 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001559 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001560 * might want to hold the result in an actual CPU register, so the
1561 * Dalvik spec requires that these only appear immediately after an
1562 * invoke or filled-new-array.
1563 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001564 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001565 * redundant with the reset done below, but it can make the debug info
1566 * easier to read in some cases.)
1567 */
1568 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001569 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001570 break;
1571 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001572 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001573 break;
1574 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001575 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001576 break;
1577
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001579 /*
jeffhao60f83e32012-02-13 17:16:30 -08001580 * This statement can only appear as the first instruction in an exception handler. We verify
1581 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001582 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001583 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001584 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001585 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
jeffhaobdb76512011-09-07 11:43:16 -07001587 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001588 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001589 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 }
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
1593 break;
1594 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001596 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001597 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1600 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 } else {
1602 // Compilers may generate synthetic functions that write byte values into boolean fields.
1603 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001604 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001605 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1607 ((return_type.IsBoolean() || return_type.IsByte() ||
1608 return_type.IsShort() || return_type.IsChar()) &&
1609 src_type.IsInteger()));
1610 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001611 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001612 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001613 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 }
jeffhaobdb76512011-09-07 11:43:16 -07001616 }
1617 }
1618 break;
1619 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001620 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001621 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001622 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 } else {
1626 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001628 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001629 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 }
jeffhaobdb76512011-09-07 11:43:16 -07001632 }
1633 }
1634 break;
1635 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001636 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001637 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 } else {
1641 /* return_type is the *expected* return type, not register value */
1642 DCHECK(!return_type.IsZero());
1643 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001645 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001646 // Disallow returning uninitialized values and verify that the reference in vAA is an
1647 // instance of the "return_type"
1648 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001649 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1650 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001651 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001652 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1653 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1654 << "' or '" << reg_type << "'";
1655 } else {
1656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1657 << "', but expected from declaration '" << return_type << "'";
1658 }
jeffhaobdb76512011-09-07 11:43:16 -07001659 }
1660 }
1661 }
1662 break;
1663
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001664 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 case Instruction::CONST_4: {
1666 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001668 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001669 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 }
1671 case Instruction::CONST_16: {
1672 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001674 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001675 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001676 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001677 case Instruction::CONST: {
1678 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001680 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001681 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001682 }
1683 case Instruction::CONST_HIGH16: {
1684 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001686 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001688 }
jeffhaobdb76512011-09-07 11:43:16 -07001689 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001690 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001692 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1693 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001696 }
1697 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001699 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1700 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001701 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001702 break;
1703 }
1704 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001706 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1707 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001708 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001709 break;
1710 }
1711 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001713 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1714 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001715 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 break;
1717 }
jeffhaobdb76512011-09-07 11:43:16 -07001718 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 break;
jeffhaobdb76512011-09-07 11:43:16 -07001721 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001722 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001723 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001725 // Get type from instruction if unresolved then we need an access check
1726 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001727 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001728 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001729 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001730 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001731 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001732 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 }
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001735 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001736 break;
1737 case Instruction::MONITOR_EXIT:
1738 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001739 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001740 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001741 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001742 * to the need to handle asynchronous exceptions, a now-deprecated
1743 * feature that Dalvik doesn't support.)
1744 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001745 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001746 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001747 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001748 * structured locking checks are working, the former would have
1749 * failed on the -enter instruction, and the latter is impossible.
1750 *
1751 * This is fortunate, because issue 3221411 prevents us from
1752 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001753 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001754 * some catch blocks (which will show up as "dead" code when
1755 * we skip them here); if we can't, then the code path could be
1756 * "live" so we still need to check it.
1757 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001758 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001759 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001760 break;
1761
Ian Rogers28ad40d2011-10-27 15:19:26 -07001762 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001764 /*
1765 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1766 * could be a "upcast" -- not expected, so we don't try to address it.)
1767 *
1768 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001769 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001770 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1772 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001773 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001774 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001775 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001776 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001777 if (klass != nullptr && klass->IsPrimitive()) {
1778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1779 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1780 << GetDeclaringClass();
1781 break;
1782 }
1783
Ian Rogersad0b3a32012-04-16 14:50:24 -07001784 DCHECK_NE(failures_.size(), 0U);
1785 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001786 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001787 }
1788 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001789 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001790 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001792 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001793 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 if (is_checkcast) {
1795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1796 } else {
1797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1798 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001799 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 if (is_checkcast) {
1801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1802 } else {
1803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1804 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001805 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001806 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001807 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001809 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001810 }
jeffhaobdb76512011-09-07 11:43:16 -07001811 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001812 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001813 }
1814 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001815 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001816 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001817 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001820 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001822 } else {
1823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 }
1825 break;
1826 }
1827 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001828 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001829 if (res_type.IsConflict()) {
1830 DCHECK_NE(failures_.size(), 0U);
1831 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001832 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001833 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1834 // can't create an instance of an interface or abstract class */
1835 if (!res_type.IsInstantiableTypes()) {
1836 Fail(VERIFY_ERROR_INSTANTIATION)
1837 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001838 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001840 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001841 // Any registers holding previous allocations from this address that have not yet been
1842 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001844 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001845 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 break;
1847 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001848 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001850 break;
1851 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001852 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001853 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001854 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001855 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001857 just_set_result = true; // Filled new array range sets result register
1858 break;
jeffhaobdb76512011-09-07 11:43:16 -07001859 case Instruction::CMPL_FLOAT:
1860 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001862 break;
1863 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001864 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001865 break;
1866 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001868 break;
1869 case Instruction::CMPL_DOUBLE:
1870 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001871 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001872 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001873 break;
1874 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001875 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001876 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001877 break;
1878 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001880 break;
1881 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001883 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001884 break;
1885 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001886 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001887 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001888 break;
1889 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001890 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001891 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001893 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001894 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001895 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1896 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001897 }
1898 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 }
jeffhaobdb76512011-09-07 11:43:16 -07001900 case Instruction::GOTO:
1901 case Instruction::GOTO_16:
1902 case Instruction::GOTO_32:
1903 /* no effect on or use of registers */
1904 break;
1905
1906 case Instruction::PACKED_SWITCH:
1907 case Instruction::SPARSE_SWITCH:
1908 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001909 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
1911
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 case Instruction::FILL_ARRAY_DATA: {
1913 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001915 /* array_type can be null if the reg type is Zero */
1916 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001917 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1919 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001920 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001921 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001922 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001923 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1925 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 } else {
jeffhao457cc512012-02-02 16:55:13 -08001927 // Now verify if the element width in the table matches the element width declared in
1928 // the array
1929 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1930 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001932 } else {
1933 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1934 // Since we don't compress the data in Dex, expect to see equal width of data stored
1935 // in the table and expected from the array class.
1936 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1938 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001939 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 }
1941 }
jeffhaobdb76512011-09-07 11:43:16 -07001942 }
1943 }
1944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
jeffhaobdb76512011-09-07 11:43:16 -07001946 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001947 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001948 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1949 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 bool mismatch = false;
1951 if (reg_type1.IsZero()) { // zero then integral or reference expected
1952 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1953 } else if (reg_type1.IsReferenceTypes()) { // both references?
1954 mismatch = !reg_type2.IsReferenceTypes();
1955 } else { // both integral?
1956 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1957 }
1958 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1960 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001961 }
1962 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 }
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::IF_LT:
1965 case Instruction::IF_GE:
1966 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1969 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001970 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1972 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001973 }
1974 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 }
jeffhaobdb76512011-09-07 11:43:16 -07001976 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001978 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1981 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001983
1984 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001985 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001986 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001987 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001988 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001989 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001990 }
Ian Rogers9b360392013-06-06 14:45:07 -07001991 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001992 } else {
1993 break;
1994 }
1995
Ian Rogers9b360392013-06-06 14:45:07 -07001996 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001997
1998 /* Check for peep-hole pattern of:
1999 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002000 * instance-of vX, vY, T;
2001 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002002 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002003 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002004 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002005 * and sharpen the type of vY to be type T.
2006 * Note, this pattern can't be if:
2007 * - if there are other branches to this branch,
2008 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002009 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002010 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002011 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2012 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2013 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002014 // Check the type of the instance-of is different than that of registers type, as if they
2015 // are the same there is no work to be done here. Check that the conversion is not to or
2016 // from an unresolved type as type information is imprecise. If the instance-of is to an
2017 // interface then ignore the type information as interfaces can only be treated as Objects
2018 // and we don't want to disallow field and other operations on the object. If the value
2019 // being instance-of checked against is known null (zero) then allow the optimization as
2020 // we didn't have type information. If the merge of the instance-of type with the original
2021 // type is assignable to the original then allow optimization. This check is performed to
2022 // ensure that subsequent merges don't lose type information - such as becoming an
2023 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002024 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002025 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002026
Ian Rogersebbdd872014-07-07 23:53:08 -07002027 if (!orig_type.Equals(cast_type) &&
2028 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002029 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002030 !cast_type.GetClass()->IsInterface() &&
2031 (orig_type.IsZero() ||
2032 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002033 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002034 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002035 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002036 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002037 branch_line.reset(update_line);
2038 }
2039 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002040 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002041 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2042 // See if instance-of was preceded by a move-object operation, common due to the small
2043 // register encoding space of instance-of, and propagate type information to the source
2044 // of the move-object.
2045 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002046 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002047 move_idx--;
2048 }
2049 CHECK(insn_flags_[move_idx].IsOpcode());
2050 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2051 switch (move_inst->Opcode()) {
2052 case Instruction::MOVE_OBJECT:
2053 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002055 }
2056 break;
2057 case Instruction::MOVE_OBJECT_FROM16:
2058 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002059 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002060 }
2061 break;
2062 case Instruction::MOVE_OBJECT_16:
2063 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002064 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002065 }
2066 break;
2067 default:
2068 break;
2069 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002070 }
2071 }
2072 }
2073
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 }
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::IF_LTZ:
2077 case Instruction::IF_GEZ:
2078 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2083 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 }
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 }
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
2115 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 break;
2118 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
jeffhaobdb76512011-09-07 11:43:16 -07002134 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 break;
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 break;
jeffhaobdb76512011-09-07 11:43:16 -07002140 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 break;
2143 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
2146 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
2149 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 break;
jeffhaobdb76512011-09-07 11:43:16 -07002152
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 break;
2156 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 break;
2159 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 break;
2162 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 break;
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174
jeffhaobdb76512011-09-07 11:43:16 -07002175 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 break;
jeffhaobdb76512011-09-07 11:43:16 -07002178 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 break;
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002182 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002183 break;
jeffhaobdb76512011-09-07 11:43:16 -07002184 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002185 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 break;
2187 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002192 break;
2193 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002194 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 break;
2196
2197 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 break;
2200 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 break;
2203 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002205 break;
2206 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
2209 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
2215 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002216 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002217 break;
2218
2219 case Instruction::INVOKE_VIRTUAL:
2220 case Instruction::INVOKE_VIRTUAL_RANGE:
2221 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2224 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002225 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2226 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002227 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2228 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002229 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002230 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002231 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002232 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2233 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002234 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002235 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002236 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2237 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002238 return_type_class->CannotBeAssignedFromOtherTypes());
2239 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002240 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2241 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002242 }
2243 }
2244 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002246 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2247 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002248 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002249 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002250 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002251 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002252 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002253 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002254 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002256 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 }
jeffhaobdb76512011-09-07 11:43:16 -07002259 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002261 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002262 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002263 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002264 const char* return_type_descriptor;
2265 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002266 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002267 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002269 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002270 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002271 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2272 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2273 } else {
2274 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002275 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002276 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002277 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2278 MethodHelper mh(h_called_method);
2279 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2280 if (return_type_class != nullptr) {
2281 return_type = &reg_types_.FromClass(return_type_descriptor,
2282 return_type_class,
2283 return_type_class->CannotBeAssignedFromOtherTypes());
2284 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2286 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002287 }
Ian Rogers46685432012-06-03 22:26:43 -07002288 }
2289 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002290 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002291 * Some additional checks when calling a constructor. We know from the invocation arg check
2292 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2293 * that to require that called_method->klass is the same as this->klass or this->super,
2294 * allowing the latter only if the "this" argument is the same as the "this" argument to
2295 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002296 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002298 if (this_type.IsConflict()) // failure.
2299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300
jeffhaob57e9522012-04-26 18:08:21 -07002301 /* no null refs allowed (?) */
2302 if (this_type.IsZero()) {
2303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2304 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002305 }
jeffhaob57e9522012-04-26 18:08:21 -07002306
2307 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002308 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002309 // TODO: re-enable constructor type verification
2310 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002311 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002312 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2313 // break;
2314 // }
jeffhaob57e9522012-04-26 18:08:21 -07002315
2316 /* arg must be an uninitialized reference */
2317 if (!this_type.IsUninitializedTypes()) {
2318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2319 << this_type;
2320 break;
2321 }
2322
2323 /*
2324 * Replace the uninitialized reference with an initialized one. We need to do this for all
2325 * registers that have the same object instance in them, not just the "this" register.
2326 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002327 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002328 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002329 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002330 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2331 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002332 }
2333 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002336 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002338 just_set_result = true;
2339 break;
2340 }
2341 case Instruction::INVOKE_STATIC:
2342 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002344 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002345 METHOD_STATIC,
2346 is_range,
2347 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002348 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002349 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002351 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2352 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002353 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002354 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002355 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002356 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002357 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002359 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 } else {
2361 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2362 }
jeffhaobdb76512011-09-07 11:43:16 -07002363 just_set_result = true;
2364 }
2365 break;
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002369 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002370 METHOD_INTERFACE,
2371 is_range,
2372 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002373 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002374 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2376 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2377 << PrettyMethod(abs_method) << "'";
2378 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002379 }
Ian Rogers0d604842012-04-16 14:50:24 -07002380 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002381 /* Get the type of the "this" arg, which should either be a sub-interface of called
2382 * interface or Object (see comments in RegType::JoinClass).
2383 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002384 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 if (this_type.IsZero()) {
2386 /* null pointer always passes (and always fails at runtime) */
2387 } else {
2388 if (this_type.IsUninitializedTypes()) {
2389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2390 << this_type;
2391 break;
2392 }
2393 // In the past we have tried to assert that "called_interface" is assignable
2394 // from "this_type.GetClass()", however, as we do an imprecise Join
2395 // (RegType::JoinClass) we don't have full information on what interfaces are
2396 // implemented by "this_type". For example, two classes may implement the same
2397 // interfaces and have a common parent that doesn't implement the interface. The
2398 // join will set "this_type" to the parent class and a test that this implements
2399 // the interface will incorrectly fail.
2400 }
2401 /*
2402 * We don't have an object instance, so we can't find the concrete method. However, all of
2403 * the type information is in the abstract method, so we're good.
2404 */
2405 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002407 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002408 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2409 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2410 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2411 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002412 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002413 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002414 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002416 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002417 } else {
2418 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2419 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002420 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 }
jeffhaobdb76512011-09-07 11:43:16 -07002423 case Instruction::NEG_INT:
2424 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002425 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::NEG_LONG:
2428 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002429 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002430 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002431 break;
2432 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002433 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002436 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002437 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002438 break;
2439 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002440 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002441 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002442 break;
2443 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002444 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
2446 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002447 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002448 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002452 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
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::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002459 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002460 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002463 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::FLOAT_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_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002471 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002474 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002475 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002478 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002479 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002483 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002486 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002489 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494
2495 case Instruction::ADD_INT:
2496 case Instruction::SUB_INT:
2497 case Instruction::MUL_INT:
2498 case Instruction::REM_INT:
2499 case Instruction::DIV_INT:
2500 case Instruction::SHL_INT:
2501 case Instruction::SHR_INT:
2502 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002503 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002504 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
2506 case Instruction::AND_INT:
2507 case Instruction::OR_INT:
2508 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002509 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002510 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512 case Instruction::ADD_LONG:
2513 case Instruction::SUB_LONG:
2514 case Instruction::MUL_LONG:
2515 case Instruction::DIV_LONG:
2516 case Instruction::REM_LONG:
2517 case Instruction::AND_LONG:
2518 case Instruction::OR_LONG:
2519 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002520 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002521 reg_types_.LongLo(), reg_types_.LongHi(),
2522 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::SHL_LONG:
2525 case Instruction::SHR_LONG:
2526 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002528 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002529 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002530 break;
2531 case Instruction::ADD_FLOAT:
2532 case Instruction::SUB_FLOAT:
2533 case Instruction::MUL_FLOAT:
2534 case Instruction::DIV_FLOAT:
2535 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002536 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2537 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
2539 case Instruction::ADD_DOUBLE:
2540 case Instruction::SUB_DOUBLE:
2541 case Instruction::MUL_DOUBLE:
2542 case Instruction::DIV_DOUBLE:
2543 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002544 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002545 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2546 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::ADD_INT_2ADDR:
2549 case Instruction::SUB_INT_2ADDR:
2550 case Instruction::MUL_INT_2ADDR:
2551 case Instruction::REM_INT_2ADDR:
2552 case Instruction::SHL_INT_2ADDR:
2553 case Instruction::SHR_INT_2ADDR:
2554 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2556 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::AND_INT_2ADDR:
2559 case Instruction::OR_INT_2ADDR:
2560 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002561 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2562 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
2564 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2566 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::ADD_LONG_2ADDR:
2569 case Instruction::SUB_LONG_2ADDR:
2570 case Instruction::MUL_LONG_2ADDR:
2571 case Instruction::DIV_LONG_2ADDR:
2572 case Instruction::REM_LONG_2ADDR:
2573 case Instruction::AND_LONG_2ADDR:
2574 case Instruction::OR_LONG_2ADDR:
2575 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002577 reg_types_.LongLo(), reg_types_.LongHi(),
2578 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::SHL_LONG_2ADDR:
2581 case Instruction::SHR_LONG_2ADDR:
2582 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002584 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::ADD_FLOAT_2ADDR:
2587 case Instruction::SUB_FLOAT_2ADDR:
2588 case Instruction::MUL_FLOAT_2ADDR:
2589 case Instruction::DIV_FLOAT_2ADDR:
2590 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002591 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2592 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002593 break;
2594 case Instruction::ADD_DOUBLE_2ADDR:
2595 case Instruction::SUB_DOUBLE_2ADDR:
2596 case Instruction::MUL_DOUBLE_2ADDR:
2597 case Instruction::DIV_DOUBLE_2ADDR:
2598 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002599 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002600 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2601 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::ADD_INT_LIT16:
2604 case Instruction::RSUB_INT:
2605 case Instruction::MUL_INT_LIT16:
2606 case Instruction::DIV_INT_LIT16:
2607 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2609 true);
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::AND_INT_LIT16:
2612 case Instruction::OR_INT_LIT16:
2613 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2615 true);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617 case Instruction::ADD_INT_LIT8:
2618 case Instruction::RSUB_INT_LIT8:
2619 case Instruction::MUL_INT_LIT8:
2620 case Instruction::DIV_INT_LIT8:
2621 case Instruction::REM_INT_LIT8:
2622 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002624 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2626 false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::AND_INT_LIT8:
2629 case Instruction::OR_INT_LIT8:
2630 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2632 false);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002635 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002636 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002637 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2639 }
2640 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002641 // Note: the following instructions encode offsets derived from class linking.
2642 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2643 // meaning if the class linking and resolution were successful.
2644 case Instruction::IGET_QUICK:
2645 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2646 break;
2647 case Instruction::IGET_WIDE_QUICK:
2648 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2649 break;
2650 case Instruction::IGET_OBJECT_QUICK:
2651 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2652 break;
2653 case Instruction::IPUT_QUICK:
2654 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2655 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002656 case Instruction::IPUT_BOOLEAN_QUICK:
2657 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2658 break;
2659 case Instruction::IPUT_BYTE_QUICK:
2660 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2661 break;
2662 case Instruction::IPUT_CHAR_QUICK:
2663 VerifyIPutQuick(inst, reg_types_.Char(), true);
2664 break;
2665 case Instruction::IPUT_SHORT_QUICK:
2666 VerifyIPutQuick(inst, reg_types_.Short(), true);
2667 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002668 case Instruction::IPUT_WIDE_QUICK:
2669 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2670 break;
2671 case Instruction::IPUT_OBJECT_QUICK:
2672 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2673 break;
2674 case Instruction::INVOKE_VIRTUAL_QUICK:
2675 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2676 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002677 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002678 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002679 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002680 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002681 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002683 } else {
2684 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2685 }
2686 just_set_result = true;
2687 }
2688 break;
2689 }
2690
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002692 case Instruction::UNUSED_3E:
2693 case Instruction::UNUSED_3F:
2694 case Instruction::UNUSED_40:
2695 case Instruction::UNUSED_41:
2696 case Instruction::UNUSED_42:
2697 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002698 case Instruction::UNUSED_79:
2699 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002700 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002701 case Instruction::UNUSED_F0:
2702 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002703 case Instruction::UNUSED_F2:
2704 case Instruction::UNUSED_F3:
2705 case Instruction::UNUSED_F4:
2706 case Instruction::UNUSED_F5:
2707 case Instruction::UNUSED_F6:
2708 case Instruction::UNUSED_F7:
2709 case Instruction::UNUSED_F8:
2710 case Instruction::UNUSED_F9:
2711 case Instruction::UNUSED_FA:
2712 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002713 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002714 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002715 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002716 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719
2720 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002721 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002722 * complain if an instruction is missing (which is desirable).
2723 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002724 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002725
Ian Rogersad0b3a32012-04-16 14:50:24 -07002726 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002727 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002728 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002729 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002730 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002731 /* immediate failure, reject class */
2732 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2733 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002734 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002735 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002736 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002737 }
jeffhaobdb76512011-09-07 11:43:16 -07002738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * If we didn't just set the result register, clear it out. This ensures that you can only use
2740 * "move-result" immediately after the result is set. (We could check this statically, but it's
2741 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002742 */
2743 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002744 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002745 }
2746
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002747
jeffhaobdb76512011-09-07 11:43:16 -07002748
2749 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002750 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002751 *
2752 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002753 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002754 * somebody could get a reference field, check it for zero, and if the
2755 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002756 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002757 * that, and will reject the code.
2758 *
2759 * TODO: avoid re-fetching the branch target
2760 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002761 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002762 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002764 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002766 return false;
2767 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002768 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002769 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002770 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 }
jeffhaobdb76512011-09-07 11:43:16 -07002772 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002773 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002774 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002775 return false;
2776 }
2777 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002778 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002779 return false;
2780 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
jeffhaobdb76512011-09-07 11:43:16 -07002782 }
2783
2784 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002785 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002786 *
2787 * We've already verified that the table is structurally sound, so we
2788 * just need to walk through and tag the targets.
2789 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002790 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002791 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2792 const uint16_t* switch_insns = insns + offset_to_switch;
2793 int switch_count = switch_insns[1];
2794 int offset_to_targets, targ;
2795
2796 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2797 /* 0 = sig, 1 = count, 2/3 = first key */
2798 offset_to_targets = 4;
2799 } else {
2800 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002801 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002802 offset_to_targets = 2 + 2 * switch_count;
2803 }
2804
2805 /* verify each switch target */
2806 for (targ = 0; targ < switch_count; targ++) {
2807 int offset;
2808 uint32_t abs_offset;
2809
2810 /* offsets are 32-bit, and only partly endian-swapped */
2811 offset = switch_insns[offset_to_targets + targ * 2] |
2812 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002813 abs_offset = work_insn_idx_ + offset;
2814 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002815 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002816 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002818 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002819 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 }
2822 }
2823
2824 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2826 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002827 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002828 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002829 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002830 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002831
Andreas Gampef91baf12014-07-18 15:41:00 -07002832 // Need the linker to try and resolve the handled class to check if it's Throwable.
2833 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2834
Ian Rogers0571d352011-11-03 19:51:38 -07002835 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002836 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2837 if (handler_type_idx == DexFile::kDexNoIndex16) {
2838 has_catch_all_handler = true;
2839 } else {
2840 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002841 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2842 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002843 if (klass != nullptr) {
2844 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2845 has_catch_all_handler = true;
2846 }
2847 } else {
2848 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002849 DCHECK(self_->IsExceptionPending());
2850 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002851 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 }
jeffhaobdb76512011-09-07 11:43:16 -07002853 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2855 * "work_regs", because at runtime the exception will be thrown before the instruction
2856 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002857 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002858 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002859 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 }
jeffhaobdb76512011-09-07 11:43:16 -07002861 }
2862
2863 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2865 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002866 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002867 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002868 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 * The state in work_line reflects the post-execution state. If the current instruction is a
2870 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002871 * it will do so before grabbing the lock).
2872 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002873 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002874 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002876 return false;
2877 }
2878 }
2879 }
2880
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002881 /* Handle "continue". Tag the next consecutive instruction.
2882 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2883 * because it changes work_line_ when performing peephole optimization
2884 * and this change should not be used in those cases.
2885 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002886 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2888 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002889 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2891 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002892 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002893 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2894 // next instruction isn't one.
2895 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2896 return false;
2897 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002898 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002899 // Make workline consistent with fallthrough computed from peephole optimization.
2900 work_line_->CopyFromLine(fallthrough_line.get());
2901 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002902 if (insn_flags_[next_insn_idx].IsReturn()) {
2903 // For returns we only care about the operand to the return, all other registers are dead.
2904 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2905 Instruction::Code opcode = ret_inst->Opcode();
2906 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002908 } else {
2909 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002910 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002911 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002913 }
2914 }
2915 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002916 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002917 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002918 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2919 // needed. If the merge changes the state of the registers then the work line will be
2920 // updated.
2921 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002922 return false;
2923 }
2924 } else {
2925 /*
2926 * We're not recording register data for the next instruction, so we don't know what the
2927 * prior state was. We have to assume that something has changed and re-evaluate it.
2928 */
2929 insn_flags_[next_insn_idx].SetChanged();
2930 }
2931 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002932
jeffhaod1f0fde2011-09-08 17:25:33 -07002933 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002934 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 return false;
2937 }
jeffhaobdb76512011-09-07 11:43:16 -07002938 }
2939
2940 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002941 * Update start_guess. Advance to the next instruction of that's
2942 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002943 * neither of those exists we're in a return or throw; leave start_guess
2944 * alone and let the caller sort it out.
2945 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002946 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002947 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2948 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002949 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002950 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002952 }
2953
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2955 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002956
2957 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002958} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002959
Ian Rogersd8f69b02014-09-10 21:43:52 +00002960const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002961 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002962 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002963 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002965 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2966 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002967 if (result.IsConflict()) {
2968 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2969 << "' in " << referrer;
2970 return result;
2971 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002973 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002974 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002975 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002976 // check at runtime if access is allowed and so pass here. If result is
2977 // primitive, skip the access check.
2978 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2979 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002980 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002982 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002983 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002984}
2985
Ian Rogersd8f69b02014-09-10 21:43:52 +00002986const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07002987 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07002988 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002989 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2991 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002992 CatchHandlerIterator iterator(handlers_ptr);
2993 for (; iterator.HasNext(); iterator.Next()) {
2994 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2995 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002996 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002997 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002998 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002999 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003000 if (exception.IsUnresolvedTypes()) {
3001 // We don't know enough about the type. Fail here and let runtime handle it.
3002 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3003 return exception;
3004 } else {
3005 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3006 return reg_types_.Conflict();
3007 }
Jeff Haob878f212014-04-24 16:25:36 -07003008 } else if (common_super == nullptr) {
3009 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003010 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003011 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003013 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07003014 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 }
3016 }
3017 }
3018 }
Ian Rogers0571d352011-11-03 19:51:38 -07003019 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 }
3021 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003024 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003027 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003028}
3029
Brian Carlstromea46f952013-07-30 01:26:50 -07003030mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003031 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003033 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003034 if (klass_type.IsConflict()) {
3035 std::string append(" in attempt to access method ");
3036 append += dex_file_->GetMethodName(method_id);
3037 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003038 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003039 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003040 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003041 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003042 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003043 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003044 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003045 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003047 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003048 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003049
3050 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003052 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 res_method = klass->FindInterfaceMethod(name, signature);
3054 } else {
3055 res_method = klass->FindVirtualMethod(name, signature);
3056 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003057 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003058 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003060 // If a virtual or interface method wasn't found with the expected type, look in
3061 // the direct methods. This can happen when the wrong invoke type is used or when
3062 // a class has changed, and will be flagged as an error in later checks.
3063 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3064 res_method = klass->FindDirectMethod(name, signature);
3065 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003067 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3068 << PrettyDescriptor(klass) << "." << name
3069 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003071 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 }
3073 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3075 // enforce them here.
3076 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3078 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003081 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003082 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3084 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003085 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003086 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003087 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003088 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003089 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003090 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003091 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003092 }
jeffhaode0d9c92012-02-27 13:58:13 -08003093 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3094 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3096 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003098 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003099 // Check that interface methods match interface classes.
3100 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3101 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3102 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003104 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3105 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3106 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003107 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003108 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003109 // See if the method type implied by the invoke instruction matches the access flags for the
3110 // target method.
3111 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3112 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3113 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3114 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003115 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3116 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003118 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003119 return res_method;
3120}
3121
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003122template <class T>
3123mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3124 MethodType method_type,
3125 bool is_range,
3126 mirror::ArtMethod* res_method) {
3127 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3128 // match the call to the signature. Also, we might be calling through an abstract method
3129 // definition (which doesn't have register count values).
3130 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3131 /* caught by static verifier */
3132 DCHECK(is_range || expected_args <= 5);
3133 if (expected_args > code_item_->outs_size_) {
3134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3135 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3136 return nullptr;
3137 }
3138
3139 uint32_t arg[5];
3140 if (!is_range) {
3141 inst->GetVarArgs(arg);
3142 }
3143 uint32_t sig_registers = 0;
3144
3145 /*
3146 * Check the "this" argument, which must be an instance of the class that declared the method.
3147 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3148 * rigorous check here (which is okay since we have to do it at runtime).
3149 */
3150 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003152 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3153 CHECK(have_pending_hard_failure_);
3154 return nullptr;
3155 }
3156 if (actual_arg_type.IsUninitializedReference()) {
3157 if (res_method) {
3158 if (!res_method->IsConstructor()) {
3159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3160 return nullptr;
3161 }
3162 } else {
3163 // Check whether the name of the called method is "<init>"
3164 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003165 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3167 return nullptr;
3168 }
3169 }
3170 }
3171 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003172 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003173 if (res_method != nullptr) {
3174 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003175 std::string temp;
3176 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003177 klass->CannotBeAssignedFromOtherTypes());
3178 } else {
3179 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3180 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003181 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003182 dex_file_->StringByTypeIdx(class_idx),
3183 false);
3184 }
3185 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3186 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3187 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3188 << "' not instance of '" << *res_method_class << "'";
3189 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3190 // the compiler.
3191 if (have_pending_hard_failure_) {
3192 return nullptr;
3193 }
3194 }
3195 }
3196 sig_registers = 1;
3197 }
3198
3199 for ( ; it->HasNext(); it->Next()) {
3200 if (sig_registers >= expected_args) {
3201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3202 " arguments, found " << sig_registers << " or more.";
3203 return nullptr;
3204 }
3205
3206 const char* param_descriptor = it->GetDescriptor();
3207
3208 if (param_descriptor == nullptr) {
3209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3210 "component";
3211 return nullptr;
3212 }
3213
Ian Rogersd8f69b02014-09-10 21:43:52 +00003214 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003215 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3216 arg[sig_registers];
3217 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003219 if (!src_type.IsIntegralTypes()) {
3220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3221 << " but expected " << reg_type;
3222 return res_method;
3223 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003225 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3226 // compiler.
3227 if (have_pending_hard_failure_) {
3228 return res_method;
3229 }
3230 }
3231 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3232 }
3233 if (expected_args != sig_registers) {
3234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3235 " arguments, found " << sig_registers;
3236 return nullptr;
3237 }
3238 return res_method;
3239}
3240
3241void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3242 MethodType method_type,
3243 bool is_range) {
3244 // As the method may not have been resolved, make this static check against what we expect.
3245 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3246 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3247 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3248 DexFileParameterIterator it(*dex_file_,
3249 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3250 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3251 nullptr);
3252}
3253
3254class MethodParamListDescriptorIterator {
3255 public:
3256 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3257 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3258 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3259 }
3260
3261 bool HasNext() {
3262 return pos_ < params_size_;
3263 }
3264
3265 void Next() {
3266 ++pos_;
3267 }
3268
3269 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3270 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3271 }
3272
3273 private:
3274 mirror::ArtMethod* res_method_;
3275 size_t pos_;
3276 const DexFile::TypeList* params_;
3277 const size_t params_size_;
3278};
3279
Brian Carlstromea46f952013-07-30 01:26:50 -07003280mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003281 MethodType method_type,
3282 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003283 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003284 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3285 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003286 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003287
Brian Carlstromea46f952013-07-30 01:26:50 -07003288 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003289 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003290 // Check what we can statically.
3291 if (!have_pending_hard_failure_) {
3292 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3293 }
3294 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003295 }
3296
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3298 // has a vtable entry for the target method.
3299 if (is_super) {
3300 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003301 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003302 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003303 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003304 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003305 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003306 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003307 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003308 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003309 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003310 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003311 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003312 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003313 << "." << res_method->GetName()
3314 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003315 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003316 }
3317 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003318
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003319 // Process the target method's signature. This signature may or may not
3320 MethodParamListDescriptorIterator it(res_method);
3321 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3322 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003323}
3324
Brian Carlstromea46f952013-07-30 01:26:50 -07003325mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003326 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003327 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3328 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003329 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003330 if (!actual_arg_type.HasClass()) {
3331 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003332 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003333 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003334 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003335 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003336 if (klass->IsInterface()) {
3337 // Derive Object.class from Class.class.getSuperclass().
3338 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3339 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003340 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003341 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003342 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003343 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003344 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003345 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003346 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3347 << PrettyDescriptor(klass);
3348 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Ian Rogers7b078e82014-09-10 14:44:24 -07003349 CHECK(!self_->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003350 return res_method;
3351}
3352
Brian Carlstromea46f952013-07-30 01:26:50 -07003353mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003354 bool is_range) {
3355 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003356 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003357 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003358 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003360 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003361 }
3362 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3363
3364 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3365 // match the call to the signature. Also, we might be calling through an abstract method
3366 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003367 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003368 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003369 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003370 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003371 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3372 /* caught by static verifier */
3373 DCHECK(is_range || expected_args <= 5);
3374 if (expected_args > code_item_->outs_size_) {
3375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3376 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003378 }
3379
3380 /*
3381 * Check the "this" argument, which must be an instance of the class that declared the method.
3382 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3383 * rigorous check here (which is okay since we have to do it at runtime).
3384 */
3385 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003387 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388 }
3389 if (!actual_arg_type.IsZero()) {
3390 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003391 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003392 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003393 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003394 klass->CannotBeAssignedFromOtherTypes());
3395 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003396 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3397 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003398 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003399 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003400 }
3401 }
3402 /*
3403 * Process the target method's signature. This signature may or may not
3404 * have been verified, so we can't assume it's properly formed.
3405 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003406 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003407 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003408 uint32_t arg[5];
3409 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003410 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003411 }
3412 size_t actual_args = 1;
3413 for (size_t param_index = 0; param_index < params_size; param_index++) {
3414 if (actual_args >= expected_args) {
3415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003416 << "'. Expected " << expected_args
3417 << " arguments, processing argument " << actual_args
3418 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003419 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003420 }
3421 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003422 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003423 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003425 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003427 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003428 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003429 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003430 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003431 return res_method;
3432 }
3433 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3434 }
3435 if (actual_args != expected_args) {
3436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3437 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003438 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003439 } else {
3440 return res_method;
3441 }
3442}
3443
Ian Rogers62342ec2013-06-11 10:26:37 -07003444void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003445 uint32_t type_idx;
3446 if (!is_filled) {
3447 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3448 type_idx = inst->VRegC_22c();
3449 } else if (!is_range) {
3450 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3451 type_idx = inst->VRegB_35c();
3452 } else {
3453 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3454 type_idx = inst->VRegB_3rc();
3455 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003456 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003457 if (res_type.IsConflict()) { // bad class
3458 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003459 } else {
3460 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3461 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003463 } else if (!is_filled) {
3464 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003465 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003466 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003467 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003468 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003469 } else {
3470 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3471 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003472 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003473 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3474 uint32_t arg[5];
3475 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003476 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003477 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003478 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003479 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003480 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3481 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003482 return;
3483 }
3484 }
3485 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003486 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003487 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003488 }
3489 }
3490}
3491
Sebastien Hertz5243e912013-05-21 10:55:07 +02003492void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003493 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003494 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003497 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003498 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003499 if (array_type.IsZero()) {
3500 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3501 // instruction type. TODO: have a proper notion of bottom here.
3502 if (!is_primitive || insn_type.IsCategory1Types()) {
3503 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003504 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003505 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003506 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003507 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3508 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003509 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003510 }
jeffhaofc3144e2012-02-01 17:21:15 -08003511 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003513 } else {
3514 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003515 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003516 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003518 << " source for aget-object";
3519 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003521 << " source for category 1 aget";
3522 } else if (is_primitive && !insn_type.Equals(component_type) &&
3523 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003524 (insn_type.IsLong() && component_type.IsDouble()))) {
3525 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3526 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003527 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003528 // Use knowledge of the field type which is stronger than the type inferred from the
3529 // instruction, which can't differentiate object types and ints from floats, longs from
3530 // doubles.
3531 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003532 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003533 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003534 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003535 component_type.HighHalf(&reg_types_));
3536 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003537 }
3538 }
3539 }
3540}
3541
Ian Rogersd8f69b02014-09-10 21:43:52 +00003542void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003543 const uint32_t vregA) {
3544 // Primitive assignability rules are weaker than regular assignability rules.
3545 bool instruction_compatible;
3546 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003547 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003548 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003549 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003550 value_compatible = value_type.IsIntegralTypes();
3551 } else if (target_type.IsFloat()) {
3552 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3553 value_compatible = value_type.IsFloatTypes();
3554 } else if (target_type.IsLong()) {
3555 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003556 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3557 // as target_type depends on the resolved type of the field.
3558 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003559 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003560 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3561 } else {
3562 value_compatible = false;
3563 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003564 } else if (target_type.IsDouble()) {
3565 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003566 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3567 // as target_type depends on the resolved type of the field.
3568 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003569 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003570 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3571 } else {
3572 value_compatible = false;
3573 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003574 } else {
3575 instruction_compatible = false; // reference with primitive store
3576 value_compatible = false; // unused
3577 }
3578 if (!instruction_compatible) {
3579 // This is a global failure rather than a class change failure as the instructions and
3580 // the descriptors for the type should have been consistent within the same file at
3581 // compile time.
3582 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3583 << "' but expected type '" << target_type << "'";
3584 return;
3585 }
3586 if (!value_compatible) {
3587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3588 << " of type " << value_type << " but expected " << target_type << " for put";
3589 return;
3590 }
3591}
3592
Sebastien Hertz5243e912013-05-21 10:55:07 +02003593void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003594 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003595 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003596 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003599 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003600 if (array_type.IsZero()) {
3601 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3602 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003603 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003605 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003606 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003607 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003608 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003609 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003611 if (!component_type.IsReferenceTypes()) {
3612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3613 << " source for aput-object";
3614 } else {
3615 // The instruction agrees with the type of array, confirm the value to be stored does too
3616 // Note: we use the instruction type (rather than the component type) for aput-object as
3617 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003618 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003619 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 }
3621 }
3622 }
3623}
3624
Brian Carlstromea46f952013-07-30 01:26:50 -07003625mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003626 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3627 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003628 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003629 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003630 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3631 field_idx, dex_file_->GetFieldName(field_id),
3632 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003633 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003634 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003635 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003636 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003637 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003638 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003639 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3640 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003641 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003642 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003643 << dex_file_->GetFieldName(field_id) << ") in "
3644 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 DCHECK(self_->IsExceptionPending());
3646 self_->ClearException();
3647 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003648 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3649 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003651 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003652 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 } else if (!field->IsStatic()) {
3654 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003655 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003656 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003657 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003658}
3659
Ian Rogersd8f69b02014-09-10 21:43:52 +00003660mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003661 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3662 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003663 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003664 if (klass_type.IsConflict()) {
3665 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3666 field_idx, dex_file_->GetFieldName(field_id),
3667 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003668 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003669 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003670 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003671 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003672 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003673 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003674 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3675 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003676 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003677 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003678 << dex_file_->GetFieldName(field_id) << ") in "
3679 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003680 DCHECK(self_->IsExceptionPending());
3681 self_->ClearException();
3682 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003683 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3684 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003686 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003687 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003688 } else if (field->IsStatic()) {
3689 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3690 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 } else if (obj_type.IsZero()) {
3693 // Cannot infer and check type, however, access will cause null pointer exception
3694 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003695 } else if (!obj_type.IsReferenceTypes()) {
3696 // Trying to read a field from something that isn't a reference
3697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3698 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003699 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003700 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003701 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003702 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003703 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003704 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003705 if (obj_type.IsUninitializedTypes() &&
3706 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3707 !field_klass.Equals(GetDeclaringClass()))) {
3708 // Field accesses through uninitialized references are only allowable for constructors where
3709 // the field is declared in this class
3710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003711 << " of a not fully initialized object within the context"
3712 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003713 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003714 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3715 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3716 // of C1. For resolution to occur the declared class of the field must be compatible with
3717 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3718 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3719 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003721 } else {
3722 return field;
3723 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 }
3725}
3726
Ian Rogersd8f69b02014-09-10 21:43:52 +00003727void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003728 bool is_primitive, bool is_static) {
3729 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003730 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003731 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003732 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003733 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003734 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003735 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003736 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003737 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003738 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003739 mirror::Class* field_type_class;
3740 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003742 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3743 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3744 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003745 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003746 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003747 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003748 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3750 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003751 }
Ian Rogers0d604842012-04-16 14:50:24 -07003752 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003753 if (field_type == nullptr) {
3754 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3755 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003756 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003757 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003758 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003759 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003761 if (field_type->Equals(insn_type) ||
3762 (field_type->IsFloat() && insn_type.IsInteger()) ||
3763 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003764 // expected that read is of the correct primitive type or that int reads are reading
3765 // floats or long reads are reading doubles
3766 } else {
3767 // This is a global failure rather than a class change failure as the instructions and
3768 // the descriptors for the type should have been consistent within the same file at
3769 // compile time
3770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3771 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003772 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003773 return;
3774 }
3775 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003776 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003777 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3778 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003779 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003780 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003781 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003782 return;
3783 }
3784 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003785 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003787 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003788 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003789 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003790}
3791
Ian Rogersd8f69b02014-09-10 21:43:52 +00003792void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003793 bool is_primitive, bool is_static) {
3794 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003795 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003796 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003797 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003798 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003799 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003800 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003801 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003802 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003803 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003804 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3805 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3806 << " from other class " << GetDeclaringClass();
3807 return;
3808 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003809 mirror::Class* field_type_class;
3810 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003811 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003812 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3813 FieldHelper fh(h_field);
3814 field_type_class = fh.GetType(can_load_classes_);
3815 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003816 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003817 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003818 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003819 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003820 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3821 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003822 }
3823 }
3824 if (field_type == nullptr) {
3825 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3826 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003827 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003828 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003829 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003830 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003832 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003833 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003834 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003835 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3836 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003837 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003838 << "' in put-object";
3839 return;
3840 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003841 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 }
3843}
3844
Brian Carlstromea46f952013-07-30 01:26:50 -07003845mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003846 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003847 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3848 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3849 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3850 inst->Opcode() == Instruction::IPUT_QUICK ||
3851 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3852 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003853 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003854 if (!object_type.HasClass()) {
3855 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3856 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003857 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003858 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003859 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3860 field_offset);
3861 if (f == nullptr) {
3862 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3863 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3864 }
3865 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003866}
3867
Ian Rogersd8f69b02014-09-10 21:43:52 +00003868void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003869 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003870 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003871 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003872 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3874 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003875 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003876 mirror::Class* field_type_class;
3877 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003878 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003879 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3880 FieldHelper fh(h_field);
3881 field_type_class = fh.GetType(can_load_classes_);
3882 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003883 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003884 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003885 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003886 field_type_class->CannotBeAssignedFromOtherTypes());
3887 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003888 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3889 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003890 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003891 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003892 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003893 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003894 const uint32_t vregA = inst->VRegA_22c();
3895 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003896 if (field_type->Equals(insn_type) ||
3897 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3898 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003899 // expected that read is of the correct primitive type or that int reads are reading
3900 // floats or long reads are reading doubles
3901 } else {
3902 // This is a global failure rather than a class change failure as the instructions and
3903 // the descriptors for the type should have been consistent within the same file at
3904 // compile time
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003906 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003907 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003908 return;
3909 }
3910 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003911 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003912 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003913 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003914 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003915 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003916 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003917 return;
3918 }
3919 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003920 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003921 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003922 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003924 }
3925}
3926
Ian Rogersd8f69b02014-09-10 21:43:52 +00003927void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003928 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003929 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Brian Carlstromea46f952013-07-30 01:26:50 -07003930 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003931 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3933 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003934 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003935 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003936 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003937 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003938 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003939 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3940 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003941 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003942 return;
3943 }
3944 }
3945 const uint32_t vregA = inst->VRegA_22c();
3946 if (is_primitive) {
3947 // Primitive field assignability rules are weaker than regular assignability rules
3948 bool instruction_compatible;
3949 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003950 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003951 if (field_type.IsIntegralTypes()) {
3952 instruction_compatible = insn_type.IsIntegralTypes();
3953 value_compatible = value_type.IsIntegralTypes();
3954 } else if (field_type.IsFloat()) {
3955 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3956 value_compatible = value_type.IsFloatTypes();
3957 } else if (field_type.IsLong()) {
3958 instruction_compatible = insn_type.IsLong();
3959 value_compatible = value_type.IsLongTypes();
3960 } else if (field_type.IsDouble()) {
3961 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3962 value_compatible = value_type.IsDoubleTypes();
3963 } else {
3964 instruction_compatible = false; // reference field with primitive store
3965 value_compatible = false; // unused
3966 }
3967 if (!instruction_compatible) {
3968 // This is a global failure rather than a class change failure as the instructions and
3969 // the descriptors for the type should have been consistent within the same file at
3970 // compile time
3971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003972 << " to be of type '" << insn_type
3973 << "' but found type '" << field_type
3974 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003975 return;
3976 }
3977 if (!value_compatible) {
3978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3979 << " of type " << value_type
3980 << " but expected " << field_type
3981 << " for store to " << PrettyField(field) << " in put";
3982 return;
3983 }
3984 } else {
3985 if (!insn_type.IsAssignableFrom(field_type)) {
3986 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003987 << " to be compatible with type '" << insn_type
3988 << "' but found type '" << field_type
3989 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003990 return;
3991 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003992 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003993 }
3994}
3995
Ian Rogers776ac1f2012-04-13 23:36:36 -07003996bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003997 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003999 return false;
4000 }
4001 return true;
4002}
4003
Ian Rogersebbdd872014-07-07 23:53:08 -07004004bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4005 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 bool changed = true;
4007 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4008 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004009 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 * We haven't processed this instruction before, and we haven't touched the registers here, so
4011 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4012 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004013 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004014 if (!insn_flags_[next_insn].IsReturn()) {
4015 target_line->CopyFromLine(merge_line);
4016 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004017 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004019 return false;
4020 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004021 // For returns we only care about the operand to the return, all other registers are dead.
4022 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4023 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4024 Instruction::Code opcode = ret_inst->Opcode();
4025 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004026 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004027 } else {
4028 target_line->CopyFromLine(merge_line);
4029 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004031 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004032 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004033 }
4034 }
4035 }
jeffhaobdb76512011-09-07 11:43:16 -07004036 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004037 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004038 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004039 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004040 if (gDebugVerify) {
4041 copy->CopyFromLine(target_line);
4042 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004044 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004045 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004046 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004047 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004048 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004049 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004050 << copy->Dump(this) << " MERGE\n"
4051 << merge_line->Dump(this) << " ==\n"
4052 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004053 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004054 if (update_merge_line && changed) {
4055 merge_line->CopyFromLine(target_line);
4056 }
jeffhaobdb76512011-09-07 11:43:16 -07004057 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004058 if (changed) {
4059 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004060 }
4061 return true;
4062}
4063
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004064InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004065 return &insn_flags_[work_insn_idx_];
4066}
4067
Ian Rogersd8f69b02014-09-10 21:43:52 +00004068const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004069 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004070 if (mirror_method_.Get() != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004071 StackHandleScope<1> hs(self_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004072 mirror::Class* return_type_class =
4073 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004074 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004075 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4076 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004077 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004078 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004079 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4080 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004081 }
4082 }
4083 if (return_type_ == nullptr) {
4084 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4085 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4086 uint16_t return_type_idx = proto_id.return_type_idx_;
4087 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004088 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004089 }
4090 }
4091 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004092}
4093
Ian Rogersd8f69b02014-09-10 21:43:52 +00004094const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004095 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004096 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004097 const char* descriptor
4098 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004099 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004100 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004101 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4102 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004103 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004104 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004105 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004106 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004107 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004108}
4109
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004110std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4111 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004112 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004113 std::vector<int32_t> result;
4114 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004116 if (type.IsConstant()) {
4117 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004118 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4119 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004120 } else if (type.IsConstantLo()) {
4121 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004122 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4123 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004124 } else if (type.IsConstantHi()) {
4125 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004126 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4127 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004128 } else if (type.IsIntegralTypes()) {
4129 result.push_back(kIntVReg);
4130 result.push_back(0);
4131 } else if (type.IsFloat()) {
4132 result.push_back(kFloatVReg);
4133 result.push_back(0);
4134 } else if (type.IsLong()) {
4135 result.push_back(kLongLoVReg);
4136 result.push_back(0);
4137 result.push_back(kLongHiVReg);
4138 result.push_back(0);
4139 ++i;
4140 } else if (type.IsDouble()) {
4141 result.push_back(kDoubleLoVReg);
4142 result.push_back(0);
4143 result.push_back(kDoubleHiVReg);
4144 result.push_back(0);
4145 ++i;
4146 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4147 result.push_back(kUndefined);
4148 result.push_back(0);
4149 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004150 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004151 result.push_back(kReferenceVReg);
4152 result.push_back(0);
4153 }
4154 }
4155 return result;
4156}
4157
Ian Rogersd8f69b02014-09-10 21:43:52 +00004158const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004159 if (precise) {
4160 // Precise constant type.
4161 return reg_types_.FromCat1Const(value, true);
4162 } else {
4163 // Imprecise constant type.
4164 if (value < -32768) {
4165 return reg_types_.IntConstant();
4166 } else if (value < -128) {
4167 return reg_types_.ShortConstant();
4168 } else if (value < 0) {
4169 return reg_types_.ByteConstant();
4170 } else if (value == 0) {
4171 return reg_types_.Zero();
4172 } else if (value == 1) {
4173 return reg_types_.One();
4174 } else if (value < 128) {
4175 return reg_types_.PosByteConstant();
4176 } else if (value < 32768) {
4177 return reg_types_.PosShortConstant();
4178 } else if (value < 65536) {
4179 return reg_types_.CharConstant();
4180 } else {
4181 return reg_types_.IntConstant();
4182 }
4183 }
4184}
4185
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004186void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004187 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004188}
4189
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004190void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004191 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004192}
jeffhaod1224c72012-02-29 13:43:08 -08004193
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004194void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4195 RegTypeCache::VisitStaticRoots(callback, arg);
4196}
4197
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004198void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4199 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004200}
4201
Ian Rogersd81871c2011-10-03 13:57:23 -07004202} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004203} // namespace art