blob: e2a3335a79381af74c15e56468123c1e991b1906 [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 Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Ian Rogersebbdd872014-07-07 23:53:08 -070050static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070051// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070052
Ian Rogers7b3ddd22013-02-21 15:19:52 -080053void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070054 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070056 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070057 register_lines_.reset(new RegisterLine*[insns_size]());
58 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070059 for (uint32_t i = 0; i < insns_size; i++) {
60 bool interesting = false;
61 switch (mode) {
62 case kTrackRegsAll:
63 interesting = flags[i].IsOpcode();
64 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070065 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070066 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070067 break;
68 case kTrackRegsBranches:
69 interesting = flags[i].IsBranchTarget();
70 break;
71 default:
72 break;
73 }
74 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070075 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
76 }
77 }
78}
79
80PcToRegisterLineTable::~PcToRegisterLineTable() {
81 for (size_t i = 0; i < size_; i++) {
82 delete register_lines_[i];
83 if (kIsDebugBuild) {
84 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070085 }
86 }
87}
88
Ian Rogersef7d42f2014-01-06 12:55:46 -080089MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070090 bool allow_soft_failures,
91 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070092 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070093 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070094 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080095 bool early_failure = false;
96 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -070097 const DexFile& dex_file = klass->GetDexFile();
98 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080099 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700100 std::string temp;
101 if (super == NULL && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800102 early_failure = true;
103 failure_message = " that has no super class";
104 } else if (super != NULL && super->IsFinal()) {
105 early_failure = true;
106 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
107 } else if (class_def == NULL) {
108 early_failure = true;
109 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
110 }
111 if (early_failure) {
112 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
113 if (Runtime::Current()->IsCompiler()) {
114 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000115 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800116 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700117 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700118 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700120 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700123}
124
Ian Rogers365c1022012-06-22 15:05:28 -0700125MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700126 Handle<mirror::DexCache> dex_cache,
127 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700128 const DexFile::ClassDef* class_def,
129 bool allow_soft_failures,
130 std::string* error) {
131 DCHECK(class_def != nullptr);
132 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700133 if (class_data == NULL) {
134 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700135 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700136 }
jeffhaof56197c2012-03-05 18:01:54 -0800137 ClassDataItemIterator it(*dex_file, class_data);
138 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
139 it.Next();
140 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700141 Thread* self = Thread::Current();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700143 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700144 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700145 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800146 while (it.HasNextDirectMethod()) {
147 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700148 if (method_idx == previous_direct_method_idx) {
149 // smali can create dex files with two encoded_methods sharing the same method_idx
150 // http://code.google.com/p/smali/issues/detail?id=119
151 it.Next();
152 continue;
153 }
154 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700155 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700156 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700157 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
158 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 if (method == NULL) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700160 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700161 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700162 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700163 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700164 StackHandleScope<1> hs(self);
165 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Brian Carlstrom93c33962013-07-26 10:37:43 -0700166 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
167 dex_file,
168 dex_cache,
169 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700171 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700172 h_method,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700173 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700174 allow_soft_failures,
175 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700176 if (result != kNoFailure) {
177 if (result == kHardFailure) {
178 hard_fail = true;
179 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700180 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700181 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700182 *error = "Verifier rejected class ";
183 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
184 *error += " due to bad method ";
185 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700187 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800188 }
189 it.Next();
190 }
jeffhao9b0b1882012-10-01 16:51:22 -0700191 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800192 while (it.HasNextVirtualMethod()) {
193 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700194 if (method_idx == previous_virtual_method_idx) {
195 // smali can create dex files with two encoded_methods sharing the same method_idx
196 // http://code.google.com/p/smali/issues/detail?id=119
197 it.Next();
198 continue;
199 }
200 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700201 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700203 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
204 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 if (method == NULL) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700208 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700209 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700210 StackHandleScope<1> hs(self);
211 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Brian Carlstrom93c33962013-07-26 10:37:43 -0700212 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
213 dex_file,
214 dex_cache,
215 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700216 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700217 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700218 h_method,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700219 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700220 allow_soft_failures,
221 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700222 if (result != kNoFailure) {
223 if (result == kHardFailure) {
224 hard_fail = true;
225 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700226 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700227 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700228 *error = "Verifier rejected class ";
229 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
230 *error += " due to bad method ";
231 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700232 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700233 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800234 }
235 it.Next();
236 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700237 if (error_count == 0) {
238 return kNoFailure;
239 } else {
240 return hard_fail ? kHardFailure : kSoftFailure;
241 }
jeffhaof56197c2012-03-05 18:01:54 -0800242}
243
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
245 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700246 Handle<mirror::DexCache> dex_cache,
247 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700248 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile::CodeItem* code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700250 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700251 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700252 bool allow_soft_failures,
253 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700254 MethodVerifier::FailureKind result = kNoFailure;
255 uint64_t start_ns = NanoTime();
256
Ian Rogers46960fe2014-05-23 10:43:43 -0700257 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700258 method_idx, method, method_access_flags, true, allow_soft_failures,
259 need_precise_constants);
Ian Rogers46960fe2014-05-23 10:43:43 -0700260 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 // Verification completed, however failures may be pending that didn't cause the verification
262 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700263 CHECK(!verifier.have_pending_hard_failure_);
264 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700265 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700266 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700267 << PrettyMethod(method_idx, *dex_file) << "\n");
268 }
Ian Rogersc8982582012-09-07 16:53:25 -0700269 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800270 }
271 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700273 CHECK_NE(verifier.failures_.size(), 0U);
274 CHECK(verifier.have_pending_hard_failure_);
275 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700276 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800277 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700278 std::cout << "\n" << verifier.info_messages_.str();
279 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800280 }
Ian Rogersc8982582012-09-07 16:53:25 -0700281 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800282 }
Ian Rogersc8982582012-09-07 16:53:25 -0700283 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe073ed9b2014-06-13 15:46:46 -0700284 if (duration_ns > MsToNs(100) && !kIsDebugBuild) {
Ian Rogersc8982582012-09-07 16:53:25 -0700285 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
286 << " took " << PrettyDuration(duration_ns);
287 }
288 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800291void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700292 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700293 Handle<mirror::DexCache> dex_cache,
294 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700295 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 const DexFile::CodeItem* code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700297 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800298 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700299 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700300 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700301 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800302 verifier.DumpFailures(os);
303 os << verifier.info_messages_.str();
304 verifier.Dump(os);
305}
306
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700307MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
308 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700310 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700311 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700312 bool can_load_classes, bool allow_soft_failures,
313 bool need_precise_constants)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800314 : reg_types_(can_load_classes),
315 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800316 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700317 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700318 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700319 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800320 dex_file_(dex_file),
321 dex_cache_(dex_cache),
322 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700323 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800324 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700325 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700326 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700327 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700328 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700329 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800330 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800331 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700332 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200333 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700334 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200335 has_check_casts_(false),
336 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800337 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700338 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800339}
340
Mathieu Chartier590fee92013-09-13 13:46:47 -0700341MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800342 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700343 STLDeleteElements(&failure_messages_);
344}
345
Brian Carlstromea46f952013-07-30 01:26:50 -0700346void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700348 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700349 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
350 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700351 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700352 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700353 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
354 false, true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700355 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700356 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700357 verifier.FindLocksAtDexPc();
358}
359
360void MethodVerifier::FindLocksAtDexPc() {
361 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700362 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700363
364 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
365 // verification. In practice, the phase we want relies on data structures set up by all the
366 // earlier passes, so we just run the full method verification and bail out early when we've
367 // got what we wanted.
368 Verify();
369}
370
Brian Carlstromea46f952013-07-30 01:26:50 -0700371mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700372 uint32_t dex_pc) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700373 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700374 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
375 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700376 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700377 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700378 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
379 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200380 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200381}
382
Brian Carlstromea46f952013-07-30 01:26:50 -0700383mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700384 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200385
386 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
387 // verification. In practice, the phase we want relies on data structures set up by all the
388 // earlier passes, so we just run the full method verification and bail out early when we've
389 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200390 bool success = Verify();
391 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700392 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200393 }
394 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
395 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700396 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200397 }
398 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
399 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200400}
401
Brian Carlstromea46f952013-07-30 01:26:50 -0700402mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700403 uint32_t dex_pc) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700404 StackHandleScope<3> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
406 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700407 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700408 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700409 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
410 true, true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200411 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200412}
413
Brian Carlstromea46f952013-07-30 01:26:50 -0700414mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700415 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200416
417 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
418 // verification. In practice, the phase we want relies on data structures set up by all the
419 // earlier passes, so we just run the full method verification and bail out early when we've
420 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200421 bool success = Verify();
422 if (!success) {
423 return NULL;
424 }
425 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
426 if (register_line == NULL) {
427 return NULL;
428 }
429 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
430 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
431 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200432}
433
Ian Rogersad0b3a32012-04-16 14:50:24 -0700434bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 // If there aren't any instructions, make sure that's expected, then exit successfully.
436 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700439 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 } else {
441 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700442 }
jeffhaobdb76512011-09-07 11:43:16 -0700443 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700444 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
445 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700446 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
447 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700448 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700449 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700450 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800451 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700452 // Run through the instructions and see if the width checks out.
453 bool result = ComputeWidthsAndCountOps();
454 // Flag instructions guarded by a "try" block and check exception handlers.
455 result = result && ScanTryCatchBlocks();
456 // Perform static instruction verification.
457 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000459 result = result && VerifyCodeFlow();
460 // Compute information for compiler.
461 if (result && Runtime::Current()->IsCompiler()) {
462 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
463 }
464 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700465}
466
Ian Rogers776ac1f2012-04-13 23:36:36 -0700467std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700468 switch (error) {
469 case VERIFY_ERROR_NO_CLASS:
470 case VERIFY_ERROR_NO_FIELD:
471 case VERIFY_ERROR_NO_METHOD:
472 case VERIFY_ERROR_ACCESS_CLASS:
473 case VERIFY_ERROR_ACCESS_FIELD:
474 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700475 case VERIFY_ERROR_INSTANTIATION:
476 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800477 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700478 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
479 // class change and instantiation errors into soft verification errors so that we re-verify
480 // at runtime. We may fail to find or to agree on access because of not yet available class
481 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
482 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
483 // paths" that dynamically perform the verification and cause the behavior to be that akin
484 // to an interpreter.
485 error = VERIFY_ERROR_BAD_CLASS_SOFT;
486 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700487 // If we fail again at runtime, mark that this instruction would throw and force this
488 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700489 have_pending_runtime_throw_failure_ = true;
490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 // Indication that verification should be retried at runtime.
493 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700494 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700495 have_pending_hard_failure_ = true;
496 }
497 break;
jeffhaod5347e02012-03-22 17:25:05 -0700498 // Hard verification failures at compile time will still fail at runtime, so the class is
499 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700500 case VERIFY_ERROR_BAD_CLASS_HARD: {
501 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700502 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000503 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800504 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505 have_pending_hard_failure_ = true;
506 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800507 }
508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700509 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700510 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700511 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700512 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700513 failure_messages_.push_back(failure_message);
514 return *failure_message;
515}
516
Ian Rogers576ca0c2014-06-06 15:58:22 -0700517std::ostream& MethodVerifier::LogVerifyInfo() {
518 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
519 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
520}
521
Ian Rogersad0b3a32012-04-16 14:50:24 -0700522void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
523 size_t failure_num = failure_messages_.size();
524 DCHECK_NE(failure_num, 0U);
525 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
526 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700527 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700528 delete last_fail_message;
529}
530
531void MethodVerifier::AppendToLastFailMessage(std::string append) {
532 size_t failure_num = failure_messages_.size();
533 DCHECK_NE(failure_num, 0U);
534 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
535 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800536}
537
Ian Rogers776ac1f2012-04-13 23:36:36 -0700538bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 const uint16_t* insns = code_item_->insns_;
540 size_t insns_size = code_item_->insns_size_in_code_units_;
541 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700542 size_t new_instance_count = 0;
543 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700545
Ian Rogersd81871c2011-10-03 13:57:23 -0700546 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700547 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700548 switch (opcode) {
549 case Instruction::APUT_OBJECT:
550 case Instruction::CHECK_CAST:
551 has_check_casts_ = true;
552 break;
553 case Instruction::INVOKE_VIRTUAL:
554 case Instruction::INVOKE_VIRTUAL_RANGE:
555 case Instruction::INVOKE_INTERFACE:
556 case Instruction::INVOKE_INTERFACE_RANGE:
557 has_virtual_or_interface_invokes_ = true;
558 break;
559 case Instruction::MONITOR_ENTER:
560 monitor_enter_count++;
561 break;
562 case Instruction::NEW_INSTANCE:
563 new_instance_count++;
564 break;
565 default:
566 break;
jeffhaobdb76512011-09-07 11:43:16 -0700567 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 size_t inst_size = inst->SizeInCodeUnits();
569 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
570 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700571 inst = inst->Next();
572 }
573
Ian Rogersd81871c2011-10-03 13:57:23 -0700574 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700575 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
576 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700577 return false;
578 }
579
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 new_instance_count_ = new_instance_count;
581 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700582 return true;
583}
584
Ian Rogers776ac1f2012-04-13 23:36:36 -0700585bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700587 if (tries_size == 0) {
588 return true;
589 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700591 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700592
593 for (uint32_t idx = 0; idx < tries_size; idx++) {
594 const DexFile::TryItem* try_item = &tries[idx];
595 uint32_t start = try_item->start_addr_;
596 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700597 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
599 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700600 return false;
601 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700602 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700603 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
604 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700605 return false;
606 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 for (uint32_t dex_pc = start; dex_pc < end;
608 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
609 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700610 }
611 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800612 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700613 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700614 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700615 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700616 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700617 CatchHandlerIterator iterator(handlers_ptr);
618 for (; iterator.HasNext(); iterator.Next()) {
619 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700621 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
622 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700623 return false;
624 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700626 // Ensure exception types are resolved so that they don't need resolution to be delivered,
627 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700628 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800629 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
630 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700631 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700632 if (exception_type == NULL) {
633 DCHECK(Thread::Current()->IsExceptionPending());
634 Thread::Current()->ClearException();
635 }
636 }
jeffhaobdb76512011-09-07 11:43:16 -0700637 }
Ian Rogers0571d352011-11-03 19:51:38 -0700638 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700639 }
jeffhaobdb76512011-09-07 11:43:16 -0700640 return true;
641}
642
Ian Rogers776ac1f2012-04-13 23:36:36 -0700643bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700645
Ian Rogers0c7abda2012-09-19 13:33:42 -0700646 /* 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 -0700647 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700648 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700649
650 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700651 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700653 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 return false;
655 }
656 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700657 // All invoke points are marked as "Throw" points already.
658 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000659 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700660 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000661 } else if (inst->IsReturn()) {
662 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 }
664 dex_pc += inst->SizeInCodeUnits();
665 inst = inst->Next();
666 }
667 return true;
668}
669
Ian Rogers776ac1f2012-04-13 23:36:36 -0700670bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 bool result = true;
672 switch (inst->GetVerifyTypeArgumentA()) {
673 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700674 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700677 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 break;
679 }
680 switch (inst->GetVerifyTypeArgumentB()) {
681 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700682 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700685 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700688 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700691 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 break;
693 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700694 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 break;
696 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700697 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 break;
699 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700700 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 break;
702 }
703 switch (inst->GetVerifyTypeArgumentC()) {
704 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700705 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 break;
707 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700708 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 break;
710 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700711 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 break;
713 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700714 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 break;
716 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700717 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 break;
719 }
720 switch (inst->GetVerifyExtraFlags()) {
721 case Instruction::kVerifyArrayData:
722 result = result && CheckArrayData(code_offset);
723 break;
724 case Instruction::kVerifyBranchTarget:
725 result = result && CheckBranchTarget(code_offset);
726 break;
727 case Instruction::kVerifySwitchTargets:
728 result = result && CheckSwitchTargets(code_offset);
729 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700730 case Instruction::kVerifyVarArgNonZero:
731 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700732 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700733 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
735 "non-range invoke";
736 return false;
737 }
Ian Rogers29a26482014-05-02 15:27:29 -0700738 uint32_t args[Instruction::kMaxVarArgRegs];
739 inst->GetVarArgs(args);
740 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700742 }
Andreas Gampec3314312014-06-19 18:13:29 -0700743 case Instruction::kVerifyVarArgRangeNonZero:
744 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700746 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
747 inst->VRegA() <= 0) {
748 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
749 "range invoke";
750 return false;
751 }
Ian Rogers29a26482014-05-02 15:27:29 -0700752 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 break;
754 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700756 result = false;
757 break;
758 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700759 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) {
760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
761 result = false;
762 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700763 return result;
764}
765
Ian Rogers776ac1f2012-04-13 23:36:36 -0700766bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
769 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 }
772 return true;
773}
774
Ian Rogers776ac1f2012-04-13 23:36:36 -0700775bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
778 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return false;
780 }
781 return true;
782}
783
Ian Rogers776ac1f2012-04-13 23:36:36 -0700784bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
787 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
796 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 return true;
800}
801
Ian Rogers776ac1f2012-04-13 23:36:36 -0700802bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
805 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 return false;
807 }
808 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700809 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 }
814 return true;
815}
816
Ian Rogers776ac1f2012-04-13 23:36:36 -0700817bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
820 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 return false;
822 }
823 return true;
824}
825
Ian Rogers776ac1f2012-04-13 23:36:36 -0700826bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
829 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
832 return true;
833}
834
Ian Rogers776ac1f2012-04-13 23:36:36 -0700835bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
838 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 return false;
840 }
841 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700842 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 const char* cp = descriptor;
844 while (*cp++ == '[') {
845 bracket_count++;
846 }
847 if (bracket_count == 0) {
848 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700849 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
850 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 return false;
852 } else if (bracket_count > 255) {
853 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700854 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
855 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 return false;
857 }
858 return true;
859}
860
Ian Rogers776ac1f2012-04-13 23:36:36 -0700861bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
863 const uint16_t* insns = code_item_->insns_ + cur_offset;
864 const uint16_t* array_data;
865 int32_t array_data_offset;
866
867 DCHECK_LT(cur_offset, insn_count);
868 /* make sure the start of the array data table is in range */
869 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
870 if ((int32_t) cur_offset + array_data_offset < 0 ||
871 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700873 << ", data offset " << array_data_offset
874 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 /* offset to array data table is a relative branch-style offset */
878 array_data = insns + array_data_offset;
879 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800880 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
882 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700883 return false;
884 }
885 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700886 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
888 /* make sure the end of the switch is in range */
889 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
891 << ", data offset " << array_data_offset << ", end "
892 << cur_offset + array_data_offset + table_size
893 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 return false;
895 }
896 return true;
897}
898
Ian Rogers776ac1f2012-04-13 23:36:36 -0700899bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 int32_t offset;
901 bool isConditional, selfOkay;
902 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
903 return false;
904 }
905 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
907 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 return false;
909 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700910 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
911 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
914 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 return false;
916 }
917 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
918 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700919 if (abs_offset < 0 ||
920 (uint32_t) abs_offset >= insn_count ||
921 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700923 << reinterpret_cast<void*>(abs_offset) << ") at "
924 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 return false;
926 }
927 insn_flags_[abs_offset].SetBranchTarget();
928 return true;
929}
930
Ian Rogers776ac1f2012-04-13 23:36:36 -0700931bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700932 bool* selfOkay) {
933 const uint16_t* insns = code_item_->insns_ + cur_offset;
934 *pConditional = false;
935 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 switch (*insns & 0xff) {
937 case Instruction::GOTO:
938 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 break;
940 case Instruction::GOTO_32:
941 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 *selfOkay = true;
943 break;
944 case Instruction::GOTO_16:
945 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 break;
947 case Instruction::IF_EQ:
948 case Instruction::IF_NE:
949 case Instruction::IF_LT:
950 case Instruction::IF_GE:
951 case Instruction::IF_GT:
952 case Instruction::IF_LE:
953 case Instruction::IF_EQZ:
954 case Instruction::IF_NEZ:
955 case Instruction::IF_LTZ:
956 case Instruction::IF_GEZ:
957 case Instruction::IF_GTZ:
958 case Instruction::IF_LEZ:
959 *pOffset = (int16_t) insns[1];
960 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700961 break;
962 default:
963 return false;
964 break;
965 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700966 return true;
967}
968
Ian Rogers776ac1f2012-04-13 23:36:36 -0700969bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700970 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700971 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700973 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
975 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700977 << ", switch offset " << switch_offset
978 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return false;
980 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800984 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
986 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700987 return false;
988 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 uint32_t switch_count = switch_insns[1];
990 int32_t keys_offset, targets_offset;
991 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700992 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
993 /* 0=sig, 1=count, 2/3=firstKey */
994 targets_offset = 4;
995 keys_offset = -1;
996 expected_signature = Instruction::kPackedSwitchSignature;
997 } else {
998 /* 0=sig, 1=count, 2..count*2 = keys */
999 keys_offset = 2;
1000 targets_offset = 2 + 2 * switch_count;
1001 expected_signature = Instruction::kSparseSwitchSignature;
1002 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001004 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1006 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1007 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001008 return false;
1009 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001010 /* make sure the end of the switch is in range */
1011 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1013 << ", switch offset " << switch_offset
1014 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001015 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001016 return false;
1017 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001018 /* for a sparse switch, verify the keys are in ascending order */
1019 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1021 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001022 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1023 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1024 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1026 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001027 return false;
1028 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001029 last_key = key;
1030 }
1031 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001032 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 for (uint32_t targ = 0; targ < switch_count; targ++) {
1034 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1035 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1036 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001037 if (abs_offset < 0 ||
1038 abs_offset >= (int32_t) insn_count ||
1039 !insn_flags_[abs_offset].IsOpcode()) {
1040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1041 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1042 << reinterpret_cast<void*>(cur_offset)
1043 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001044 return false;
1045 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 insn_flags_[abs_offset].SetBranchTarget();
1047 }
1048 return true;
1049}
1050
Ian Rogers776ac1f2012-04-13 23:36:36 -07001051bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001052 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 return false;
1055 }
1056 uint16_t registers_size = code_item_->registers_size_;
1057 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001058 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1060 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 return false;
1062 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001063 }
1064
1065 return true;
1066}
1067
Ian Rogers776ac1f2012-04-13 23:36:36 -07001068bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 uint16_t registers_size = code_item_->registers_size_;
1070 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1071 // integer overflow when adding them here.
1072 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1074 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 return false;
1076 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001077 return true;
1078}
1079
Ian Rogers776ac1f2012-04-13 23:36:36 -07001080bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 uint16_t registers_size = code_item_->registers_size_;
1082 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001083
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001085 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1086 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 }
1088 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001089 reg_table_.Init(kTrackCompilerInterestPoints,
1090 insn_flags_.get(),
1091 insns_size,
1092 registers_size,
1093 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001094
jeffhaobdb76512011-09-07 11:43:16 -07001095
Ian Rogersd0fbd852013-09-24 18:17:04 -07001096 work_line_.reset(RegisterLine::Create(registers_size, this));
1097 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001098
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 /* Initialize register types of method arguments. */
1100 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 DCHECK_NE(failures_.size(), 0U);
1102 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001103 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001104 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return false;
1106 }
1107 /* Perform code flow verification. */
1108 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001109 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001111 }
jeffhaobdb76512011-09-07 11:43:16 -07001112 return true;
1113}
1114
Ian Rogersad0b3a32012-04-16 14:50:24 -07001115std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1116 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001117 for (size_t i = 0; i < failures_.size(); ++i) {
1118 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001119 }
1120 return os;
1121}
1122
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001123extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001125 v->Dump(std::cerr);
1126}
1127
Ian Rogers776ac1f2012-04-13 23:36:36 -07001128void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001129 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001130 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 return;
jeffhaobdb76512011-09-07 11:43:16 -07001132 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001133 {
1134 os << "Register Types:\n";
1135 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1136 std::ostream indent_os(&indent_filter);
1137 reg_types_.Dump(indent_os);
1138 }
Ian Rogersb4903572012-10-11 11:52:56 -07001139 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1141 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 const Instruction* inst = Instruction::At(code_item_->insns_);
1143 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1144 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1146 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001147 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001148 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001149 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001150 const bool kDumpHexOfInstruction = false;
1151 if (kDumpHexOfInstruction) {
1152 indent_os << inst->DumpHex(5) << " ";
1153 }
1154 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001155 inst = inst->Next();
1156 }
jeffhaobdb76512011-09-07 11:43:16 -07001157}
1158
Ian Rogersd81871c2011-10-03 13:57:23 -07001159static bool IsPrimitiveDescriptor(char descriptor) {
1160 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001161 case 'I':
1162 case 'C':
1163 case 'S':
1164 case 'B':
1165 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001166 case 'F':
1167 case 'D':
1168 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001170 default:
1171 return false;
1172 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001173}
1174
Ian Rogers776ac1f2012-04-13 23:36:36 -07001175bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 RegisterLine* reg_line = reg_table_.GetLine(0);
1177 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1178 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001179
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001181 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001183 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1185 // argument as uninitialized. This restricts field access until the superclass constructor is
1186 // called.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001187 RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001188 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 reg_line->SetRegisterType(arg_start + cur_arg,
1190 reg_types_.UninitializedThisArgument(declaring_class));
1191 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001192 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001193 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001195 }
1196
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001197 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001198 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001199 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001200
1201 for (; iterator.HasNext(); iterator.Next()) {
1202 const char* descriptor = iterator.GetDescriptor();
1203 if (descriptor == NULL) {
1204 LOG(FATAL) << "Null descriptor";
1205 }
1206 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1208 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 return false;
1210 }
1211 switch (descriptor[0]) {
1212 case 'L':
1213 case '[':
1214 // We assume that reference arguments are initialized. The only way it could be otherwise
1215 // (assuming the caller was verified) is if the current method is <init>, but in that case
1216 // it's effectively considered initialized the instant we reach here (in the sense that we
1217 // can return without doing anything or call virtual methods).
1218 {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001219 RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001220 if (!reg_type.IsNonZeroReferenceTypes()) {
1221 DCHECK(HasFailures());
1222 return false;
1223 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001224 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 }
1226 break;
1227 case 'Z':
1228 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1229 break;
1230 case 'C':
1231 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1232 break;
1233 case 'B':
1234 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1235 break;
1236 case 'I':
1237 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1238 break;
1239 case 'S':
1240 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1241 break;
1242 case 'F':
1243 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1244 break;
1245 case 'J':
1246 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001247 if (cur_arg + 1 >= expected_args) {
1248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1249 << " args, found more (" << descriptor << ")";
1250 return false;
1251 }
1252
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001253 RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1254 RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001255 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 cur_arg++;
1257 break;
1258 }
1259 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1261 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 return false;
1263 }
1264 cur_arg++;
1265 }
1266 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1268 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 return false;
1270 }
1271 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1272 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1273 // format. Only major difference from the method argument format is that 'V' is supported.
1274 bool result;
1275 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1276 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001277 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 size_t i = 0;
1279 do {
1280 i++;
1281 } while (descriptor[i] == '['); // process leading [
1282 if (descriptor[i] == 'L') { // object array
1283 do {
1284 i++; // find closing ;
1285 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1286 result = descriptor[i] == ';';
1287 } else { // primitive array
1288 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1289 }
1290 } else if (descriptor[0] == 'L') {
1291 // could be more thorough here, but shouldn't be required
1292 size_t i = 0;
1293 do {
1294 i++;
1295 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1296 result = descriptor[i] == ';';
1297 } else {
1298 result = false;
1299 }
1300 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1302 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 }
1304 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001305}
1306
Ian Rogers776ac1f2012-04-13 23:36:36 -07001307bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 const uint16_t* insns = code_item_->insns_;
1309 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001310
jeffhaobdb76512011-09-07 11:43:16 -07001311 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 insn_flags_[0].SetChanged();
1313 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001314
jeffhaobdb76512011-09-07 11:43:16 -07001315 /* Continue until no instructions are marked "changed". */
1316 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1318 uint32_t insn_idx = start_guess;
1319 for (; insn_idx < insns_size; insn_idx++) {
1320 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001321 break;
1322 }
jeffhaobdb76512011-09-07 11:43:16 -07001323 if (insn_idx == insns_size) {
1324 if (start_guess != 0) {
1325 /* try again, starting from the top */
1326 start_guess = 0;
1327 continue;
1328 } else {
1329 /* all flags are clear */
1330 break;
1331 }
1332 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 // We carry the working set of registers from instruction to instruction. If this address can
1334 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1335 // "changed" flags, we need to load the set of registers from the table.
1336 // Because we always prefer to continue on to the next instruction, we should never have a
1337 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1338 // target.
1339 work_insn_idx_ = insn_idx;
1340 if (insn_flags_[insn_idx].IsBranchTarget()) {
1341 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001342 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001343 /*
1344 * Sanity check: retrieve the stored register line (assuming
1345 * a full table) and make sure it actually matches.
1346 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1348 if (register_line != NULL) {
1349 if (work_line_->CompareLine(register_line) != 0) {
1350 Dump(std::cout);
1351 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001352 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001353 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1354 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001355 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 }
jeffhaobdb76512011-09-07 11:43:16 -07001357 }
jeffhaobdb76512011-09-07 11:43:16 -07001358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001360 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001361 prepend += " failed to verify: ";
1362 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 return false;
1364 }
jeffhaobdb76512011-09-07 11:43:16 -07001365 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 insn_flags_[insn_idx].SetVisited();
1367 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001368 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001369
Ian Rogers1c849e52012-06-28 14:00:33 -07001370 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001371 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001372 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001373 * (besides the wasted space), but it indicates a flaw somewhere
1374 * down the line, possibly in the verifier.
1375 *
1376 * If we've substituted "always throw" instructions into the stream,
1377 * we are almost certainly going to have some dead code.
1378 */
1379 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 uint32_t insn_idx = 0;
1381 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001382 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001383 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001384 * may or may not be preceded by a padding NOP (for alignment).
1385 */
1386 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1387 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1388 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001389 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001390 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1391 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1392 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
1395
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001397 if (dead_start < 0)
1398 dead_start = insn_idx;
1399 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001400 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1401 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001402 dead_start = -1;
1403 }
1404 }
1405 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001406 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1407 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001408 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001409 // To dump the state of the verify after a method, do something like:
1410 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1411 // "boolean java.lang.String.equals(java.lang.Object)") {
1412 // LOG(INFO) << info_messages_.str();
1413 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 }
jeffhaobdb76512011-09-07 11:43:16 -07001415 return true;
1416}
1417
Ian Rogers776ac1f2012-04-13 23:36:36 -07001418bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001419 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1420 // We want the state _before_ the instruction, for the case where the dex pc we're
1421 // interested in is itself a monitor-enter instruction (which is a likely place
1422 // for a thread to be suspended).
1423 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001424 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001425 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1426 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1427 }
1428 }
1429
jeffhaobdb76512011-09-07 11:43:16 -07001430 /*
1431 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001432 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001433 * control to another statement:
1434 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001435 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001436 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001437 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001438 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001439 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001440 * throw an exception that is handled by an encompassing "try"
1441 * block.
1442 *
1443 * We can also return, in which case there is no successor instruction
1444 * from this point.
1445 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001446 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001447 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1449 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001450 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001451
jeffhaobdb76512011-09-07 11:43:16 -07001452 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001453 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001454 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001456 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1457 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 }
jeffhaobdb76512011-09-07 11:43:16 -07001459
1460 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001461 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001462 * can throw an exception, we will copy/merge this into the "catch"
1463 * address rather than work_line, because we don't want the result
1464 * from the "successful" code path (e.g. a check-cast that "improves"
1465 * a type) to be visible to the exception handler.
1466 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001467 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001469 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001470 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001471 }
1472
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001473
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001474 // We need to ensure the work line is consistent while performing validation. When we spot a
1475 // peephole pattern we compute a new line for either the fallthrough instruction or the
1476 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001477 std::unique_ptr<RegisterLine> branch_line;
1478 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001479
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001481 case Instruction::NOP:
1482 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001483 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001484 * a signature that looks like a NOP; if we see one of these in
1485 * the course of executing code then we have a problem.
1486 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001488 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001489 }
1490 break;
1491
1492 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1494 break;
jeffhaobdb76512011-09-07 11:43:16 -07001495 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1497 break;
jeffhaobdb76512011-09-07 11:43:16 -07001498 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1503 break;
jeffhaobdb76512011-09-07 11:43:16 -07001504 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1506 break;
jeffhaobdb76512011-09-07 11:43:16 -07001507 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001509 break;
1510 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1512 break;
jeffhaobdb76512011-09-07 11:43:16 -07001513 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1515 break;
jeffhaobdb76512011-09-07 11:43:16 -07001516 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001518 break;
1519
1520 /*
1521 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001522 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001523 * might want to hold the result in an actual CPU register, so the
1524 * Dalvik spec requires that these only appear immediately after an
1525 * invoke or filled-new-array.
1526 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001527 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001528 * redundant with the reset done below, but it can make the debug info
1529 * easier to read in some cases.)
1530 */
1531 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001532 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001533 break;
1534 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001536 break;
1537 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001539 break;
1540
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001542 /*
jeffhao60f83e32012-02-13 17:16:30 -08001543 * This statement can only appear as the first instruction in an exception handler. We verify
1544 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001545 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001546 RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001548 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 }
jeffhaobdb76512011-09-07 11:43:16 -07001550 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001551 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1552 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001553 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 }
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
1556 break;
1557 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001558 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001559 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001560 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1563 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 } else {
1565 // Compilers may generate synthetic functions that write byte values into boolean fields.
1566 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001567 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001568 RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1570 ((return_type.IsBoolean() || return_type.IsByte() ||
1571 return_type.IsShort() || return_type.IsChar()) &&
1572 src_type.IsInteger()));
1573 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001574 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001576 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 }
jeffhaobdb76512011-09-07 11:43:16 -07001579 }
1580 }
1581 break;
1582 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001583 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001584 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001585 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 } else {
1589 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 const uint32_t vregA = inst->VRegA_11x();
1591 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001592 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001593 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 }
jeffhaobdb76512011-09-07 11:43:16 -07001595 }
1596 }
1597 break;
1598 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001599 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001600 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 } else {
1604 /* return_type is the *expected* return type, not register value */
1605 DCHECK(!return_type.IsZero());
1606 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001607 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001608 RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001609 // Disallow returning uninitialized values and verify that the reference in vAA is an
1610 // instance of the "return_type"
1611 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001612 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1613 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001614 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001615 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1616 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1617 << "' or '" << reg_type << "'";
1618 } else {
1619 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1620 << "', but expected from declaration '" << return_type << "'";
1621 }
jeffhaobdb76512011-09-07 11:43:16 -07001622 }
1623 }
1624 }
1625 break;
1626
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001627 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 case Instruction::CONST_4: {
1629 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001630 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001631 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001632 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 }
1634 case Instruction::CONST_16: {
1635 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001636 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001637 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001638 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001640 case Instruction::CONST: {
1641 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001643 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001644 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001645 }
1646 case Instruction::CONST_HIGH16: {
1647 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001649 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001651 }
jeffhaobdb76512011-09-07 11:43:16 -07001652 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001653 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001655 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1656 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001658 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 }
1660 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001662 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1663 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001665 break;
1666 }
1667 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001668 int64_t val = inst->VRegB_51l();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001669 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1670 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 break;
1673 }
1674 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001676 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1677 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001679 break;
1680 }
jeffhaobdb76512011-09-07 11:43:16 -07001681 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001682 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1683 break;
jeffhaobdb76512011-09-07 11:43:16 -07001684 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001687 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001688 // Get type from instruction if unresolved then we need an access check
1689 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001690 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001691 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001692 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001693 res_type.IsConflict() ? res_type
1694 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001696 }
jeffhaobdb76512011-09-07 11:43:16 -07001697 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001699 break;
1700 case Instruction::MONITOR_EXIT:
1701 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001702 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001703 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001704 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001705 * to the need to handle asynchronous exceptions, a now-deprecated
1706 * feature that Dalvik doesn't support.)
1707 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001708 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001709 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001710 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001711 * structured locking checks are working, the former would have
1712 * failed on the -enter instruction, and the latter is impossible.
1713 *
1714 * This is fortunate, because issue 3221411 prevents us from
1715 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001716 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001717 * some catch blocks (which will show up as "dead" code when
1718 * we skip them here); if we can't, then the code path could be
1719 * "live" so we still need to check it.
1720 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001721 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001722 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001723 break;
1724
Ian Rogers28ad40d2011-10-27 15:19:26 -07001725 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 /*
1728 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1729 * could be a "upcast" -- not expected, so we don't try to address it.)
1730 *
1731 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001732 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001733 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1735 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001736 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001737 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001738 // If this is a primitive type, fail HARD.
1739 mirror::Class* klass = (*dex_cache_)->GetResolvedType(type_idx);
1740 if (klass != nullptr && klass->IsPrimitive()) {
1741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1742 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1743 << GetDeclaringClass();
1744 break;
1745 }
1746
Ian Rogersad0b3a32012-04-16 14:50:24 -07001747 DCHECK_NE(failures_.size(), 0U);
1748 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001750 }
1751 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001752 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001753 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001754 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001755 RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001756 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 if (is_checkcast) {
1758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1759 } else {
1760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1761 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001762 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 if (is_checkcast) {
1764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1765 } else {
1766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1767 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001768 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001769 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001773 }
jeffhaobdb76512011-09-07 11:43:16 -07001774 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001775 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
1777 case Instruction::ARRAY_LENGTH: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001778 RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001779 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001780 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001784 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001785 } else {
1786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
1788 break;
1789 }
1790 case Instruction::NEW_INSTANCE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001791 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001792 if (res_type.IsConflict()) {
1793 DCHECK_NE(failures_.size(), 0U);
1794 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001795 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001796 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1797 // can't create an instance of an interface or abstract class */
1798 if (!res_type.IsInstantiableTypes()) {
1799 Fail(VERIFY_ERROR_INSTANTIATION)
1800 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001801 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001803 RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001804 // Any registers holding previous allocations from this address that have not yet been
1805 // initialized must be marked invalid.
1806 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1807 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 break;
1810 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001811 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
1814 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001816 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001817 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001818 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001820 just_set_result = true; // Filled new array range sets result register
1821 break;
jeffhaobdb76512011-09-07 11:43:16 -07001822 case Instruction::CMPL_FLOAT:
1823 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001824 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001825 break;
1826 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001828 break;
1829 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001831 break;
1832 case Instruction::CMPL_DOUBLE:
1833 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001836 break;
1837 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001838 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001839 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001840 break;
1841 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001842 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001843 break;
1844 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001845 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001847 break;
1848 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001851 break;
1852 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001853 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001854 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 case Instruction::THROW: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001856 RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001857 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001858 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1859 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001860 }
1861 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 }
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::GOTO:
1864 case Instruction::GOTO_16:
1865 case Instruction::GOTO_32:
1866 /* no effect on or use of registers */
1867 break;
1868
1869 case Instruction::PACKED_SWITCH:
1870 case Instruction::SPARSE_SWITCH:
1871 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001872 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001873 break;
1874
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 case Instruction::FILL_ARRAY_DATA: {
1876 /* Similar to the verification done for APUT */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001877 RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001878 /* array_type can be null if the reg type is Zero */
1879 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001880 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1882 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001883 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001884 RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001885 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001886 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001887 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1889 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 } else {
jeffhao457cc512012-02-02 16:55:13 -08001891 // Now verify if the element width in the table matches the element width declared in
1892 // the array
1893 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1894 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001896 } else {
1897 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1898 // Since we don't compress the data in Dex, expect to see equal width of data stored
1899 // in the table and expected from the array class.
1900 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1902 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001903 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 }
1905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906 }
1907 }
1908 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 case Instruction::IF_NE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001912 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1913 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 bool mismatch = false;
1915 if (reg_type1.IsZero()) { // zero then integral or reference expected
1916 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1917 } else if (reg_type1.IsReferenceTypes()) { // both references?
1918 mismatch = !reg_type2.IsReferenceTypes();
1919 } else { // both integral?
1920 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1921 }
1922 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1924 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001925 }
1926 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001927 }
jeffhaobdb76512011-09-07 11:43:16 -07001928 case Instruction::IF_LT:
1929 case Instruction::IF_GE:
1930 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 case Instruction::IF_LE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001932 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1933 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1936 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 }
jeffhaobdb76512011-09-07 11:43:16 -07001940 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 case Instruction::IF_NEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001942 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1945 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001947
1948 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001949 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001950 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001951 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001952 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001953 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001954 }
Ian Rogers9b360392013-06-06 14:45:07 -07001955 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001956 } else {
1957 break;
1958 }
1959
Ian Rogers9b360392013-06-06 14:45:07 -07001960 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001961
1962 /* Check for peep-hole pattern of:
1963 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001964 * instance-of vX, vY, T;
1965 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001966 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001967 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001968 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001969 * and sharpen the type of vY to be type T.
1970 * Note, this pattern can't be if:
1971 * - if there are other branches to this branch,
1972 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001973 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001974 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001975 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1976 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1977 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07001978 // Check the type of the instance-of is different than that of registers type, as if they
1979 // are the same there is no work to be done here. Check that the conversion is not to or
1980 // from an unresolved type as type information is imprecise. If the instance-of is to an
1981 // interface then ignore the type information as interfaces can only be treated as Objects
1982 // and we don't want to disallow field and other operations on the object. If the value
1983 // being instance-of checked against is known null (zero) then allow the optimization as
1984 // we didn't have type information. If the merge of the instance-of type with the original
1985 // type is assignable to the original then allow optimization. This check is performed to
1986 // ensure that subsequent merges don't lose type information - such as becoming an
1987 // interface from a class that would lose information relevant to field checks.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001988 RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
1989 RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001990
Ian Rogersebbdd872014-07-07 23:53:08 -07001991 if (!orig_type.Equals(cast_type) &&
1992 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07001993 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07001994 !cast_type.GetClass()->IsInterface() &&
1995 (orig_type.IsZero() ||
1996 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001997 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001998 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001999 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002000 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002001 branch_line.reset(update_line);
2002 }
2003 update_line->CopyFromLine(work_line_.get());
2004 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
2005 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2006 // See if instance-of was preceded by a move-object operation, common due to the small
2007 // register encoding space of instance-of, and propagate type information to the source
2008 // of the move-object.
2009 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002010 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002011 move_idx--;
2012 }
2013 CHECK(insn_flags_[move_idx].IsOpcode());
2014 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2015 switch (move_inst->Opcode()) {
2016 case Instruction::MOVE_OBJECT:
2017 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
2018 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
2019 }
2020 break;
2021 case Instruction::MOVE_OBJECT_FROM16:
2022 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
2023 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
2024 }
2025 break;
2026 case Instruction::MOVE_OBJECT_16:
2027 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2028 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2029 }
2030 break;
2031 default:
2032 break;
2033 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002034 }
2035 }
2036 }
2037
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 }
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IF_LTZ:
2041 case Instruction::IF_GEZ:
2042 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 case Instruction::IF_LEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002044 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2047 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 }
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 }
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 break;
jeffhaobdb76512011-09-07 11:43:16 -07002054 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 break;
jeffhaobdb76512011-09-07 11:43:16 -07002066 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 break;
2069 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
2076 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 break;
2079 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 break;
2082 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002087 break;
2088 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
jeffhaobdb76512011-09-07 11:43:16 -07002098 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
jeffhaobdb76512011-09-07 11:43:16 -07002101 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 break;
jeffhaobdb76512011-09-07 11:43:16 -07002104 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
2107 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 break;
jeffhaobdb76512011-09-07 11:43:16 -07002116
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 break;
2120 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002122 break;
2123 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 break;
2126 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002130 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002131 break;
2132 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 break;
jeffhaobdb76512011-09-07 11:43:16 -07002135 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002137 break;
2138
jeffhaobdb76512011-09-07 11:43:16 -07002139 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 break;
jeffhaobdb76512011-09-07 11:43:16 -07002142 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 break;
jeffhaobdb76512011-09-07 11:43:16 -07002145 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 break;
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 break;
2151 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002153 break;
2154 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002155 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002156 break;
2157 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002159 break;
2160
2161 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 break;
2164 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002166 break;
2167 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002169 break;
2170 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002175 break;
2176 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002178 break;
2179 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
2182
2183 case Instruction::INVOKE_VIRTUAL:
2184 case Instruction::INVOKE_VIRTUAL_RANGE:
2185 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002186 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002187 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2188 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002189 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2190 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002191 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2192 is_super);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002193 RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002194 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002195 Thread* self = Thread::Current();
2196 StackHandleScope<1> hs(self);
2197 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2198 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002199 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002200 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002201 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2202 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002203 return_type_class->CannotBeAssignedFromOtherTypes());
2204 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002205 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002206 self->ClearException();
2207 }
2208 }
2209 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002211 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2212 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002213 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002214 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002215 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002216 if (!return_type->IsLowHalf()) {
2217 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002218 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002219 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002220 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002221 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002223 }
jeffhaobdb76512011-09-07 11:43:16 -07002224 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002226 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002227 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002228 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002229 const char* return_type_descriptor;
2230 bool is_constructor;
Ian Rogers1ff3c982014-08-12 02:30:58 -07002231 RegType* return_type = nullptr;
Ian Rogers46685432012-06-03 22:26:43 -07002232 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002233 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002234 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002235 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002236 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2237 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2238 } else {
2239 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002240 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002241 Thread* self = Thread::Current();
2242 StackHandleScope<1> hs(self);
2243 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2244 MethodHelper mh(h_called_method);
2245 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2246 if (return_type_class != nullptr) {
2247 return_type = &reg_types_.FromClass(return_type_descriptor,
2248 return_type_class,
2249 return_type_class->CannotBeAssignedFromOtherTypes());
2250 } else {
2251 DCHECK(!can_load_classes_ || self->IsExceptionPending());
2252 self->ClearException();
2253 }
Ian Rogers46685432012-06-03 22:26:43 -07002254 }
2255 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002256 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 * Some additional checks when calling a constructor. We know from the invocation arg check
2258 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2259 * that to require that called_method->klass is the same as this->klass or this->super,
2260 * allowing the latter only if the "this" argument is the same as the "this" argument to
2261 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002262 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002263 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002264 if (this_type.IsConflict()) // failure.
2265 break;
jeffhaobdb76512011-09-07 11:43:16 -07002266
jeffhaob57e9522012-04-26 18:08:21 -07002267 /* no null refs allowed (?) */
2268 if (this_type.IsZero()) {
2269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2270 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002271 }
jeffhaob57e9522012-04-26 18:08:21 -07002272
2273 /* must be in same class or in superclass */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002274 // RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002275 // TODO: re-enable constructor type verification
2276 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002277 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002278 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2279 // break;
2280 // }
jeffhaob57e9522012-04-26 18:08:21 -07002281
2282 /* arg must be an uninitialized reference */
2283 if (!this_type.IsUninitializedTypes()) {
2284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2285 << this_type;
2286 break;
2287 }
2288
2289 /*
2290 * Replace the uninitialized reference with an initialized one. We need to do this for all
2291 * registers that have the same object instance in them, not just the "this" register.
2292 */
2293 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002294 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002295 if (return_type == nullptr) {
2296 return_type = &reg_types_.FromDescriptor(class_loader_->Get(),
2297 return_type_descriptor, false);
2298 }
2299 if (!return_type->IsLowHalf()) {
2300 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002301 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002302 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002304 just_set_result = true;
2305 break;
2306 }
2307 case Instruction::INVOKE_STATIC:
2308 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002309 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002310 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002311 METHOD_STATIC,
2312 is_range,
2313 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002314 const char* descriptor;
2315 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002316 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002317 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2318 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002319 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002320 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002321 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002322 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002323 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002324 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 if (!return_type.IsLowHalf()) {
2326 work_line_->SetResultRegisterType(return_type);
2327 } else {
2328 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2329 }
jeffhaobdb76512011-09-07 11:43:16 -07002330 just_set_result = true;
2331 }
2332 break;
jeffhaobdb76512011-09-07 11:43:16 -07002333 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002336 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002337 METHOD_INTERFACE,
2338 is_range,
2339 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002341 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002342 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2343 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2344 << PrettyMethod(abs_method) << "'";
2345 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 }
Ian Rogers0d604842012-04-16 14:50:24 -07002347 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002348 /* Get the type of the "this" arg, which should either be a sub-interface of called
2349 * interface or Object (see comments in RegType::JoinClass).
2350 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002351 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002352 if (this_type.IsZero()) {
2353 /* null pointer always passes (and always fails at runtime) */
2354 } else {
2355 if (this_type.IsUninitializedTypes()) {
2356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2357 << this_type;
2358 break;
2359 }
2360 // In the past we have tried to assert that "called_interface" is assignable
2361 // from "this_type.GetClass()", however, as we do an imprecise Join
2362 // (RegType::JoinClass) we don't have full information on what interfaces are
2363 // implemented by "this_type". For example, two classes may implement the same
2364 // interfaces and have a common parent that doesn't implement the interface. The
2365 // join will set "this_type" to the parent class and a test that this implements
2366 // the interface will incorrectly fail.
2367 }
2368 /*
2369 * We don't have an object instance, so we can't find the concrete method. However, all of
2370 * the type information is in the abstract method, so we're good.
2371 */
2372 const char* descriptor;
2373 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002375 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2376 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2377 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2378 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002379 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002381 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002382 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 if (!return_type.IsLowHalf()) {
2384 work_line_->SetResultRegisterType(return_type);
2385 } else {
2386 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2387 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002388 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 }
jeffhaobdb76512011-09-07 11:43:16 -07002391 case Instruction::NEG_INT:
2392 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::NEG_LONG:
2396 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
2407 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002416 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002419 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
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::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002424 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002434 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002439 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002442 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002443 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002447 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002450 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002460 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462
2463 case Instruction::ADD_INT:
2464 case Instruction::SUB_INT:
2465 case Instruction::MUL_INT:
2466 case Instruction::REM_INT:
2467 case Instruction::DIV_INT:
2468 case Instruction::SHL_INT:
2469 case Instruction::SHR_INT:
2470 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::AND_INT:
2475 case Instruction::OR_INT:
2476 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002477 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::ADD_LONG:
2481 case Instruction::SUB_LONG:
2482 case Instruction::MUL_LONG:
2483 case Instruction::DIV_LONG:
2484 case Instruction::REM_LONG:
2485 case Instruction::AND_LONG:
2486 case Instruction::OR_LONG:
2487 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002488 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.LongLo(), reg_types_.LongHi(),
2490 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::SHL_LONG:
2493 case Instruction::SHR_LONG:
2494 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002496 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002497 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::ADD_FLOAT:
2500 case Instruction::SUB_FLOAT:
2501 case Instruction::MUL_FLOAT:
2502 case Instruction::DIV_FLOAT:
2503 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002504 work_line_->CheckBinaryOp(inst,
2505 reg_types_.Float(),
2506 reg_types_.Float(),
2507 reg_types_.Float(),
2508 false);
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::ADD_DOUBLE:
2511 case Instruction::SUB_DOUBLE:
2512 case Instruction::MUL_DOUBLE:
2513 case Instruction::DIV_DOUBLE:
2514 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002515 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002516 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2517 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::ADD_INT_2ADDR:
2520 case Instruction::SUB_INT_2ADDR:
2521 case Instruction::MUL_INT_2ADDR:
2522 case Instruction::REM_INT_2ADDR:
2523 case Instruction::SHL_INT_2ADDR:
2524 case Instruction::SHR_INT_2ADDR:
2525 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002526 work_line_->CheckBinaryOp2addr(inst,
2527 reg_types_.Integer(),
2528 reg_types_.Integer(),
2529 reg_types_.Integer(),
2530 false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::AND_INT_2ADDR:
2533 case Instruction::OR_INT_2ADDR:
2534 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002535 work_line_->CheckBinaryOp2addr(inst,
2536 reg_types_.Integer(),
2537 reg_types_.Integer(),
2538 reg_types_.Integer(),
2539 true);
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002542 work_line_->CheckBinaryOp2addr(inst,
2543 reg_types_.Integer(),
2544 reg_types_.Integer(),
2545 reg_types_.Integer(),
2546 false);
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::ADD_LONG_2ADDR:
2549 case Instruction::SUB_LONG_2ADDR:
2550 case Instruction::MUL_LONG_2ADDR:
2551 case Instruction::DIV_LONG_2ADDR:
2552 case Instruction::REM_LONG_2ADDR:
2553 case Instruction::AND_LONG_2ADDR:
2554 case Instruction::OR_LONG_2ADDR:
2555 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002556 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002557 reg_types_.LongLo(), reg_types_.LongHi(),
2558 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
2560 case Instruction::SHL_LONG_2ADDR:
2561 case Instruction::SHR_LONG_2ADDR:
2562 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002563 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002564 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566 case Instruction::ADD_FLOAT_2ADDR:
2567 case Instruction::SUB_FLOAT_2ADDR:
2568 case Instruction::MUL_FLOAT_2ADDR:
2569 case Instruction::DIV_FLOAT_2ADDR:
2570 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002571 work_line_->CheckBinaryOp2addr(inst,
2572 reg_types_.Float(),
2573 reg_types_.Float(),
2574 reg_types_.Float(),
2575 false);
jeffhaobdb76512011-09-07 11:43:16 -07002576 break;
2577 case Instruction::ADD_DOUBLE_2ADDR:
2578 case Instruction::SUB_DOUBLE_2ADDR:
2579 case Instruction::MUL_DOUBLE_2ADDR:
2580 case Instruction::DIV_DOUBLE_2ADDR:
2581 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002582 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002583 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2584 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::ADD_INT_LIT16:
2587 case Instruction::RSUB_INT:
2588 case Instruction::MUL_INT_LIT16:
2589 case Instruction::DIV_INT_LIT16:
2590 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002591 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::AND_INT_LIT16:
2594 case Instruction::OR_INT_LIT16:
2595 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002596 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002597 break;
2598 case Instruction::ADD_INT_LIT8:
2599 case Instruction::RSUB_INT_LIT8:
2600 case Instruction::MUL_INT_LIT8:
2601 case Instruction::DIV_INT_LIT8:
2602 case Instruction::REM_INT_LIT8:
2603 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002606 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::AND_INT_LIT8:
2609 case Instruction::OR_INT_LIT8:
2610 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002611 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002614 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002615 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002616 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002617 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2618 }
2619 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002620 // Note: the following instructions encode offsets derived from class linking.
2621 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2622 // meaning if the class linking and resolution were successful.
2623 case Instruction::IGET_QUICK:
2624 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2625 break;
2626 case Instruction::IGET_WIDE_QUICK:
2627 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2628 break;
2629 case Instruction::IGET_OBJECT_QUICK:
2630 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2631 break;
2632 case Instruction::IPUT_QUICK:
2633 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2634 break;
2635 case Instruction::IPUT_WIDE_QUICK:
2636 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2637 break;
2638 case Instruction::IPUT_OBJECT_QUICK:
2639 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2640 break;
2641 case Instruction::INVOKE_VIRTUAL_QUICK:
2642 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2643 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002644 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002645 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002646 const char* descriptor = called_method->GetReturnTypeDescriptor();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002647 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002648 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002649 if (!return_type.IsLowHalf()) {
2650 work_line_->SetResultRegisterType(return_type);
2651 } else {
2652 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2653 }
2654 just_set_result = true;
2655 }
2656 break;
2657 }
2658
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002660 case Instruction::UNUSED_3E:
2661 case Instruction::UNUSED_3F:
2662 case Instruction::UNUSED_40:
2663 case Instruction::UNUSED_41:
2664 case Instruction::UNUSED_42:
2665 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002666 case Instruction::UNUSED_79:
2667 case Instruction::UNUSED_7A:
2668 case Instruction::UNUSED_EB:
2669 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002670 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002671 case Instruction::UNUSED_EE:
2672 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002673 case Instruction::UNUSED_F0:
2674 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002675 case Instruction::UNUSED_F2:
2676 case Instruction::UNUSED_F3:
2677 case Instruction::UNUSED_F4:
2678 case Instruction::UNUSED_F5:
2679 case Instruction::UNUSED_F6:
2680 case Instruction::UNUSED_F7:
2681 case Instruction::UNUSED_F8:
2682 case Instruction::UNUSED_F9:
2683 case Instruction::UNUSED_FA:
2684 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002685 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002686 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002687 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002688 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691
2692 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002693 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002694 * complain if an instruction is missing (which is desirable).
2695 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002696 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002697
Ian Rogersad0b3a32012-04-16 14:50:24 -07002698 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002699 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002700 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002701 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002702 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002703 /* immediate failure, reject class */
2704 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2705 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002706 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002707 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002708 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002709 }
jeffhaobdb76512011-09-07 11:43:16 -07002710 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 * If we didn't just set the result register, clear it out. This ensures that you can only use
2712 * "move-result" immediately after the result is set. (We could check this statically, but it's
2713 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002714 */
2715 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002717 }
2718
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002719
jeffhaobdb76512011-09-07 11:43:16 -07002720
2721 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002722 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002723 *
2724 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002725 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002726 * somebody could get a reference field, check it for zero, and if the
2727 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002728 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002729 * that, and will reject the code.
2730 *
2731 * TODO: avoid re-fetching the branch target
2732 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002733 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002734 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002736 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002738 return false;
2739 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002741 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002742 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 }
jeffhaobdb76512011-09-07 11:43:16 -07002744 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002745 if (NULL != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002746 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002747 return false;
2748 }
2749 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002750 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002751 return false;
2752 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 }
jeffhaobdb76512011-09-07 11:43:16 -07002754 }
2755
2756 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002757 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002758 *
2759 * We've already verified that the table is structurally sound, so we
2760 * just need to walk through and tag the targets.
2761 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002762 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002763 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2764 const uint16_t* switch_insns = insns + offset_to_switch;
2765 int switch_count = switch_insns[1];
2766 int offset_to_targets, targ;
2767
2768 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2769 /* 0 = sig, 1 = count, 2/3 = first key */
2770 offset_to_targets = 4;
2771 } else {
2772 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002773 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002774 offset_to_targets = 2 + 2 * switch_count;
2775 }
2776
2777 /* verify each switch target */
2778 for (targ = 0; targ < switch_count; targ++) {
2779 int offset;
2780 uint32_t abs_offset;
2781
2782 /* offsets are 32-bit, and only partly endian-swapped */
2783 offset = switch_insns[offset_to_targets + targ * 2] |
2784 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 abs_offset = work_insn_idx_ + offset;
2786 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002787 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002788 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002790 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002791 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002792 }
jeffhaobdb76512011-09-07 11:43:16 -07002793 }
2794 }
2795
2796 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2798 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002799 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002800 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002801 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002802 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002803
Andreas Gampef91baf12014-07-18 15:41:00 -07002804 // Need the linker to try and resolve the handled class to check if it's Throwable.
2805 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2806
Ian Rogers0571d352011-11-03 19:51:38 -07002807 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002808 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2809 if (handler_type_idx == DexFile::kDexNoIndex16) {
2810 has_catch_all_handler = true;
2811 } else {
2812 // It is also a catch-all if it is java.lang.Throwable.
2813 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, *dex_cache_,
2814 *class_loader_);
2815 if (klass != nullptr) {
2816 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2817 has_catch_all_handler = true;
2818 }
2819 } else {
2820 // Clear exception.
2821 Thread* self = Thread::Current();
2822 DCHECK(self->IsExceptionPending());
2823 self->ClearException();
2824 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 }
jeffhaobdb76512011-09-07 11:43:16 -07002826 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2828 * "work_regs", because at runtime the exception will be thrown before the instruction
2829 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002830 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002831 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002832 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 }
jeffhaobdb76512011-09-07 11:43:16 -07002834 }
2835
2836 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2838 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002839 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002840 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002841 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 * The state in work_line reflects the post-execution state. If the current instruction is a
2843 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002844 * it will do so before grabbing the lock).
2845 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002846 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002847 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002849 return false;
2850 }
2851 }
2852 }
2853
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002854 /* Handle "continue". Tag the next consecutive instruction.
2855 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2856 * because it changes work_line_ when performing peephole optimization
2857 * and this change should not be used in those cases.
2858 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002859 if ((opcode_flags & Instruction::kContinue) != 0) {
2860 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2861 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2863 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002864 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002865 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2866 // next instruction isn't one.
2867 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2868 return false;
2869 }
2870 if (NULL != fallthrough_line.get()) {
2871 // Make workline consistent with fallthrough computed from peephole optimization.
2872 work_line_->CopyFromLine(fallthrough_line.get());
2873 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002874 if (insn_flags_[next_insn_idx].IsReturn()) {
2875 // For returns we only care about the operand to the return, all other registers are dead.
2876 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2877 Instruction::Code opcode = ret_inst->Opcode();
2878 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2879 work_line_->MarkAllRegistersAsConflicts();
2880 } else {
2881 if (opcode == Instruction::RETURN_WIDE) {
2882 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2883 } else {
2884 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2885 }
2886 }
2887 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002888 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2889 if (next_line != NULL) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002890 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2891 // needed. If the merge changes the state of the registers then the work line will be
2892 // updated.
2893 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002894 return false;
2895 }
2896 } else {
2897 /*
2898 * We're not recording register data for the next instruction, so we don't know what the
2899 * prior state was. We have to assume that something has changed and re-evaluate it.
2900 */
2901 insn_flags_[next_insn_idx].SetChanged();
2902 }
2903 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002904
jeffhaod1f0fde2011-09-08 17:25:33 -07002905 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002906 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002907 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 return false;
2909 }
jeffhaobdb76512011-09-07 11:43:16 -07002910 }
2911
2912 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002913 * Update start_guess. Advance to the next instruction of that's
2914 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002915 * neither of those exists we're in a return or throw; leave start_guess
2916 * alone and let the caller sort it out.
2917 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002918 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002920 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002921 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002923 }
2924
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2926 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002927
2928 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002929} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002930
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002931RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002932 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002933 RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002934 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002935 RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002936 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2937 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002938 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002939 if (result.IsConflict()) {
2940 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2941 << "' in " << referrer;
2942 return result;
2943 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002944 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002945 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002948 // check at runtime if access is allowed and so pass here. If result is
2949 // primitive, skip the access check.
2950 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2951 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002952 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002953 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002954 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002955 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002956}
2957
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002958RegType& MethodVerifier::GetCaughtExceptionType() {
2959 RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002960 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002961 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2963 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002964 CatchHandlerIterator iterator(handlers_ptr);
2965 for (; iterator.HasNext(); iterator.Next()) {
2966 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2967 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002968 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002970 RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002971 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002972 if (exception.IsUnresolvedTypes()) {
2973 // We don't know enough about the type. Fail here and let runtime handle it.
2974 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2975 return exception;
2976 } else {
2977 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2978 return reg_types_.Conflict();
2979 }
Jeff Haob878f212014-04-24 16:25:36 -07002980 } else if (common_super == nullptr) {
2981 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002982 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002983 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002985 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002986 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 }
2988 }
2989 }
2990 }
Ian Rogers0571d352011-11-03 19:51:38 -07002991 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002992 }
2993 }
2994 if (common_super == NULL) {
2995 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002996 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002997 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002999 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003000}
3001
Brian Carlstromea46f952013-07-30 01:26:50 -07003002mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003003 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003005 RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003006 if (klass_type.IsConflict()) {
3007 std::string append(" in attempt to access method ");
3008 append += dex_file_->GetMethodName(method_id);
3009 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08003010 return NULL;
3011 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003012 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003013 return NULL; // Can't resolve Class so no more to do here
3014 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003015 mirror::Class* klass = klass_type.GetClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003016 RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003017 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003019 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003020 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003021
3022 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003024 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 res_method = klass->FindInterfaceMethod(name, signature);
3026 } else {
3027 res_method = klass->FindVirtualMethod(name, signature);
3028 }
3029 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003030 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003032 // If a virtual or interface method wasn't found with the expected type, look in
3033 // the direct methods. This can happen when the wrong invoke type is used or when
3034 // a class has changed, and will be flagged as an error in later checks.
3035 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3036 res_method = klass->FindDirectMethod(name, signature);
3037 }
3038 if (res_method == NULL) {
3039 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3040 << PrettyDescriptor(klass) << "." << name
3041 << " " << signature;
3042 return NULL;
3043 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 }
3045 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3047 // enforce them here.
3048 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3050 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 return NULL;
3052 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003053 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003054 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3056 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003057 return NULL;
3058 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003059 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003060 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003061 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003062 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003063 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003064 }
jeffhaode0d9c92012-02-27 13:58:13 -08003065 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3066 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3068 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003069 return NULL;
3070 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003071 // Check that interface methods match interface classes.
3072 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3073 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3074 << " is in an interface class " << PrettyClass(klass);
3075 return NULL;
3076 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3077 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3078 << " is in a non-interface class " << PrettyClass(klass);
3079 return NULL;
3080 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 // See if the method type implied by the invoke instruction matches the access flags for the
3082 // target method.
3083 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3084 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3085 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3086 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003087 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3088 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003089 return NULL;
3090 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003091 return res_method;
3092}
3093
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003094template <class T>
3095mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3096 MethodType method_type,
3097 bool is_range,
3098 mirror::ArtMethod* res_method) {
3099 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3100 // match the call to the signature. Also, we might be calling through an abstract method
3101 // definition (which doesn't have register count values).
3102 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3103 /* caught by static verifier */
3104 DCHECK(is_range || expected_args <= 5);
3105 if (expected_args > code_item_->outs_size_) {
3106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3107 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3108 return nullptr;
3109 }
3110
3111 uint32_t arg[5];
3112 if (!is_range) {
3113 inst->GetVarArgs(arg);
3114 }
3115 uint32_t sig_registers = 0;
3116
3117 /*
3118 * Check the "this" argument, which must be an instance of the class that declared the method.
3119 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3120 * rigorous check here (which is okay since we have to do it at runtime).
3121 */
3122 if (method_type != METHOD_STATIC) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003123 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003124 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3125 CHECK(have_pending_hard_failure_);
3126 return nullptr;
3127 }
3128 if (actual_arg_type.IsUninitializedReference()) {
3129 if (res_method) {
3130 if (!res_method->IsConstructor()) {
3131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3132 return nullptr;
3133 }
3134 } else {
3135 // Check whether the name of the called method is "<init>"
3136 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003137 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3139 return nullptr;
3140 }
3141 }
3142 }
3143 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003144 RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003145 if (res_method != nullptr) {
3146 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003147 std::string temp;
3148 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003149 klass->CannotBeAssignedFromOtherTypes());
3150 } else {
3151 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3152 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3153 res_method_class = &reg_types_.FromDescriptor(class_loader_->Get(),
3154 dex_file_->StringByTypeIdx(class_idx),
3155 false);
3156 }
3157 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3158 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3159 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3160 << "' not instance of '" << *res_method_class << "'";
3161 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3162 // the compiler.
3163 if (have_pending_hard_failure_) {
3164 return nullptr;
3165 }
3166 }
3167 }
3168 sig_registers = 1;
3169 }
3170
3171 for ( ; it->HasNext(); it->Next()) {
3172 if (sig_registers >= expected_args) {
3173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3174 " arguments, found " << sig_registers << " or more.";
3175 return nullptr;
3176 }
3177
3178 const char* param_descriptor = it->GetDescriptor();
3179
3180 if (param_descriptor == nullptr) {
3181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3182 "component";
3183 return nullptr;
3184 }
3185
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003186 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), param_descriptor,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003187 false);
3188 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3189 arg[sig_registers];
3190 if (reg_type.IsIntegralTypes()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003191 RegType& src_type = work_line_->GetRegisterType(get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003192 if (!src_type.IsIntegralTypes()) {
3193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3194 << " but expected " << reg_type;
3195 return res_method;
3196 }
3197 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3198 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3199 // compiler.
3200 if (have_pending_hard_failure_) {
3201 return res_method;
3202 }
3203 }
3204 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3205 }
3206 if (expected_args != sig_registers) {
3207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3208 " arguments, found " << sig_registers;
3209 return nullptr;
3210 }
3211 return res_method;
3212}
3213
3214void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3215 MethodType method_type,
3216 bool is_range) {
3217 // As the method may not have been resolved, make this static check against what we expect.
3218 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3219 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3220 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3221 DexFileParameterIterator it(*dex_file_,
3222 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3223 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3224 nullptr);
3225}
3226
3227class MethodParamListDescriptorIterator {
3228 public:
3229 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3230 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3231 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3232 }
3233
3234 bool HasNext() {
3235 return pos_ < params_size_;
3236 }
3237
3238 void Next() {
3239 ++pos_;
3240 }
3241
3242 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3243 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3244 }
3245
3246 private:
3247 mirror::ArtMethod* res_method_;
3248 size_t pos_;
3249 const DexFile::TypeList* params_;
3250 const size_t params_size_;
3251};
3252
Brian Carlstromea46f952013-07-30 01:26:50 -07003253mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003254 MethodType method_type,
3255 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003256 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003257 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3258 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003259 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003260
Brian Carlstromea46f952013-07-30 01:26:50 -07003261 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003262 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003263 // Check what we can statically.
3264 if (!have_pending_hard_failure_) {
3265 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3266 }
3267 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003268 }
3269
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3271 // has a vtable entry for the target method.
3272 if (is_super) {
3273 DCHECK(method_type == METHOD_VIRTUAL);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003274 RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003275 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003276 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003277 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003278 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003279 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003280 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003281 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003282 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003283 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003284 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003285 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003286 << "." << res_method->GetName()
3287 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003288 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003289 }
3290 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003291
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003292 // Process the target method's signature. This signature may or may not
3293 MethodParamListDescriptorIterator it(res_method);
3294 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3295 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003296}
3297
Brian Carlstromea46f952013-07-30 01:26:50 -07003298mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003299 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003300 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3301 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003302 RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003303 if (!actual_arg_type.HasClass()) {
3304 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003305 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003306 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003307 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003308 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003309 if (klass->IsInterface()) {
3310 // Derive Object.class from Class.class.getSuperclass().
3311 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3312 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003313 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003314 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003315 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003316 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003317 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003318 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003319 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3320 << PrettyDescriptor(klass);
3321 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003322 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003323 return res_method;
3324}
3325
Brian Carlstromea46f952013-07-30 01:26:50 -07003326mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003327 bool is_range) {
3328 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003329 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003330 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003331 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003333 return NULL;
3334 }
3335 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3336
3337 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3338 // match the call to the signature. Also, we might be calling through an abstract method
3339 // definition (which doesn't have register count values).
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003340 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003341 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3342 return NULL;
3343 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003344 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3345 /* caught by static verifier */
3346 DCHECK(is_range || expected_args <= 5);
3347 if (expected_args > code_item_->outs_size_) {
3348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3349 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3350 return NULL;
3351 }
3352
3353 /*
3354 * Check the "this" argument, which must be an instance of the class that declared the method.
3355 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3356 * rigorous check here (which is okay since we have to do it at runtime).
3357 */
3358 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3360 return NULL;
3361 }
3362 if (!actual_arg_type.IsZero()) {
3363 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003364 std::string temp;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003365 RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003366 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003367 klass->CannotBeAssignedFromOtherTypes());
3368 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003369 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3370 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003371 << "' not instance of '" << res_method_class << "'";
3372 return NULL;
3373 }
3374 }
3375 /*
3376 * Process the target method's signature. This signature may or may not
3377 * have been verified, so we can't assume it's properly formed.
3378 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003379 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003380 size_t params_size = params == NULL ? 0 : params->Size();
3381 uint32_t arg[5];
3382 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003383 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003384 }
3385 size_t actual_args = 1;
3386 for (size_t param_index = 0; param_index < params_size; param_index++) {
3387 if (actual_args >= expected_args) {
3388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003389 << "'. Expected " << expected_args
3390 << " arguments, processing argument " << actual_args
3391 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003392 return NULL;
3393 }
3394 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003395 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003396 if (descriptor == NULL) {
3397 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003398 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003399 return NULL;
3400 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003401 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003402 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3403 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3404 return res_method;
3405 }
3406 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3407 }
3408 if (actual_args != expected_args) {
3409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3410 << " expected " << expected_args << " arguments, found " << actual_args;
3411 return NULL;
3412 } else {
3413 return res_method;
3414 }
3415}
3416
Ian Rogers62342ec2013-06-11 10:26:37 -07003417void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003418 uint32_t type_idx;
3419 if (!is_filled) {
3420 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3421 type_idx = inst->VRegC_22c();
3422 } else if (!is_range) {
3423 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3424 type_idx = inst->VRegB_35c();
3425 } else {
3426 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3427 type_idx = inst->VRegB_3rc();
3428 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003429 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003430 if (res_type.IsConflict()) { // bad class
3431 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003432 } else {
3433 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3434 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003436 } else if (!is_filled) {
3437 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003438 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003439 /* set register type to array class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003440 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003441 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003442 } else {
3443 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3444 // the list and fail. It's legal, if silly, for arg_count to be zero.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003445 RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003446 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3447 uint32_t arg[5];
3448 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003449 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003450 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003451 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003452 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003453 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003454 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003455 return;
3456 }
3457 }
3458 // filled-array result goes into "result" register
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003459 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003460 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003461 }
3462 }
3463}
3464
Sebastien Hertz5243e912013-05-21 10:55:07 +02003465void MethodVerifier::VerifyAGet(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003466 RegType& insn_type, bool is_primitive) {
3467 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003471 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003472 if (array_type.IsZero()) {
3473 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3474 // instruction type. TODO: have a proper notion of bottom here.
3475 if (!is_primitive || insn_type.IsCategory1Types()) {
3476 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003477 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003478 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003479 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003480 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003481 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003482 }
jeffhaofc3144e2012-02-01 17:21:15 -08003483 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003485 } else {
3486 /* verify the class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003487 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003488 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003490 << " source for aget-object";
3491 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003493 << " source for category 1 aget";
3494 } else if (is_primitive && !insn_type.Equals(component_type) &&
3495 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003496 (insn_type.IsLong() && component_type.IsDouble()))) {
3497 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3498 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003499 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003500 // Use knowledge of the field type which is stronger than the type inferred from the
3501 // instruction, which can't differentiate object types and ints from floats, longs from
3502 // doubles.
3503 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003505 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003506 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003507 component_type.HighHalf(&reg_types_));
3508 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 }
3510 }
3511 }
3512}
3513
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003514void MethodVerifier::VerifyPrimitivePut(RegType& target_type, RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003515 const uint32_t vregA) {
3516 // Primitive assignability rules are weaker than regular assignability rules.
3517 bool instruction_compatible;
3518 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003519 RegType& value_type = work_line_->GetRegisterType(vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003520 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003521 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003522 value_compatible = value_type.IsIntegralTypes();
3523 } else if (target_type.IsFloat()) {
3524 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3525 value_compatible = value_type.IsFloatTypes();
3526 } else if (target_type.IsLong()) {
3527 instruction_compatible = insn_type.IsLong();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003528 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003529 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003530 } else if (target_type.IsDouble()) {
3531 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003532 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003533 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003534 } else {
3535 instruction_compatible = false; // reference with primitive store
3536 value_compatible = false; // unused
3537 }
3538 if (!instruction_compatible) {
3539 // This is a global failure rather than a class change failure as the instructions and
3540 // the descriptors for the type should have been consistent within the same file at
3541 // compile time.
3542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3543 << "' but expected type '" << target_type << "'";
3544 return;
3545 }
3546 if (!value_compatible) {
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3548 << " of type " << value_type << " but expected " << target_type << " for put";
3549 return;
3550 }
3551}
3552
Sebastien Hertz5243e912013-05-21 10:55:07 +02003553void MethodVerifier::VerifyAPut(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003554 RegType& insn_type, bool is_primitive) {
3555 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003559 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003560 if (array_type.IsZero()) {
3561 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3562 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003563 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003565 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003566 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003567 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003568 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003569 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003570 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003571 if (!component_type.IsReferenceTypes()) {
3572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3573 << " source for aput-object";
3574 } else {
3575 // The instruction agrees with the type of array, confirm the value to be stored does too
3576 // Note: we use the instruction type (rather than the component type) for aput-object as
3577 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003578 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003579 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 }
3581 }
3582 }
3583}
3584
Brian Carlstromea46f952013-07-30 01:26:50 -07003585mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003586 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3587 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003588 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003589 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003590 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3591 field_idx, dex_file_->GetFieldName(field_id),
3592 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003593 return NULL;
3594 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003595 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003596 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003597 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003598 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3599 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3600 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003602 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003603 << dex_file_->GetFieldName(field_id) << ") in "
3604 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003605 DCHECK(Thread::Current()->IsExceptionPending());
3606 Thread::Current()->ClearException();
3607 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003608 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3609 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003611 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 return NULL;
3613 } else if (!field->IsStatic()) {
3614 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3615 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003616 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003617 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003618}
3619
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003620mirror::ArtField* MethodVerifier::GetInstanceField(RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003621 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3622 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003623 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003624 if (klass_type.IsConflict()) {
3625 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3626 field_idx, dex_file_->GetFieldName(field_id),
3627 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003628 return NULL;
3629 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003630 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003631 return NULL; // Can't resolve Class so no more to do here
3632 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3634 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3635 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003637 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003638 << dex_file_->GetFieldName(field_id) << ") in "
3639 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 DCHECK(Thread::Current()->IsExceptionPending());
3641 Thread::Current()->ClearException();
3642 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003643 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3644 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003645 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003646 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 return NULL;
3648 } else if (field->IsStatic()) {
3649 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3650 << " to not be static";
3651 return NULL;
3652 } else if (obj_type.IsZero()) {
3653 // Cannot infer and check type, however, access will cause null pointer exception
3654 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003655 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003656 mirror::Class* klass = field->GetDeclaringClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003657 RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003658 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003659 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003660 if (obj_type.IsUninitializedTypes() &&
3661 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3662 !field_klass.Equals(GetDeclaringClass()))) {
3663 // Field accesses through uninitialized references are only allowable for constructors where
3664 // the field is declared in this class
3665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003666 << " of a not fully initialized object within the context"
3667 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003668 return NULL;
3669 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3670 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3671 // of C1. For resolution to occur the declared class of the field must be compatible with
3672 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3673 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3674 << " from object of type " << obj_type;
3675 return NULL;
3676 } else {
3677 return field;
3678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 }
3680}
3681
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003682void MethodVerifier::VerifyISGet(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003683 bool is_primitive, bool is_static) {
3684 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003685 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003686 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003687 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003688 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003689 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003690 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003691 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003692 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003693 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003694 Thread* self = Thread::Current();
3695 mirror::Class* field_type_class;
3696 {
3697 StackHandleScope<1> hs(self);
3698 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3699 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3700 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003701 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003702 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003703 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003704 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003705 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3706 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003707 }
Ian Rogers0d604842012-04-16 14:50:24 -07003708 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003709 if (field_type == nullptr) {
3710 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3711 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003712 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003713 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003714 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003715 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003716 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003717 if (field_type->Equals(insn_type) ||
3718 (field_type->IsFloat() && insn_type.IsInteger()) ||
3719 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003720 // expected that read is of the correct primitive type or that int reads are reading
3721 // floats or long reads are reading doubles
3722 } else {
3723 // This is a global failure rather than a class change failure as the instructions and
3724 // the descriptors for the type should have been consistent within the same file at
3725 // compile time
3726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3727 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003728 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003729 return;
3730 }
3731 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003732 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003733 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3734 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003735 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003736 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003737 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003738 return;
3739 }
3740 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003741 if (!field_type->IsLowHalf()) {
3742 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003743 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003744 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003745 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003746}
3747
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003748void MethodVerifier::VerifyISPut(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003749 bool is_primitive, bool is_static) {
3750 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003751 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003752 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003753 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003754 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003755 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003756 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003757 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003758 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003759 if (field != NULL) {
3760 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3761 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3762 << " from other class " << GetDeclaringClass();
3763 return;
3764 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003765 mirror::Class* field_type_class;
3766 {
3767 StackHandleScope<1> hs(Thread::Current());
3768 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3769 FieldHelper fh(h_field);
3770 field_type_class = fh.GetType(can_load_classes_);
3771 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003772 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003773 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003774 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003775 } else {
3776 Thread* self = Thread::Current();
3777 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3778 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003779 }
3780 }
3781 if (field_type == nullptr) {
3782 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3783 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003784 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003785 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003786 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003787 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003788 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003789 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003790 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003791 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003792 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3793 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003794 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003795 << "' in put-object";
3796 return;
3797 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003798 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 }
3800}
3801
Brian Carlstromea46f952013-07-30 01:26:50 -07003802mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003803 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003804 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3805 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3806 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3807 inst->Opcode() == Instruction::IPUT_QUICK ||
3808 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3809 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003810 RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003811 if (!object_type.HasClass()) {
3812 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3813 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003814 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003815 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003816 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3817 field_offset);
3818 if (f == nullptr) {
3819 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3820 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3821 }
3822 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003823}
3824
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003825void MethodVerifier::VerifyIGetQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003826 bool is_primitive) {
3827 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003828 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003829 if (field == NULL) {
3830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3831 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003832 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003833 mirror::Class* field_type_class;
3834 {
3835 StackHandleScope<1> hs(Thread::Current());
3836 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3837 FieldHelper fh(h_field);
3838 field_type_class = fh.GetType(can_load_classes_);
3839 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003840 RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003841 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003842 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003843 field_type_class->CannotBeAssignedFromOtherTypes());
3844 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003845 Thread* self = Thread::Current();
3846 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3847 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003848 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003849 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003850 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003851 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003852 const uint32_t vregA = inst->VRegA_22c();
3853 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003854 if (field_type->Equals(insn_type) ||
3855 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3856 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003857 // expected that read is of the correct primitive type or that int reads are reading
3858 // floats or long reads are reading doubles
3859 } else {
3860 // This is a global failure rather than a class change failure as the instructions and
3861 // the descriptors for the type should have been consistent within the same file at
3862 // compile time
3863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003864 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003865 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003866 return;
3867 }
3868 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003869 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003870 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003871 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003872 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003873 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003874 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3875 return;
3876 }
3877 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003878 if (!field_type->IsLowHalf()) {
3879 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003880 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003881 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003882 }
3883}
3884
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003885void MethodVerifier::VerifyIPutQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003886 bool is_primitive) {
3887 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003888 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003889 if (field == NULL) {
3890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3891 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003892 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003893 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003894 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003895 RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003896 if (field != NULL) {
3897 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3898 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003899 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003900 return;
3901 }
3902 }
3903 const uint32_t vregA = inst->VRegA_22c();
3904 if (is_primitive) {
3905 // Primitive field assignability rules are weaker than regular assignability rules
3906 bool instruction_compatible;
3907 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003908 RegType& value_type = work_line_->GetRegisterType(vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003909 if (field_type.IsIntegralTypes()) {
3910 instruction_compatible = insn_type.IsIntegralTypes();
3911 value_compatible = value_type.IsIntegralTypes();
3912 } else if (field_type.IsFloat()) {
3913 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3914 value_compatible = value_type.IsFloatTypes();
3915 } else if (field_type.IsLong()) {
3916 instruction_compatible = insn_type.IsLong();
3917 value_compatible = value_type.IsLongTypes();
3918 } else if (field_type.IsDouble()) {
3919 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3920 value_compatible = value_type.IsDoubleTypes();
3921 } else {
3922 instruction_compatible = false; // reference field with primitive store
3923 value_compatible = false; // unused
3924 }
3925 if (!instruction_compatible) {
3926 // This is a global failure rather than a class change failure as the instructions and
3927 // the descriptors for the type should have been consistent within the same file at
3928 // compile time
3929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003930 << " to be of type '" << insn_type
3931 << "' but found type '" << field_type
3932 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003933 return;
3934 }
3935 if (!value_compatible) {
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3937 << " of type " << value_type
3938 << " but expected " << field_type
3939 << " for store to " << PrettyField(field) << " in put";
3940 return;
3941 }
3942 } else {
3943 if (!insn_type.IsAssignableFrom(field_type)) {
3944 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003945 << " to be compatible with type '" << insn_type
3946 << "' but found type '" << field_type
3947 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003948 return;
3949 }
3950 work_line_->VerifyRegisterType(vregA, field_type);
3951 }
3952}
3953
Ian Rogers776ac1f2012-04-13 23:36:36 -07003954bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003955 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003957 return false;
3958 }
3959 return true;
3960}
3961
Ian Rogersebbdd872014-07-07 23:53:08 -07003962bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3963 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003964 bool changed = true;
3965 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3966 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003967 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003968 * We haven't processed this instruction before, and we haven't touched the registers here, so
3969 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3970 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003971 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003972 if (!insn_flags_[next_insn].IsReturn()) {
3973 target_line->CopyFromLine(merge_line);
3974 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003975 // Verify that the monitor stack is empty on return.
3976 if (!merge_line->VerifyMonitorStackEmpty()) {
3977 return false;
3978 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003979 // For returns we only care about the operand to the return, all other registers are dead.
3980 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3981 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3982 Instruction::Code opcode = ret_inst->Opcode();
3983 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3984 target_line->MarkAllRegistersAsConflicts();
3985 } else {
3986 target_line->CopyFromLine(merge_line);
3987 if (opcode == Instruction::RETURN_WIDE) {
3988 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3989 } else {
3990 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3991 }
3992 }
3993 }
jeffhaobdb76512011-09-07 11:43:16 -07003994 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003995 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003996 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003997 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003998 if (gDebugVerify) {
3999 copy->CopyFromLine(target_line);
4000 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004001 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004002 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004003 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004004 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004005 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004006 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004007 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
4008 << *copy.get() << " MERGE\n"
4009 << *merge_line << " ==\n"
4010 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004011 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004012 if (update_merge_line && changed) {
4013 merge_line->CopyFromLine(target_line);
4014 }
jeffhaobdb76512011-09-07 11:43:16 -07004015 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004016 if (changed) {
4017 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004018 }
4019 return true;
4020}
4021
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004022InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004023 return &insn_flags_[work_insn_idx_];
4024}
4025
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004026RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004027 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004028 if (mirror_method_.Get() != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004029 Thread* self = Thread::Current();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004030 mirror::Class* return_type_class;
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004031 return_type_class = MethodHelper(mirror_method_).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004032 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004033 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4034 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004035 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004036 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004037 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004038 self->ClearException();
4039 }
4040 }
4041 if (return_type_ == nullptr) {
4042 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4043 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4044 uint16_t return_type_idx = proto_id.return_type_idx_;
4045 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004046 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004047 }
4048 }
4049 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004050}
4051
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004052RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004053 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004054 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004055 const char* descriptor
4056 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004057 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004058 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004059 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4060 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004061 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004062 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004063 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004064 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004065 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004066}
4067
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004068std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4069 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004070 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004071 std::vector<int32_t> result;
4072 for (size_t i = 0; i < line->NumRegs(); ++i) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004073 RegType& type = line->GetRegisterType(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004074 if (type.IsConstant()) {
4075 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4076 result.push_back(type.ConstantValue());
4077 } else if (type.IsConstantLo()) {
4078 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4079 result.push_back(type.ConstantValueLo());
4080 } else if (type.IsConstantHi()) {
4081 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4082 result.push_back(type.ConstantValueHi());
4083 } else if (type.IsIntegralTypes()) {
4084 result.push_back(kIntVReg);
4085 result.push_back(0);
4086 } else if (type.IsFloat()) {
4087 result.push_back(kFloatVReg);
4088 result.push_back(0);
4089 } else if (type.IsLong()) {
4090 result.push_back(kLongLoVReg);
4091 result.push_back(0);
4092 result.push_back(kLongHiVReg);
4093 result.push_back(0);
4094 ++i;
4095 } else if (type.IsDouble()) {
4096 result.push_back(kDoubleLoVReg);
4097 result.push_back(0);
4098 result.push_back(kDoubleHiVReg);
4099 result.push_back(0);
4100 ++i;
4101 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4102 result.push_back(kUndefined);
4103 result.push_back(0);
4104 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004105 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004106 result.push_back(kReferenceVReg);
4107 result.push_back(0);
4108 }
4109 }
4110 return result;
4111}
4112
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004113RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004114 if (precise) {
4115 // Precise constant type.
4116 return reg_types_.FromCat1Const(value, true);
4117 } else {
4118 // Imprecise constant type.
4119 if (value < -32768) {
4120 return reg_types_.IntConstant();
4121 } else if (value < -128) {
4122 return reg_types_.ShortConstant();
4123 } else if (value < 0) {
4124 return reg_types_.ByteConstant();
4125 } else if (value == 0) {
4126 return reg_types_.Zero();
4127 } else if (value == 1) {
4128 return reg_types_.One();
4129 } else if (value < 128) {
4130 return reg_types_.PosByteConstant();
4131 } else if (value < 32768) {
4132 return reg_types_.PosShortConstant();
4133 } else if (value < 65536) {
4134 return reg_types_.CharConstant();
4135 } else {
4136 return reg_types_.IntConstant();
4137 }
4138 }
4139}
4140
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004141void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004142 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004143}
4144
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004145void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004146 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004147}
jeffhaod1224c72012-02-29 13:43:08 -08004148
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004149void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4150 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004151}
4152
Ian Rogersd81871c2011-10-03 13:57:23 -07004153} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004154} // namespace art