blob: 0de1e76c6ffe8ab7d5cf469d2e4824e621b9ee3c [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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700130 ConstHandle<mirror::DexCache> dex_cache,
131 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700253 ConstHandle<mirror::DexCache> dex_cache,
254 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700257 ConstHandle<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
Ian Rogers7b078e82014-09-10 14:44:24 -0700300void MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700301 const DexFile* dex_file,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700302 ConstHandle<mirror::DexCache> dex_cache,
303 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700306 ConstHandle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800307 uint32_t method_access_flags) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700308 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700309 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800311 verifier.DumpFailures(os);
312 os << verifier.info_messages_.str();
313 verifier.Dump(os);
314}
315
Ian Rogers7b078e82014-09-10 14:44:24 -0700316MethodVerifier::MethodVerifier(Thread* self,
317 const DexFile* dex_file, ConstHandle<mirror::DexCache> dex_cache,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700318 ConstHandle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700319 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700320 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700321 ConstHandle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700322 bool can_load_classes, bool allow_soft_failures,
323 bool need_precise_constants)
Ian Rogers7b078e82014-09-10 14:44:24 -0700324 : self_(self),
325 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800326 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800327 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700328 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700329 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700330 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800331 dex_file_(dex_file),
332 dex_cache_(dex_cache),
333 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700334 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800335 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700336 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700337 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700338 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700339 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700340 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800341 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800342 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700343 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200344 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700345 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200346 has_check_casts_(false),
347 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800348 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700349 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800350}
351
Mathieu Chartier590fee92013-09-13 13:46:47 -0700352MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800353 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700354 STLDeleteElements(&failure_messages_);
355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700358 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700359 Thread* self = Thread::Current();
360 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700361 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
362 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700363 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700364 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700365 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
366 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700367 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700368 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700369 verifier.FindLocksAtDexPc();
370}
371
372void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700373 CHECK(monitor_enter_dex_pcs_ != nullptr);
374 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700375
376 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
377 // verification. In practice, the phase we want relies on data structures set up by all the
378 // earlier passes, so we just run the full method verification and bail out early when we've
379 // got what we wanted.
380 Verify();
381}
382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700384 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700385 Thread* self = Thread::Current();
386 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700387 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
388 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700389 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700390 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700391 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
392 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200393 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200394}
395
Brian Carlstromea46f952013-07-30 01:26:50 -0700396mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700397 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398
399 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
400 // verification. In practice, the phase we want relies on data structures set up by all the
401 // earlier passes, so we just run the full method verification and bail out early when we've
402 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200403 bool success = Verify();
404 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700405 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200406 }
407 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700408 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700409 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200410 }
411 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
412 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200413}
414
Brian Carlstromea46f952013-07-30 01:26:50 -0700415mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700417 Thread* self = Thread::Current();
418 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700419 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
420 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700421 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700422 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700423 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
424 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200425 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426}
427
Brian Carlstromea46f952013-07-30 01:26:50 -0700428mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700429 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430
431 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
432 // verification. In practice, the phase we want relies on data structures set up by all the
433 // earlier passes, so we just run the full method verification and bail out early when we've
434 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200435 bool success = Verify();
436 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700437 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200438 }
439 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700440 if (register_line == nullptr) {
441 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200442 }
443 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
444 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
445 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446}
447
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700449 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700450 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700453 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700454 } else {
455 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700456 }
jeffhaobdb76512011-09-07 11:43:16 -0700457 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700458 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
459 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
461 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700462 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700463 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700464 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800465 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700466 // Run through the instructions and see if the width checks out.
467 bool result = ComputeWidthsAndCountOps();
468 // Flag instructions guarded by a "try" block and check exception handlers.
469 result = result && ScanTryCatchBlocks();
470 // Perform static instruction verification.
471 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700472 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000473 result = result && VerifyCodeFlow();
474 // Compute information for compiler.
475 if (result && Runtime::Current()->IsCompiler()) {
476 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
477 }
478 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700479}
480
Ian Rogers776ac1f2012-04-13 23:36:36 -0700481std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 switch (error) {
483 case VERIFY_ERROR_NO_CLASS:
484 case VERIFY_ERROR_NO_FIELD:
485 case VERIFY_ERROR_NO_METHOD:
486 case VERIFY_ERROR_ACCESS_CLASS:
487 case VERIFY_ERROR_ACCESS_FIELD:
488 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700489 case VERIFY_ERROR_INSTANTIATION:
490 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800491 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700492 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
493 // class change and instantiation errors into soft verification errors so that we re-verify
494 // at runtime. We may fail to find or to agree on access because of not yet available class
495 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
496 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
497 // paths" that dynamically perform the verification and cause the behavior to be that akin
498 // to an interpreter.
499 error = VERIFY_ERROR_BAD_CLASS_SOFT;
500 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700501 // If we fail again at runtime, mark that this instruction would throw and force this
502 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700503 have_pending_runtime_throw_failure_ = true;
504 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700506 // Indication that verification should be retried at runtime.
507 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700508 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700509 have_pending_hard_failure_ = true;
510 }
511 break;
jeffhaod5347e02012-03-22 17:25:05 -0700512 // Hard verification failures at compile time will still fail at runtime, so the class is
513 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700514 case VERIFY_ERROR_BAD_CLASS_HARD: {
515 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700516 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000517 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800518 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519 have_pending_hard_failure_ = true;
520 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800521 }
522 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700523 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700524 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700525 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700526 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700527 failure_messages_.push_back(failure_message);
528 return *failure_message;
529}
530
Ian Rogers576ca0c2014-06-06 15:58:22 -0700531std::ostream& MethodVerifier::LogVerifyInfo() {
532 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
533 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
534}
535
Ian Rogersad0b3a32012-04-16 14:50:24 -0700536void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
537 size_t failure_num = failure_messages_.size();
538 DCHECK_NE(failure_num, 0U);
539 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
540 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700541 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700542 delete last_fail_message;
543}
544
545void MethodVerifier::AppendToLastFailMessage(std::string append) {
546 size_t failure_num = failure_messages_.size();
547 DCHECK_NE(failure_num, 0U);
548 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
549 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800550}
551
Ian Rogers776ac1f2012-04-13 23:36:36 -0700552bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 const uint16_t* insns = code_item_->insns_;
554 size_t insns_size = code_item_->insns_size_in_code_units_;
555 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700556 size_t new_instance_count = 0;
557 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700559
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700561 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700562 switch (opcode) {
563 case Instruction::APUT_OBJECT:
564 case Instruction::CHECK_CAST:
565 has_check_casts_ = true;
566 break;
567 case Instruction::INVOKE_VIRTUAL:
568 case Instruction::INVOKE_VIRTUAL_RANGE:
569 case Instruction::INVOKE_INTERFACE:
570 case Instruction::INVOKE_INTERFACE_RANGE:
571 has_virtual_or_interface_invokes_ = true;
572 break;
573 case Instruction::MONITOR_ENTER:
574 monitor_enter_count++;
575 break;
576 case Instruction::NEW_INSTANCE:
577 new_instance_count++;
578 break;
579 default:
580 break;
jeffhaobdb76512011-09-07 11:43:16 -0700581 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700583 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700585 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700586 }
587
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
590 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700591 return false;
592 }
593
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 new_instance_count_ = new_instance_count;
595 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700596 return true;
597}
598
Ian Rogers776ac1f2012-04-13 23:36:36 -0700599bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700601 if (tries_size == 0) {
602 return true;
603 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700605 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700606
607 for (uint32_t idx = 0; idx < tries_size; idx++) {
608 const DexFile::TryItem* try_item = &tries[idx];
609 uint32_t start = try_item->start_addr_;
610 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700611 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
613 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700614 return false;
615 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700617 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
618 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700619 return false;
620 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700621 uint32_t dex_pc = start;
622 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
623 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700625 size_t insn_size = inst->SizeInCodeUnits();
626 dex_pc += insn_size;
627 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700628 }
629 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800630 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700631 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700632 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700633 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700634 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700635 CatchHandlerIterator iterator(handlers_ptr);
636 for (; iterator.HasNext(); iterator.Next()) {
637 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700639 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
640 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700641 return false;
642 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700644 // Ensure exception types are resolved so that they don't need resolution to be delivered,
645 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700646 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
648 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700649 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700650 if (exception_type == nullptr) {
651 DCHECK(self_->IsExceptionPending());
652 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700653 }
654 }
jeffhaobdb76512011-09-07 11:43:16 -0700655 }
Ian Rogers0571d352011-11-03 19:51:38 -0700656 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700657 }
jeffhaobdb76512011-09-07 11:43:16 -0700658 return true;
659}
660
Ian Rogers776ac1f2012-04-13 23:36:36 -0700661bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700663
Ian Rogers0c7abda2012-09-19 13:33:42 -0700664 /* 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 -0700665 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700666 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700667
668 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700669 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700671 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 return false;
673 }
674 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700675 // All invoke points are marked as "Throw" points already.
676 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000677 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700678 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000679 } else if (inst->IsReturn()) {
680 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 }
682 dex_pc += inst->SizeInCodeUnits();
683 inst = inst->Next();
684 }
685 return true;
686}
687
Ian Rogers776ac1f2012-04-13 23:36:36 -0700688bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 bool result = true;
690 switch (inst->GetVerifyTypeArgumentA()) {
691 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700692 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 break;
694 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700695 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700696 break;
697 }
698 switch (inst->GetVerifyTypeArgumentB()) {
699 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700700 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 break;
702 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700703 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 break;
705 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700706 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 break;
708 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700709 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 break;
711 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700712 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 break;
714 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700715 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 break;
717 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700718 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 break;
720 }
721 switch (inst->GetVerifyTypeArgumentC()) {
722 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700723 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 break;
725 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700726 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 break;
728 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700729 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 break;
731 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700732 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 break;
734 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700735 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 break;
737 }
738 switch (inst->GetVerifyExtraFlags()) {
739 case Instruction::kVerifyArrayData:
740 result = result && CheckArrayData(code_offset);
741 break;
742 case Instruction::kVerifyBranchTarget:
743 result = result && CheckBranchTarget(code_offset);
744 break;
745 case Instruction::kVerifySwitchTargets:
746 result = result && CheckSwitchTargets(code_offset);
747 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700748 case Instruction::kVerifyVarArgNonZero:
749 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700750 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700751 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
753 "non-range invoke";
754 return false;
755 }
Ian Rogers29a26482014-05-02 15:27:29 -0700756 uint32_t args[Instruction::kMaxVarArgRegs];
757 inst->GetVarArgs(args);
758 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700760 }
Andreas Gampec3314312014-06-19 18:13:29 -0700761 case Instruction::kVerifyVarArgRangeNonZero:
762 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700764 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
765 inst->VRegA() <= 0) {
766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
767 "range invoke";
768 return false;
769 }
Ian Rogers29a26482014-05-02 15:27:29 -0700770 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 break;
772 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 result = false;
775 break;
776 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700777 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) {
778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
779 result = false;
780 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 return result;
782}
783
Ian Rogers7b078e82014-09-10 14:44:24 -0700784inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
787 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 return true;
791}
792
Ian Rogers7b078e82014-09-10 14:44:24 -0700793inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
796 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 return true;
800}
801
Ian Rogers7b078e82014-09-10 14:44:24 -0700802inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
805 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 return false;
807 }
808 return true;
809}
810
Ian Rogers7b078e82014-09-10 14:44:24 -0700811inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
814 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 return true;
818}
819
Ian Rogers7b078e82014-09-10 14:44:24 -0700820inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
823 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 return false;
825 }
826 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700827 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
832 return true;
833}
834
Ian Rogers7b078e82014-09-10 14:44:24 -0700835inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
838 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 return false;
840 }
841 return true;
842}
843
Ian Rogers7b078e82014-09-10 14:44:24 -0700844inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
847 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 return false;
849 }
850 return true;
851}
852
Ian Rogers776ac1f2012-04-13 23:36:36 -0700853bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
856 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 return false;
858 }
859 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700860 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 const char* cp = descriptor;
862 while (*cp++ == '[') {
863 bracket_count++;
864 }
865 if (bracket_count == 0) {
866 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700867 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
868 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700869 return false;
870 } else if (bracket_count > 255) {
871 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
873 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 return false;
875 }
876 return true;
877}
878
Ian Rogers776ac1f2012-04-13 23:36:36 -0700879bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
881 const uint16_t* insns = code_item_->insns_ + cur_offset;
882 const uint16_t* array_data;
883 int32_t array_data_offset;
884
885 DCHECK_LT(cur_offset, insn_count);
886 /* make sure the start of the array data table is in range */
887 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
888 if ((int32_t) cur_offset + array_data_offset < 0 ||
889 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700891 << ", data offset " << array_data_offset
892 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 return false;
894 }
895 /* offset to array data table is a relative branch-style offset */
896 array_data = insns + array_data_offset;
897 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800898 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
900 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 return false;
902 }
903 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700904 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
906 /* make sure the end of the switch is in range */
907 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
909 << ", data offset " << array_data_offset << ", end "
910 << cur_offset + array_data_offset + table_size
911 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 return false;
913 }
914 return true;
915}
916
Ian Rogers776ac1f2012-04-13 23:36:36 -0700917bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 int32_t offset;
919 bool isConditional, selfOkay;
920 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
921 return false;
922 }
923 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
925 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 return false;
927 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700928 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
929 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
932 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
936 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700937 if (abs_offset < 0 ||
938 (uint32_t) abs_offset >= insn_count ||
939 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700941 << reinterpret_cast<void*>(abs_offset) << ") at "
942 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 return false;
944 }
945 insn_flags_[abs_offset].SetBranchTarget();
946 return true;
947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 bool* selfOkay) {
951 const uint16_t* insns = code_item_->insns_ + cur_offset;
952 *pConditional = false;
953 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 switch (*insns & 0xff) {
955 case Instruction::GOTO:
956 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700957 break;
958 case Instruction::GOTO_32:
959 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700960 *selfOkay = true;
961 break;
962 case Instruction::GOTO_16:
963 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 break;
965 case Instruction::IF_EQ:
966 case Instruction::IF_NE:
967 case Instruction::IF_LT:
968 case Instruction::IF_GE:
969 case Instruction::IF_GT:
970 case Instruction::IF_LE:
971 case Instruction::IF_EQZ:
972 case Instruction::IF_NEZ:
973 case Instruction::IF_LTZ:
974 case Instruction::IF_GEZ:
975 case Instruction::IF_GTZ:
976 case Instruction::IF_LEZ:
977 *pOffset = (int16_t) insns[1];
978 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 break;
980 default:
981 return false;
982 break;
983 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 return true;
985}
986
Ian Rogers776ac1f2012-04-13 23:36:36 -0700987bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700989 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700991 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
993 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700995 << ", switch offset " << switch_offset
996 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700997 return false;
998 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700999 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001001 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001002 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1004 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001005 return false;
1006 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 uint32_t switch_count = switch_insns[1];
1008 int32_t keys_offset, targets_offset;
1009 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001010 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1011 /* 0=sig, 1=count, 2/3=firstKey */
1012 targets_offset = 4;
1013 keys_offset = -1;
1014 expected_signature = Instruction::kPackedSwitchSignature;
1015 } else {
1016 /* 0=sig, 1=count, 2..count*2 = keys */
1017 keys_offset = 2;
1018 targets_offset = 2 + 2 * switch_count;
1019 expected_signature = Instruction::kSparseSwitchSignature;
1020 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001022 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001023 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1024 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1025 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001026 return false;
1027 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001028 /* make sure the end of the switch is in range */
1029 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1031 << ", switch offset " << switch_offset
1032 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001033 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001034 return false;
1035 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 /* for a sparse switch, verify the keys are in ascending order */
1037 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1039 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001040 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1041 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1042 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001043 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1044 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001045 return false;
1046 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001047 last_key = key;
1048 }
1049 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001050 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 for (uint32_t targ = 0; targ < switch_count; targ++) {
1052 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1053 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1054 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001055 if (abs_offset < 0 ||
1056 abs_offset >= (int32_t) insn_count ||
1057 !insn_flags_[abs_offset].IsOpcode()) {
1058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1059 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1060 << reinterpret_cast<void*>(cur_offset)
1061 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001062 return false;
1063 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 insn_flags_[abs_offset].SetBranchTarget();
1065 }
1066 return true;
1067}
1068
Ian Rogers776ac1f2012-04-13 23:36:36 -07001069bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001070 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 return false;
1073 }
1074 uint16_t registers_size = code_item_->registers_size_;
1075 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001076 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1078 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 return false;
1080 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 }
1082
1083 return true;
1084}
1085
Ian Rogers776ac1f2012-04-13 23:36:36 -07001086bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 uint16_t registers_size = code_item_->registers_size_;
1088 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1089 // integer overflow when adding them here.
1090 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1092 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001093 return false;
1094 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001095 return true;
1096}
1097
Ian Rogers776ac1f2012-04-13 23:36:36 -07001098bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 uint16_t registers_size = code_item_->registers_size_;
1100 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001101
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001103 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1104 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 }
1106 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001107 reg_table_.Init(kTrackCompilerInterestPoints,
1108 insn_flags_.get(),
1109 insns_size,
1110 registers_size,
1111 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001112
jeffhaobdb76512011-09-07 11:43:16 -07001113
Ian Rogersd0fbd852013-09-24 18:17:04 -07001114 work_line_.reset(RegisterLine::Create(registers_size, this));
1115 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001116
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 /* Initialize register types of method arguments. */
1118 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001119 DCHECK_NE(failures_.size(), 0U);
1120 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001121 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001122 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 return false;
1124 }
1125 /* Perform code flow verification. */
1126 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001127 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001129 }
jeffhaobdb76512011-09-07 11:43:16 -07001130 return true;
1131}
1132
Ian Rogersad0b3a32012-04-16 14:50:24 -07001133std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1134 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001135 for (size_t i = 0; i < failures_.size(); ++i) {
1136 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001137 }
1138 return os;
1139}
1140
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001141extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001142 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001143 v->Dump(std::cerr);
1144}
1145
Ian Rogers776ac1f2012-04-13 23:36:36 -07001146void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001147 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001148 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 return;
jeffhaobdb76512011-09-07 11:43:16 -07001150 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001151 {
1152 os << "Register Types:\n";
1153 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1154 std::ostream indent_os(&indent_filter);
1155 reg_types_.Dump(indent_os);
1156 }
Ian Rogersb4903572012-10-11 11:52:56 -07001157 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1159 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 const Instruction* inst = Instruction::At(code_item_->insns_);
1161 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001162 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001164 if (reg_line != nullptr) {
1165 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001166 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001167 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001168 const bool kDumpHexOfInstruction = false;
1169 if (kDumpHexOfInstruction) {
1170 indent_os << inst->DumpHex(5) << " ";
1171 }
1172 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 inst = inst->Next();
1174 }
jeffhaobdb76512011-09-07 11:43:16 -07001175}
1176
Ian Rogersd81871c2011-10-03 13:57:23 -07001177static bool IsPrimitiveDescriptor(char descriptor) {
1178 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001179 case 'I':
1180 case 'C':
1181 case 'S':
1182 case 'B':
1183 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001184 case 'F':
1185 case 'D':
1186 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001188 default:
1189 return false;
1190 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001191}
1192
Ian Rogers776ac1f2012-04-13 23:36:36 -07001193bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 RegisterLine* reg_line = reg_table_.GetLine(0);
1195 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1196 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001197
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001199 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001201 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1203 // argument as uninitialized. This restricts field access until the superclass constructor is
1204 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001205 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001206 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001207 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 reg_types_.UninitializedThisArgument(declaring_class));
1209 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001210 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001211 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001213 }
1214
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001215 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001216 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001217 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001218
1219 for (; iterator.HasNext(); iterator.Next()) {
1220 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001221 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 LOG(FATAL) << "Null descriptor";
1223 }
1224 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1226 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 return false;
1228 }
1229 switch (descriptor[0]) {
1230 case 'L':
1231 case '[':
1232 // We assume that reference arguments are initialized. The only way it could be otherwise
1233 // (assuming the caller was verified) is if the current method is <init>, but in that case
1234 // it's effectively considered initialized the instant we reach here (in the sense that we
1235 // can return without doing anything or call virtual methods).
1236 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001237 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001238 if (!reg_type.IsNonZeroReferenceTypes()) {
1239 DCHECK(HasFailures());
1240 return false;
1241 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001242 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 }
1244 break;
1245 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001246 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 break;
1248 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001249 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 break;
1251 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001252 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 break;
1254 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001255 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 break;
1257 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001258 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 break;
1260 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001261 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 break;
1263 case 'J':
1264 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001265 if (cur_arg + 1 >= expected_args) {
1266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1267 << " args, found more (" << descriptor << ")";
1268 return false;
1269 }
1270
Ian Rogers7b078e82014-09-10 14:44:24 -07001271 const RegType* lo_half;
1272 const RegType* hi_half;
1273 if (descriptor[0] == 'J') {
1274 lo_half = &reg_types_.LongLo();
1275 hi_half = &reg_types_.LongHi();
1276 } else {
1277 lo_half = &reg_types_.DoubleLo();
1278 hi_half = &reg_types_.DoubleHi();
1279 }
1280 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 cur_arg++;
1282 break;
1283 }
1284 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1286 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 return false;
1288 }
1289 cur_arg++;
1290 }
1291 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1293 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 return false;
1295 }
1296 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1297 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1298 // format. Only major difference from the method argument format is that 'V' is supported.
1299 bool result;
1300 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1301 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001302 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 size_t i = 0;
1304 do {
1305 i++;
1306 } while (descriptor[i] == '['); // process leading [
1307 if (descriptor[i] == 'L') { // object array
1308 do {
1309 i++; // find closing ;
1310 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1311 result = descriptor[i] == ';';
1312 } else { // primitive array
1313 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1314 }
1315 } else if (descriptor[0] == 'L') {
1316 // could be more thorough here, but shouldn't be required
1317 size_t i = 0;
1318 do {
1319 i++;
1320 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1321 result = descriptor[i] == ';';
1322 } else {
1323 result = false;
1324 }
1325 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1327 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 }
1329 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001330}
1331
Ian Rogers776ac1f2012-04-13 23:36:36 -07001332bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 const uint16_t* insns = code_item_->insns_;
1334 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001335
jeffhaobdb76512011-09-07 11:43:16 -07001336 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 insn_flags_[0].SetChanged();
1338 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001339
jeffhaobdb76512011-09-07 11:43:16 -07001340 /* Continue until no instructions are marked "changed". */
1341 while (true) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001342 self_->AllowThreadSuspension();
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1344 uint32_t insn_idx = start_guess;
1345 for (; insn_idx < insns_size; insn_idx++) {
1346 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001347 break;
1348 }
jeffhaobdb76512011-09-07 11:43:16 -07001349 if (insn_idx == insns_size) {
1350 if (start_guess != 0) {
1351 /* try again, starting from the top */
1352 start_guess = 0;
1353 continue;
1354 } else {
1355 /* all flags are clear */
1356 break;
1357 }
1358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 // We carry the working set of registers from instruction to instruction. If this address can
1360 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1361 // "changed" flags, we need to load the set of registers from the table.
1362 // Because we always prefer to continue on to the next instruction, we should never have a
1363 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1364 // target.
1365 work_insn_idx_ = insn_idx;
1366 if (insn_flags_[insn_idx].IsBranchTarget()) {
1367 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001368 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001369 /*
1370 * Sanity check: retrieve the stored register line (assuming
1371 * a full table) and make sure it actually matches.
1372 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001374 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 if (work_line_->CompareLine(register_line) != 0) {
1376 Dump(std::cout);
1377 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001378 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001379 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001380 << " work_line=" << work_line_->Dump(this) << "\n"
1381 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001382 }
jeffhaobdb76512011-09-07 11:43:16 -07001383 }
jeffhaobdb76512011-09-07 11:43:16 -07001384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001386 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001387 prepend += " failed to verify: ";
1388 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001389 return false;
1390 }
jeffhaobdb76512011-09-07 11:43:16 -07001391 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 insn_flags_[insn_idx].SetVisited();
1393 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001395
Ian Rogers1c849e52012-06-28 14:00:33 -07001396 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001397 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001398 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001399 * (besides the wasted space), but it indicates a flaw somewhere
1400 * down the line, possibly in the verifier.
1401 *
1402 * If we've substituted "always throw" instructions into the stream,
1403 * we are almost certainly going to have some dead code.
1404 */
1405 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001407 for (; insn_idx < insns_size;
1408 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001409 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001410 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001411 * may or may not be preceded by a padding NOP (for alignment).
1412 */
1413 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1414 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1415 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001416 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001417 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1418 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1419 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001421 }
1422
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001424 if (dead_start < 0)
1425 dead_start = insn_idx;
1426 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001427 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1428 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001429 dead_start = -1;
1430 }
1431 }
1432 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001433 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1434 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001436 // To dump the state of the verify after a method, do something like:
1437 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1438 // "boolean java.lang.String.equals(java.lang.Object)") {
1439 // LOG(INFO) << info_messages_.str();
1440 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001441 }
jeffhaobdb76512011-09-07 11:43:16 -07001442 return true;
1443}
1444
Ian Rogers776ac1f2012-04-13 23:36:36 -07001445bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001446 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1447 // We want the state _before_ the instruction, for the case where the dex pc we're
1448 // interested in is itself a monitor-enter instruction (which is a likely place
1449 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001450 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001451 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001452 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1453 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1454 }
1455 }
1456
jeffhaobdb76512011-09-07 11:43:16 -07001457 /*
1458 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001459 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001460 * control to another statement:
1461 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001462 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001463 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001464 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001465 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001466 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001467 * throw an exception that is handled by an encompassing "try"
1468 * block.
1469 *
1470 * We can also return, in which case there is no successor instruction
1471 * from this point.
1472 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001473 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001474 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1476 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001477 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001478
jeffhaobdb76512011-09-07 11:43:16 -07001479 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001480 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001481 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001483 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001484 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001485 }
jeffhaobdb76512011-09-07 11:43:16 -07001486
1487 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001488 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001489 * can throw an exception, we will copy/merge this into the "catch"
1490 * address rather than work_line, because we don't want the result
1491 * from the "successful" code path (e.g. a check-cast that "improves"
1492 * a type) to be visible to the exception handler.
1493 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001494 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001496 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001498 }
1499
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001500
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001501 // We need to ensure the work line is consistent while performing validation. When we spot a
1502 // peephole pattern we compute a new line for either the fallthrough instruction or the
1503 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001504 std::unique_ptr<RegisterLine> branch_line;
1505 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001506
Sebastien Hertz5243e912013-05-21 10:55:07 +02001507 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001508 case Instruction::NOP:
1509 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001510 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001511 * a signature that looks like a NOP; if we see one of these in
1512 * the course of executing code then we have a problem.
1513 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001516 }
1517 break;
1518
1519 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001520 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001521 break;
jeffhaobdb76512011-09-07 11:43:16 -07001522 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001523 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 break;
jeffhaobdb76512011-09-07 11:43:16 -07001525 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001526 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001527 break;
1528 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001529 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 break;
jeffhaobdb76512011-09-07 11:43:16 -07001531 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001532 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001533 break;
jeffhaobdb76512011-09-07 11:43:16 -07001534 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001535 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001536 break;
1537 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001538 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 break;
jeffhaobdb76512011-09-07 11:43:16 -07001540 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001541 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001542 break;
jeffhaobdb76512011-09-07 11:43:16 -07001543 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001544 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001545 break;
1546
1547 /*
1548 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001549 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001550 * might want to hold the result in an actual CPU register, so the
1551 * Dalvik spec requires that these only appear immediately after an
1552 * invoke or filled-new-array.
1553 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001554 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001555 * redundant with the reset done below, but it can make the debug info
1556 * easier to read in some cases.)
1557 */
1558 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001559 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001560 break;
1561 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001562 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001563 break;
1564 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001565 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001566 break;
1567
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001569 /*
jeffhao60f83e32012-02-13 17:16:30 -08001570 * This statement can only appear as the first instruction in an exception handler. We verify
1571 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001572 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001573 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001574 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001575 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 }
jeffhaobdb76512011-09-07 11:43:16 -07001577 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001578 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001579 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 }
jeffhaobdb76512011-09-07 11:43:16 -07001582 }
1583 break;
1584 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001585 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001586 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001587 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1590 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 } else {
1592 // Compilers may generate synthetic functions that write byte values into boolean fields.
1593 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001595 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1597 ((return_type.IsBoolean() || return_type.IsByte() ||
1598 return_type.IsShort() || return_type.IsChar()) &&
1599 src_type.IsInteger()));
1600 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001601 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001602 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001603 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001604 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 }
jeffhaobdb76512011-09-07 11:43:16 -07001606 }
1607 }
1608 break;
1609 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001610 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001611 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001612 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001615 } else {
1616 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001619 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001621 }
jeffhaobdb76512011-09-07 11:43:16 -07001622 }
1623 }
1624 break;
1625 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001626 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001627 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001628 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 } else {
1631 /* return_type is the *expected* return type, not register value */
1632 DCHECK(!return_type.IsZero());
1633 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001635 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001636 // Disallow returning uninitialized values and verify that the reference in vAA is an
1637 // instance of the "return_type"
1638 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001639 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1640 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001641 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001642 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1643 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1644 << "' or '" << reg_type << "'";
1645 } else {
1646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1647 << "', but expected from declaration '" << return_type << "'";
1648 }
jeffhaobdb76512011-09-07 11:43:16 -07001649 }
1650 }
1651 }
1652 break;
1653
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001654 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 case Instruction::CONST_4: {
1656 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001657 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001658 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001659 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 }
1661 case Instruction::CONST_16: {
1662 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001663 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001664 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001665 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001666 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001667 case Instruction::CONST: {
1668 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001669 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001670 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001671 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001672 }
1673 case Instruction::CONST_HIGH16: {
1674 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001675 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001676 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001677 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001678 }
jeffhaobdb76512011-09-07 11:43:16 -07001679 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001680 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001682 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1683 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001684 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001685 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 }
1687 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001688 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001689 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1690 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001692 break;
1693 }
1694 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001696 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1697 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001698 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 break;
1700 }
1701 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001702 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001703 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1704 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001705 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001706 break;
1707 }
jeffhaobdb76512011-09-07 11:43:16 -07001708 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001709 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 break;
jeffhaobdb76512011-09-07 11:43:16 -07001711 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001712 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001713 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001715 // Get type from instruction if unresolved then we need an access check
1716 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001717 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001718 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001720 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001721 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001722 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 }
jeffhaobdb76512011-09-07 11:43:16 -07001724 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001725 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001726 break;
1727 case Instruction::MONITOR_EXIT:
1728 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001729 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001730 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001731 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001732 * to the need to handle asynchronous exceptions, a now-deprecated
1733 * feature that Dalvik doesn't support.)
1734 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001735 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001736 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001737 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001738 * structured locking checks are working, the former would have
1739 * failed on the -enter instruction, and the latter is impossible.
1740 *
1741 * This is fortunate, because issue 3221411 prevents us from
1742 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001743 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001744 * some catch blocks (which will show up as "dead" code when
1745 * we skip them here); if we can't, then the code path could be
1746 * "live" so we still need to check it.
1747 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001748 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001749 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001750 break;
1751
Ian Rogers28ad40d2011-10-27 15:19:26 -07001752 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001754 /*
1755 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1756 * could be a "upcast" -- not expected, so we don't try to address it.)
1757 *
1758 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001759 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001760 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1762 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001763 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001764 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001765 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001766 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001767 if (klass != nullptr && klass->IsPrimitive()) {
1768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1769 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1770 << GetDeclaringClass();
1771 break;
1772 }
1773
Ian Rogersad0b3a32012-04-16 14:50:24 -07001774 DCHECK_NE(failures_.size(), 0U);
1775 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001776 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001777 }
1778 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001779 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001780 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001782 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001783 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 if (is_checkcast) {
1785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1786 } else {
1787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1788 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001789 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 if (is_checkcast) {
1791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1792 } else {
1793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1794 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001795 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001796 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001797 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001799 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001800 }
jeffhaobdb76512011-09-07 11:43:16 -07001801 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001802 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 }
1804 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001806 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001807 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001808 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001810 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001812 } else {
1813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001814 }
1815 break;
1816 }
1817 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001818 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001819 if (res_type.IsConflict()) {
1820 DCHECK_NE(failures_.size(), 0U);
1821 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001822 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001823 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1824 // can't create an instance of an interface or abstract class */
1825 if (!res_type.IsInstantiableTypes()) {
1826 Fail(VERIFY_ERROR_INSTANTIATION)
1827 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001828 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001830 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001831 // Any registers holding previous allocations from this address that have not yet been
1832 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001834 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001835 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 break;
1837 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001838 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001840 break;
1841 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001843 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001844 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001845 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001846 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001847 just_set_result = true; // Filled new array range sets result register
1848 break;
jeffhaobdb76512011-09-07 11:43:16 -07001849 case Instruction::CMPL_FLOAT:
1850 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001852 break;
1853 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001855 break;
1856 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001857 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001858 break;
1859 case Instruction::CMPL_DOUBLE:
1860 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001863 break;
1864 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001865 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001866 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001867 break;
1868 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
1871 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07001872 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001873 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001874 break;
1875 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001877 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001878 break;
1879 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001881 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001883 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001884 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001885 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1886 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001887 }
1888 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 }
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::GOTO:
1891 case Instruction::GOTO_16:
1892 case Instruction::GOTO_32:
1893 /* no effect on or use of registers */
1894 break;
1895
1896 case Instruction::PACKED_SWITCH:
1897 case Instruction::SPARSE_SWITCH:
1898 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07001899 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001900 break;
1901
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 case Instruction::FILL_ARRAY_DATA: {
1903 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001905 /* array_type can be null if the reg type is Zero */
1906 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001907 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1909 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001910 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001911 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001912 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001913 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1915 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 } else {
jeffhao457cc512012-02-02 16:55:13 -08001917 // Now verify if the element width in the table matches the element width declared in
1918 // the array
1919 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1920 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001922 } else {
1923 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1924 // Since we don't compress the data in Dex, expect to see equal width of data stored
1925 // in the table and expected from the array class.
1926 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1928 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001929 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 }
1931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 }
1933 }
1934 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 }
jeffhaobdb76512011-09-07 11:43:16 -07001936 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1939 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 bool mismatch = false;
1941 if (reg_type1.IsZero()) { // zero then integral or reference expected
1942 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1943 } else if (reg_type1.IsReferenceTypes()) { // both references?
1944 mismatch = !reg_type2.IsReferenceTypes();
1945 } else { // both integral?
1946 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1947 }
1948 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1950 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001951 }
1952 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 }
jeffhaobdb76512011-09-07 11:43:16 -07001954 case Instruction::IF_LT:
1955 case Instruction::IF_GE:
1956 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
1959 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1962 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001963 }
1964 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 }
jeffhaobdb76512011-09-07 11:43:16 -07001966 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1971 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001973
1974 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001975 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001976 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001977 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001978 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001979 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001980 }
Ian Rogers9b360392013-06-06 14:45:07 -07001981 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001982 } else {
1983 break;
1984 }
1985
Ian Rogers9b360392013-06-06 14:45:07 -07001986 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001987
1988 /* Check for peep-hole pattern of:
1989 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001990 * instance-of vX, vY, T;
1991 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001992 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001993 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001994 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001995 * and sharpen the type of vY to be type T.
1996 * Note, this pattern can't be if:
1997 * - if there are other branches to this branch,
1998 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001999 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002000 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002001 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2002 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2003 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002004 // Check the type of the instance-of is different than that of registers type, as if they
2005 // are the same there is no work to be done here. Check that the conversion is not to or
2006 // from an unresolved type as type information is imprecise. If the instance-of is to an
2007 // interface then ignore the type information as interfaces can only be treated as Objects
2008 // and we don't want to disallow field and other operations on the object. If the value
2009 // being instance-of checked against is known null (zero) then allow the optimization as
2010 // we didn't have type information. If the merge of the instance-of type with the original
2011 // type is assignable to the original then allow optimization. This check is performed to
2012 // ensure that subsequent merges don't lose type information - such as becoming an
2013 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002015 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002016
Ian Rogersebbdd872014-07-07 23:53:08 -07002017 if (!orig_type.Equals(cast_type) &&
2018 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002019 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002020 !cast_type.GetClass()->IsInterface() &&
2021 (orig_type.IsZero() ||
2022 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002023 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002024 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002025 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002026 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002027 branch_line.reset(update_line);
2028 }
2029 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002031 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2032 // See if instance-of was preceded by a move-object operation, common due to the small
2033 // register encoding space of instance-of, and propagate type information to the source
2034 // of the move-object.
2035 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002036 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002037 move_idx--;
2038 }
2039 CHECK(insn_flags_[move_idx].IsOpcode());
2040 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2041 switch (move_inst->Opcode()) {
2042 case Instruction::MOVE_OBJECT:
2043 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002044 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002045 }
2046 break;
2047 case Instruction::MOVE_OBJECT_FROM16:
2048 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002049 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002050 }
2051 break;
2052 case Instruction::MOVE_OBJECT_16:
2053 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002055 }
2056 break;
2057 default:
2058 break;
2059 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002060 }
2061 }
2062 }
2063
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 }
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::IF_LTZ:
2067 case Instruction::IF_GEZ:
2068 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002070 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2073 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 }
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 }
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 break;
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 break;
jeffhaobdb76512011-09-07 11:43:16 -07002083 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
2095 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 break;
2108 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002110 break;
2111 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120
jeffhaobdb76512011-09-07 11:43:16 -07002121 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 break;
jeffhaobdb76512011-09-07 11:43:16 -07002124 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 break;
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 break;
jeffhaobdb76512011-09-07 11:43:16 -07002130 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 break;
2133 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 break;
2146 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002148 break;
2149 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 break;
2152 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002153 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002154 break;
2155 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002156 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 break;
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164
jeffhaobdb76512011-09-07 11:43:16 -07002165 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 break;
jeffhaobdb76512011-09-07 11:43:16 -07002168 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002170 break;
jeffhaobdb76512011-09-07 11:43:16 -07002171 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 break;
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 break;
2177 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
2180 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002182 break;
2183 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002184 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 break;
2186
2187 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002189 break;
2190 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002191 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 break;
2193 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002194 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002195 break;
2196 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002198 break;
2199 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002200 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
2202 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002204 break;
2205 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002207 break;
2208
2209 case Instruction::INVOKE_VIRTUAL:
2210 case Instruction::INVOKE_VIRTUAL_RANGE:
2211 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002212 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2214 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002215 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2216 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002217 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2218 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002219 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002220 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002221 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002222 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2223 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002224 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002225 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002226 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2227 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002228 return_type_class->CannotBeAssignedFromOtherTypes());
2229 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002230 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2231 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002232 }
2233 }
2234 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002236 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2237 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002238 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002239 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002240 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002241 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002244 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002245 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002246 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 }
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002252 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002253 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002254 const char* return_type_descriptor;
2255 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002256 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002259 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002260 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002261 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2262 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2263 } else {
2264 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002265 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002266 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002267 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2268 MethodHelper mh(h_called_method);
2269 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2270 if (return_type_class != nullptr) {
2271 return_type = &reg_types_.FromClass(return_type_descriptor,
2272 return_type_class,
2273 return_type_class->CannotBeAssignedFromOtherTypes());
2274 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002275 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2276 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002277 }
Ian Rogers46685432012-06-03 22:26:43 -07002278 }
2279 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002280 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 * Some additional checks when calling a constructor. We know from the invocation arg check
2282 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2283 * that to require that called_method->klass is the same as this->klass or this->super,
2284 * allowing the latter only if the "this" argument is the same as the "this" argument to
2285 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002286 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002287 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002288 if (this_type.IsConflict()) // failure.
2289 break;
jeffhaobdb76512011-09-07 11:43:16 -07002290
jeffhaob57e9522012-04-26 18:08:21 -07002291 /* no null refs allowed (?) */
2292 if (this_type.IsZero()) {
2293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2294 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002295 }
jeffhaob57e9522012-04-26 18:08:21 -07002296
2297 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002298 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002299 // TODO: re-enable constructor type verification
2300 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002301 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002302 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2303 // break;
2304 // }
jeffhaob57e9522012-04-26 18:08:21 -07002305
2306 /* arg must be an uninitialized reference */
2307 if (!this_type.IsUninitializedTypes()) {
2308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2309 << this_type;
2310 break;
2311 }
2312
2313 /*
2314 * Replace the uninitialized reference with an initialized one. We need to do this for all
2315 * registers that have the same object instance in them, not just the "this" register.
2316 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002318 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002319 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002320 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2321 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002322 }
2323 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002326 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002328 just_set_result = true;
2329 break;
2330 }
2331 case Instruction::INVOKE_STATIC:
2332 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002334 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002335 METHOD_STATIC,
2336 is_range,
2337 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002338 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002339 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002341 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2342 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002343 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002344 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002345 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002347 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002349 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 } else {
2351 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2352 }
jeffhaobdb76512011-09-07 11:43:16 -07002353 just_set_result = true;
2354 }
2355 break;
jeffhaobdb76512011-09-07 11:43:16 -07002356 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002357 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002359 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002360 METHOD_INTERFACE,
2361 is_range,
2362 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002363 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002364 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002365 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2366 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2367 << PrettyMethod(abs_method) << "'";
2368 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002369 }
Ian Rogers0d604842012-04-16 14:50:24 -07002370 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002371 /* Get the type of the "this" arg, which should either be a sub-interface of called
2372 * interface or Object (see comments in RegType::JoinClass).
2373 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 if (this_type.IsZero()) {
2376 /* null pointer always passes (and always fails at runtime) */
2377 } else {
2378 if (this_type.IsUninitializedTypes()) {
2379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2380 << this_type;
2381 break;
2382 }
2383 // In the past we have tried to assert that "called_interface" is assignable
2384 // from "this_type.GetClass()", however, as we do an imprecise Join
2385 // (RegType::JoinClass) we don't have full information on what interfaces are
2386 // implemented by "this_type". For example, two classes may implement the same
2387 // interfaces and have a common parent that doesn't implement the interface. The
2388 // join will set "this_type" to the parent class and a test that this implements
2389 // the interface will incorrectly fail.
2390 }
2391 /*
2392 * We don't have an object instance, so we can't find the concrete method. However, all of
2393 * the type information is in the abstract method, so we're good.
2394 */
2395 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002396 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002398 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2399 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2400 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2401 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002402 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002403 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002404 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 } else {
2408 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2409 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002410 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 }
jeffhaobdb76512011-09-07 11:43:16 -07002413 case Instruction::NEG_INT:
2414 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002415 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417 case Instruction::NEG_LONG:
2418 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002426 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002427 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002430 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002431 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002434 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002435 break;
2436 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002437 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002438 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002442 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002445 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002446 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002449 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002450 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002451 break;
2452 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002453 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
2455 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002456 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002457 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002460 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002461 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002464 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002465 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002469 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002472 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002473 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002474 break;
2475 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002476 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002477 break;
2478 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002479 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002482 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484
2485 case Instruction::ADD_INT:
2486 case Instruction::SUB_INT:
2487 case Instruction::MUL_INT:
2488 case Instruction::REM_INT:
2489 case Instruction::DIV_INT:
2490 case Instruction::SHL_INT:
2491 case Instruction::SHR_INT:
2492 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002493 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002494 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::AND_INT:
2497 case Instruction::OR_INT:
2498 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002499 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002500 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 case Instruction::ADD_LONG:
2503 case Instruction::SUB_LONG:
2504 case Instruction::MUL_LONG:
2505 case Instruction::DIV_LONG:
2506 case Instruction::REM_LONG:
2507 case Instruction::AND_LONG:
2508 case Instruction::OR_LONG:
2509 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002510 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002511 reg_types_.LongLo(), reg_types_.LongHi(),
2512 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::SHL_LONG:
2515 case Instruction::SHR_LONG:
2516 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002518 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002519 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::ADD_FLOAT:
2522 case Instruction::SUB_FLOAT:
2523 case Instruction::MUL_FLOAT:
2524 case Instruction::DIV_FLOAT:
2525 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002526 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2527 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::ADD_DOUBLE:
2530 case Instruction::SUB_DOUBLE:
2531 case Instruction::MUL_DOUBLE:
2532 case Instruction::DIV_DOUBLE:
2533 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002535 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2536 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::ADD_INT_2ADDR:
2539 case Instruction::SUB_INT_2ADDR:
2540 case Instruction::MUL_INT_2ADDR:
2541 case Instruction::REM_INT_2ADDR:
2542 case Instruction::SHL_INT_2ADDR:
2543 case Instruction::SHR_INT_2ADDR:
2544 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002545 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2546 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::AND_INT_2ADDR:
2549 case Instruction::OR_INT_2ADDR:
2550 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002551 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2552 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002553 break;
2554 case Instruction::DIV_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::ADD_LONG_2ADDR:
2559 case Instruction::SUB_LONG_2ADDR:
2560 case Instruction::MUL_LONG_2ADDR:
2561 case Instruction::DIV_LONG_2ADDR:
2562 case Instruction::REM_LONG_2ADDR:
2563 case Instruction::AND_LONG_2ADDR:
2564 case Instruction::OR_LONG_2ADDR:
2565 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002566 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002567 reg_types_.LongLo(), reg_types_.LongHi(),
2568 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::SHL_LONG_2ADDR:
2571 case Instruction::SHR_LONG_2ADDR:
2572 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002573 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002574 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::ADD_FLOAT_2ADDR:
2577 case Instruction::SUB_FLOAT_2ADDR:
2578 case Instruction::MUL_FLOAT_2ADDR:
2579 case Instruction::DIV_FLOAT_2ADDR:
2580 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002581 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2582 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::ADD_DOUBLE_2ADDR:
2585 case Instruction::SUB_DOUBLE_2ADDR:
2586 case Instruction::MUL_DOUBLE_2ADDR:
2587 case Instruction::DIV_DOUBLE_2ADDR:
2588 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002589 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2591 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::ADD_INT_LIT16:
2594 case Instruction::RSUB_INT:
2595 case Instruction::MUL_INT_LIT16:
2596 case Instruction::DIV_INT_LIT16:
2597 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2599 true);
jeffhaobdb76512011-09-07 11:43:16 -07002600 break;
2601 case Instruction::AND_INT_LIT16:
2602 case Instruction::OR_INT_LIT16:
2603 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2605 true);
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::ADD_INT_LIT8:
2608 case Instruction::RSUB_INT_LIT8:
2609 case Instruction::MUL_INT_LIT8:
2610 case Instruction::DIV_INT_LIT8:
2611 case Instruction::REM_INT_LIT8:
2612 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002613 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002614 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002615 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2616 false);
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::AND_INT_LIT8:
2619 case Instruction::OR_INT_LIT8:
2620 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002621 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2622 false);
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002625 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002626 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002627 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2629 }
2630 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002631 // Note: the following instructions encode offsets derived from class linking.
2632 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2633 // meaning if the class linking and resolution were successful.
2634 case Instruction::IGET_QUICK:
2635 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2636 break;
2637 case Instruction::IGET_WIDE_QUICK:
2638 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2639 break;
2640 case Instruction::IGET_OBJECT_QUICK:
2641 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2642 break;
2643 case Instruction::IPUT_QUICK:
2644 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2645 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002646 case Instruction::IPUT_BOOLEAN_QUICK:
2647 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2648 break;
2649 case Instruction::IPUT_BYTE_QUICK:
2650 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2651 break;
2652 case Instruction::IPUT_CHAR_QUICK:
2653 VerifyIPutQuick(inst, reg_types_.Char(), true);
2654 break;
2655 case Instruction::IPUT_SHORT_QUICK:
2656 VerifyIPutQuick(inst, reg_types_.Short(), true);
2657 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002658 case Instruction::IPUT_WIDE_QUICK:
2659 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2660 break;
2661 case Instruction::IPUT_OBJECT_QUICK:
2662 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2663 break;
2664 case Instruction::INVOKE_VIRTUAL_QUICK:
2665 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2666 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002667 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002668 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002669 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002670 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002671 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002672 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002673 } else {
2674 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2675 }
2676 just_set_result = true;
2677 }
2678 break;
2679 }
2680
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002682 case Instruction::UNUSED_3E:
2683 case Instruction::UNUSED_3F:
2684 case Instruction::UNUSED_40:
2685 case Instruction::UNUSED_41:
2686 case Instruction::UNUSED_42:
2687 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002688 case Instruction::UNUSED_79:
2689 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002690 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002691 case Instruction::UNUSED_F0:
2692 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002693 case Instruction::UNUSED_F2:
2694 case Instruction::UNUSED_F3:
2695 case Instruction::UNUSED_F4:
2696 case Instruction::UNUSED_F5:
2697 case Instruction::UNUSED_F6:
2698 case Instruction::UNUSED_F7:
2699 case Instruction::UNUSED_F8:
2700 case Instruction::UNUSED_F9:
2701 case Instruction::UNUSED_FA:
2702 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002703 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002704 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002705 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002706 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709
2710 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002711 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002712 * complain if an instruction is missing (which is desirable).
2713 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002714 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002715
Ian Rogersad0b3a32012-04-16 14:50:24 -07002716 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002717 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002718 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002720 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002721 /* immediate failure, reject class */
2722 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2723 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002724 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002725 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002726 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002727 }
jeffhaobdb76512011-09-07 11:43:16 -07002728 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 * If we didn't just set the result register, clear it out. This ensures that you can only use
2730 * "move-result" immediately after the result is set. (We could check this statically, but it's
2731 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002732 */
2733 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002735 }
2736
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002737
jeffhaobdb76512011-09-07 11:43:16 -07002738
2739 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002740 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002741 *
2742 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002743 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002744 * somebody could get a reference field, check it for zero, and if the
2745 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002746 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002747 * that, and will reject the code.
2748 *
2749 * TODO: avoid re-fetching the branch target
2750 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002751 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002752 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002754 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002756 return false;
2757 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002758 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002759 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002760 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 }
jeffhaobdb76512011-09-07 11:43:16 -07002762 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002763 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002764 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002765 return false;
2766 }
2767 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002768 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002769 return false;
2770 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 }
jeffhaobdb76512011-09-07 11:43:16 -07002772 }
2773
2774 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002775 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002776 *
2777 * We've already verified that the table is structurally sound, so we
2778 * just need to walk through and tag the targets.
2779 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002780 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002781 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2782 const uint16_t* switch_insns = insns + offset_to_switch;
2783 int switch_count = switch_insns[1];
2784 int offset_to_targets, targ;
2785
2786 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2787 /* 0 = sig, 1 = count, 2/3 = first key */
2788 offset_to_targets = 4;
2789 } else {
2790 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002791 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002792 offset_to_targets = 2 + 2 * switch_count;
2793 }
2794
2795 /* verify each switch target */
2796 for (targ = 0; targ < switch_count; targ++) {
2797 int offset;
2798 uint32_t abs_offset;
2799
2800 /* offsets are 32-bit, and only partly endian-swapped */
2801 offset = switch_insns[offset_to_targets + targ * 2] |
2802 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 abs_offset = work_insn_idx_ + offset;
2804 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002805 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002806 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002808 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002809 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002810 }
jeffhaobdb76512011-09-07 11:43:16 -07002811 }
2812 }
2813
2814 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2816 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002817 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002818 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002819 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002820 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002821
Andreas Gampef91baf12014-07-18 15:41:00 -07002822 // Need the linker to try and resolve the handled class to check if it's Throwable.
2823 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2824
Ian Rogers0571d352011-11-03 19:51:38 -07002825 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002826 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2827 if (handler_type_idx == DexFile::kDexNoIndex16) {
2828 has_catch_all_handler = true;
2829 } else {
2830 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002831 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2832 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002833 if (klass != nullptr) {
2834 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2835 has_catch_all_handler = true;
2836 }
2837 } else {
2838 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07002839 DCHECK(self_->IsExceptionPending());
2840 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07002841 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 }
jeffhaobdb76512011-09-07 11:43:16 -07002843 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2845 * "work_regs", because at runtime the exception will be thrown before the instruction
2846 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002847 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002848 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002849 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 }
jeffhaobdb76512011-09-07 11:43:16 -07002851 }
2852
2853 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2855 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002856 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002857 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002858 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 * The state in work_line reflects the post-execution state. If the current instruction is a
2860 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002861 * it will do so before grabbing the lock).
2862 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002863 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002864 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002866 return false;
2867 }
2868 }
2869 }
2870
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002871 /* Handle "continue". Tag the next consecutive instruction.
2872 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2873 * because it changes work_line_ when performing peephole optimization
2874 * and this change should not be used in those cases.
2875 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002876 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002877 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2878 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07002879 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2881 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002882 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002883 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2884 // next instruction isn't one.
2885 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2886 return false;
2887 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002889 // Make workline consistent with fallthrough computed from peephole optimization.
2890 work_line_->CopyFromLine(fallthrough_line.get());
2891 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002892 if (insn_flags_[next_insn_idx].IsReturn()) {
2893 // For returns we only care about the operand to the return, all other registers are dead.
2894 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2895 Instruction::Code opcode = ret_inst->Opcode();
2896 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00002898 } else {
2899 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002901 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002902 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00002903 }
2904 }
2905 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002906 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002908 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2909 // needed. If the merge changes the state of the registers then the work line will be
2910 // updated.
2911 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002912 return false;
2913 }
2914 } else {
2915 /*
2916 * We're not recording register data for the next instruction, so we don't know what the
2917 * prior state was. We have to assume that something has changed and re-evaluate it.
2918 */
2919 insn_flags_[next_insn_idx].SetChanged();
2920 }
2921 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002922
jeffhaod1f0fde2011-09-08 17:25:33 -07002923 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002924 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 return false;
2927 }
jeffhaobdb76512011-09-07 11:43:16 -07002928 }
2929
2930 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002931 * Update start_guess. Advance to the next instruction of that's
2932 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002933 * neither of those exists we're in a return or throw; leave start_guess
2934 * alone and let the caller sort it out.
2935 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002936 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
2938 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002939 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002940 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002942 }
2943
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2945 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002946
2947 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002948} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002949
Ian Rogersd8f69b02014-09-10 21:43:52 +00002950const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002951 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002952 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002953 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07002954 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002955 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2956 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 if (result.IsConflict()) {
2958 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2959 << "' in " << referrer;
2960 return result;
2961 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002963 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002964 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002965 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002966 // check at runtime if access is allowed and so pass here. If result is
2967 // primitive, skip the access check.
2968 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2969 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002970 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002972 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002973 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002974}
2975
Ian Rogersd8f69b02014-09-10 21:43:52 +00002976const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07002977 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07002978 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002979 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2981 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002982 CatchHandlerIterator iterator(handlers_ptr);
2983 for (; iterator.HasNext(); iterator.Next()) {
2984 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2985 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002986 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002988 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002989 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002990 if (exception.IsUnresolvedTypes()) {
2991 // We don't know enough about the type. Fail here and let runtime handle it.
2992 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2993 return exception;
2994 } else {
2995 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2996 return reg_types_.Conflict();
2997 }
Jeff Haob878f212014-04-24 16:25:36 -07002998 } else if (common_super == nullptr) {
2999 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003000 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003001 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003003 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07003004 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 }
3006 }
3007 }
3008 }
Ian Rogers0571d352011-11-03 19:51:38 -07003009 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 }
3011 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003014 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003015 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003017 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003018}
3019
Brian Carlstromea46f952013-07-30 01:26:50 -07003020mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003021 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003022 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003023 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003024 if (klass_type.IsConflict()) {
3025 std::string append(" in attempt to access method ");
3026 append += dex_file_->GetMethodName(method_id);
3027 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003028 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003029 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003030 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003031 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003032 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003033 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003034 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003035 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003037 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003038 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003039
3040 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003041 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003042 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 res_method = klass->FindInterfaceMethod(name, signature);
3044 } else {
3045 res_method = klass->FindVirtualMethod(name, signature);
3046 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003047 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003048 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003050 // If a virtual or interface method wasn't found with the expected type, look in
3051 // the direct methods. This can happen when the wrong invoke type is used or when
3052 // a class has changed, and will be flagged as an error in later checks.
3053 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3054 res_method = klass->FindDirectMethod(name, signature);
3055 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003057 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3058 << PrettyDescriptor(klass) << "." << name
3059 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003061 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 }
3063 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3065 // enforce them here.
3066 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3068 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003071 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003072 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3074 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003076 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003077 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003078 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003079 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003080 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003081 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003082 }
jeffhaode0d9c92012-02-27 13:58:13 -08003083 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3084 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3086 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003087 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003088 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003089 // Check that interface methods match interface classes.
3090 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3091 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3092 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003094 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3095 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3096 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003098 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 // See if the method type implied by the invoke instruction matches the access flags for the
3100 // target method.
3101 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3102 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3103 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3104 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003105 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3106 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003107 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003109 return res_method;
3110}
3111
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003112template <class T>
3113mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3114 MethodType method_type,
3115 bool is_range,
3116 mirror::ArtMethod* res_method) {
3117 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3118 // match the call to the signature. Also, we might be calling through an abstract method
3119 // definition (which doesn't have register count values).
3120 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3121 /* caught by static verifier */
3122 DCHECK(is_range || expected_args <= 5);
3123 if (expected_args > code_item_->outs_size_) {
3124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3125 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3126 return nullptr;
3127 }
3128
3129 uint32_t arg[5];
3130 if (!is_range) {
3131 inst->GetVarArgs(arg);
3132 }
3133 uint32_t sig_registers = 0;
3134
3135 /*
3136 * Check the "this" argument, which must be an instance of the class that declared the method.
3137 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3138 * rigorous check here (which is okay since we have to do it at runtime).
3139 */
3140 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003141 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003142 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3143 CHECK(have_pending_hard_failure_);
3144 return nullptr;
3145 }
3146 if (actual_arg_type.IsUninitializedReference()) {
3147 if (res_method) {
3148 if (!res_method->IsConstructor()) {
3149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3150 return nullptr;
3151 }
3152 } else {
3153 // Check whether the name of the called method is "<init>"
3154 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003155 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3157 return nullptr;
3158 }
3159 }
3160 }
3161 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003162 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003163 if (res_method != nullptr) {
3164 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003165 std::string temp;
3166 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003167 klass->CannotBeAssignedFromOtherTypes());
3168 } else {
3169 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3170 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003171 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003172 dex_file_->StringByTypeIdx(class_idx),
3173 false);
3174 }
3175 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3176 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3177 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3178 << "' not instance of '" << *res_method_class << "'";
3179 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3180 // the compiler.
3181 if (have_pending_hard_failure_) {
3182 return nullptr;
3183 }
3184 }
3185 }
3186 sig_registers = 1;
3187 }
3188
3189 for ( ; it->HasNext(); it->Next()) {
3190 if (sig_registers >= expected_args) {
3191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3192 " arguments, found " << sig_registers << " or more.";
3193 return nullptr;
3194 }
3195
3196 const char* param_descriptor = it->GetDescriptor();
3197
3198 if (param_descriptor == nullptr) {
3199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3200 "component";
3201 return nullptr;
3202 }
3203
Ian Rogersd8f69b02014-09-10 21:43:52 +00003204 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003205 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3206 arg[sig_registers];
3207 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003208 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003209 if (!src_type.IsIntegralTypes()) {
3210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3211 << " but expected " << reg_type;
3212 return res_method;
3213 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003215 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3216 // compiler.
3217 if (have_pending_hard_failure_) {
3218 return res_method;
3219 }
3220 }
3221 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3222 }
3223 if (expected_args != sig_registers) {
3224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3225 " arguments, found " << sig_registers;
3226 return nullptr;
3227 }
3228 return res_method;
3229}
3230
3231void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3232 MethodType method_type,
3233 bool is_range) {
3234 // As the method may not have been resolved, make this static check against what we expect.
3235 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3236 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3237 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3238 DexFileParameterIterator it(*dex_file_,
3239 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3240 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3241 nullptr);
3242}
3243
3244class MethodParamListDescriptorIterator {
3245 public:
3246 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3247 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3248 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3249 }
3250
3251 bool HasNext() {
3252 return pos_ < params_size_;
3253 }
3254
3255 void Next() {
3256 ++pos_;
3257 }
3258
3259 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3260 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3261 }
3262
3263 private:
3264 mirror::ArtMethod* res_method_;
3265 size_t pos_;
3266 const DexFile::TypeList* params_;
3267 const size_t params_size_;
3268};
3269
Brian Carlstromea46f952013-07-30 01:26:50 -07003270mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003271 MethodType method_type,
3272 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003273 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003274 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3275 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003276 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003277
Brian Carlstromea46f952013-07-30 01:26:50 -07003278 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003279 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003280 // Check what we can statically.
3281 if (!have_pending_hard_failure_) {
3282 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3283 }
3284 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003285 }
3286
Ian Rogersd81871c2011-10-03 13:57:23 -07003287 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3288 // has a vtable entry for the target method.
3289 if (is_super) {
3290 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003291 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003292 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003293 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003294 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003295 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003296 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003297 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003298 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003299 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003300 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003301 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003302 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003303 << "." << res_method->GetName()
3304 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003305 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 }
3307 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003308
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003309 // Process the target method's signature. This signature may or may not
3310 MethodParamListDescriptorIterator it(res_method);
3311 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3312 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003313}
3314
Brian Carlstromea46f952013-07-30 01:26:50 -07003315mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003316 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003317 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3318 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003319 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003320 if (!actual_arg_type.HasClass()) {
3321 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003322 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003323 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003324 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003325 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003326 if (klass->IsInterface()) {
3327 // Derive Object.class from Class.class.getSuperclass().
3328 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3329 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003330 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003331 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003332 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003333 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003334 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003335 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003336 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3337 << PrettyDescriptor(klass);
3338 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Ian Rogers7b078e82014-09-10 14:44:24 -07003339 CHECK(!self_->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003340 return res_method;
3341}
3342
Brian Carlstromea46f952013-07-30 01:26:50 -07003343mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003344 bool is_range) {
3345 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003346 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003347 is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003348 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003350 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003351 }
3352 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3353
3354 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3355 // match the call to the signature. Also, we might be calling through an abstract method
3356 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003357 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003358 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003359 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003360 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003361 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3362 /* caught by static verifier */
3363 DCHECK(is_range || expected_args <= 5);
3364 if (expected_args > code_item_->outs_size_) {
3365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3366 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003367 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003368 }
3369
3370 /*
3371 * Check the "this" argument, which must be an instance of the class that declared the method.
3372 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3373 * rigorous check here (which is okay since we have to do it at runtime).
3374 */
3375 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003377 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003378 }
3379 if (!actual_arg_type.IsZero()) {
3380 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003381 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003382 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003383 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003384 klass->CannotBeAssignedFromOtherTypes());
3385 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003386 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3387 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003389 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003390 }
3391 }
3392 /*
3393 * Process the target method's signature. This signature may or may not
3394 * have been verified, so we can't assume it's properly formed.
3395 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003396 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003397 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003398 uint32_t arg[5];
3399 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003400 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003401 }
3402 size_t actual_args = 1;
3403 for (size_t param_index = 0; param_index < params_size; param_index++) {
3404 if (actual_args >= expected_args) {
3405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003406 << "'. Expected " << expected_args
3407 << " arguments, processing argument " << actual_args
3408 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003409 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003410 }
3411 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003412 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003413 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003415 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003416 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003417 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003418 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003419 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003420 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003421 return res_method;
3422 }
3423 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3424 }
3425 if (actual_args != expected_args) {
3426 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3427 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003429 } else {
3430 return res_method;
3431 }
3432}
3433
Ian Rogers62342ec2013-06-11 10:26:37 -07003434void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003435 uint32_t type_idx;
3436 if (!is_filled) {
3437 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3438 type_idx = inst->VRegC_22c();
3439 } else if (!is_range) {
3440 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3441 type_idx = inst->VRegB_35c();
3442 } else {
3443 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3444 type_idx = inst->VRegB_3rc();
3445 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003446 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 if (res_type.IsConflict()) { // bad class
3448 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003449 } else {
3450 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3451 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003453 } else if (!is_filled) {
3454 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003455 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003456 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003457 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003458 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003459 } else {
3460 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3461 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003462 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003463 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3464 uint32_t arg[5];
3465 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003466 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003467 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003468 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003469 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003470 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3471 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003472 return;
3473 }
3474 }
3475 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003476 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003478 }
3479 }
3480}
3481
Sebastien Hertz5243e912013-05-21 10:55:07 +02003482void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003483 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003484 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003487 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003488 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003489 if (array_type.IsZero()) {
3490 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3491 // instruction type. TODO: have a proper notion of bottom here.
3492 if (!is_primitive || insn_type.IsCategory1Types()) {
3493 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003494 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003496 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003497 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3498 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003499 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003500 }
jeffhaofc3144e2012-02-01 17:21:15 -08003501 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003503 } else {
3504 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003505 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003506 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003508 << " source for aget-object";
3509 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003511 << " source for category 1 aget";
3512 } else if (is_primitive && !insn_type.Equals(component_type) &&
3513 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003514 (insn_type.IsLong() && component_type.IsDouble()))) {
3515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3516 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003517 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003518 // Use knowledge of the field type which is stronger than the type inferred from the
3519 // instruction, which can't differentiate object types and ints from floats, longs from
3520 // doubles.
3521 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003522 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003523 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003524 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003525 component_type.HighHalf(&reg_types_));
3526 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 }
3528 }
3529 }
3530}
3531
Ian Rogersd8f69b02014-09-10 21:43:52 +00003532void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003533 const uint32_t vregA) {
3534 // Primitive assignability rules are weaker than regular assignability rules.
3535 bool instruction_compatible;
3536 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003537 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003538 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003539 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003540 value_compatible = value_type.IsIntegralTypes();
3541 } else if (target_type.IsFloat()) {
3542 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3543 value_compatible = value_type.IsFloatTypes();
3544 } else if (target_type.IsLong()) {
3545 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003546 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3547 // as target_type depends on the resolved type of the field.
3548 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003549 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003550 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3551 } else {
3552 value_compatible = false;
3553 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003554 } else if (target_type.IsDouble()) {
3555 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
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.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3561 } else {
3562 value_compatible = false;
3563 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003564 } else {
3565 instruction_compatible = false; // reference with primitive store
3566 value_compatible = false; // unused
3567 }
3568 if (!instruction_compatible) {
3569 // This is a global failure rather than a class change failure as the instructions and
3570 // the descriptors for the type should have been consistent within the same file at
3571 // compile time.
3572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3573 << "' but expected type '" << target_type << "'";
3574 return;
3575 }
3576 if (!value_compatible) {
3577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3578 << " of type " << value_type << " but expected " << target_type << " for put";
3579 return;
3580 }
3581}
3582
Sebastien Hertz5243e912013-05-21 10:55:07 +02003583void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003584 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003585 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003588 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003589 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003590 if (array_type.IsZero()) {
3591 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3592 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003593 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003595 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003596 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003597 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003598 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003599 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003601 if (!component_type.IsReferenceTypes()) {
3602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3603 << " source for aput-object";
3604 } else {
3605 // The instruction agrees with the type of array, confirm the value to be stored does too
3606 // Note: we use the instruction type (rather than the component type) for aput-object as
3607 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003609 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 }
3611 }
3612 }
3613}
3614
Brian Carlstromea46f952013-07-30 01:26:50 -07003615mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003616 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3617 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003618 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003619 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003620 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3621 field_idx, dex_file_->GetFieldName(field_id),
3622 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003624 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003625 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003626 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003627 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003628 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003629 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3630 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003631 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003632 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003633 << dex_file_->GetFieldName(field_id) << ") in "
3634 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 DCHECK(self_->IsExceptionPending());
3636 self_->ClearException();
3637 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003638 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3639 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003641 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003643 } else if (!field->IsStatic()) {
3644 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003647 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003648}
3649
Ian Rogersd8f69b02014-09-10 21:43:52 +00003650mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003651 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3652 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003653 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003654 if (klass_type.IsConflict()) {
3655 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3656 field_idx, dex_file_->GetFieldName(field_id),
3657 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003658 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003659 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003660 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003661 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003662 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003663 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003664 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3665 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003667 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003668 << dex_file_->GetFieldName(field_id) << ") in "
3669 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003670 DCHECK(self_->IsExceptionPending());
3671 self_->ClearException();
3672 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003673 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3674 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003676 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003677 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 } else if (field->IsStatic()) {
3679 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3680 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 } else if (obj_type.IsZero()) {
3683 // Cannot infer and check type, however, access will cause null pointer exception
3684 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003685 } else if (!obj_type.IsReferenceTypes()) {
3686 // Trying to read a field from something that isn't a reference
3687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3688 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003689 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003690 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003691 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003692 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003693 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003694 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003695 if (obj_type.IsUninitializedTypes() &&
3696 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3697 !field_klass.Equals(GetDeclaringClass()))) {
3698 // Field accesses through uninitialized references are only allowable for constructors where
3699 // the field is declared in this class
3700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003701 << " of a not fully initialized object within the context"
3702 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003703 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003704 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3705 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3706 // of C1. For resolution to occur the declared class of the field must be compatible with
3707 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3708 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3709 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003711 } else {
3712 return field;
3713 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 }
3715}
3716
Ian Rogersd8f69b02014-09-10 21:43:52 +00003717void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003718 bool is_primitive, bool is_static) {
3719 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003720 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003721 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003722 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003723 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003724 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003725 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003726 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003727 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003728 if (field != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003729 mirror::Class* field_type_class;
3730 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003731 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003732 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3733 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3734 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003735 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003736 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003737 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003738 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003739 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3740 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003741 }
Ian Rogers0d604842012-04-16 14:50:24 -07003742 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003743 if (field_type == nullptr) {
3744 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3745 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003746 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003747 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003748 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003749 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003750 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003751 if (field_type->Equals(insn_type) ||
3752 (field_type->IsFloat() && insn_type.IsInteger()) ||
3753 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003754 // expected that read is of the correct primitive type or that int reads are reading
3755 // floats or long reads are reading doubles
3756 } else {
3757 // This is a global failure rather than a class change failure as the instructions and
3758 // the descriptors for the type should have been consistent within the same file at
3759 // compile time
3760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3761 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003762 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003763 return;
3764 }
3765 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003766 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003767 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3768 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003769 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003770 << "' in Get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003771 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003772 return;
3773 }
3774 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003775 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 work_line_->SetRegisterType(this, vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003777 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003778 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003780}
3781
Ian Rogersd8f69b02014-09-10 21:43:52 +00003782void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003783 bool is_primitive, bool is_static) {
3784 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003785 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003786 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003787 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003788 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003790 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003791 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003792 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 if (field != nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003794 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3795 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3796 << " from other class " << GetDeclaringClass();
3797 return;
3798 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003799 mirror::Class* field_type_class;
3800 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003802 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3803 FieldHelper fh(h_field);
3804 field_type_class = fh.GetType(can_load_classes_);
3805 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003806 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003807 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003808 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003809 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003810 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3811 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003812 }
3813 }
3814 if (field_type == nullptr) {
3815 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3816 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003817 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003818 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003819 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003820 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003821 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003822 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003823 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003824 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003825 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3826 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003827 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003828 << "' in put-object";
3829 return;
3830 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003831 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003832 }
3833}
3834
Brian Carlstromea46f952013-07-30 01:26:50 -07003835mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003836 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003837 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3838 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3839 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3840 inst->Opcode() == Instruction::IPUT_QUICK ||
3841 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3842 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Ian Rogers7b078e82014-09-10 14:44:24 -07003843 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003844 if (!object_type.HasClass()) {
3845 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3846 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003847 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003848 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003849 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3850 field_offset);
3851 if (f == nullptr) {
3852 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3853 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3854 }
3855 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003856}
3857
Ian Rogersd8f69b02014-09-10 21:43:52 +00003858void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003859 bool is_primitive) {
3860 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003861 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3864 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003865 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003866 mirror::Class* field_type_class;
3867 {
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 StackHandleScope<1> hs(self_);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003869 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3870 FieldHelper fh(h_field);
3871 field_type_class = fh.GetType(can_load_classes_);
3872 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003873 const RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003874 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003875 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 field_type_class->CannotBeAssignedFromOtherTypes());
3877 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003878 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3879 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003880 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003881 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003882 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003883 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003884 const uint32_t vregA = inst->VRegA_22c();
3885 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003886 if (field_type->Equals(insn_type) ||
3887 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3888 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003889 // expected that read is of the correct primitive type or that int reads are reading
3890 // floats or long reads are reading doubles
3891 } else {
3892 // This is a global failure rather than a class change failure as the instructions and
3893 // the descriptors for the type should have been consistent within the same file at
3894 // compile time
3895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003896 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003897 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003898 return;
3899 }
3900 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003901 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003902 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003903 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003904 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003905 << "' in get-object";
Ian Rogers7b078e82014-09-10 14:44:24 -07003906 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003907 return;
3908 }
3909 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003910 if (!field_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003911 work_line_->SetRegisterType(this, vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003912 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003913 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003914 }
3915}
3916
Ian Rogersd8f69b02014-09-10 21:43:52 +00003917void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 bool is_primitive) {
3919 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003920 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07003921 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3923 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003924 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003925 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003926 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003927 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003928 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003929 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3930 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003931 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003932 return;
3933 }
3934 }
3935 const uint32_t vregA = inst->VRegA_22c();
3936 if (is_primitive) {
3937 // Primitive field assignability rules are weaker than regular assignability rules
3938 bool instruction_compatible;
3939 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003940 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003941 if (field_type.IsIntegralTypes()) {
3942 instruction_compatible = insn_type.IsIntegralTypes();
3943 value_compatible = value_type.IsIntegralTypes();
3944 } else if (field_type.IsFloat()) {
3945 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3946 value_compatible = value_type.IsFloatTypes();
3947 } else if (field_type.IsLong()) {
3948 instruction_compatible = insn_type.IsLong();
3949 value_compatible = value_type.IsLongTypes();
3950 } else if (field_type.IsDouble()) {
3951 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3952 value_compatible = value_type.IsDoubleTypes();
3953 } else {
3954 instruction_compatible = false; // reference field with primitive store
3955 value_compatible = false; // unused
3956 }
3957 if (!instruction_compatible) {
3958 // This is a global failure rather than a class change failure as the instructions and
3959 // the descriptors for the type should have been consistent within the same file at
3960 // compile time
3961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003962 << " to be of type '" << insn_type
3963 << "' but found type '" << field_type
3964 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003965 return;
3966 }
3967 if (!value_compatible) {
3968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3969 << " of type " << value_type
3970 << " but expected " << field_type
3971 << " for store to " << PrettyField(field) << " in put";
3972 return;
3973 }
3974 } else {
3975 if (!insn_type.IsAssignableFrom(field_type)) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003977 << " to be compatible with type '" << insn_type
3978 << "' but found type '" << field_type
3979 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003980 return;
3981 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003982 work_line_->VerifyRegisterType(this, vregA, field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003983 }
3984}
3985
Ian Rogers776ac1f2012-04-13 23:36:36 -07003986bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003989 return false;
3990 }
3991 return true;
3992}
3993
Ian Rogersebbdd872014-07-07 23:53:08 -07003994bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3995 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003996 bool changed = true;
3997 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3998 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003999 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004000 * We haven't processed this instruction before, and we haven't touched the registers here, so
4001 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4002 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004003 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004004 if (!insn_flags_[next_insn].IsReturn()) {
4005 target_line->CopyFromLine(merge_line);
4006 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004007 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004008 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004009 return false;
4010 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004011 // For returns we only care about the operand to the return, all other registers are dead.
4012 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4013 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4014 Instruction::Code opcode = ret_inst->Opcode();
4015 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004016 target_line->MarkAllRegistersAsConflicts(this);
Ian Rogersb8c78592013-07-25 23:52:52 +00004017 } else {
4018 target_line->CopyFromLine(merge_line);
4019 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004020 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004021 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004022 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004023 }
4024 }
4025 }
jeffhaobdb76512011-09-07 11:43:16 -07004026 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004027 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004028 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004029 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004030 if (gDebugVerify) {
4031 copy->CopyFromLine(target_line);
4032 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004033 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004034 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004036 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004037 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004038 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004039 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 << copy->Dump(this) << " MERGE\n"
4041 << merge_line->Dump(this) << " ==\n"
4042 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004043 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004044 if (update_merge_line && changed) {
4045 merge_line->CopyFromLine(target_line);
4046 }
jeffhaobdb76512011-09-07 11:43:16 -07004047 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004048 if (changed) {
4049 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004050 }
4051 return true;
4052}
4053
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004054InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004055 return &insn_flags_[work_insn_idx_];
4056}
4057
Ian Rogersd8f69b02014-09-10 21:43:52 +00004058const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004059 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004060 if (mirror_method_.Get() != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004061 StackHandleScope<1> hs(self_);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004062 mirror::Class* return_type_class =
4063 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004064 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004065 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4066 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004067 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004068 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004069 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4070 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004071 }
4072 }
4073 if (return_type_ == nullptr) {
4074 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4075 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4076 uint16_t return_type_idx = proto_id.return_type_idx_;
4077 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004078 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004079 }
4080 }
4081 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004082}
4083
Ian Rogersd8f69b02014-09-10 21:43:52 +00004084const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004085 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004086 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004087 const char* descriptor
4088 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004089 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004090 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004091 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4092 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004093 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004094 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004096 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004097 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004098}
4099
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004100std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4101 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004102 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004103 std::vector<int32_t> result;
4104 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004105 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004106 if (type.IsConstant()) {
4107 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004108 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4109 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004110 } else if (type.IsConstantLo()) {
4111 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004112 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4113 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004114 } else if (type.IsConstantHi()) {
4115 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4117 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004118 } else if (type.IsIntegralTypes()) {
4119 result.push_back(kIntVReg);
4120 result.push_back(0);
4121 } else if (type.IsFloat()) {
4122 result.push_back(kFloatVReg);
4123 result.push_back(0);
4124 } else if (type.IsLong()) {
4125 result.push_back(kLongLoVReg);
4126 result.push_back(0);
4127 result.push_back(kLongHiVReg);
4128 result.push_back(0);
4129 ++i;
4130 } else if (type.IsDouble()) {
4131 result.push_back(kDoubleLoVReg);
4132 result.push_back(0);
4133 result.push_back(kDoubleHiVReg);
4134 result.push_back(0);
4135 ++i;
4136 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4137 result.push_back(kUndefined);
4138 result.push_back(0);
4139 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004140 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004141 result.push_back(kReferenceVReg);
4142 result.push_back(0);
4143 }
4144 }
4145 return result;
4146}
4147
Ian Rogersd8f69b02014-09-10 21:43:52 +00004148const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004149 if (precise) {
4150 // Precise constant type.
4151 return reg_types_.FromCat1Const(value, true);
4152 } else {
4153 // Imprecise constant type.
4154 if (value < -32768) {
4155 return reg_types_.IntConstant();
4156 } else if (value < -128) {
4157 return reg_types_.ShortConstant();
4158 } else if (value < 0) {
4159 return reg_types_.ByteConstant();
4160 } else if (value == 0) {
4161 return reg_types_.Zero();
4162 } else if (value == 1) {
4163 return reg_types_.One();
4164 } else if (value < 128) {
4165 return reg_types_.PosByteConstant();
4166 } else if (value < 32768) {
4167 return reg_types_.PosShortConstant();
4168 } else if (value < 65536) {
4169 return reg_types_.CharConstant();
4170 } else {
4171 return reg_types_.IntConstant();
4172 }
4173 }
4174}
4175
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004176void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004177 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004178}
4179
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004180void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004181 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004182}
jeffhaod1224c72012-02-29 13:43:08 -08004183
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004184void MethodVerifier::VisitStaticRoots(RootCallback* callback, void* arg) {
4185 RegTypeCache::VisitStaticRoots(callback, arg);
4186}
4187
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004188void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4189 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004190}
4191
Ian Rogersd81871c2011-10-03 13:57:23 -07004192} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004193} // namespace art