blob: ce2ec54aab998be0fd19f7282445245569976d36 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000024#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "field_helper.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080030#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070031#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070032#include "leb128.h"
Ian Rogerse5877a12014-07-16 12:06:35 -070033#include "method_helper-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_field-inl.h"
35#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Ian Rogersebbdd872014-07-07 23:53:08 -070050static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070051// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070052
Ian Rogers7b3ddd22013-02-21 15:19:52 -080053void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070054 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070056 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070057 register_lines_.reset(new RegisterLine*[insns_size]());
58 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070059 for (uint32_t i = 0; i < insns_size; i++) {
60 bool interesting = false;
61 switch (mode) {
62 case kTrackRegsAll:
63 interesting = flags[i].IsOpcode();
64 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070065 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070066 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070067 break;
68 case kTrackRegsBranches:
69 interesting = flags[i].IsBranchTarget();
70 break;
71 default:
72 break;
73 }
74 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070075 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
76 }
77 }
78}
79
80PcToRegisterLineTable::~PcToRegisterLineTable() {
81 for (size_t i = 0; i < size_; i++) {
82 delete register_lines_[i];
83 if (kIsDebugBuild) {
84 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070085 }
86 }
87}
88
Ian Rogersef7d42f2014-01-06 12:55:46 -080089MethodVerifier::FailureKind MethodVerifier::VerifyClass(mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070090 bool allow_soft_failures,
91 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070092 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070093 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070094 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080095 bool early_failure = false;
96 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -070097 const DexFile& dex_file = klass->GetDexFile();
98 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -080099 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700100 std::string temp;
101 if (super == NULL && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800102 early_failure = true;
103 failure_message = " that has no super class";
104 } else if (super != NULL && super->IsFinal()) {
105 early_failure = true;
106 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
107 } else if (class_def == NULL) {
108 early_failure = true;
109 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
110 }
111 if (early_failure) {
112 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
113 if (Runtime::Current()->IsCompiler()) {
114 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000115 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800116 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700117 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700118 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700120 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700122 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700123}
124
Ian Rogers365c1022012-06-22 15:05:28 -0700125MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700126 Handle<mirror::DexCache> dex_cache,
127 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700128 const DexFile::ClassDef* class_def,
129 bool allow_soft_failures,
130 std::string* error) {
131 DCHECK(class_def != nullptr);
132 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700133 if (class_data == NULL) {
134 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700135 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700136 }
jeffhaof56197c2012-03-05 18:01:54 -0800137 ClassDataItemIterator it(*dex_file, class_data);
138 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
139 it.Next();
140 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700142 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700143 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700144 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800145 while (it.HasNextDirectMethod()) {
146 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700147 if (method_idx == previous_direct_method_idx) {
148 // smali can create dex files with two encoded_methods sharing the same method_idx
149 // http://code.google.com/p/smali/issues/detail?id=119
150 it.Next();
151 continue;
152 }
153 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700154 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700155 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700156 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
157 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 if (method == NULL) {
159 DCHECK(Thread::Current()->IsExceptionPending());
160 // We couldn't resolve the method, but continue regardless.
161 Thread::Current()->ClearException();
162 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700163 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
164 dex_file,
165 dex_cache,
166 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700167 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700168 it.GetMethodCodeItem(),
169 method,
170 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700171 allow_soft_failures,
172 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700173 if (result != kNoFailure) {
174 if (result == kHardFailure) {
175 hard_fail = true;
176 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700177 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700178 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700179 *error = "Verifier rejected class ";
180 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
181 *error += " due to bad method ";
182 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700184 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800185 }
186 it.Next();
187 }
jeffhao9b0b1882012-10-01 16:51:22 -0700188 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800189 while (it.HasNextVirtualMethod()) {
190 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700191 if (method_idx == previous_virtual_method_idx) {
192 // smali can create dex files with two encoded_methods sharing the same method_idx
193 // http://code.google.com/p/smali/issues/detail?id=119
194 it.Next();
195 continue;
196 }
197 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700199 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700200 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
201 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700202 if (method == NULL) {
203 DCHECK(Thread::Current()->IsExceptionPending());
204 // We couldn't resolve the method, but continue regardless.
205 Thread::Current()->ClearException();
206 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700207 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
208 dex_file,
209 dex_cache,
210 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700211 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700212 it.GetMethodCodeItem(),
213 method,
214 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700215 allow_soft_failures,
216 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 if (result != kNoFailure) {
218 if (result == kHardFailure) {
219 hard_fail = true;
220 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700221 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700222 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error = "Verifier rejected class ";
224 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
225 *error += " due to bad method ";
226 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800229 }
230 it.Next();
231 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700232 if (error_count == 0) {
233 return kNoFailure;
234 } else {
235 return hard_fail ? kHardFailure : kSoftFailure;
236 }
jeffhaof56197c2012-03-05 18:01:54 -0800237}
238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
240 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700241 Handle<mirror::DexCache> dex_cache,
242 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700243 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700245 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700246 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700247 bool allow_soft_failures,
248 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700249 MethodVerifier::FailureKind result = kNoFailure;
250 uint64_t start_ns = NanoTime();
251
Ian Rogers46960fe2014-05-23 10:43:43 -0700252 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
253 method_idx, method, method_access_flags, true, allow_soft_failures,
254 need_precise_constants);
255 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 // Verification completed, however failures may be pending that didn't cause the verification
257 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700258 CHECK(!verifier.have_pending_hard_failure_);
259 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700260 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700261 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700262 << PrettyMethod(method_idx, *dex_file) << "\n");
263 }
Ian Rogersc8982582012-09-07 16:53:25 -0700264 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800265 }
266 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700267 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700268 CHECK_NE(verifier.failures_.size(), 0U);
269 CHECK(verifier.have_pending_hard_failure_);
270 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700271 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800272 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700273 std::cout << "\n" << verifier.info_messages_.str();
274 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800275 }
Ian Rogersc8982582012-09-07 16:53:25 -0700276 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800277 }
Ian Rogersc8982582012-09-07 16:53:25 -0700278 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe073ed9b2014-06-13 15:46:46 -0700279 if (duration_ns > MsToNs(100) && !kIsDebugBuild) {
Ian Rogersc8982582012-09-07 16:53:25 -0700280 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
281 << " took " << PrettyDuration(duration_ns);
282 }
283 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800284}
285
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800286void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700287 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700288 Handle<mirror::DexCache> dex_cache,
289 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700290 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700292 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800293 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700295 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700296 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800297 verifier.DumpFailures(os);
298 os << verifier.info_messages_.str();
299 verifier.Dump(os);
300}
301
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700302MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
303 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700305 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
306 mirror::ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700307 bool can_load_classes, bool allow_soft_failures,
308 bool need_precise_constants)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800309 : reg_types_(can_load_classes),
310 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800311 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700312 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700313 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700314 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800315 dex_file_(dex_file),
316 dex_cache_(dex_cache),
317 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700318 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800319 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700320 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700321 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700322 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700324 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800325 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800326 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700327 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200328 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700329 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200330 has_check_casts_(false),
331 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800332 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700333 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800334}
335
Mathieu Chartier590fee92013-09-13 13:46:47 -0700336MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800337 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700338 STLDeleteElements(&failure_messages_);
339}
340
Brian Carlstromea46f952013-07-30 01:26:50 -0700341void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700342 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700343 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700344 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
345 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
346 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
347 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700349 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700350 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700351 verifier.FindLocksAtDexPc();
352}
353
354void MethodVerifier::FindLocksAtDexPc() {
355 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700356 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700357
358 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
359 // verification. In practice, the phase we want relies on data structures set up by all the
360 // earlier passes, so we just run the full method verification and bail out early when we've
361 // got what we wanted.
362 Verify();
363}
364
Brian Carlstromea46f952013-07-30 01:26:50 -0700365mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700366 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700367 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
369 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
370 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
371 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700372 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200373 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200374}
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700377 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200378
379 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
380 // verification. In practice, the phase we want relies on data structures set up by all the
381 // earlier passes, so we just run the full method verification and bail out early when we've
382 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200383 bool success = Verify();
384 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700385 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200386 }
387 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
388 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700389 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200390 }
391 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
392 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393}
394
Brian Carlstromea46f952013-07-30 01:26:50 -0700395mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700396 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700397 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700398 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
399 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
400 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
401 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700402 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200403 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200404}
405
Brian Carlstromea46f952013-07-30 01:26:50 -0700406mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700407 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200408
409 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
410 // verification. In practice, the phase we want relies on data structures set up by all the
411 // earlier passes, so we just run the full method verification and bail out early when we've
412 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200413 bool success = Verify();
414 if (!success) {
415 return NULL;
416 }
417 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
418 if (register_line == NULL) {
419 return NULL;
420 }
421 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
422 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
423 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200424}
425
Ian Rogersad0b3a32012-04-16 14:50:24 -0700426bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700427 // If there aren't any instructions, make sure that's expected, then exit successfully.
428 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700431 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700432 } else {
433 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700434 }
jeffhaobdb76512011-09-07 11:43:16 -0700435 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700436 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
437 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700438 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
439 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700440 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700441 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700442 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800443 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700444 // Run through the instructions and see if the width checks out.
445 bool result = ComputeWidthsAndCountOps();
446 // Flag instructions guarded by a "try" block and check exception handlers.
447 result = result && ScanTryCatchBlocks();
448 // Perform static instruction verification.
449 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700450 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000451 result = result && VerifyCodeFlow();
452 // Compute information for compiler.
453 if (result && Runtime::Current()->IsCompiler()) {
454 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
455 }
456 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700457}
458
Ian Rogers776ac1f2012-04-13 23:36:36 -0700459std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460 switch (error) {
461 case VERIFY_ERROR_NO_CLASS:
462 case VERIFY_ERROR_NO_FIELD:
463 case VERIFY_ERROR_NO_METHOD:
464 case VERIFY_ERROR_ACCESS_CLASS:
465 case VERIFY_ERROR_ACCESS_FIELD:
466 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700467 case VERIFY_ERROR_INSTANTIATION:
468 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800469 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700470 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
471 // class change and instantiation errors into soft verification errors so that we re-verify
472 // at runtime. We may fail to find or to agree on access because of not yet available class
473 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
474 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
475 // paths" that dynamically perform the verification and cause the behavior to be that akin
476 // to an interpreter.
477 error = VERIFY_ERROR_BAD_CLASS_SOFT;
478 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700479 // If we fail again at runtime, mark that this instruction would throw and force this
480 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700481 have_pending_runtime_throw_failure_ = true;
482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700484 // Indication that verification should be retried at runtime.
485 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700486 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700487 have_pending_hard_failure_ = true;
488 }
489 break;
jeffhaod5347e02012-03-22 17:25:05 -0700490 // Hard verification failures at compile time will still fail at runtime, so the class is
491 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700492 case VERIFY_ERROR_BAD_CLASS_HARD: {
493 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700494 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000495 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800496 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700497 have_pending_hard_failure_ = true;
498 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800499 }
500 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700501 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700502 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700503 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700504 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700505 failure_messages_.push_back(failure_message);
506 return *failure_message;
507}
508
Ian Rogers576ca0c2014-06-06 15:58:22 -0700509std::ostream& MethodVerifier::LogVerifyInfo() {
510 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
511 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
512}
513
Ian Rogersad0b3a32012-04-16 14:50:24 -0700514void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
515 size_t failure_num = failure_messages_.size();
516 DCHECK_NE(failure_num, 0U);
517 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
518 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700519 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700520 delete last_fail_message;
521}
522
523void MethodVerifier::AppendToLastFailMessage(std::string append) {
524 size_t failure_num = failure_messages_.size();
525 DCHECK_NE(failure_num, 0U);
526 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
527 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800528}
529
Ian Rogers776ac1f2012-04-13 23:36:36 -0700530bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700531 const uint16_t* insns = code_item_->insns_;
532 size_t insns_size = code_item_->insns_size_in_code_units_;
533 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700534 size_t new_instance_count = 0;
535 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700537
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700539 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700540 switch (opcode) {
541 case Instruction::APUT_OBJECT:
542 case Instruction::CHECK_CAST:
543 has_check_casts_ = true;
544 break;
545 case Instruction::INVOKE_VIRTUAL:
546 case Instruction::INVOKE_VIRTUAL_RANGE:
547 case Instruction::INVOKE_INTERFACE:
548 case Instruction::INVOKE_INTERFACE_RANGE:
549 has_virtual_or_interface_invokes_ = true;
550 break;
551 case Instruction::MONITOR_ENTER:
552 monitor_enter_count++;
553 break;
554 case Instruction::NEW_INSTANCE:
555 new_instance_count++;
556 break;
557 default:
558 break;
jeffhaobdb76512011-09-07 11:43:16 -0700559 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 size_t inst_size = inst->SizeInCodeUnits();
561 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
562 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700563 inst = inst->Next();
564 }
565
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
568 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700569 return false;
570 }
571
Ian Rogersd81871c2011-10-03 13:57:23 -0700572 new_instance_count_ = new_instance_count;
573 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700574 return true;
575}
576
Ian Rogers776ac1f2012-04-13 23:36:36 -0700577bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700579 if (tries_size == 0) {
580 return true;
581 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700583 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700584
585 for (uint32_t idx = 0; idx < tries_size; idx++) {
586 const DexFile::TryItem* try_item = &tries[idx];
587 uint32_t start = try_item->start_addr_;
588 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700589 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
591 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700592 return false;
593 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700595 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
596 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700597 return false;
598 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 for (uint32_t dex_pc = start; dex_pc < end;
600 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
601 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700602 }
603 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800604 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700605 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700606 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700607 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700608 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700609 CatchHandlerIterator iterator(handlers_ptr);
610 for (; iterator.HasNext(); iterator.Next()) {
611 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700613 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
614 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700615 return false;
616 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700618 // Ensure exception types are resolved so that they don't need resolution to be delivered,
619 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700620 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
622 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700623 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700624 if (exception_type == NULL) {
625 DCHECK(Thread::Current()->IsExceptionPending());
626 Thread::Current()->ClearException();
627 }
628 }
jeffhaobdb76512011-09-07 11:43:16 -0700629 }
Ian Rogers0571d352011-11-03 19:51:38 -0700630 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700631 }
jeffhaobdb76512011-09-07 11:43:16 -0700632 return true;
633}
634
Ian Rogers776ac1f2012-04-13 23:36:36 -0700635bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700637
Ian Rogers0c7abda2012-09-19 13:33:42 -0700638 /* 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 -0700639 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700640 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700641
642 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700643 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700645 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 return false;
647 }
648 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700649 // All invoke points are marked as "Throw" points already.
650 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000651 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700652 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000653 } else if (inst->IsReturn()) {
654 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 }
656 dex_pc += inst->SizeInCodeUnits();
657 inst = inst->Next();
658 }
659 return true;
660}
661
Ian Rogers776ac1f2012-04-13 23:36:36 -0700662bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 bool result = true;
664 switch (inst->GetVerifyTypeArgumentA()) {
665 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700666 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 break;
668 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700669 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700670 break;
671 }
672 switch (inst->GetVerifyTypeArgumentB()) {
673 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700674 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700677 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 break;
679 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700680 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 break;
682 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700683 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 break;
685 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700686 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700687 break;
688 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700689 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 break;
691 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700692 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 break;
694 }
695 switch (inst->GetVerifyTypeArgumentC()) {
696 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700697 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 break;
699 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700700 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 break;
702 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700703 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 break;
705 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700706 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 break;
708 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700709 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 break;
711 }
712 switch (inst->GetVerifyExtraFlags()) {
713 case Instruction::kVerifyArrayData:
714 result = result && CheckArrayData(code_offset);
715 break;
716 case Instruction::kVerifyBranchTarget:
717 result = result && CheckBranchTarget(code_offset);
718 break;
719 case Instruction::kVerifySwitchTargets:
720 result = result && CheckSwitchTargets(code_offset);
721 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700722 case Instruction::kVerifyVarArgNonZero:
723 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700724 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700725 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
727 "non-range invoke";
728 return false;
729 }
Ian Rogers29a26482014-05-02 15:27:29 -0700730 uint32_t args[Instruction::kMaxVarArgRegs];
731 inst->GetVarArgs(args);
732 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700734 }
Andreas Gampec3314312014-06-19 18:13:29 -0700735 case Instruction::kVerifyVarArgRangeNonZero:
736 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700738 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
739 inst->VRegA() <= 0) {
740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
741 "range invoke";
742 return false;
743 }
Ian Rogers29a26482014-05-02 15:27:29 -0700744 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 break;
746 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 result = false;
749 break;
750 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700751 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) {
752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
753 result = false;
754 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 return result;
756}
757
Ian Rogers776ac1f2012-04-13 23:36:36 -0700758bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
761 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 return false;
763 }
764 return true;
765}
766
Ian Rogers776ac1f2012-04-13 23:36:36 -0700767bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
770 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 return true;
774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
779 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 return false;
781 }
782 return true;
783}
784
Ian Rogers776ac1f2012-04-13 23:36:36 -0700785bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
788 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700789 return false;
790 }
791 return true;
792}
793
Ian Rogers776ac1f2012-04-13 23:36:36 -0700794bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
797 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700801 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 return false;
805 }
806 return true;
807}
808
Ian Rogers776ac1f2012-04-13 23:36:36 -0700809bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
812 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 return false;
814 }
815 return true;
816}
817
Ian Rogers776ac1f2012-04-13 23:36:36 -0700818bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
821 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 return false;
823 }
824 return true;
825}
826
Ian Rogers776ac1f2012-04-13 23:36:36 -0700827bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700829 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
830 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 return false;
832 }
833 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700834 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 const char* cp = descriptor;
836 while (*cp++ == '[') {
837 bracket_count++;
838 }
839 if (bracket_count == 0) {
840 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
842 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 return false;
844 } else if (bracket_count > 255) {
845 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700846 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
847 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 return false;
849 }
850 return true;
851}
852
Ian Rogers776ac1f2012-04-13 23:36:36 -0700853bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
855 const uint16_t* insns = code_item_->insns_ + cur_offset;
856 const uint16_t* array_data;
857 int32_t array_data_offset;
858
859 DCHECK_LT(cur_offset, insn_count);
860 /* make sure the start of the array data table is in range */
861 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
862 if ((int32_t) cur_offset + array_data_offset < 0 ||
863 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700865 << ", data offset " << array_data_offset
866 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
869 /* offset to array data table is a relative branch-style offset */
870 array_data = insns + array_data_offset;
871 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800872 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
874 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700875 return false;
876 }
877 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700878 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700879 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
880 /* make sure the end of the switch is in range */
881 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
883 << ", data offset " << array_data_offset << ", end "
884 << cur_offset + array_data_offset + table_size
885 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return false;
887 }
888 return true;
889}
890
Ian Rogers776ac1f2012-04-13 23:36:36 -0700891bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 int32_t offset;
893 bool isConditional, selfOkay;
894 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
895 return false;
896 }
897 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
899 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 return false;
901 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700902 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
903 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
906 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700907 return false;
908 }
909 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
910 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700911 if (abs_offset < 0 ||
912 (uint32_t) abs_offset >= insn_count ||
913 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700915 << reinterpret_cast<void*>(abs_offset) << ") at "
916 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700917 return false;
918 }
919 insn_flags_[abs_offset].SetBranchTarget();
920 return true;
921}
922
Ian Rogers776ac1f2012-04-13 23:36:36 -0700923bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 bool* selfOkay) {
925 const uint16_t* insns = code_item_->insns_ + cur_offset;
926 *pConditional = false;
927 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700928 switch (*insns & 0xff) {
929 case Instruction::GOTO:
930 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700931 break;
932 case Instruction::GOTO_32:
933 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700934 *selfOkay = true;
935 break;
936 case Instruction::GOTO_16:
937 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700938 break;
939 case Instruction::IF_EQ:
940 case Instruction::IF_NE:
941 case Instruction::IF_LT:
942 case Instruction::IF_GE:
943 case Instruction::IF_GT:
944 case Instruction::IF_LE:
945 case Instruction::IF_EQZ:
946 case Instruction::IF_NEZ:
947 case Instruction::IF_LTZ:
948 case Instruction::IF_GEZ:
949 case Instruction::IF_GTZ:
950 case Instruction::IF_LEZ:
951 *pOffset = (int16_t) insns[1];
952 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700953 break;
954 default:
955 return false;
956 break;
957 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 return true;
959}
960
Ian Rogers776ac1f2012-04-13 23:36:36 -0700961bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700962 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700963 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700965 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
967 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700969 << ", switch offset " << switch_offset
970 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700971 return false;
972 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700973 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700975 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800976 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
978 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return false;
980 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 uint32_t switch_count = switch_insns[1];
982 int32_t keys_offset, targets_offset;
983 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
985 /* 0=sig, 1=count, 2/3=firstKey */
986 targets_offset = 4;
987 keys_offset = -1;
988 expected_signature = Instruction::kPackedSwitchSignature;
989 } else {
990 /* 0=sig, 1=count, 2..count*2 = keys */
991 keys_offset = 2;
992 targets_offset = 2 + 2 * switch_count;
993 expected_signature = Instruction::kSparseSwitchSignature;
994 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700997 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
998 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
999 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001000 return false;
1001 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001002 /* make sure the end of the switch is in range */
1003 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1005 << ", switch offset " << switch_offset
1006 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001007 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001008 return false;
1009 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001010 /* for a sparse switch, verify the keys are in ascending order */
1011 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1013 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001014 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1015 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1016 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1018 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001019 return false;
1020 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001021 last_key = key;
1022 }
1023 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001024 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 for (uint32_t targ = 0; targ < switch_count; targ++) {
1026 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1027 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1028 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001029 if (abs_offset < 0 ||
1030 abs_offset >= (int32_t) insn_count ||
1031 !insn_flags_[abs_offset].IsOpcode()) {
1032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1033 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1034 << reinterpret_cast<void*>(cur_offset)
1035 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001036 return false;
1037 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 insn_flags_[abs_offset].SetBranchTarget();
1039 }
1040 return true;
1041}
1042
Ian Rogers776ac1f2012-04-13 23:36:36 -07001043bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001044 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 return false;
1047 }
1048 uint16_t registers_size = code_item_->registers_size_;
1049 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001050 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1052 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 return false;
1054 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001055 }
1056
1057 return true;
1058}
1059
Ian Rogers776ac1f2012-04-13 23:36:36 -07001060bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 uint16_t registers_size = code_item_->registers_size_;
1062 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1063 // integer overflow when adding them here.
1064 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1066 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001067 return false;
1068 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001069 return true;
1070}
1071
Ian Rogers776ac1f2012-04-13 23:36:36 -07001072bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 uint16_t registers_size = code_item_->registers_size_;
1074 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001075
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001077 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1078 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 }
1080 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001081 reg_table_.Init(kTrackCompilerInterestPoints,
1082 insn_flags_.get(),
1083 insns_size,
1084 registers_size,
1085 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001086
jeffhaobdb76512011-09-07 11:43:16 -07001087
Ian Rogersd0fbd852013-09-24 18:17:04 -07001088 work_line_.reset(RegisterLine::Create(registers_size, this));
1089 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001090
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 /* Initialize register types of method arguments. */
1092 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001093 DCHECK_NE(failures_.size(), 0U);
1094 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001095 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 return false;
1098 }
1099 /* Perform code flow verification. */
1100 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001103 }
jeffhaobdb76512011-09-07 11:43:16 -07001104 return true;
1105}
1106
Ian Rogersad0b3a32012-04-16 14:50:24 -07001107std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1108 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001109 for (size_t i = 0; i < failures_.size(); ++i) {
1110 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001111 }
1112 return os;
1113}
1114
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001117 v->Dump(std::cerr);
1118}
1119
Ian Rogers776ac1f2012-04-13 23:36:36 -07001120void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001121 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001122 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 return;
jeffhaobdb76512011-09-07 11:43:16 -07001124 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001125 {
1126 os << "Register Types:\n";
1127 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1128 std::ostream indent_os(&indent_filter);
1129 reg_types_.Dump(indent_os);
1130 }
Ian Rogersb4903572012-10-11 11:52:56 -07001131 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001132 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1133 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 const Instruction* inst = Instruction::At(code_item_->insns_);
1135 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1136 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1138 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001139 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001140 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001141 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001142 const bool kDumpHexOfInstruction = false;
1143 if (kDumpHexOfInstruction) {
1144 indent_os << inst->DumpHex(5) << " ";
1145 }
1146 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001147 inst = inst->Next();
1148 }
jeffhaobdb76512011-09-07 11:43:16 -07001149}
1150
Ian Rogersd81871c2011-10-03 13:57:23 -07001151static bool IsPrimitiveDescriptor(char descriptor) {
1152 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001153 case 'I':
1154 case 'C':
1155 case 'S':
1156 case 'B':
1157 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001158 case 'F':
1159 case 'D':
1160 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001162 default:
1163 return false;
1164 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001165}
1166
Ian Rogers776ac1f2012-04-13 23:36:36 -07001167bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 RegisterLine* reg_line = reg_table_.GetLine(0);
1169 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1170 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001171
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001173 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001175 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1177 // argument as uninitialized. This restricts field access until the superclass constructor is
1178 // called.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001179 RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001180 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 reg_line->SetRegisterType(arg_start + cur_arg,
1182 reg_types_.UninitializedThisArgument(declaring_class));
1183 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001184 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001185 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001187 }
1188
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001189 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001190 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001191 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001192
1193 for (; iterator.HasNext(); iterator.Next()) {
1194 const char* descriptor = iterator.GetDescriptor();
1195 if (descriptor == NULL) {
1196 LOG(FATAL) << "Null descriptor";
1197 }
1198 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1200 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 return false;
1202 }
1203 switch (descriptor[0]) {
1204 case 'L':
1205 case '[':
1206 // We assume that reference arguments are initialized. The only way it could be otherwise
1207 // (assuming the caller was verified) is if the current method is <init>, but in that case
1208 // it's effectively considered initialized the instant we reach here (in the sense that we
1209 // can return without doing anything or call virtual methods).
1210 {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001211 RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001212 if (!reg_type.IsNonZeroReferenceTypes()) {
1213 DCHECK(HasFailures());
1214 return false;
1215 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001216 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 }
1218 break;
1219 case 'Z':
1220 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1221 break;
1222 case 'C':
1223 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1224 break;
1225 case 'B':
1226 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1227 break;
1228 case 'I':
1229 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1230 break;
1231 case 'S':
1232 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1233 break;
1234 case 'F':
1235 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1236 break;
1237 case 'J':
1238 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001239 if (cur_arg + 1 >= expected_args) {
1240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1241 << " args, found more (" << descriptor << ")";
1242 return false;
1243 }
1244
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001245 RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1246 RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001247 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 cur_arg++;
1249 break;
1250 }
1251 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1253 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 return false;
1255 }
1256 cur_arg++;
1257 }
1258 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1260 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 return false;
1262 }
1263 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1264 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1265 // format. Only major difference from the method argument format is that 'V' is supported.
1266 bool result;
1267 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1268 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001269 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 size_t i = 0;
1271 do {
1272 i++;
1273 } while (descriptor[i] == '['); // process leading [
1274 if (descriptor[i] == 'L') { // object array
1275 do {
1276 i++; // find closing ;
1277 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1278 result = descriptor[i] == ';';
1279 } else { // primitive array
1280 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1281 }
1282 } else if (descriptor[0] == 'L') {
1283 // could be more thorough here, but shouldn't be required
1284 size_t i = 0;
1285 do {
1286 i++;
1287 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1288 result = descriptor[i] == ';';
1289 } else {
1290 result = false;
1291 }
1292 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1294 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001295 }
1296 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001297}
1298
Ian Rogers776ac1f2012-04-13 23:36:36 -07001299bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 const uint16_t* insns = code_item_->insns_;
1301 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001302
jeffhaobdb76512011-09-07 11:43:16 -07001303 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 insn_flags_[0].SetChanged();
1305 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001306
jeffhaobdb76512011-09-07 11:43:16 -07001307 /* Continue until no instructions are marked "changed". */
1308 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1310 uint32_t insn_idx = start_guess;
1311 for (; insn_idx < insns_size; insn_idx++) {
1312 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001313 break;
1314 }
jeffhaobdb76512011-09-07 11:43:16 -07001315 if (insn_idx == insns_size) {
1316 if (start_guess != 0) {
1317 /* try again, starting from the top */
1318 start_guess = 0;
1319 continue;
1320 } else {
1321 /* all flags are clear */
1322 break;
1323 }
1324 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 // We carry the working set of registers from instruction to instruction. If this address can
1326 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1327 // "changed" flags, we need to load the set of registers from the table.
1328 // Because we always prefer to continue on to the next instruction, we should never have a
1329 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1330 // target.
1331 work_insn_idx_ = insn_idx;
1332 if (insn_flags_[insn_idx].IsBranchTarget()) {
1333 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001334 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001335 /*
1336 * Sanity check: retrieve the stored register line (assuming
1337 * a full table) and make sure it actually matches.
1338 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1340 if (register_line != NULL) {
1341 if (work_line_->CompareLine(register_line) != 0) {
1342 Dump(std::cout);
1343 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001344 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001345 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1346 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001347 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 }
jeffhaobdb76512011-09-07 11:43:16 -07001349 }
jeffhaobdb76512011-09-07 11:43:16 -07001350 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001352 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001353 prepend += " failed to verify: ";
1354 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001355 return false;
1356 }
jeffhaobdb76512011-09-07 11:43:16 -07001357 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 insn_flags_[insn_idx].SetVisited();
1359 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001360 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001361
Ian Rogers1c849e52012-06-28 14:00:33 -07001362 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001364 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001365 * (besides the wasted space), but it indicates a flaw somewhere
1366 * down the line, possibly in the verifier.
1367 *
1368 * If we've substituted "always throw" instructions into the stream,
1369 * we are almost certainly going to have some dead code.
1370 */
1371 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 uint32_t insn_idx = 0;
1373 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001374 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001375 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001376 * may or may not be preceded by a padding NOP (for alignment).
1377 */
1378 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1379 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1380 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001381 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001382 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1383 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1384 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001386 }
1387
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001389 if (dead_start < 0)
1390 dead_start = insn_idx;
1391 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001392 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1393 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001394 dead_start = -1;
1395 }
1396 }
1397 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001398 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1399 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001400 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001401 // To dump the state of the verify after a method, do something like:
1402 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1403 // "boolean java.lang.String.equals(java.lang.Object)") {
1404 // LOG(INFO) << info_messages_.str();
1405 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001406 }
jeffhaobdb76512011-09-07 11:43:16 -07001407 return true;
1408}
1409
Ian Rogers776ac1f2012-04-13 23:36:36 -07001410bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001411 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1412 // We want the state _before_ the instruction, for the case where the dex pc we're
1413 // interested in is itself a monitor-enter instruction (which is a likely place
1414 // for a thread to be suspended).
1415 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001416 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001417 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1418 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1419 }
1420 }
1421
jeffhaobdb76512011-09-07 11:43:16 -07001422 /*
1423 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001424 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001425 * control to another statement:
1426 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001427 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001428 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001429 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001430 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001431 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001432 * throw an exception that is handled by an encompassing "try"
1433 * block.
1434 *
1435 * We can also return, in which case there is no successor instruction
1436 * from this point.
1437 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001438 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001439 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1441 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001442 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001443
jeffhaobdb76512011-09-07 11:43:16 -07001444 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001445 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001446 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001448 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1449 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 }
jeffhaobdb76512011-09-07 11:43:16 -07001451
1452 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001453 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001454 * can throw an exception, we will copy/merge this into the "catch"
1455 * address rather than work_line, because we don't want the result
1456 * from the "successful" code path (e.g. a check-cast that "improves"
1457 * a type) to be visible to the exception handler.
1458 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001459 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001460 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001461 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001463 }
1464
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001465
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001466 // We need to ensure the work line is consistent while performing validation. When we spot a
1467 // peephole pattern we compute a new line for either the fallthrough instruction or the
1468 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001469 std::unique_ptr<RegisterLine> branch_line;
1470 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001471
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001473 case Instruction::NOP:
1474 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001475 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001476 * a signature that looks like a NOP; if we see one of these in
1477 * the course of executing code then we have a problem.
1478 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001481 }
1482 break;
1483
1484 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1486 break;
jeffhaobdb76512011-09-07 11:43:16 -07001487 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1489 break;
jeffhaobdb76512011-09-07 11:43:16 -07001490 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001491 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001492 break;
1493 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001494 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1495 break;
jeffhaobdb76512011-09-07 11:43:16 -07001496 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1498 break;
jeffhaobdb76512011-09-07 11:43:16 -07001499 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1504 break;
jeffhaobdb76512011-09-07 11:43:16 -07001505 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1507 break;
jeffhaobdb76512011-09-07 11:43:16 -07001508 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001509 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001510 break;
1511
1512 /*
1513 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001514 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001515 * might want to hold the result in an actual CPU register, so the
1516 * Dalvik spec requires that these only appear immediately after an
1517 * invoke or filled-new-array.
1518 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001519 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001520 * redundant with the reset done below, but it can make the debug info
1521 * easier to read in some cases.)
1522 */
1523 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001525 break;
1526 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001527 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001528 break;
1529 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001531 break;
1532
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001534 /*
jeffhao60f83e32012-02-13 17:16:30 -08001535 * This statement can only appear as the first instruction in an exception handler. We verify
1536 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001537 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001538 RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001540 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 }
jeffhaobdb76512011-09-07 11:43:16 -07001542 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001543 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1544 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 }
jeffhaobdb76512011-09-07 11:43:16 -07001547 }
1548 break;
1549 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001550 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001551 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001552 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1555 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 } else {
1557 // Compilers may generate synthetic functions that write byte values into boolean fields.
1558 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001559 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001560 RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1562 ((return_type.IsBoolean() || return_type.IsByte() ||
1563 return_type.IsShort() || return_type.IsChar()) &&
1564 src_type.IsInteger()));
1565 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001566 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001567 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001568 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 }
jeffhaobdb76512011-09-07 11:43:16 -07001571 }
1572 }
1573 break;
1574 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001576 /* check the method signature */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001577 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001579 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 } else {
1581 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001582 const uint32_t vregA = inst->VRegA_11x();
1583 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001584 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001585 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
jeffhaobdb76512011-09-07 11:43:16 -07001587 }
1588 }
1589 break;
1590 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001591 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001592 RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 } else {
1596 /* return_type is the *expected* return type, not register value */
1597 DCHECK(!return_type.IsZero());
1598 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 const uint32_t vregA = inst->VRegA_11x();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001600 RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001601 // Disallow returning uninitialized values and verify that the reference in vAA is an
1602 // instance of the "return_type"
1603 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1605 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001606 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001607 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1608 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1609 << "' or '" << reg_type << "'";
1610 } else {
1611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1612 << "', but expected from declaration '" << return_type << "'";
1613 }
jeffhaobdb76512011-09-07 11:43:16 -07001614 }
1615 }
1616 }
1617 break;
1618
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001619 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 case Instruction::CONST_4: {
1621 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001622 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001623 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001624 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 }
1626 case Instruction::CONST_16: {
1627 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001628 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001629 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001630 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001632 case Instruction::CONST: {
1633 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001635 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001636 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001637 }
1638 case Instruction::CONST_HIGH16: {
1639 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001641 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001642 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001643 }
jeffhaobdb76512011-09-07 11:43:16 -07001644 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001647 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1648 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 }
1652 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001654 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1655 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001657 break;
1658 }
1659 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 int64_t val = inst->VRegB_51l();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001661 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1662 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001664 break;
1665 }
1666 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001668 RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1669 RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001671 break;
1672 }
jeffhaobdb76512011-09-07 11:43:16 -07001673 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1675 break;
jeffhaobdb76512011-09-07 11:43:16 -07001676 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001677 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001678 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001680 // Get type from instruction if unresolved then we need an access check
1681 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001682 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001683 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001684 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001685 res_type.IsConflict() ? res_type
1686 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 }
jeffhaobdb76512011-09-07 11:43:16 -07001689 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001691 break;
1692 case Instruction::MONITOR_EXIT:
1693 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001694 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001695 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001696 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001697 * to the need to handle asynchronous exceptions, a now-deprecated
1698 * feature that Dalvik doesn't support.)
1699 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001700 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001701 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001702 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001703 * structured locking checks are working, the former would have
1704 * failed on the -enter instruction, and the latter is impossible.
1705 *
1706 * This is fortunate, because issue 3221411 prevents us from
1707 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001708 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001709 * some catch blocks (which will show up as "dead" code when
1710 * we skip them here); if we can't, then the code path could be
1711 * "live" so we still need to check it.
1712 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001713 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001714 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001715 break;
1716
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 /*
1720 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1721 * could be a "upcast" -- not expected, so we don't try to address it.)
1722 *
1723 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001725 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1727 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001728 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001729 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001730 // If this is a primitive type, fail HARD.
1731 mirror::Class* klass = (*dex_cache_)->GetResolvedType(type_idx);
1732 if (klass != nullptr && klass->IsPrimitive()) {
1733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1734 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1735 << GetDeclaringClass();
1736 break;
1737 }
1738
Ian Rogersad0b3a32012-04-16 14:50:24 -07001739 DCHECK_NE(failures_.size(), 0U);
1740 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001742 }
1743 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001744 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001745 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001747 RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001748 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 if (is_checkcast) {
1750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1751 } else {
1752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1753 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001754 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 if (is_checkcast) {
1756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1757 } else {
1758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1759 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001760 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001761 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001762 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001765 }
jeffhaobdb76512011-09-07 11:43:16 -07001766 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001767 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 }
1769 case Instruction::ARRAY_LENGTH: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001770 RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001771 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001772 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001776 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001777 } else {
1778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001779 }
1780 break;
1781 }
1782 case Instruction::NEW_INSTANCE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001783 RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001784 if (res_type.IsConflict()) {
1785 DCHECK_NE(failures_.size(), 0U);
1786 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001787 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001788 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1789 // can't create an instance of an interface or abstract class */
1790 if (!res_type.IsInstantiableTypes()) {
1791 Fail(VERIFY_ERROR_INSTANTIATION)
1792 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001793 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001795 RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001796 // Any registers holding previous allocations from this address that have not yet been
1797 // initialized must be marked invalid.
1798 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1799 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001801 break;
1802 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001803 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
1806 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001808 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001809 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001810 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001812 just_set_result = true; // Filled new array range sets result register
1813 break;
jeffhaobdb76512011-09-07 11:43:16 -07001814 case Instruction::CMPL_FLOAT:
1815 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001816 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001817 break;
1818 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001820 break;
1821 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001822 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001823 break;
1824 case Instruction::CMPL_DOUBLE:
1825 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001827 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001828 break;
1829 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001832 break;
1833 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
1836 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001839 break;
1840 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001842 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001843 break;
1844 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001845 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001846 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 case Instruction::THROW: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001848 RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001849 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001850 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1851 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001852 }
1853 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001854 }
jeffhaobdb76512011-09-07 11:43:16 -07001855 case Instruction::GOTO:
1856 case Instruction::GOTO_16:
1857 case Instruction::GOTO_32:
1858 /* no effect on or use of registers */
1859 break;
1860
1861 case Instruction::PACKED_SWITCH:
1862 case Instruction::SPARSE_SWITCH:
1863 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001865 break;
1866
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 case Instruction::FILL_ARRAY_DATA: {
1868 /* Similar to the verification done for APUT */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001869 RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001870 /* array_type can be null if the reg type is Zero */
1871 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001872 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1874 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001875 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001876 RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001877 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001878 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001879 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1881 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 } else {
jeffhao457cc512012-02-02 16:55:13 -08001883 // Now verify if the element width in the table matches the element width declared in
1884 // the array
1885 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1886 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001888 } else {
1889 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1890 // Since we don't compress the data in Dex, expect to see equal width of data stored
1891 // in the table and expected from the array class.
1892 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1894 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001895 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 }
1897 }
jeffhaobdb76512011-09-07 11:43:16 -07001898 }
1899 }
1900 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 }
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 case Instruction::IF_NE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001904 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1905 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 bool mismatch = false;
1907 if (reg_type1.IsZero()) { // zero then integral or reference expected
1908 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1909 } else if (reg_type1.IsReferenceTypes()) { // both references?
1910 mismatch = !reg_type2.IsReferenceTypes();
1911 } else { // both integral?
1912 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1913 }
1914 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1916 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001917 }
1918 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 }
jeffhaobdb76512011-09-07 11:43:16 -07001920 case Instruction::IF_LT:
1921 case Instruction::IF_GE:
1922 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001923 case Instruction::IF_LE: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001924 RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1925 RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1928 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001929 }
1930 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 }
jeffhaobdb76512011-09-07 11:43:16 -07001932 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 case Instruction::IF_NEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001934 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001935 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1937 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001939
1940 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001941 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001942 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001943 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001944 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001945 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001946 }
Ian Rogers9b360392013-06-06 14:45:07 -07001947 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001948 } else {
1949 break;
1950 }
1951
Ian Rogers9b360392013-06-06 14:45:07 -07001952 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001953
1954 /* Check for peep-hole pattern of:
1955 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001956 * instance-of vX, vY, T;
1957 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001958 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001959 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001960 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001961 * and sharpen the type of vY to be type T.
1962 * Note, this pattern can't be if:
1963 * - if there are other branches to this branch,
1964 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001965 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001966 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001967 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1968 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1969 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07001970 // Check the type of the instance-of is different than that of registers type, as if they
1971 // are the same there is no work to be done here. Check that the conversion is not to or
1972 // from an unresolved type as type information is imprecise. If the instance-of is to an
1973 // interface then ignore the type information as interfaces can only be treated as Objects
1974 // and we don't want to disallow field and other operations on the object. If the value
1975 // being instance-of checked against is known null (zero) then allow the optimization as
1976 // we didn't have type information. If the merge of the instance-of type with the original
1977 // type is assignable to the original then allow optimization. This check is performed to
1978 // ensure that subsequent merges don't lose type information - such as becoming an
1979 // interface from a class that would lose information relevant to field checks.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07001980 RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
1981 RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001982
Ian Rogersebbdd872014-07-07 23:53:08 -07001983 if (!orig_type.Equals(cast_type) &&
1984 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07001985 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07001986 !cast_type.GetClass()->IsInterface() &&
1987 (orig_type.IsZero() ||
1988 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001989 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001990 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001991 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001992 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001993 branch_line.reset(update_line);
1994 }
1995 update_line->CopyFromLine(work_line_.get());
1996 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1997 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1998 // See if instance-of was preceded by a move-object operation, common due to the small
1999 // register encoding space of instance-of, and propagate type information to the source
2000 // of the move-object.
2001 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002002 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002003 move_idx--;
2004 }
2005 CHECK(insn_flags_[move_idx].IsOpcode());
2006 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2007 switch (move_inst->Opcode()) {
2008 case Instruction::MOVE_OBJECT:
2009 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
2010 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
2011 }
2012 break;
2013 case Instruction::MOVE_OBJECT_FROM16:
2014 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
2015 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
2016 }
2017 break;
2018 case Instruction::MOVE_OBJECT_16:
2019 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2020 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2021 }
2022 break;
2023 default:
2024 break;
2025 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002026 }
2027 }
2028 }
2029
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 }
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::IF_LTZ:
2033 case Instruction::IF_GEZ:
2034 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 case Instruction::IF_LEZ: {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002036 RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002038 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2039 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 }
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 }
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
jeffhaobdb76512011-09-07 11:43:16 -07002052 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
jeffhaobdb76512011-09-07 11:43:16 -07002058 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
2061 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064
Ian Rogersd81871c2011-10-03 13:57:23 -07002065 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
2071 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 break;
2074 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
2099 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 break;
jeffhaobdb76512011-09-07 11:43:16 -07002108
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
2115 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 break;
2118 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 break;
jeffhaobdb76512011-09-07 11:43:16 -07002127 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130
jeffhaobdb76512011-09-07 11:43:16 -07002131 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
jeffhaobdb76512011-09-07 11:43:16 -07002134 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002135 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 break;
jeffhaobdb76512011-09-07 11:43:16 -07002137 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002139 break;
jeffhaobdb76512011-09-07 11:43:16 -07002140 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 break;
2143 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002144 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002145 break;
2146 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002148 break;
2149 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 break;
2152
2153 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002155 break;
2156 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 break;
2159 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002161 break;
2162 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002164 break;
2165 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002167 break;
2168 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
2171 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002172 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002173 break;
2174
2175 case Instruction::INVOKE_VIRTUAL:
2176 case Instruction::INVOKE_VIRTUAL_RANGE:
2177 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002178 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002179 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2180 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002181 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2182 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002183 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2184 is_super);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002185 RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002186 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002187 Thread* self = Thread::Current();
2188 StackHandleScope<1> hs(self);
2189 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2190 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002191 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002192 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002193 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2194 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002195 return_type_class->CannotBeAssignedFromOtherTypes());
2196 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002197 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002198 self->ClearException();
2199 }
2200 }
2201 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002202 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002203 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2204 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002205 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002206 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002207 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002208 if (!return_type->IsLowHalf()) {
2209 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002210 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002211 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002212 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002213 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002219 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002220 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002221 const char* return_type_descriptor;
2222 bool is_constructor;
Ian Rogers1ff3c982014-08-12 02:30:58 -07002223 RegType* return_type = nullptr;
Ian Rogers46685432012-06-03 22:26:43 -07002224 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002226 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002227 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002228 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2229 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2230 } else {
2231 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002232 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002233 Thread* self = Thread::Current();
2234 StackHandleScope<1> hs(self);
2235 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2236 MethodHelper mh(h_called_method);
2237 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
2238 if (return_type_class != nullptr) {
2239 return_type = &reg_types_.FromClass(return_type_descriptor,
2240 return_type_class,
2241 return_type_class->CannotBeAssignedFromOtherTypes());
2242 } else {
2243 DCHECK(!can_load_classes_ || self->IsExceptionPending());
2244 self->ClearException();
2245 }
Ian Rogers46685432012-06-03 22:26:43 -07002246 }
2247 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002248 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 * Some additional checks when calling a constructor. We know from the invocation arg check
2250 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2251 * that to require that called_method->klass is the same as this->klass or this->super,
2252 * allowing the latter only if the "this" argument is the same as the "this" argument to
2253 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002254 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002255 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002256 if (this_type.IsConflict()) // failure.
2257 break;
jeffhaobdb76512011-09-07 11:43:16 -07002258
jeffhaob57e9522012-04-26 18:08:21 -07002259 /* no null refs allowed (?) */
2260 if (this_type.IsZero()) {
2261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2262 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002263 }
jeffhaob57e9522012-04-26 18:08:21 -07002264
2265 /* must be in same class or in superclass */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002266 // RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002267 // TODO: re-enable constructor type verification
2268 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002269 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002270 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2271 // break;
2272 // }
jeffhaob57e9522012-04-26 18:08:21 -07002273
2274 /* arg must be an uninitialized reference */
2275 if (!this_type.IsUninitializedTypes()) {
2276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2277 << this_type;
2278 break;
2279 }
2280
2281 /*
2282 * Replace the uninitialized reference with an initialized one. We need to do this for all
2283 * registers that have the same object instance in them, not just the "this" register.
2284 */
2285 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002286 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002287 if (return_type == nullptr) {
2288 return_type = &reg_types_.FromDescriptor(class_loader_->Get(),
2289 return_type_descriptor, false);
2290 }
2291 if (!return_type->IsLowHalf()) {
2292 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002293 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002294 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002295 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002296 just_set_result = true;
2297 break;
2298 }
2299 case Instruction::INVOKE_STATIC:
2300 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002302 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002303 METHOD_STATIC,
2304 is_range,
2305 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002306 const char* descriptor;
2307 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002308 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002309 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2310 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002311 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002312 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002313 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002314 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002315 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002316 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 if (!return_type.IsLowHalf()) {
2318 work_line_->SetResultRegisterType(return_type);
2319 } else {
2320 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2321 }
jeffhaobdb76512011-09-07 11:43:16 -07002322 just_set_result = true;
2323 }
2324 break;
jeffhaobdb76512011-09-07 11:43:16 -07002325 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002326 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002327 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002328 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002329 METHOD_INTERFACE,
2330 is_range,
2331 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002332 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002333 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002334 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2335 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2336 << PrettyMethod(abs_method) << "'";
2337 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002338 }
Ian Rogers0d604842012-04-16 14:50:24 -07002339 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 /* Get the type of the "this" arg, which should either be a sub-interface of called
2341 * interface or Object (see comments in RegType::JoinClass).
2342 */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002343 RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002344 if (this_type.IsZero()) {
2345 /* null pointer always passes (and always fails at runtime) */
2346 } else {
2347 if (this_type.IsUninitializedTypes()) {
2348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2349 << this_type;
2350 break;
2351 }
2352 // In the past we have tried to assert that "called_interface" is assignable
2353 // from "this_type.GetClass()", however, as we do an imprecise Join
2354 // (RegType::JoinClass) we don't have full information on what interfaces are
2355 // implemented by "this_type". For example, two classes may implement the same
2356 // interfaces and have a common parent that doesn't implement the interface. The
2357 // join will set "this_type" to the parent class and a test that this implements
2358 // the interface will incorrectly fail.
2359 }
2360 /*
2361 * We don't have an object instance, so we can't find the concrete method. However, all of
2362 * the type information is in the abstract method, so we're good.
2363 */
2364 const char* descriptor;
2365 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002367 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2368 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2369 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2370 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002371 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002372 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002373 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002374 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 if (!return_type.IsLowHalf()) {
2376 work_line_->SetResultRegisterType(return_type);
2377 } else {
2378 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2379 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002380 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 }
jeffhaobdb76512011-09-07 11:43:16 -07002383 case Instruction::NEG_INT:
2384 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::NEG_LONG:
2388 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002390 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002397 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002398 break;
2399 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002400 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002401 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002405 break;
2406 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002407 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002408 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002409 break;
2410 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002416 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002419 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002421 break;
2422 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002423 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002426 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002427 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002430 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002431 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002432 break;
2433 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002434 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002435 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002438 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002439 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002442 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002443 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002444 break;
2445 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002447 break;
2448 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002449 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454
2455 case Instruction::ADD_INT:
2456 case Instruction::SUB_INT:
2457 case Instruction::MUL_INT:
2458 case Instruction::REM_INT:
2459 case Instruction::DIV_INT:
2460 case Instruction::SHL_INT:
2461 case Instruction::SHR_INT:
2462 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002463 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::AND_INT:
2467 case Instruction::OR_INT:
2468 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002469 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002470 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::ADD_LONG:
2473 case Instruction::SUB_LONG:
2474 case Instruction::MUL_LONG:
2475 case Instruction::DIV_LONG:
2476 case Instruction::REM_LONG:
2477 case Instruction::AND_LONG:
2478 case Instruction::OR_LONG:
2479 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002480 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002481 reg_types_.LongLo(), reg_types_.LongHi(),
2482 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002483 break;
2484 case Instruction::SHL_LONG:
2485 case Instruction::SHR_LONG:
2486 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002488 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::ADD_FLOAT:
2492 case Instruction::SUB_FLOAT:
2493 case Instruction::MUL_FLOAT:
2494 case Instruction::DIV_FLOAT:
2495 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002496 work_line_->CheckBinaryOp(inst,
2497 reg_types_.Float(),
2498 reg_types_.Float(),
2499 reg_types_.Float(),
2500 false);
jeffhaobdb76512011-09-07 11:43:16 -07002501 break;
2502 case Instruction::ADD_DOUBLE:
2503 case Instruction::SUB_DOUBLE:
2504 case Instruction::MUL_DOUBLE:
2505 case Instruction::DIV_DOUBLE:
2506 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002507 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002508 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2509 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002510 break;
2511 case Instruction::ADD_INT_2ADDR:
2512 case Instruction::SUB_INT_2ADDR:
2513 case Instruction::MUL_INT_2ADDR:
2514 case Instruction::REM_INT_2ADDR:
2515 case Instruction::SHL_INT_2ADDR:
2516 case Instruction::SHR_INT_2ADDR:
2517 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002518 work_line_->CheckBinaryOp2addr(inst,
2519 reg_types_.Integer(),
2520 reg_types_.Integer(),
2521 reg_types_.Integer(),
2522 false);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::AND_INT_2ADDR:
2525 case Instruction::OR_INT_2ADDR:
2526 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002527 work_line_->CheckBinaryOp2addr(inst,
2528 reg_types_.Integer(),
2529 reg_types_.Integer(),
2530 reg_types_.Integer(),
2531 true);
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
2533 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002534 work_line_->CheckBinaryOp2addr(inst,
2535 reg_types_.Integer(),
2536 reg_types_.Integer(),
2537 reg_types_.Integer(),
2538 false);
jeffhaobdb76512011-09-07 11:43:16 -07002539 break;
2540 case Instruction::ADD_LONG_2ADDR:
2541 case Instruction::SUB_LONG_2ADDR:
2542 case Instruction::MUL_LONG_2ADDR:
2543 case Instruction::DIV_LONG_2ADDR:
2544 case Instruction::REM_LONG_2ADDR:
2545 case Instruction::AND_LONG_2ADDR:
2546 case Instruction::OR_LONG_2ADDR:
2547 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002548 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002549 reg_types_.LongLo(), reg_types_.LongHi(),
2550 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::SHL_LONG_2ADDR:
2553 case Instruction::SHR_LONG_2ADDR:
2554 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002556 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
2558 case Instruction::ADD_FLOAT_2ADDR:
2559 case Instruction::SUB_FLOAT_2ADDR:
2560 case Instruction::MUL_FLOAT_2ADDR:
2561 case Instruction::DIV_FLOAT_2ADDR:
2562 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002563 work_line_->CheckBinaryOp2addr(inst,
2564 reg_types_.Float(),
2565 reg_types_.Float(),
2566 reg_types_.Float(),
2567 false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::ADD_DOUBLE_2ADDR:
2570 case Instruction::SUB_DOUBLE_2ADDR:
2571 case Instruction::MUL_DOUBLE_2ADDR:
2572 case Instruction::DIV_DOUBLE_2ADDR:
2573 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002575 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2576 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::ADD_INT_LIT16:
2579 case Instruction::RSUB_INT:
2580 case Instruction::MUL_INT_LIT16:
2581 case Instruction::DIV_INT_LIT16:
2582 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::AND_INT_LIT16:
2586 case Instruction::OR_INT_LIT16:
2587 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002588 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590 case Instruction::ADD_INT_LIT8:
2591 case Instruction::RSUB_INT_LIT8:
2592 case Instruction::MUL_INT_LIT8:
2593 case Instruction::DIV_INT_LIT8:
2594 case Instruction::REM_INT_LIT8:
2595 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002598 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002599 break;
2600 case Instruction::AND_INT_LIT8:
2601 case Instruction::OR_INT_LIT8:
2602 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002603 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002606 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002607 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002608 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2610 }
2611 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002612 // Note: the following instructions encode offsets derived from class linking.
2613 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2614 // meaning if the class linking and resolution were successful.
2615 case Instruction::IGET_QUICK:
2616 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2617 break;
2618 case Instruction::IGET_WIDE_QUICK:
2619 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2620 break;
2621 case Instruction::IGET_OBJECT_QUICK:
2622 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2623 break;
2624 case Instruction::IPUT_QUICK:
2625 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2626 break;
2627 case Instruction::IPUT_WIDE_QUICK:
2628 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2629 break;
2630 case Instruction::IPUT_OBJECT_QUICK:
2631 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2632 break;
2633 case Instruction::INVOKE_VIRTUAL_QUICK:
2634 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2635 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002636 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002637 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002638 const char* descriptor = called_method->GetReturnTypeDescriptor();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002639 RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002640 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002641 if (!return_type.IsLowHalf()) {
2642 work_line_->SetResultRegisterType(return_type);
2643 } else {
2644 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2645 }
2646 just_set_result = true;
2647 }
2648 break;
2649 }
2650
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002652 case Instruction::UNUSED_3E:
2653 case Instruction::UNUSED_3F:
2654 case Instruction::UNUSED_40:
2655 case Instruction::UNUSED_41:
2656 case Instruction::UNUSED_42:
2657 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002658 case Instruction::UNUSED_79:
2659 case Instruction::UNUSED_7A:
2660 case Instruction::UNUSED_EB:
2661 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002662 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002663 case Instruction::UNUSED_EE:
2664 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002665 case Instruction::UNUSED_F0:
2666 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002667 case Instruction::UNUSED_F2:
2668 case Instruction::UNUSED_F3:
2669 case Instruction::UNUSED_F4:
2670 case Instruction::UNUSED_F5:
2671 case Instruction::UNUSED_F6:
2672 case Instruction::UNUSED_F7:
2673 case Instruction::UNUSED_F8:
2674 case Instruction::UNUSED_F9:
2675 case Instruction::UNUSED_FA:
2676 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002677 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002678 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002679 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002680 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683
2684 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002685 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002686 * complain if an instruction is missing (which is desirable).
2687 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002689
Ian Rogersad0b3a32012-04-16 14:50:24 -07002690 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002691 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002692 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002693 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002694 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002695 /* immediate failure, reject class */
2696 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2697 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002698 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002699 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002700 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002701 }
jeffhaobdb76512011-09-07 11:43:16 -07002702 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 * If we didn't just set the result register, clear it out. This ensures that you can only use
2704 * "move-result" immediately after the result is set. (We could check this statically, but it's
2705 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002706 */
2707 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002709 }
2710
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002711
jeffhaobdb76512011-09-07 11:43:16 -07002712
2713 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002714 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002715 *
2716 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002717 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002718 * somebody could get a reference field, check it for zero, and if the
2719 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002720 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002721 * that, and will reject the code.
2722 *
2723 * TODO: avoid re-fetching the branch target
2724 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002725 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002726 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002728 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002730 return false;
2731 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002732 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002733 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002734 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 }
jeffhaobdb76512011-09-07 11:43:16 -07002736 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002737 if (NULL != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002738 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002739 return false;
2740 }
2741 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002742 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002743 return false;
2744 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
jeffhaobdb76512011-09-07 11:43:16 -07002746 }
2747
2748 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002749 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002750 *
2751 * We've already verified that the table is structurally sound, so we
2752 * just need to walk through and tag the targets.
2753 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002754 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002755 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2756 const uint16_t* switch_insns = insns + offset_to_switch;
2757 int switch_count = switch_insns[1];
2758 int offset_to_targets, targ;
2759
2760 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2761 /* 0 = sig, 1 = count, 2/3 = first key */
2762 offset_to_targets = 4;
2763 } else {
2764 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002765 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002766 offset_to_targets = 2 + 2 * switch_count;
2767 }
2768
2769 /* verify each switch target */
2770 for (targ = 0; targ < switch_count; targ++) {
2771 int offset;
2772 uint32_t abs_offset;
2773
2774 /* offsets are 32-bit, and only partly endian-swapped */
2775 offset = switch_insns[offset_to_targets + targ * 2] |
2776 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 abs_offset = work_insn_idx_ + offset;
2778 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002779 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002780 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002782 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002783 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002784 }
jeffhaobdb76512011-09-07 11:43:16 -07002785 }
2786 }
2787
2788 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2790 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002791 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002792 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002793 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002794 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002795
Andreas Gampef91baf12014-07-18 15:41:00 -07002796 // Need the linker to try and resolve the handled class to check if it's Throwable.
2797 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2798
Ian Rogers0571d352011-11-03 19:51:38 -07002799 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002800 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2801 if (handler_type_idx == DexFile::kDexNoIndex16) {
2802 has_catch_all_handler = true;
2803 } else {
2804 // It is also a catch-all if it is java.lang.Throwable.
2805 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, *dex_cache_,
2806 *class_loader_);
2807 if (klass != nullptr) {
2808 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2809 has_catch_all_handler = true;
2810 }
2811 } else {
2812 // Clear exception.
2813 Thread* self = Thread::Current();
2814 DCHECK(self->IsExceptionPending());
2815 self->ClearException();
2816 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 }
jeffhaobdb76512011-09-07 11:43:16 -07002818 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2820 * "work_regs", because at runtime the exception will be thrown before the instruction
2821 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002822 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002823 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002824 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 }
jeffhaobdb76512011-09-07 11:43:16 -07002826 }
2827
2828 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002829 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2830 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002831 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002832 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002833 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002834 * The state in work_line reflects the post-execution state. If the current instruction is a
2835 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002836 * it will do so before grabbing the lock).
2837 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002838 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002839 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002840 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002841 return false;
2842 }
2843 }
2844 }
2845
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002846 /* Handle "continue". Tag the next consecutive instruction.
2847 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2848 * because it changes work_line_ when performing peephole optimization
2849 * and this change should not be used in those cases.
2850 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002851 if ((opcode_flags & Instruction::kContinue) != 0) {
2852 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2853 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2855 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002856 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002857 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2858 // next instruction isn't one.
2859 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2860 return false;
2861 }
2862 if (NULL != fallthrough_line.get()) {
2863 // Make workline consistent with fallthrough computed from peephole optimization.
2864 work_line_->CopyFromLine(fallthrough_line.get());
2865 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002866 if (insn_flags_[next_insn_idx].IsReturn()) {
2867 // For returns we only care about the operand to the return, all other registers are dead.
2868 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2869 Instruction::Code opcode = ret_inst->Opcode();
2870 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2871 work_line_->MarkAllRegistersAsConflicts();
2872 } else {
2873 if (opcode == Instruction::RETURN_WIDE) {
2874 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2875 } else {
2876 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2877 }
2878 }
2879 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002880 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2881 if (next_line != NULL) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002882 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2883 // needed. If the merge changes the state of the registers then the work line will be
2884 // updated.
2885 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002886 return false;
2887 }
2888 } else {
2889 /*
2890 * We're not recording register data for the next instruction, so we don't know what the
2891 * prior state was. We have to assume that something has changed and re-evaluate it.
2892 */
2893 insn_flags_[next_insn_idx].SetChanged();
2894 }
2895 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002896
jeffhaod1f0fde2011-09-08 17:25:33 -07002897 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002898 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002899 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 return false;
2901 }
jeffhaobdb76512011-09-07 11:43:16 -07002902 }
2903
2904 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002905 * Update start_guess. Advance to the next instruction of that's
2906 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002907 * neither of those exists we're in a return or throw; leave start_guess
2908 * alone and let the caller sort it out.
2909 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002910 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002912 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002913 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002915 }
2916
Ian Rogersd81871c2011-10-03 13:57:23 -07002917 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2918 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002919
2920 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002921} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002922
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002923RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002924 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002925 RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002926 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002927 RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002928 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2929 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002930 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 if (result.IsConflict()) {
2932 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2933 << "' in " << referrer;
2934 return result;
2935 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002936 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002937 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002938 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002939 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002940 // check at runtime if access is allowed and so pass here. If result is
2941 // primitive, skip the access check.
2942 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2943 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002944 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002945 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002946 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002947 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002948}
2949
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002950RegType& MethodVerifier::GetCaughtExceptionType() {
2951 RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002952 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002953 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2955 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002956 CatchHandlerIterator iterator(handlers_ptr);
2957 for (; iterator.HasNext(); iterator.Next()) {
2958 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2959 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002960 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002962 RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002963 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002964 if (exception.IsUnresolvedTypes()) {
2965 // We don't know enough about the type. Fail here and let runtime handle it.
2966 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2967 return exception;
2968 } else {
2969 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2970 return reg_types_.Conflict();
2971 }
Jeff Haob878f212014-04-24 16:25:36 -07002972 } else if (common_super == nullptr) {
2973 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002974 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002975 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002976 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002977 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002978 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 }
2980 }
2981 }
2982 }
Ian Rogers0571d352011-11-03 19:51:38 -07002983 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 }
2985 }
2986 if (common_super == NULL) {
2987 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002988 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002989 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002991 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002992}
2993
Brian Carlstromea46f952013-07-30 01:26:50 -07002994mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002995 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002996 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07002997 RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 if (klass_type.IsConflict()) {
2999 std::string append(" in attempt to access method ");
3000 append += dex_file_->GetMethodName(method_id);
3001 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08003002 return NULL;
3003 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003004 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003005 return NULL; // Can't resolve Class so no more to do here
3006 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003007 mirror::Class* klass = klass_type.GetClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003008 RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003009 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003011 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003012 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003013
3014 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003015 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003016 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003017 res_method = klass->FindInterfaceMethod(name, signature);
3018 } else {
3019 res_method = klass->FindVirtualMethod(name, signature);
3020 }
3021 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003022 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003024 // If a virtual or interface method wasn't found with the expected type, look in
3025 // the direct methods. This can happen when the wrong invoke type is used or when
3026 // a class has changed, and will be flagged as an error in later checks.
3027 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3028 res_method = klass->FindDirectMethod(name, signature);
3029 }
3030 if (res_method == NULL) {
3031 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3032 << PrettyDescriptor(klass) << "." << name
3033 << " " << signature;
3034 return NULL;
3035 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 }
3037 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3039 // enforce them here.
3040 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3042 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 return NULL;
3044 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003045 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003046 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3048 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003049 return NULL;
3050 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003051 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003052 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003053 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003054 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003055 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003056 }
jeffhaode0d9c92012-02-27 13:58:13 -08003057 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3058 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003059 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3060 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003061 return NULL;
3062 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003063 // Check that interface methods match interface classes.
3064 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3065 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3066 << " is in an interface class " << PrettyClass(klass);
3067 return NULL;
3068 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3069 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3070 << " is in a non-interface class " << PrettyClass(klass);
3071 return NULL;
3072 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003073 // See if the method type implied by the invoke instruction matches the access flags for the
3074 // target method.
3075 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3076 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3077 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3078 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003079 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3080 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 return NULL;
3082 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003083 return res_method;
3084}
3085
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003086template <class T>
3087mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3088 MethodType method_type,
3089 bool is_range,
3090 mirror::ArtMethod* res_method) {
3091 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3092 // match the call to the signature. Also, we might be calling through an abstract method
3093 // definition (which doesn't have register count values).
3094 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3095 /* caught by static verifier */
3096 DCHECK(is_range || expected_args <= 5);
3097 if (expected_args > code_item_->outs_size_) {
3098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3099 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3100 return nullptr;
3101 }
3102
3103 uint32_t arg[5];
3104 if (!is_range) {
3105 inst->GetVarArgs(arg);
3106 }
3107 uint32_t sig_registers = 0;
3108
3109 /*
3110 * Check the "this" argument, which must be an instance of the class that declared the method.
3111 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3112 * rigorous check here (which is okay since we have to do it at runtime).
3113 */
3114 if (method_type != METHOD_STATIC) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003115 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003116 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3117 CHECK(have_pending_hard_failure_);
3118 return nullptr;
3119 }
3120 if (actual_arg_type.IsUninitializedReference()) {
3121 if (res_method) {
3122 if (!res_method->IsConstructor()) {
3123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3124 return nullptr;
3125 }
3126 } else {
3127 // Check whether the name of the called method is "<init>"
3128 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003129 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3131 return nullptr;
3132 }
3133 }
3134 }
3135 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003136 RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003137 if (res_method != nullptr) {
3138 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003139 std::string temp;
3140 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003141 klass->CannotBeAssignedFromOtherTypes());
3142 } else {
3143 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3144 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3145 res_method_class = &reg_types_.FromDescriptor(class_loader_->Get(),
3146 dex_file_->StringByTypeIdx(class_idx),
3147 false);
3148 }
3149 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3150 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3151 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3152 << "' not instance of '" << *res_method_class << "'";
3153 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3154 // the compiler.
3155 if (have_pending_hard_failure_) {
3156 return nullptr;
3157 }
3158 }
3159 }
3160 sig_registers = 1;
3161 }
3162
3163 for ( ; it->HasNext(); it->Next()) {
3164 if (sig_registers >= expected_args) {
3165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3166 " arguments, found " << sig_registers << " or more.";
3167 return nullptr;
3168 }
3169
3170 const char* param_descriptor = it->GetDescriptor();
3171
3172 if (param_descriptor == nullptr) {
3173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3174 "component";
3175 return nullptr;
3176 }
3177
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003178 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), param_descriptor,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003179 false);
3180 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3181 arg[sig_registers];
3182 if (reg_type.IsIntegralTypes()) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003183 RegType& src_type = work_line_->GetRegisterType(get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003184 if (!src_type.IsIntegralTypes()) {
3185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3186 << " but expected " << reg_type;
3187 return res_method;
3188 }
3189 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3190 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3191 // compiler.
3192 if (have_pending_hard_failure_) {
3193 return res_method;
3194 }
3195 }
3196 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3197 }
3198 if (expected_args != sig_registers) {
3199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3200 " arguments, found " << sig_registers;
3201 return nullptr;
3202 }
3203 return res_method;
3204}
3205
3206void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3207 MethodType method_type,
3208 bool is_range) {
3209 // As the method may not have been resolved, make this static check against what we expect.
3210 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3211 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3212 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3213 DexFileParameterIterator it(*dex_file_,
3214 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3215 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3216 nullptr);
3217}
3218
3219class MethodParamListDescriptorIterator {
3220 public:
3221 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3222 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3223 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3224 }
3225
3226 bool HasNext() {
3227 return pos_ < params_size_;
3228 }
3229
3230 void Next() {
3231 ++pos_;
3232 }
3233
3234 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3235 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3236 }
3237
3238 private:
3239 mirror::ArtMethod* res_method_;
3240 size_t pos_;
3241 const DexFile::TypeList* params_;
3242 const size_t params_size_;
3243};
3244
Brian Carlstromea46f952013-07-30 01:26:50 -07003245mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003246 MethodType method_type,
3247 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003248 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003249 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3250 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003251 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003252
Brian Carlstromea46f952013-07-30 01:26:50 -07003253 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003254 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003255 // Check what we can statically.
3256 if (!have_pending_hard_failure_) {
3257 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3258 }
3259 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003260 }
3261
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3263 // has a vtable entry for the target method.
3264 if (is_super) {
3265 DCHECK(method_type == METHOD_VIRTUAL);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003266 RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003267 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003268 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003269 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003270 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003271 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003272 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003273 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003274 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003275 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003276 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003277 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003278 << "." << res_method->GetName()
3279 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003280 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 }
3282 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003283
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003284 // Process the target method's signature. This signature may or may not
3285 MethodParamListDescriptorIterator it(res_method);
3286 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3287 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003288}
3289
Brian Carlstromea46f952013-07-30 01:26:50 -07003290mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003291 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003292 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3293 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003294 RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003295 if (!actual_arg_type.HasClass()) {
3296 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003297 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003298 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003299 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003300 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003301 if (klass->IsInterface()) {
3302 // Derive Object.class from Class.class.getSuperclass().
3303 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3304 CHECK(object_klass->IsObjectClass());
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003305 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003306 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003307 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003308 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003309 CHECK(dispatch_class->HasVTable()) << PrettyDescriptor(dispatch_class);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003310 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003311 CHECK_LT(static_cast<int32_t>(vtable_index), dispatch_class->GetVTableLength())
3312 << PrettyDescriptor(klass);
3313 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003314 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003315 return res_method;
3316}
3317
Brian Carlstromea46f952013-07-30 01:26:50 -07003318mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003319 bool is_range) {
3320 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003321 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003322 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003323 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003325 return NULL;
3326 }
3327 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3328
3329 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3330 // match the call to the signature. Also, we might be calling through an abstract method
3331 // definition (which doesn't have register count values).
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003332 RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003333 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3334 return NULL;
3335 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003336 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3337 /* caught by static verifier */
3338 DCHECK(is_range || expected_args <= 5);
3339 if (expected_args > code_item_->outs_size_) {
3340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3341 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3342 return NULL;
3343 }
3344
3345 /*
3346 * Check the "this" argument, which must be an instance of the class that declared the method.
3347 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3348 * rigorous check here (which is okay since we have to do it at runtime).
3349 */
3350 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3352 return NULL;
3353 }
3354 if (!actual_arg_type.IsZero()) {
3355 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003356 std::string temp;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003357 RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003358 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003359 klass->CannotBeAssignedFromOtherTypes());
3360 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003361 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3362 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003363 << "' not instance of '" << res_method_class << "'";
3364 return NULL;
3365 }
3366 }
3367 /*
3368 * Process the target method's signature. This signature may or may not
3369 * have been verified, so we can't assume it's properly formed.
3370 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003371 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003372 size_t params_size = params == NULL ? 0 : params->Size();
3373 uint32_t arg[5];
3374 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003375 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003376 }
3377 size_t actual_args = 1;
3378 for (size_t param_index = 0; param_index < params_size; param_index++) {
3379 if (actual_args >= expected_args) {
3380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003381 << "'. Expected " << expected_args
3382 << " arguments, processing argument " << actual_args
3383 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003384 return NULL;
3385 }
3386 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003387 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003388 if (descriptor == NULL) {
3389 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003390 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003391 return NULL;
3392 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003393 RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003394 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3395 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3396 return res_method;
3397 }
3398 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3399 }
3400 if (actual_args != expected_args) {
3401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3402 << " expected " << expected_args << " arguments, found " << actual_args;
3403 return NULL;
3404 } else {
3405 return res_method;
3406 }
3407}
3408
Ian Rogers62342ec2013-06-11 10:26:37 -07003409void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003410 uint32_t type_idx;
3411 if (!is_filled) {
3412 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3413 type_idx = inst->VRegC_22c();
3414 } else if (!is_range) {
3415 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3416 type_idx = inst->VRegB_35c();
3417 } else {
3418 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3419 type_idx = inst->VRegB_3rc();
3420 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003421 RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003422 if (res_type.IsConflict()) { // bad class
3423 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003424 } else {
3425 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3426 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003428 } else if (!is_filled) {
3429 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003430 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003431 /* set register type to array class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003432 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003433 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003434 } else {
3435 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3436 // the list and fail. It's legal, if silly, for arg_count to be zero.
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003437 RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003438 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3439 uint32_t arg[5];
3440 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003441 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003442 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003443 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003444 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003445 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003446 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003447 return;
3448 }
3449 }
3450 // filled-array result goes into "result" register
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003451 RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers62342ec2013-06-11 10:26:37 -07003452 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003453 }
3454 }
3455}
3456
Sebastien Hertz5243e912013-05-21 10:55:07 +02003457void MethodVerifier::VerifyAGet(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003458 RegType& insn_type, bool is_primitive) {
3459 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003463 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003464 if (array_type.IsZero()) {
3465 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3466 // instruction type. TODO: have a proper notion of bottom here.
3467 if (!is_primitive || insn_type.IsCategory1Types()) {
3468 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003469 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003470 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003471 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003472 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003473 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003474 }
jeffhaofc3144e2012-02-01 17:21:15 -08003475 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003477 } else {
3478 /* verify the class */
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003479 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003480 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003482 << " source for aget-object";
3483 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003485 << " source for category 1 aget";
3486 } else if (is_primitive && !insn_type.Equals(component_type) &&
3487 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003488 (insn_type.IsLong() && component_type.IsDouble()))) {
3489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3490 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003491 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003492 // Use knowledge of the field type which is stronger than the type inferred from the
3493 // instruction, which can't differentiate object types and ints from floats, longs from
3494 // doubles.
3495 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003496 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003497 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003498 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003499 component_type.HighHalf(&reg_types_));
3500 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003501 }
3502 }
3503 }
3504}
3505
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003506void MethodVerifier::VerifyPrimitivePut(RegType& target_type, RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003507 const uint32_t vregA) {
3508 // Primitive assignability rules are weaker than regular assignability rules.
3509 bool instruction_compatible;
3510 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003511 RegType& value_type = work_line_->GetRegisterType(vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003512 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003513 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003514 value_compatible = value_type.IsIntegralTypes();
3515 } else if (target_type.IsFloat()) {
3516 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3517 value_compatible = value_type.IsFloatTypes();
3518 } else if (target_type.IsLong()) {
3519 instruction_compatible = insn_type.IsLong();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003520 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003521 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003522 } else if (target_type.IsDouble()) {
3523 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003524 RegType& value_type_hi = work_line_->GetRegisterType(vregA + 1);
Andreas Gampe2a593a12014-07-21 22:11:42 -07003525 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003526 } else {
3527 instruction_compatible = false; // reference with primitive store
3528 value_compatible = false; // unused
3529 }
3530 if (!instruction_compatible) {
3531 // This is a global failure rather than a class change failure as the instructions and
3532 // the descriptors for the type should have been consistent within the same file at
3533 // compile time.
3534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3535 << "' but expected type '" << target_type << "'";
3536 return;
3537 }
3538 if (!value_compatible) {
3539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3540 << " of type " << value_type << " but expected " << target_type << " for put";
3541 return;
3542 }
3543}
3544
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545void MethodVerifier::VerifyAPut(const Instruction* inst,
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003546 RegType& insn_type, bool is_primitive) {
3547 RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003548 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003551 RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003552 if (array_type.IsZero()) {
3553 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3554 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003555 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003557 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003558 RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003559 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003560 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003561 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003563 if (!component_type.IsReferenceTypes()) {
3564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3565 << " source for aput-object";
3566 } else {
3567 // The instruction agrees with the type of array, confirm the value to be stored does too
3568 // Note: we use the instruction type (rather than the component type) for aput-object as
3569 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003570 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003571 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003572 }
3573 }
3574 }
3575}
3576
Brian Carlstromea46f952013-07-30 01:26:50 -07003577mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003578 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3579 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003580 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003581 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003582 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3583 field_idx, dex_file_->GetFieldName(field_id),
3584 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003585 return NULL;
3586 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003587 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003588 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003589 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003590 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3591 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3592 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003593 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003594 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003595 << dex_file_->GetFieldName(field_id) << ") in "
3596 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 DCHECK(Thread::Current()->IsExceptionPending());
3598 Thread::Current()->ClearException();
3599 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3601 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003602 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003604 return NULL;
3605 } else if (!field->IsStatic()) {
3606 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3607 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003608 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003609 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003610}
3611
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003612mirror::ArtField* MethodVerifier::GetInstanceField(RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003613 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3614 // Check access to class
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003615 RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003616 if (klass_type.IsConflict()) {
3617 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3618 field_idx, dex_file_->GetFieldName(field_id),
3619 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003620 return NULL;
3621 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003622 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003623 return NULL; // Can't resolve Class so no more to do here
3624 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003625 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3626 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3627 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003629 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003630 << dex_file_->GetFieldName(field_id) << ") in "
3631 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003632 DCHECK(Thread::Current()->IsExceptionPending());
3633 Thread::Current()->ClearException();
3634 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003635 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3636 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003637 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003638 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003639 return NULL;
3640 } else if (field->IsStatic()) {
3641 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3642 << " to not be static";
3643 return NULL;
3644 } else if (obj_type.IsZero()) {
3645 // Cannot infer and check type, however, access will cause null pointer exception
3646 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003647 } else if (!obj_type.IsReferenceTypes()) {
3648 // Trying to read a field from something that isn't a reference
3649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3650 << "non-reference type " << obj_type;
3651 return NULL;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003652 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003653 mirror::Class* klass = field->GetDeclaringClass();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003654 RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003655 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003656 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003657 if (obj_type.IsUninitializedTypes() &&
3658 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3659 !field_klass.Equals(GetDeclaringClass()))) {
3660 // Field accesses through uninitialized references are only allowable for constructors where
3661 // the field is declared in this class
3662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003663 << " of a not fully initialized object within the context"
3664 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003665 return NULL;
3666 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3667 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3668 // of C1. For resolution to occur the declared class of the field must be compatible with
3669 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3670 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3671 << " from object of type " << obj_type;
3672 return NULL;
3673 } else {
3674 return field;
3675 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003676 }
3677}
3678
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003679void MethodVerifier::VerifyISGet(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003680 bool is_primitive, bool is_static) {
3681 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003682 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003683 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003684 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003685 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003686 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003687 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003688 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003689 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003690 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003691 Thread* self = Thread::Current();
3692 mirror::Class* field_type_class;
3693 {
3694 StackHandleScope<1> hs(self);
3695 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3696 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3697 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003698 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003699 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003700 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003701 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003702 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3703 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003704 }
Ian Rogers0d604842012-04-16 14:50:24 -07003705 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003706 if (field_type == nullptr) {
3707 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3708 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003709 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003710 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003711 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003712 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003713 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003714 if (field_type->Equals(insn_type) ||
3715 (field_type->IsFloat() && insn_type.IsInteger()) ||
3716 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003717 // expected that read is of the correct primitive type or that int reads are reading
3718 // floats or long reads are reading doubles
3719 } else {
3720 // This is a global failure rather than a class change failure as the instructions and
3721 // the descriptors for the type should have been consistent within the same file at
3722 // compile time
3723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3724 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003725 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003726 return;
3727 }
3728 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003729 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003730 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3731 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003732 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003733 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003734 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003735 return;
3736 }
3737 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003738 if (!field_type->IsLowHalf()) {
3739 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003740 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003741 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003743}
3744
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003745void MethodVerifier::VerifyISPut(const Instruction* inst, RegType& insn_type,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003746 bool is_primitive, bool is_static) {
3747 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003748 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003749 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003750 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003751 } else {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003752 RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003753 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003754 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003755 RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003756 if (field != NULL) {
3757 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3758 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3759 << " from other class " << GetDeclaringClass();
3760 return;
3761 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003762 mirror::Class* field_type_class;
3763 {
3764 StackHandleScope<1> hs(Thread::Current());
3765 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3766 FieldHelper fh(h_field);
3767 field_type_class = fh.GetType(can_load_classes_);
3768 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003769 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003770 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003771 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003772 } else {
3773 Thread* self = Thread::Current();
3774 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3775 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003776 }
3777 }
3778 if (field_type == nullptr) {
3779 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3780 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003781 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003782 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003783 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003784 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003785 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003786 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003787 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003788 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003789 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3790 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003791 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003792 << "' in put-object";
3793 return;
3794 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003795 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 }
3797}
3798
Brian Carlstromea46f952013-07-30 01:26:50 -07003799mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003800 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003801 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3802 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3803 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3804 inst->Opcode() == Instruction::IPUT_QUICK ||
3805 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3806 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003807 RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003808 if (!object_type.HasClass()) {
3809 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3810 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003811 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003812 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003813 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3814 field_offset);
3815 if (f == nullptr) {
3816 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3817 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3818 }
3819 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003820}
3821
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003822void MethodVerifier::VerifyIGetQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003823 bool is_primitive) {
3824 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003825 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003826 if (field == NULL) {
3827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3828 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003829 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003830 mirror::Class* field_type_class;
3831 {
3832 StackHandleScope<1> hs(Thread::Current());
3833 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3834 FieldHelper fh(h_field);
3835 field_type_class = fh.GetType(can_load_classes_);
3836 }
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003837 RegType* field_type;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003838 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003839 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003840 field_type_class->CannotBeAssignedFromOtherTypes());
3841 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003842 Thread* self = Thread::Current();
3843 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3844 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003845 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003846 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003847 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003848 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003849 const uint32_t vregA = inst->VRegA_22c();
3850 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003851 if (field_type->Equals(insn_type) ||
3852 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3853 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003854 // expected that read is of the correct primitive type or that int reads are reading
3855 // floats or long reads are reading doubles
3856 } else {
3857 // This is a global failure rather than a class change failure as the instructions and
3858 // the descriptors for the type should have been consistent within the same file at
3859 // compile time
3860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003861 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003862 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003863 return;
3864 }
3865 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003866 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003867 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003868 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003869 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003870 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003871 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3872 return;
3873 }
3874 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003875 if (!field_type->IsLowHalf()) {
3876 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003877 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003878 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003879 }
3880}
3881
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003882void MethodVerifier::VerifyIPutQuick(const Instruction* inst, RegType& insn_type,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003883 bool is_primitive) {
3884 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003885 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003886 if (field == NULL) {
3887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3888 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003889 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003890 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003891 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003892 RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003893 if (field != NULL) {
3894 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3895 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003896 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003897 return;
3898 }
3899 }
3900 const uint32_t vregA = inst->VRegA_22c();
3901 if (is_primitive) {
3902 // Primitive field assignability rules are weaker than regular assignability rules
3903 bool instruction_compatible;
3904 bool value_compatible;
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07003905 RegType& value_type = work_line_->GetRegisterType(vregA);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003906 if (field_type.IsIntegralTypes()) {
3907 instruction_compatible = insn_type.IsIntegralTypes();
3908 value_compatible = value_type.IsIntegralTypes();
3909 } else if (field_type.IsFloat()) {
3910 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3911 value_compatible = value_type.IsFloatTypes();
3912 } else if (field_type.IsLong()) {
3913 instruction_compatible = insn_type.IsLong();
3914 value_compatible = value_type.IsLongTypes();
3915 } else if (field_type.IsDouble()) {
3916 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3917 value_compatible = value_type.IsDoubleTypes();
3918 } else {
3919 instruction_compatible = false; // reference field with primitive store
3920 value_compatible = false; // unused
3921 }
3922 if (!instruction_compatible) {
3923 // This is a global failure rather than a class change failure as the instructions and
3924 // the descriptors for the type should have been consistent within the same file at
3925 // compile time
3926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003927 << " to be of type '" << insn_type
3928 << "' but found type '" << field_type
3929 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003930 return;
3931 }
3932 if (!value_compatible) {
3933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3934 << " of type " << value_type
3935 << " but expected " << field_type
3936 << " for store to " << PrettyField(field) << " in put";
3937 return;
3938 }
3939 } else {
3940 if (!insn_type.IsAssignableFrom(field_type)) {
3941 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003942 << " to be compatible with type '" << insn_type
3943 << "' but found type '" << field_type
3944 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003945 return;
3946 }
3947 work_line_->VerifyRegisterType(vregA, field_type);
3948 }
3949}
3950
Ian Rogers776ac1f2012-04-13 23:36:36 -07003951bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003952 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003954 return false;
3955 }
3956 return true;
3957}
3958
Ian Rogersebbdd872014-07-07 23:53:08 -07003959bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3960 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003961 bool changed = true;
3962 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3963 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003964 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003965 * We haven't processed this instruction before, and we haven't touched the registers here, so
3966 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3967 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003968 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003969 if (!insn_flags_[next_insn].IsReturn()) {
3970 target_line->CopyFromLine(merge_line);
3971 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003972 // Verify that the monitor stack is empty on return.
3973 if (!merge_line->VerifyMonitorStackEmpty()) {
3974 return false;
3975 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003976 // For returns we only care about the operand to the return, all other registers are dead.
3977 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3978 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3979 Instruction::Code opcode = ret_inst->Opcode();
3980 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3981 target_line->MarkAllRegistersAsConflicts();
3982 } else {
3983 target_line->CopyFromLine(merge_line);
3984 if (opcode == Instruction::RETURN_WIDE) {
3985 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3986 } else {
3987 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3988 }
3989 }
3990 }
jeffhaobdb76512011-09-07 11:43:16 -07003991 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003992 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003993 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003994 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003995 if (gDebugVerify) {
3996 copy->CopyFromLine(target_line);
3997 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003998 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003999 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004000 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004001 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004002 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004003 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004004 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
4005 << *copy.get() << " MERGE\n"
4006 << *merge_line << " ==\n"
4007 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004008 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004009 if (update_merge_line && changed) {
4010 merge_line->CopyFromLine(target_line);
4011 }
jeffhaobdb76512011-09-07 11:43:16 -07004012 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 if (changed) {
4014 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004015 }
4016 return true;
4017}
4018
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004019InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004020 return &insn_flags_[work_insn_idx_];
4021}
4022
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004023RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004024 if (return_type_ == nullptr) {
4025 if (mirror_method_ != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004026 Thread* self = Thread::Current();
4027 StackHandleScope<1> hs(self);
4028 mirror::Class* return_type_class;
4029 {
4030 HandleWrapper<mirror::ArtMethod> h_mirror_method(hs.NewHandleWrapper(&mirror_method_));
4031 return_type_class = MethodHelper(h_mirror_method).GetReturnType(can_load_classes_);
4032 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004033 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004034 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4035 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004036 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004037 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004038 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004039 self->ClearException();
4040 }
4041 }
4042 if (return_type_ == nullptr) {
4043 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4044 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4045 uint16_t return_type_idx = proto_id.return_type_idx_;
4046 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004047 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004048 }
4049 }
4050 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004051}
4052
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004053RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004054 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004055 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004056 const char* descriptor
4057 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07004058 if (mirror_method_ != NULL) {
4059 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004060 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4061 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004062 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004063 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004064 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004065 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004066 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004067}
4068
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004069std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4070 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004071 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004072 std::vector<int32_t> result;
4073 for (size_t i = 0; i < line->NumRegs(); ++i) {
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004074 RegType& type = line->GetRegisterType(i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004075 if (type.IsConstant()) {
4076 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4077 result.push_back(type.ConstantValue());
4078 } else if (type.IsConstantLo()) {
4079 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4080 result.push_back(type.ConstantValueLo());
4081 } else if (type.IsConstantHi()) {
4082 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4083 result.push_back(type.ConstantValueHi());
4084 } else if (type.IsIntegralTypes()) {
4085 result.push_back(kIntVReg);
4086 result.push_back(0);
4087 } else if (type.IsFloat()) {
4088 result.push_back(kFloatVReg);
4089 result.push_back(0);
4090 } else if (type.IsLong()) {
4091 result.push_back(kLongLoVReg);
4092 result.push_back(0);
4093 result.push_back(kLongHiVReg);
4094 result.push_back(0);
4095 ++i;
4096 } else if (type.IsDouble()) {
4097 result.push_back(kDoubleLoVReg);
4098 result.push_back(0);
4099 result.push_back(kDoubleHiVReg);
4100 result.push_back(0);
4101 ++i;
4102 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4103 result.push_back(kUndefined);
4104 result.push_back(0);
4105 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004106 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004107 result.push_back(kReferenceVReg);
4108 result.push_back(0);
4109 }
4110 }
4111 return result;
4112}
4113
Hiroshi Yamauchi7da95862014-07-30 14:26:22 -07004114RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004115 if (precise) {
4116 // Precise constant type.
4117 return reg_types_.FromCat1Const(value, true);
4118 } else {
4119 // Imprecise constant type.
4120 if (value < -32768) {
4121 return reg_types_.IntConstant();
4122 } else if (value < -128) {
4123 return reg_types_.ShortConstant();
4124 } else if (value < 0) {
4125 return reg_types_.ByteConstant();
4126 } else if (value == 0) {
4127 return reg_types_.Zero();
4128 } else if (value == 1) {
4129 return reg_types_.One();
4130 } else if (value < 128) {
4131 return reg_types_.PosByteConstant();
4132 } else if (value < 32768) {
4133 return reg_types_.PosShortConstant();
4134 } else if (value < 65536) {
4135 return reg_types_.CharConstant();
4136 } else {
4137 return reg_types_.IntConstant();
4138 }
4139 }
4140}
4141
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004142void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004143 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004144}
4145
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004146void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004147 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004148}
jeffhaod1224c72012-02-29 13:43:08 -08004149
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004150void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4151 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004152}
4153
Ian Rogersd81871c2011-10-03 13:57:23 -07004154} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004155} // namespace art