blob: 6f9680f2c4e0ce0b60dc32616dd8e56959c89bc3 [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 Chartierbf99f772014-08-23 16:37:27 -0700126 ConstHandle<mirror::DexCache> dex_cache,
127 ConstHandle<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 Chartierbf99f772014-08-23 16:37:27 -0700246 ConstHandle<mirror::DexCache> dex_cache,
247 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700250 ConstHandle<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
Mathieu Chartierbf99f772014-08-23 16:37:27 -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 Chartierbf99f772014-08-23 16:37:27 -0700293 ConstHandle<mirror::DexCache> dex_cache,
294 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700297 ConstHandle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800298 uint32_t method_access_flags) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -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 Chartierbf99f772014-08-23 16:37:27 -0700307MethodVerifier::MethodVerifier(const DexFile* dex_file, ConstHandle<mirror::DexCache> dex_cache,
308 ConstHandle<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,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700311 ConstHandle<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 Chartierbf99f772014-08-23 16:37:27 -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 Chartierbf99f772014-08-23 16:37:27 -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 Chartierbf99f772014-08-23 16:37:27 -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 Chartierbf99f772014-08-23 16:37:27 -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.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001739 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001740 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 {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001884 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001885 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001886 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1888 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 } else {
jeffhao457cc512012-02-02 16:55:13 -08001890 // Now verify if the element width in the table matches the element width declared in
1891 // the array
1892 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1893 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001895 } else {
1896 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1897 // Since we don't compress the data in Dex, expect to see equal width of data stored
1898 // in the table and expected from the array class.
1899 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1901 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001902 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 }
1904 }
jeffhaobdb76512011-09-07 11:43:16 -07001905 }
1906 }
1907 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 }
jeffhaobdb76512011-09-07 11:43:16 -07001909 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 case Instruction::IF_NE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001911 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1912 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 bool mismatch = false;
1914 if (reg_type1.IsZero()) { // zero then integral or reference expected
1915 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1916 } else if (reg_type1.IsReferenceTypes()) { // both references?
1917 mismatch = !reg_type2.IsReferenceTypes();
1918 } else { // both integral?
1919 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1920 }
1921 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1923 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001924 }
1925 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 }
jeffhaobdb76512011-09-07 11:43:16 -07001927 case Instruction::IF_LT:
1928 case Instruction::IF_GE:
1929 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 case Instruction::IF_LE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001931 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1932 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1935 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001936 }
1937 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 }
jeffhaobdb76512011-09-07 11:43:16 -07001939 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 case Instruction::IF_NEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001941 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1944 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001946
1947 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001948 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001949 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001950 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001951 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001953 }
Ian Rogers9b360392013-06-06 14:45:07 -07001954 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001955 } else {
1956 break;
1957 }
1958
Ian Rogers9b360392013-06-06 14:45:07 -07001959 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001960
1961 /* Check for peep-hole pattern of:
1962 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001963 * instance-of vX, vY, T;
1964 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001965 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001966 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001967 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001968 * and sharpen the type of vY to be type T.
1969 * Note, this pattern can't be if:
1970 * - if there are other branches to this branch,
1971 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001972 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001973 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001974 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1975 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1976 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07001977 // Check the type of the instance-of is different than that of registers type, as if they
1978 // are the same there is no work to be done here. Check that the conversion is not to or
1979 // from an unresolved type as type information is imprecise. If the instance-of is to an
1980 // interface then ignore the type information as interfaces can only be treated as Objects
1981 // and we don't want to disallow field and other operations on the object. If the value
1982 // being instance-of checked against is known null (zero) then allow the optimization as
1983 // we didn't have type information. If the merge of the instance-of type with the original
1984 // type is assignable to the original then allow optimization. This check is performed to
1985 // ensure that subsequent merges don't lose type information - such as becoming an
1986 // interface from a class that would lose information relevant to field checks.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001987 RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
1988 RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001989
Ian Rogersebbdd872014-07-07 23:53:08 -07001990 if (!orig_type.Equals(cast_type) &&
1991 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07001992 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07001993 !cast_type.GetClass()->IsInterface() &&
1994 (orig_type.IsZero() ||
1995 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001996 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001997 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001998 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001999 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002000 branch_line.reset(update_line);
2001 }
2002 update_line->CopyFromLine(work_line_.get());
2003 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
2004 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2005 // See if instance-of was preceded by a move-object operation, common due to the small
2006 // register encoding space of instance-of, and propagate type information to the source
2007 // of the move-object.
2008 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002009 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002010 move_idx--;
2011 }
2012 CHECK(insn_flags_[move_idx].IsOpcode());
2013 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2014 switch (move_inst->Opcode()) {
2015 case Instruction::MOVE_OBJECT:
2016 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
2017 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
2018 }
2019 break;
2020 case Instruction::MOVE_OBJECT_FROM16:
2021 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
2022 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
2023 }
2024 break;
2025 case Instruction::MOVE_OBJECT_16:
2026 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2027 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2028 }
2029 break;
2030 default:
2031 break;
2032 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002033 }
2034 }
2035 }
2036
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 }
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::IF_LTZ:
2040 case Instruction::IF_GEZ:
2041 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 case Instruction::IF_LEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002043 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2046 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 }
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 }
jeffhaobdb76512011-09-07 11:43:16 -07002050 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
jeffhaobdb76512011-09-07 11:43:16 -07002059 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
2075 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
2078 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
2081 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002092 break;
2093
jeffhaobdb76512011-09-07 11:43:16 -07002094 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
jeffhaobdb76512011-09-07 11:43:16 -07002100 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
jeffhaobdb76512011-09-07 11:43:16 -07002103 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
jeffhaobdb76512011-09-07 11:43:16 -07002115
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 break;
2119 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 break;
2122 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 break;
2125 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
jeffhaobdb76512011-09-07 11:43:16 -07002134 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002136 break;
2137
jeffhaobdb76512011-09-07 11:43:16 -07002138 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 break;
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 break;
jeffhaobdb76512011-09-07 11:43:16 -07002147 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 break;
2150 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 break;
2159
2160 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 break;
2163 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002164 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002165 break;
2166 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 break;
2169 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002170 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002180 break;
2181
2182 case Instruction::INVOKE_VIRTUAL:
2183 case Instruction::INVOKE_VIRTUAL_RANGE:
2184 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002185 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002186 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2187 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002188 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2189 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002190 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2191 is_super);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002192 RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002193 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002194 Thread* self = Thread::Current();
2195 StackHandleScope<1> hs(self);
2196 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2197 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002198 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002199 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002200 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2201 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002202 return_type_class->CannotBeAssignedFromOtherTypes());
2203 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002204 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002205 self->ClearException();
2206 }
2207 }
2208 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002210 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2211 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002212 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002213 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002214 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002215 if (!return_type->IsLowHalf()) {
2216 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002217 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002218 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002219 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002220 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 }
jeffhaobdb76512011-09-07 11:43:16 -07002223 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002226 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002227 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002228 const char* return_type_descriptor;
2229 bool is_constructor;
Ian Rogers1ff3c982014-08-12 02:30:58 -07002230 RegType* return_type = nullptr;
Ian Rogers46685432012-06-03 22:26:43 -07002231 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002233 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002234 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002235 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2236 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2237 } else {
2238 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002239 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002240 Thread* self = Thread::Current();
2241 StackHandleScope<1> hs(self);
2242 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2243 MethodHelper mh(h_called_method);
2244 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2245 if (return_type_class != nullptr) {
2246 return_type = &reg_types_.FromClass(return_type_descriptor,
2247 return_type_class,
2248 return_type_class->CannotBeAssignedFromOtherTypes());
2249 } else {
2250 DCHECK(!can_load_classes_ || self->IsExceptionPending());
2251 self->ClearException();
2252 }
Ian Rogers46685432012-06-03 22:26:43 -07002253 }
2254 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002255 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 * Some additional checks when calling a constructor. We know from the invocation arg check
2257 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2258 * that to require that called_method->klass is the same as this->klass or this->super,
2259 * allowing the latter only if the "this" argument is the same as the "this" argument to
2260 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002261 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002262 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002263 if (this_type.IsConflict()) // failure.
2264 break;
jeffhaobdb76512011-09-07 11:43:16 -07002265
jeffhaob57e9522012-04-26 18:08:21 -07002266 /* no null refs allowed (?) */
2267 if (this_type.IsZero()) {
2268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2269 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002270 }
jeffhaob57e9522012-04-26 18:08:21 -07002271
2272 /* must be in same class or in superclass */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002273 // RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002274 // TODO: re-enable constructor type verification
2275 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002276 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002277 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2278 // break;
2279 // }
jeffhaob57e9522012-04-26 18:08:21 -07002280
2281 /* arg must be an uninitialized reference */
2282 if (!this_type.IsUninitializedTypes()) {
2283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2284 << this_type;
2285 break;
2286 }
2287
2288 /*
2289 * Replace the uninitialized reference with an initialized one. We need to do this for all
2290 * registers that have the same object instance in them, not just the "this" register.
2291 */
2292 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002293 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002294 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002295 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2296 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002297 }
2298 if (!return_type->IsLowHalf()) {
2299 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002301 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002302 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002303 just_set_result = true;
2304 break;
2305 }
2306 case Instruction::INVOKE_STATIC:
2307 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002309 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002310 METHOD_STATIC,
2311 is_range,
2312 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002313 const char* descriptor;
2314 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002315 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002316 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2317 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002318 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002319 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002320 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002321 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002322 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 if (!return_type.IsLowHalf()) {
2324 work_line_->SetResultRegisterType(return_type);
2325 } else {
2326 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2327 }
jeffhaobdb76512011-09-07 11:43:16 -07002328 just_set_result = true;
2329 }
2330 break;
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002332 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002334 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002335 METHOD_INTERFACE,
2336 is_range,
2337 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002338 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002339 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2341 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2342 << PrettyMethod(abs_method) << "'";
2343 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002344 }
Ian Rogers0d604842012-04-16 14:50:24 -07002345 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002346 /* Get the type of the "this" arg, which should either be a sub-interface of called
2347 * interface or Object (see comments in RegType::JoinClass).
2348 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002349 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002350 if (this_type.IsZero()) {
2351 /* null pointer always passes (and always fails at runtime) */
2352 } else {
2353 if (this_type.IsUninitializedTypes()) {
2354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2355 << this_type;
2356 break;
2357 }
2358 // In the past we have tried to assert that "called_interface" is assignable
2359 // from "this_type.GetClass()", however, as we do an imprecise Join
2360 // (RegType::JoinClass) we don't have full information on what interfaces are
2361 // implemented by "this_type". For example, two classes may implement the same
2362 // interfaces and have a common parent that doesn't implement the interface. The
2363 // join will set "this_type" to the parent class and a test that this implements
2364 // the interface will incorrectly fail.
2365 }
2366 /*
2367 * We don't have an object instance, so we can't find the concrete method. However, all of
2368 * the type information is in the abstract method, so we're good.
2369 */
2370 const char* descriptor;
2371 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002373 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2374 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2375 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2376 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002377 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002378 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002379 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 if (!return_type.IsLowHalf()) {
2381 work_line_->SetResultRegisterType(return_type);
2382 } else {
2383 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2384 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002385 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 }
jeffhaobdb76512011-09-07 11:43:16 -07002388 case Instruction::NEG_INT:
2389 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::NEG_LONG:
2393 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002406 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002413 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002414 break;
2415 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002417 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002421 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
2423 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002424 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002425 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002426 break;
2427 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002428 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002432 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002436 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002439 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002440 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002443 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002444 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002445 break;
2446 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002447 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002448 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002451 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002457 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459
2460 case Instruction::ADD_INT:
2461 case Instruction::SUB_INT:
2462 case Instruction::MUL_INT:
2463 case Instruction::REM_INT:
2464 case Instruction::DIV_INT:
2465 case Instruction::SHL_INT:
2466 case Instruction::SHR_INT:
2467 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002468 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002469 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::AND_INT:
2472 case Instruction::OR_INT:
2473 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002474 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002475 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002476 break;
2477 case Instruction::ADD_LONG:
2478 case Instruction::SUB_LONG:
2479 case Instruction::MUL_LONG:
2480 case Instruction::DIV_LONG:
2481 case Instruction::REM_LONG:
2482 case Instruction::AND_LONG:
2483 case Instruction::OR_LONG:
2484 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002485 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 reg_types_.LongLo(), reg_types_.LongHi(),
2487 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002488 break;
2489 case Instruction::SHL_LONG:
2490 case Instruction::SHR_LONG:
2491 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002492 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002493 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002494 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::ADD_FLOAT:
2497 case Instruction::SUB_FLOAT:
2498 case Instruction::MUL_FLOAT:
2499 case Instruction::DIV_FLOAT:
2500 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002501 work_line_->CheckBinaryOp(inst,
2502 reg_types_.Float(),
2503 reg_types_.Float(),
2504 reg_types_.Float(),
2505 false);
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::ADD_DOUBLE:
2508 case Instruction::SUB_DOUBLE:
2509 case Instruction::MUL_DOUBLE:
2510 case Instruction::DIV_DOUBLE:
2511 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002513 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2514 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::ADD_INT_2ADDR:
2517 case Instruction::SUB_INT_2ADDR:
2518 case Instruction::MUL_INT_2ADDR:
2519 case Instruction::REM_INT_2ADDR:
2520 case Instruction::SHL_INT_2ADDR:
2521 case Instruction::SHR_INT_2ADDR:
2522 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002523 work_line_->CheckBinaryOp2addr(inst,
2524 reg_types_.Integer(),
2525 reg_types_.Integer(),
2526 reg_types_.Integer(),
2527 false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::AND_INT_2ADDR:
2530 case Instruction::OR_INT_2ADDR:
2531 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002532 work_line_->CheckBinaryOp2addr(inst,
2533 reg_types_.Integer(),
2534 reg_types_.Integer(),
2535 reg_types_.Integer(),
2536 true);
jeffhaobdb76512011-09-07 11:43:16 -07002537 break;
2538 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002539 work_line_->CheckBinaryOp2addr(inst,
2540 reg_types_.Integer(),
2541 reg_types_.Integer(),
2542 reg_types_.Integer(),
2543 false);
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545 case Instruction::ADD_LONG_2ADDR:
2546 case Instruction::SUB_LONG_2ADDR:
2547 case Instruction::MUL_LONG_2ADDR:
2548 case Instruction::DIV_LONG_2ADDR:
2549 case Instruction::REM_LONG_2ADDR:
2550 case Instruction::AND_LONG_2ADDR:
2551 case Instruction::OR_LONG_2ADDR:
2552 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002553 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002554 reg_types_.LongLo(), reg_types_.LongHi(),
2555 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
2557 case Instruction::SHL_LONG_2ADDR:
2558 case Instruction::SHR_LONG_2ADDR:
2559 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002560 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002561 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::ADD_FLOAT_2ADDR:
2564 case Instruction::SUB_FLOAT_2ADDR:
2565 case Instruction::MUL_FLOAT_2ADDR:
2566 case Instruction::DIV_FLOAT_2ADDR:
2567 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002568 work_line_->CheckBinaryOp2addr(inst,
2569 reg_types_.Float(),
2570 reg_types_.Float(),
2571 reg_types_.Float(),
2572 false);
jeffhaobdb76512011-09-07 11:43:16 -07002573 break;
2574 case Instruction::ADD_DOUBLE_2ADDR:
2575 case Instruction::SUB_DOUBLE_2ADDR:
2576 case Instruction::MUL_DOUBLE_2ADDR:
2577 case Instruction::DIV_DOUBLE_2ADDR:
2578 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002579 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002580 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2581 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::ADD_INT_LIT16:
2584 case Instruction::RSUB_INT:
2585 case Instruction::MUL_INT_LIT16:
2586 case Instruction::DIV_INT_LIT16:
2587 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002588 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::AND_INT_LIT16:
2591 case Instruction::OR_INT_LIT16:
2592 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002593 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::ADD_INT_LIT8:
2596 case Instruction::RSUB_INT_LIT8:
2597 case Instruction::MUL_INT_LIT8:
2598 case Instruction::DIV_INT_LIT8:
2599 case Instruction::REM_INT_LIT8:
2600 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002602 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002603 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::AND_INT_LIT8:
2606 case Instruction::OR_INT_LIT8:
2607 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002608 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002611 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002612 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002613 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2615 }
2616 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002617 // Note: the following instructions encode offsets derived from class linking.
2618 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2619 // meaning if the class linking and resolution were successful.
2620 case Instruction::IGET_QUICK:
2621 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2622 break;
2623 case Instruction::IGET_WIDE_QUICK:
2624 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2625 break;
2626 case Instruction::IGET_OBJECT_QUICK:
2627 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2628 break;
2629 case Instruction::IPUT_QUICK:
2630 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2631 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002632 case Instruction::IPUT_BOOLEAN_QUICK:
2633 VerifyIPutQuick(inst, reg_types_.Boolean(), true);
2634 break;
2635 case Instruction::IPUT_BYTE_QUICK:
2636 VerifyIPutQuick(inst, reg_types_.Byte(), true);
2637 break;
2638 case Instruction::IPUT_CHAR_QUICK:
2639 VerifyIPutQuick(inst, reg_types_.Char(), true);
2640 break;
2641 case Instruction::IPUT_SHORT_QUICK:
2642 VerifyIPutQuick(inst, reg_types_.Short(), true);
2643 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002644 case Instruction::IPUT_WIDE_QUICK:
2645 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2646 break;
2647 case Instruction::IPUT_OBJECT_QUICK:
2648 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2649 break;
2650 case Instruction::INVOKE_VIRTUAL_QUICK:
2651 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2652 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002653 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002654 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002655 const char* descriptor = called_method->GetReturnTypeDescriptor();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002656 RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002657 if (!return_type.IsLowHalf()) {
2658 work_line_->SetResultRegisterType(return_type);
2659 } else {
2660 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2661 }
2662 just_set_result = true;
2663 }
2664 break;
2665 }
2666
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002668 case Instruction::UNUSED_3E:
2669 case Instruction::UNUSED_3F:
2670 case Instruction::UNUSED_40:
2671 case Instruction::UNUSED_41:
2672 case Instruction::UNUSED_42:
2673 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002674 case Instruction::UNUSED_79:
2675 case Instruction::UNUSED_7A:
jeffhaobdb76512011-09-07 11:43:16 -07002676 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002677 case Instruction::UNUSED_F0:
2678 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002679 case Instruction::UNUSED_F2:
2680 case Instruction::UNUSED_F3:
2681 case Instruction::UNUSED_F4:
2682 case Instruction::UNUSED_F5:
2683 case Instruction::UNUSED_F6:
2684 case Instruction::UNUSED_F7:
2685 case Instruction::UNUSED_F8:
2686 case Instruction::UNUSED_F9:
2687 case Instruction::UNUSED_FA:
2688 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002689 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002690 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002691 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002692 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002694 break;
2695
2696 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002697 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002698 * complain if an instruction is missing (which is desirable).
2699 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002700 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002701
Ian Rogersad0b3a32012-04-16 14:50:24 -07002702 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002703 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002704 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002705 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002706 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002707 /* immediate failure, reject class */
2708 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2709 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002710 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002711 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002712 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002713 }
jeffhaobdb76512011-09-07 11:43:16 -07002714 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 * If we didn't just set the result register, clear it out. This ensures that you can only use
2716 * "move-result" immediately after the result is set. (We could check this statically, but it's
2717 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002718 */
2719 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002721 }
2722
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002723
jeffhaobdb76512011-09-07 11:43:16 -07002724
2725 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002726 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002727 *
2728 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002729 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002730 * somebody could get a reference field, check it for zero, and if the
2731 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002732 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002733 * that, and will reject the code.
2734 *
2735 * TODO: avoid re-fetching the branch target
2736 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002737 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002738 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002740 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002742 return false;
2743 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002744 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002745 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002746 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 }
jeffhaobdb76512011-09-07 11:43:16 -07002748 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002749 if (NULL != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002750 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002751 return false;
2752 }
2753 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002754 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002755 return false;
2756 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 }
jeffhaobdb76512011-09-07 11:43:16 -07002758 }
2759
2760 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002761 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002762 *
2763 * We've already verified that the table is structurally sound, so we
2764 * just need to walk through and tag the targets.
2765 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002766 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002767 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2768 const uint16_t* switch_insns = insns + offset_to_switch;
2769 int switch_count = switch_insns[1];
2770 int offset_to_targets, targ;
2771
2772 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2773 /* 0 = sig, 1 = count, 2/3 = first key */
2774 offset_to_targets = 4;
2775 } else {
2776 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002777 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002778 offset_to_targets = 2 + 2 * switch_count;
2779 }
2780
2781 /* verify each switch target */
2782 for (targ = 0; targ < switch_count; targ++) {
2783 int offset;
2784 uint32_t abs_offset;
2785
2786 /* offsets are 32-bit, and only partly endian-swapped */
2787 offset = switch_insns[offset_to_targets + targ * 2] |
2788 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 abs_offset = work_insn_idx_ + offset;
2790 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002791 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002792 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002794 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002795 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002796 }
jeffhaobdb76512011-09-07 11:43:16 -07002797 }
2798 }
2799
2800 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2802 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002803 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002804 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002805 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002806 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002807
Andreas Gampef91baf12014-07-18 15:41:00 -07002808 // Need the linker to try and resolve the handled class to check if it's Throwable.
2809 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2810
Ian Rogers0571d352011-11-03 19:51:38 -07002811 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002812 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2813 if (handler_type_idx == DexFile::kDexNoIndex16) {
2814 has_catch_all_handler = true;
2815 } else {
2816 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002817 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2818 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002819 if (klass != nullptr) {
2820 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2821 has_catch_all_handler = true;
2822 }
2823 } else {
2824 // Clear exception.
2825 Thread* self = Thread::Current();
2826 DCHECK(self->IsExceptionPending());
2827 self->ClearException();
2828 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 }
jeffhaobdb76512011-09-07 11:43:16 -07002830 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002831 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2832 * "work_regs", because at runtime the exception will be thrown before the instruction
2833 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002834 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002835 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002836 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 }
jeffhaobdb76512011-09-07 11:43:16 -07002838 }
2839
2840 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2842 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002843 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002844 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002845 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 * The state in work_line reflects the post-execution state. If the current instruction is a
2847 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002848 * it will do so before grabbing the lock).
2849 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002850 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002851 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002853 return false;
2854 }
2855 }
2856 }
2857
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002858 /* Handle "continue". Tag the next consecutive instruction.
2859 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2860 * because it changes work_line_ when performing peephole optimization
2861 * and this change should not be used in those cases.
2862 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002863 if ((opcode_flags & Instruction::kContinue) != 0) {
2864 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2865 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2867 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002868 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002869 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2870 // next instruction isn't one.
2871 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2872 return false;
2873 }
2874 if (NULL != fallthrough_line.get()) {
2875 // Make workline consistent with fallthrough computed from peephole optimization.
2876 work_line_->CopyFromLine(fallthrough_line.get());
2877 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002878 if (insn_flags_[next_insn_idx].IsReturn()) {
2879 // For returns we only care about the operand to the return, all other registers are dead.
2880 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2881 Instruction::Code opcode = ret_inst->Opcode();
2882 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2883 work_line_->MarkAllRegistersAsConflicts();
2884 } else {
2885 if (opcode == Instruction::RETURN_WIDE) {
2886 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2887 } else {
2888 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2889 }
2890 }
2891 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002892 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2893 if (next_line != NULL) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002894 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2895 // needed. If the merge changes the state of the registers then the work line will be
2896 // updated.
2897 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002898 return false;
2899 }
2900 } else {
2901 /*
2902 * We're not recording register data for the next instruction, so we don't know what the
2903 * prior state was. We have to assume that something has changed and re-evaluate it.
2904 */
2905 insn_flags_[next_insn_idx].SetChanged();
2906 }
2907 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002908
jeffhaod1f0fde2011-09-08 17:25:33 -07002909 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002910 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002911 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 return false;
2913 }
jeffhaobdb76512011-09-07 11:43:16 -07002914 }
2915
2916 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002917 * Update start_guess. Advance to the next instruction of that's
2918 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002919 * neither of those exists we're in a return or throw; leave start_guess
2920 * alone and let the caller sort it out.
2921 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002922 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002924 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002925 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002927 }
2928
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2930 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002931
2932 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002933} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002934
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002935RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002936 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002937 RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002938 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
2939 RegType& result = klass != NULL ?
2940 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
2941 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 if (result.IsConflict()) {
2943 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2944 << "' in " << referrer;
2945 return result;
2946 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002947 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002948 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002949 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002950 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002951 // check at runtime if access is allowed and so pass here. If result is
2952 // primitive, skip the access check.
2953 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2954 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002955 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002956 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002957 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002958 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002959}
2960
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002961RegType& MethodVerifier::GetCaughtExceptionType() {
2962 RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002964 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2966 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002967 CatchHandlerIterator iterator(handlers_ptr);
2968 for (; iterator.HasNext(); iterator.Next()) {
2969 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2970 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002971 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002972 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002973 RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002974 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002975 if (exception.IsUnresolvedTypes()) {
2976 // We don't know enough about the type. Fail here and let runtime handle it.
2977 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2978 return exception;
2979 } else {
2980 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2981 return reg_types_.Conflict();
2982 }
Jeff Haob878f212014-04-24 16:25:36 -07002983 } else if (common_super == nullptr) {
2984 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002985 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002986 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002988 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002989 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 }
2991 }
2992 }
2993 }
Ian Rogers0571d352011-11-03 19:51:38 -07002994 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 }
2996 }
2997 if (common_super == NULL) {
2998 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002999 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003001 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003002 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003003}
3004
Brian Carlstromea46f952013-07-30 01:26:50 -07003005mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003006 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003008 RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 if (klass_type.IsConflict()) {
3010 std::string append(" in attempt to access method ");
3011 append += dex_file_->GetMethodName(method_id);
3012 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08003013 return NULL;
3014 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003015 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003016 return NULL; // Can't resolve Class so no more to do here
3017 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003018 mirror::Class* klass = klass_type.GetClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003019 RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003020 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003022 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003023 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003024
3025 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003027 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003028 res_method = klass->FindInterfaceMethod(name, signature);
3029 } else {
3030 res_method = klass->FindVirtualMethod(name, signature);
3031 }
3032 if (res_method != NULL) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003033 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003034 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003035 // If a virtual or interface method wasn't found with the expected type, look in
3036 // the direct methods. This can happen when the wrong invoke type is used or when
3037 // a class has changed, and will be flagged as an error in later checks.
3038 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3039 res_method = klass->FindDirectMethod(name, signature);
3040 }
3041 if (res_method == NULL) {
3042 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3043 << PrettyDescriptor(klass) << "." << name
3044 << " " << signature;
3045 return NULL;
3046 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 }
3048 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3050 // enforce them here.
3051 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3053 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 return NULL;
3055 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003056 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003057 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3059 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003060 return NULL;
3061 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003062 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003064 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003065 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003066 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003067 }
jeffhaode0d9c92012-02-27 13:58:13 -08003068 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3069 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3071 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003072 return NULL;
3073 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003074 // Check that interface methods match interface classes.
3075 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3076 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3077 << " is in an interface class " << PrettyClass(klass);
3078 return NULL;
3079 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3080 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3081 << " is in a non-interface class " << PrettyClass(klass);
3082 return NULL;
3083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 // See if the method type implied by the invoke instruction matches the access flags for the
3085 // target method.
3086 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3087 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3088 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3089 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003090 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3091 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 return NULL;
3093 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003094 return res_method;
3095}
3096
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003097template <class T>
3098mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3099 MethodType method_type,
3100 bool is_range,
3101 mirror::ArtMethod* res_method) {
3102 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3103 // match the call to the signature. Also, we might be calling through an abstract method
3104 // definition (which doesn't have register count values).
3105 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3106 /* caught by static verifier */
3107 DCHECK(is_range || expected_args <= 5);
3108 if (expected_args > code_item_->outs_size_) {
3109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3110 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3111 return nullptr;
3112 }
3113
3114 uint32_t arg[5];
3115 if (!is_range) {
3116 inst->GetVarArgs(arg);
3117 }
3118 uint32_t sig_registers = 0;
3119
3120 /*
3121 * Check the "this" argument, which must be an instance of the class that declared the method.
3122 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3123 * rigorous check here (which is okay since we have to do it at runtime).
3124 */
3125 if (method_type != METHOD_STATIC) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003126 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003127 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3128 CHECK(have_pending_hard_failure_);
3129 return nullptr;
3130 }
3131 if (actual_arg_type.IsUninitializedReference()) {
3132 if (res_method) {
3133 if (!res_method->IsConstructor()) {
3134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3135 return nullptr;
3136 }
3137 } else {
3138 // Check whether the name of the called method is "<init>"
3139 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003140 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3142 return nullptr;
3143 }
3144 }
3145 }
3146 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003147 RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003148 if (res_method != nullptr) {
3149 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003150 std::string temp;
3151 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003152 klass->CannotBeAssignedFromOtherTypes());
3153 } else {
3154 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3155 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003156 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003157 dex_file_->StringByTypeIdx(class_idx),
3158 false);
3159 }
3160 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3161 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3162 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3163 << "' not instance of '" << *res_method_class << "'";
3164 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3165 // the compiler.
3166 if (have_pending_hard_failure_) {
3167 return nullptr;
3168 }
3169 }
3170 }
3171 sig_registers = 1;
3172 }
3173
3174 for ( ; it->HasNext(); it->Next()) {
3175 if (sig_registers >= expected_args) {
3176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3177 " arguments, found " << sig_registers << " or more.";
3178 return nullptr;
3179 }
3180
3181 const char* param_descriptor = it->GetDescriptor();
3182
3183 if (param_descriptor == nullptr) {
3184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3185 "component";
3186 return nullptr;
3187 }
3188
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003189 RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003190 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3191 arg[sig_registers];
3192 if (reg_type.IsIntegralTypes()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003193 RegType& src_type = work_line_->GetRegisterType(get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003194 if (!src_type.IsIntegralTypes()) {
3195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3196 << " but expected " << reg_type;
3197 return res_method;
3198 }
3199 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3200 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3201 // compiler.
3202 if (have_pending_hard_failure_) {
3203 return res_method;
3204 }
3205 }
3206 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3207 }
3208 if (expected_args != sig_registers) {
3209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3210 " arguments, found " << sig_registers;
3211 return nullptr;
3212 }
3213 return res_method;
3214}
3215
3216void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3217 MethodType method_type,
3218 bool is_range) {
3219 // As the method may not have been resolved, make this static check against what we expect.
3220 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3221 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3222 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3223 DexFileParameterIterator it(*dex_file_,
3224 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3225 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3226 nullptr);
3227}
3228
3229class MethodParamListDescriptorIterator {
3230 public:
3231 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3232 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3233 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3234 }
3235
3236 bool HasNext() {
3237 return pos_ < params_size_;
3238 }
3239
3240 void Next() {
3241 ++pos_;
3242 }
3243
3244 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3245 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3246 }
3247
3248 private:
3249 mirror::ArtMethod* res_method_;
3250 size_t pos_;
3251 const DexFile::TypeList* params_;
3252 const size_t params_size_;
3253};
3254
Brian Carlstromea46f952013-07-30 01:26:50 -07003255mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003256 MethodType method_type,
3257 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003258 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003259 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3260 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003261 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003262
Brian Carlstromea46f952013-07-30 01:26:50 -07003263 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003264 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003265 // Check what we can statically.
3266 if (!have_pending_hard_failure_) {
3267 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3268 }
3269 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003270 }
3271
Ian Rogersd81871c2011-10-03 13:57:23 -07003272 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3273 // has a vtable entry for the target method.
3274 if (is_super) {
3275 DCHECK(method_type == METHOD_VIRTUAL);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003276 RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003277 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003278 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003279 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003280 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003281 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003282 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003283 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003284 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003285 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003286 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003287 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003288 << "." << res_method->GetName()
3289 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003290 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003291 }
3292 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003293
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003294 // Process the target method's signature. This signature may or may not
3295 MethodParamListDescriptorIterator it(res_method);
3296 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3297 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003298}
3299
Brian Carlstromea46f952013-07-30 01:26:50 -07003300mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003301 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003302 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3303 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003304 RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003305 if (!actual_arg_type.HasClass()) {
3306 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003307 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003308 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003309 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003310 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003311 if (klass->IsInterface()) {
3312 // Derive Object.class from Class.class.getSuperclass().
3313 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3314 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003315 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003316 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003317 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003318 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003319 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003320 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003321 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3322 << PrettyDescriptor(klass);
3323 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003324 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003325 return res_method;
3326}
3327
Brian Carlstromea46f952013-07-30 01:26:50 -07003328mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003329 bool is_range) {
3330 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003331 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003332 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003333 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003335 return NULL;
3336 }
3337 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3338
3339 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3340 // match the call to the signature. Also, we might be calling through an abstract method
3341 // definition (which doesn't have register count values).
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003342 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003343 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3344 return NULL;
3345 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003346 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3347 /* caught by static verifier */
3348 DCHECK(is_range || expected_args <= 5);
3349 if (expected_args > code_item_->outs_size_) {
3350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3351 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3352 return NULL;
3353 }
3354
3355 /*
3356 * Check the "this" argument, which must be an instance of the class that declared the method.
3357 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3358 * rigorous check here (which is okay since we have to do it at runtime).
3359 */
3360 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3362 return NULL;
3363 }
3364 if (!actual_arg_type.IsZero()) {
3365 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003366 std::string temp;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003367 RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003368 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003369 klass->CannotBeAssignedFromOtherTypes());
3370 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003371 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3372 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003373 << "' not instance of '" << res_method_class << "'";
3374 return NULL;
3375 }
3376 }
3377 /*
3378 * Process the target method's signature. This signature may or may not
3379 * have been verified, so we can't assume it's properly formed.
3380 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003381 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003382 size_t params_size = params == NULL ? 0 : params->Size();
3383 uint32_t arg[5];
3384 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003385 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003386 }
3387 size_t actual_args = 1;
3388 for (size_t param_index = 0; param_index < params_size; param_index++) {
3389 if (actual_args >= expected_args) {
3390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003391 << "'. Expected " << expected_args
3392 << " arguments, processing argument " << actual_args
3393 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003394 return NULL;
3395 }
3396 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003397 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003398 if (descriptor == NULL) {
3399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003400 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003401 return NULL;
3402 }
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003403 RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003404 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3405 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3406 return res_method;
3407 }
3408 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3409 }
3410 if (actual_args != expected_args) {
3411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3412 << " expected " << expected_args << " arguments, found " << actual_args;
3413 return NULL;
3414 } else {
3415 return res_method;
3416 }
3417}
3418
Ian Rogers62342ec2013-06-11 10:26:37 -07003419void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003420 uint32_t type_idx;
3421 if (!is_filled) {
3422 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3423 type_idx = inst->VRegC_22c();
3424 } else if (!is_range) {
3425 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3426 type_idx = inst->VRegB_35c();
3427 } else {
3428 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3429 type_idx = inst->VRegB_3rc();
3430 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003431 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003432 if (res_type.IsConflict()) { // bad class
3433 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003434 } else {
3435 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3436 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003438 } else if (!is_filled) {
3439 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003440 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003441 /* set register type to array class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003442 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003443 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003444 } else {
3445 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3446 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003447 RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003448 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3449 uint32_t arg[5];
3450 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003451 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003452 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003453 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003454 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003455 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003456 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003457 return;
3458 }
3459 }
3460 // filled-array result goes into "result" register
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003461 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003462 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003463 }
3464 }
3465}
3466
Sebastien Hertz5243e912013-05-21 10:55:07 +02003467void MethodVerifier::VerifyAGet(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003468 RegType& insn_type, bool is_primitive) {
3469 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003473 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003474 if (array_type.IsZero()) {
3475 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3476 // instruction type. TODO: have a proper notion of bottom here.
3477 if (!is_primitive || insn_type.IsCategory1Types()) {
3478 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003479 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003481 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003482 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003483 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003484 }
jeffhaofc3144e2012-02-01 17:21:15 -08003485 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003487 } else {
3488 /* verify the class */
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003489 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003490 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003492 << " source for aget-object";
3493 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003495 << " source for category 1 aget";
3496 } else if (is_primitive && !insn_type.Equals(component_type) &&
3497 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003498 (insn_type.IsLong() && component_type.IsDouble()))) {
3499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3500 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003501 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003502 // Use knowledge of the field type which is stronger than the type inferred from the
3503 // instruction, which can't differentiate object types and ints from floats, longs from
3504 // doubles.
3505 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003506 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003507 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003508 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003509 component_type.HighHalf(&reg_types_));
3510 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 }
3512 }
3513 }
3514}
3515
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003516void MethodVerifier::VerifyPrimitivePut(RegType& target_type, RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003517 const uint32_t vregA) {
3518 // Primitive assignability rules are weaker than regular assignability rules.
3519 bool instruction_compatible;
3520 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003521 RegType& value_type = work_line_->GetRegisterType(vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003522 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003523 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003524 value_compatible = value_type.IsIntegralTypes();
3525 } else if (target_type.IsFloat()) {
3526 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3527 value_compatible = value_type.IsFloatTypes();
3528 } else if (target_type.IsLong()) {
3529 instruction_compatible = insn_type.IsLong();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003530 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003531 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003532 } else if (target_type.IsDouble()) {
3533 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003534 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003535 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003536 } else {
3537 instruction_compatible = false; // reference with primitive store
3538 value_compatible = false; // unused
3539 }
3540 if (!instruction_compatible) {
3541 // This is a global failure rather than a class change failure as the instructions and
3542 // the descriptors for the type should have been consistent within the same file at
3543 // compile time.
3544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3545 << "' but expected type '" << target_type << "'";
3546 return;
3547 }
3548 if (!value_compatible) {
3549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3550 << " of type " << value_type << " but expected " << target_type << " for put";
3551 return;
3552 }
3553}
3554
Sebastien Hertz5243e912013-05-21 10:55:07 +02003555void MethodVerifier::VerifyAPut(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003556 RegType& insn_type, bool is_primitive) {
3557 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003560 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003561 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003562 if (array_type.IsZero()) {
3563 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3564 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003565 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003567 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003568 RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003569 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003570 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003571 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003573 if (!component_type.IsReferenceTypes()) {
3574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3575 << " source for aput-object";
3576 } else {
3577 // The instruction agrees with the type of array, confirm the value to be stored does too
3578 // Note: we use the instruction type (rather than the component type) for aput-object as
3579 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003580 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003582 }
3583 }
3584 }
3585}
3586
Brian Carlstromea46f952013-07-30 01:26:50 -07003587mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003588 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3589 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003590 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003591 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003592 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3593 field_idx, dex_file_->GetFieldName(field_id),
3594 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003595 return NULL;
3596 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003597 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003599 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003600 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003601 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3602 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003604 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003605 << dex_file_->GetFieldName(field_id) << ") in "
3606 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 DCHECK(Thread::Current()->IsExceptionPending());
3608 Thread::Current()->ClearException();
3609 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003610 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3611 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003612 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003613 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 return NULL;
3615 } else if (!field->IsStatic()) {
3616 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3617 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003619 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003620}
3621
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003622mirror::ArtField* MethodVerifier::GetInstanceField(RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003623 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3624 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003625 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003626 if (klass_type.IsConflict()) {
3627 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3628 field_idx, dex_file_->GetFieldName(field_id),
3629 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003630 return NULL;
3631 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003632 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003633 return NULL; // Can't resolve Class so no more to do here
3634 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003635 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003636 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3637 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003639 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003640 << dex_file_->GetFieldName(field_id) << ") in "
3641 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 DCHECK(Thread::Current()->IsExceptionPending());
3643 Thread::Current()->ClearException();
3644 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003645 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3646 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003647 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003648 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 return NULL;
3650 } else if (field->IsStatic()) {
3651 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3652 << " to not be static";
3653 return NULL;
3654 } else if (obj_type.IsZero()) {
3655 // Cannot infer and check type, however, access will cause null pointer exception
3656 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003657 } else if (!obj_type.IsReferenceTypes()) {
3658 // Trying to read a field from something that isn't a reference
3659 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3660 << "non-reference type " << obj_type;
3661 return NULL;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003662 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003663 mirror::Class* klass = field->GetDeclaringClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003664 RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003665 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003666 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003667 if (obj_type.IsUninitializedTypes() &&
3668 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3669 !field_klass.Equals(GetDeclaringClass()))) {
3670 // Field accesses through uninitialized references are only allowable for constructors where
3671 // the field is declared in this class
3672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003673 << " of a not fully initialized object within the context"
3674 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003675 return NULL;
3676 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3677 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3678 // of C1. For resolution to occur the declared class of the field must be compatible with
3679 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3680 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3681 << " from object of type " << obj_type;
3682 return NULL;
3683 } else {
3684 return field;
3685 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003686 }
3687}
3688
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003689void MethodVerifier::VerifyISGet(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003690 bool is_primitive, bool is_static) {
3691 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003692 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003693 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003694 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003695 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003696 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003697 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003698 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003699 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003700 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003701 Thread* self = Thread::Current();
3702 mirror::Class* field_type_class;
3703 {
3704 StackHandleScope<1> hs(self);
3705 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3706 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3707 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003708 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003709 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003710 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003711 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003712 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3713 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003714 }
Ian Rogers0d604842012-04-16 14:50:24 -07003715 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003716 if (field_type == nullptr) {
3717 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3718 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003719 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003720 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003721 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003722 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003723 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003724 if (field_type->Equals(insn_type) ||
3725 (field_type->IsFloat() && insn_type.IsInteger()) ||
3726 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003727 // expected that read is of the correct primitive type or that int reads are reading
3728 // floats or long reads are reading doubles
3729 } else {
3730 // This is a global failure rather than a class change failure as the instructions and
3731 // the descriptors for the type should have been consistent within the same file at
3732 // compile time
3733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3734 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003735 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003736 return;
3737 }
3738 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003739 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003740 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3741 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003742 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003743 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003744 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 return;
3746 }
3747 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003748 if (!field_type->IsLowHalf()) {
3749 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003750 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003751 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003752 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003753}
3754
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003755void MethodVerifier::VerifyISPut(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003756 bool is_primitive, bool is_static) {
3757 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003758 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003759 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003760 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003761 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003762 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003763 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003764 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003765 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003766 if (field != NULL) {
3767 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3768 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3769 << " from other class " << GetDeclaringClass();
3770 return;
3771 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003772 mirror::Class* field_type_class;
3773 {
3774 StackHandleScope<1> hs(Thread::Current());
3775 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3776 FieldHelper fh(h_field);
3777 field_type_class = fh.GetType(can_load_classes_);
3778 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003779 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003780 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003781 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003782 } else {
3783 Thread* self = Thread::Current();
3784 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3785 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003786 }
3787 }
3788 if (field_type == nullptr) {
3789 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3790 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003791 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003792 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003793 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003794 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003795 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003796 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003797 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003798 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003799 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3800 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003801 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003802 << "' in put-object";
3803 return;
3804 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003805 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003806 }
3807}
3808
Brian Carlstromea46f952013-07-30 01:26:50 -07003809mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003810 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003811 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3812 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3813 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3814 inst->Opcode() == Instruction::IPUT_QUICK ||
3815 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3816 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003817 RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003818 if (!object_type.HasClass()) {
3819 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3820 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003821 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003822 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003823 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3824 field_offset);
3825 if (f == nullptr) {
3826 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3827 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3828 }
3829 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003830}
3831
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003832void MethodVerifier::VerifyIGetQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003833 bool is_primitive) {
3834 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003835 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003836 if (field == NULL) {
3837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3838 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003839 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003840 mirror::Class* field_type_class;
3841 {
3842 StackHandleScope<1> hs(Thread::Current());
3843 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3844 FieldHelper fh(h_field);
3845 field_type_class = fh.GetType(can_load_classes_);
3846 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003847 RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003848 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003849 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003850 field_type_class->CannotBeAssignedFromOtherTypes());
3851 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003852 Thread* self = Thread::Current();
3853 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3854 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003855 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003856 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003857 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003858 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003859 const uint32_t vregA = inst->VRegA_22c();
3860 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003861 if (field_type->Equals(insn_type) ||
3862 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3863 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003864 // expected that read is of the correct primitive type or that int reads are reading
3865 // floats or long reads are reading doubles
3866 } else {
3867 // This is a global failure rather than a class change failure as the instructions and
3868 // the descriptors for the type should have been consistent within the same file at
3869 // compile time
3870 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003871 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003872 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003873 return;
3874 }
3875 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003877 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003878 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003879 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003880 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003881 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3882 return;
3883 }
3884 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003885 if (!field_type->IsLowHalf()) {
3886 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003887 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003888 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003889 }
3890}
3891
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003892void MethodVerifier::VerifyIPutQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003893 bool is_primitive) {
3894 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003895 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003896 if (field == NULL) {
3897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3898 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003899 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003900 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003901 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003902 RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003903 if (field != NULL) {
3904 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3905 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003906 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003907 return;
3908 }
3909 }
3910 const uint32_t vregA = inst->VRegA_22c();
3911 if (is_primitive) {
3912 // Primitive field assignability rules are weaker than regular assignability rules
3913 bool instruction_compatible;
3914 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003915 RegType& value_type = work_line_->GetRegisterType(vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003916 if (field_type.IsIntegralTypes()) {
3917 instruction_compatible = insn_type.IsIntegralTypes();
3918 value_compatible = value_type.IsIntegralTypes();
3919 } else if (field_type.IsFloat()) {
3920 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3921 value_compatible = value_type.IsFloatTypes();
3922 } else if (field_type.IsLong()) {
3923 instruction_compatible = insn_type.IsLong();
3924 value_compatible = value_type.IsLongTypes();
3925 } else if (field_type.IsDouble()) {
3926 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3927 value_compatible = value_type.IsDoubleTypes();
3928 } else {
3929 instruction_compatible = false; // reference field with primitive store
3930 value_compatible = false; // unused
3931 }
3932 if (!instruction_compatible) {
3933 // This is a global failure rather than a class change failure as the instructions and
3934 // the descriptors for the type should have been consistent within the same file at
3935 // compile time
3936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003937 << " to be of type '" << insn_type
3938 << "' but found type '" << field_type
3939 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003940 return;
3941 }
3942 if (!value_compatible) {
3943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3944 << " of type " << value_type
3945 << " but expected " << field_type
3946 << " for store to " << PrettyField(field) << " in put";
3947 return;
3948 }
3949 } else {
3950 if (!insn_type.IsAssignableFrom(field_type)) {
3951 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003952 << " to be compatible with type '" << insn_type
3953 << "' but found type '" << field_type
3954 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003955 return;
3956 }
3957 work_line_->VerifyRegisterType(vregA, field_type);
3958 }
3959}
3960
Ian Rogers776ac1f2012-04-13 23:36:36 -07003961bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003962 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003964 return false;
3965 }
3966 return true;
3967}
3968
Ian Rogersebbdd872014-07-07 23:53:08 -07003969bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3970 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 bool changed = true;
3972 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3973 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003974 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 * We haven't processed this instruction before, and we haven't touched the registers here, so
3976 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3977 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003978 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003979 if (!insn_flags_[next_insn].IsReturn()) {
3980 target_line->CopyFromLine(merge_line);
3981 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003982 // Verify that the monitor stack is empty on return.
3983 if (!merge_line->VerifyMonitorStackEmpty()) {
3984 return false;
3985 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003986 // For returns we only care about the operand to the return, all other registers are dead.
3987 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3988 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3989 Instruction::Code opcode = ret_inst->Opcode();
3990 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3991 target_line->MarkAllRegistersAsConflicts();
3992 } else {
3993 target_line->CopyFromLine(merge_line);
3994 if (opcode == Instruction::RETURN_WIDE) {
3995 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3996 } else {
3997 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3998 }
3999 }
4000 }
jeffhaobdb76512011-09-07 11:43:16 -07004001 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004002 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004003 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07004004 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004005 if (gDebugVerify) {
4006 copy->CopyFromLine(target_line);
4007 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004008 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004009 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004011 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004012 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004013 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004014 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
4015 << *copy.get() << " MERGE\n"
4016 << *merge_line << " ==\n"
4017 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004018 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004019 if (update_merge_line && changed) {
4020 merge_line->CopyFromLine(target_line);
4021 }
jeffhaobdb76512011-09-07 11:43:16 -07004022 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 if (changed) {
4024 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004025 }
4026 return true;
4027}
4028
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004029InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004030 return &insn_flags_[work_insn_idx_];
4031}
4032
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004033RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004034 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004035 if (mirror_method_.Get() != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004036 Thread* self = Thread::Current();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004037 StackHandleScope<1> hs(self);
4038 mirror::Class* return_type_class =
4039 MethodHelper(hs.NewHandle(mirror_method_.Get())).GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004040 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004041 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4042 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004043 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004044 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004045 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004046 self->ClearException();
4047 }
4048 }
4049 if (return_type_ == nullptr) {
4050 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4051 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4052 uint16_t return_type_idx = proto_id.return_type_idx_;
4053 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004054 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004055 }
4056 }
4057 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004058}
4059
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004060RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004061 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004062 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004063 const char* descriptor
4064 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004065 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004066 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004067 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4068 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004069 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004070 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004071 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004072 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004073 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004074}
4075
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004076std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4077 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004078 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004079 std::vector<int32_t> result;
4080 for (size_t i = 0; i < line->NumRegs(); ++i) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004081 RegType& type = line->GetRegisterType(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004082 if (type.IsConstant()) {
4083 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4084 result.push_back(type.ConstantValue());
4085 } else if (type.IsConstantLo()) {
4086 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4087 result.push_back(type.ConstantValueLo());
4088 } else if (type.IsConstantHi()) {
4089 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4090 result.push_back(type.ConstantValueHi());
4091 } else if (type.IsIntegralTypes()) {
4092 result.push_back(kIntVReg);
4093 result.push_back(0);
4094 } else if (type.IsFloat()) {
4095 result.push_back(kFloatVReg);
4096 result.push_back(0);
4097 } else if (type.IsLong()) {
4098 result.push_back(kLongLoVReg);
4099 result.push_back(0);
4100 result.push_back(kLongHiVReg);
4101 result.push_back(0);
4102 ++i;
4103 } else if (type.IsDouble()) {
4104 result.push_back(kDoubleLoVReg);
4105 result.push_back(0);
4106 result.push_back(kDoubleHiVReg);
4107 result.push_back(0);
4108 ++i;
4109 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4110 result.push_back(kUndefined);
4111 result.push_back(0);
4112 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004113 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004114 result.push_back(kReferenceVReg);
4115 result.push_back(0);
4116 }
4117 }
4118 return result;
4119}
4120
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004121RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004122 if (precise) {
4123 // Precise constant type.
4124 return reg_types_.FromCat1Const(value, true);
4125 } else {
4126 // Imprecise constant type.
4127 if (value < -32768) {
4128 return reg_types_.IntConstant();
4129 } else if (value < -128) {
4130 return reg_types_.ShortConstant();
4131 } else if (value < 0) {
4132 return reg_types_.ByteConstant();
4133 } else if (value == 0) {
4134 return reg_types_.Zero();
4135 } else if (value == 1) {
4136 return reg_types_.One();
4137 } else if (value < 128) {
4138 return reg_types_.PosByteConstant();
4139 } else if (value < 32768) {
4140 return reg_types_.PosShortConstant();
4141 } else if (value < 65536) {
4142 return reg_types_.CharConstant();
4143 } else {
4144 return reg_types_.IntConstant();
4145 }
4146 }
4147}
4148
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004149void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004150 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004151}
4152
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004153void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004154 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004155}
jeffhaod1224c72012-02-29 13:43:08 -08004156
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004157void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4158 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004159}
4160
Ian Rogersd81871c2011-10-03 13:57:23 -07004161} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004162} // namespace art