blob: fbd710c2139890674bbc7db3e23862e8ce479e25 [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();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700100 if (super == NULL && "Ljava/lang/Object;" != klass->GetDescriptor()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800101 early_failure = true;
102 failure_message = " that has no super class";
103 } else if (super != NULL && super->IsFinal()) {
104 early_failure = true;
105 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
106 } else if (class_def == NULL) {
107 early_failure = true;
108 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
109 }
110 if (early_failure) {
111 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
112 if (Runtime::Current()->IsCompiler()) {
113 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000114 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800115 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700116 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700117 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -0700119 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700121 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700122}
123
Ian Rogers365c1022012-06-22 15:05:28 -0700124MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700125 Handle<mirror::DexCache> dex_cache,
126 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 const DexFile::ClassDef* class_def,
128 bool allow_soft_failures,
129 std::string* error) {
130 DCHECK(class_def != nullptr);
131 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700132 if (class_data == NULL) {
133 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700135 }
jeffhaof56197c2012-03-05 18:01:54 -0800136 ClassDataItemIterator it(*dex_file, class_data);
137 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
138 it.Next();
139 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700141 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700143 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800144 while (it.HasNextDirectMethod()) {
145 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700146 if (method_idx == previous_direct_method_idx) {
147 // smali can create dex files with two encoded_methods sharing the same method_idx
148 // http://code.google.com/p/smali/issues/detail?id=119
149 it.Next();
150 continue;
151 }
152 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700155 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
156 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157 if (method == NULL) {
158 DCHECK(Thread::Current()->IsExceptionPending());
159 // We couldn't resolve the method, but continue regardless.
160 Thread::Current()->ClearException();
161 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700162 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
163 dex_file,
164 dex_cache,
165 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700166 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700167 it.GetMethodCodeItem(),
168 method,
169 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700170 allow_soft_failures,
171 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700172 if (result != kNoFailure) {
173 if (result == kHardFailure) {
174 hard_fail = true;
175 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700178 *error = "Verifier rejected class ";
179 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
180 *error += " due to bad method ";
181 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800184 }
185 it.Next();
186 }
jeffhao9b0b1882012-10-01 16:51:22 -0700187 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800188 while (it.HasNextVirtualMethod()) {
189 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700190 if (method_idx == previous_virtual_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it.Next();
194 continue;
195 }
196 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700197 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700199 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
200 NullHandle<mirror::ArtMethod>(), type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700201 if (method == NULL) {
202 DCHECK(Thread::Current()->IsExceptionPending());
203 // We couldn't resolve the method, but continue regardless.
204 Thread::Current()->ClearException();
205 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700206 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
207 dex_file,
208 dex_cache,
209 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700211 it.GetMethodCodeItem(),
212 method,
213 it.GetMemberAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700214 allow_soft_failures,
215 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700216 if (result != kNoFailure) {
217 if (result == kHardFailure) {
218 hard_fail = true;
219 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700220 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700221 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700222 *error = "Verifier rejected class ";
223 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
224 *error += " due to bad method ";
225 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700226 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700227 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800228 }
229 it.Next();
230 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 if (error_count == 0) {
232 return kNoFailure;
233 } else {
234 return hard_fail ? kHardFailure : kSoftFailure;
235 }
jeffhaof56197c2012-03-05 18:01:54 -0800236}
237
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
239 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700240 Handle<mirror::DexCache> dex_cache,
241 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700242 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700244 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700245 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700246 bool allow_soft_failures,
247 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700248 MethodVerifier::FailureKind result = kNoFailure;
249 uint64_t start_ns = NanoTime();
250
Ian Rogers46960fe2014-05-23 10:43:43 -0700251 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
252 method_idx, method, method_access_flags, true, allow_soft_failures,
253 need_precise_constants);
254 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 // Verification completed, however failures may be pending that didn't cause the verification
256 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700257 CHECK(!verifier.have_pending_hard_failure_);
258 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700259 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700260 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700261 << PrettyMethod(method_idx, *dex_file) << "\n");
262 }
Ian Rogersc8982582012-09-07 16:53:25 -0700263 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800264 }
265 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700267 CHECK_NE(verifier.failures_.size(), 0U);
268 CHECK(verifier.have_pending_hard_failure_);
269 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700270 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800271 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700272 std::cout << "\n" << verifier.info_messages_.str();
273 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800274 }
Ian Rogersc8982582012-09-07 16:53:25 -0700275 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800276 }
Ian Rogersc8982582012-09-07 16:53:25 -0700277 uint64_t duration_ns = NanoTime() - start_ns;
Andreas Gampe073ed9b2014-06-13 15:46:46 -0700278 if (duration_ns > MsToNs(100) && !kIsDebugBuild) {
Ian Rogersc8982582012-09-07 16:53:25 -0700279 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
280 << " took " << PrettyDuration(duration_ns);
281 }
282 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800283}
284
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 const DexFile* dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700287 Handle<mirror::DexCache> dex_cache,
288 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700289 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700291 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800292 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700293 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Ian Rogers46960fe2014-05-23 10:43:43 -0700294 dex_method_idx, method, method_access_flags, true, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700295 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800296 verifier.DumpFailures(os);
297 os << verifier.info_messages_.str();
298 verifier.Dump(os);
299}
300
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301MethodVerifier::MethodVerifier(const DexFile* dex_file, Handle<mirror::DexCache>* dex_cache,
302 Handle<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700303 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700304 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
305 mirror::ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700306 bool can_load_classes, bool allow_soft_failures,
307 bool need_precise_constants)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800308 : reg_types_(can_load_classes),
309 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800310 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700311 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700312 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800314 dex_file_(dex_file),
315 dex_cache_(dex_cache),
316 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700317 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800318 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700319 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700320 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700321 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700323 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800324 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800325 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700326 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200327 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700328 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200329 has_check_casts_(false),
330 has_virtual_or_interface_invokes_(false) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800331 Runtime::Current()->AddMethodVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700332 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800333}
334
Mathieu Chartier590fee92013-09-13 13:46:47 -0700335MethodVerifier::~MethodVerifier() {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800336 Runtime::Current()->RemoveMethodVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700337 STLDeleteElements(&failure_messages_);
338}
339
Brian Carlstromea46f952013-07-30 01:26:50 -0700340void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700341 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
344 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
345 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
346 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 true, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700348 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700349 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700350 verifier.FindLocksAtDexPc();
351}
352
353void MethodVerifier::FindLocksAtDexPc() {
354 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700355 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700356
357 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
358 // verification. In practice, the phase we want relies on data structures set up by all the
359 // earlier passes, so we just run the full method verification and bail out early when we've
360 // got what we wanted.
361 Verify();
362}
363
Brian Carlstromea46f952013-07-30 01:26:50 -0700364mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700365 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700366 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700367 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
368 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
369 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
370 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700371 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200372 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200373}
374
Brian Carlstromea46f952013-07-30 01:26:50 -0700375mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700376 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377
378 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
379 // verification. In practice, the phase we want relies on data structures set up by all the
380 // earlier passes, so we just run the full method verification and bail out early when we've
381 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200382 bool success = Verify();
383 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700384 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200385 }
386 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
387 if (register_line == NULL) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700388 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200389 }
390 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
391 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392}
393
Brian Carlstromea46f952013-07-30 01:26:50 -0700394mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 uint32_t dex_pc) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700396 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700397 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
398 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
399 MethodVerifier verifier(m->GetDexFile(), &dex_cache, &class_loader, &m->GetClassDef(),
400 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
Ian Rogers46960fe2014-05-23 10:43:43 -0700401 true, false);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200402 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403}
404
Brian Carlstromea46f952013-07-30 01:26:50 -0700405mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700406 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200407
408 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
409 // verification. In practice, the phase we want relies on data structures set up by all the
410 // earlier passes, so we just run the full method verification and bail out early when we've
411 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200412 bool success = Verify();
413 if (!success) {
414 return NULL;
415 }
416 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
417 if (register_line == NULL) {
418 return NULL;
419 }
420 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
421 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
422 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423}
424
Ian Rogersad0b3a32012-04-16 14:50:24 -0700425bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 // If there aren't any instructions, make sure that's expected, then exit successfully.
427 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700428 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700430 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 } else {
432 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700433 }
jeffhaobdb76512011-09-07 11:43:16 -0700434 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
436 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
438 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700440 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700441 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800442 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700443 // Run through the instructions and see if the width checks out.
444 bool result = ComputeWidthsAndCountOps();
445 // Flag instructions guarded by a "try" block and check exception handlers.
446 result = result && ScanTryCatchBlocks();
447 // Perform static instruction verification.
448 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700449 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000450 result = result && VerifyCodeFlow();
451 // Compute information for compiler.
452 if (result && Runtime::Current()->IsCompiler()) {
453 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
454 }
455 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700456}
457
Ian Rogers776ac1f2012-04-13 23:36:36 -0700458std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700459 switch (error) {
460 case VERIFY_ERROR_NO_CLASS:
461 case VERIFY_ERROR_NO_FIELD:
462 case VERIFY_ERROR_NO_METHOD:
463 case VERIFY_ERROR_ACCESS_CLASS:
464 case VERIFY_ERROR_ACCESS_FIELD:
465 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700466 case VERIFY_ERROR_INSTANTIATION:
467 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800468 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700469 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
470 // class change and instantiation errors into soft verification errors so that we re-verify
471 // at runtime. We may fail to find or to agree on access because of not yet available class
472 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
473 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
474 // paths" that dynamically perform the verification and cause the behavior to be that akin
475 // to an interpreter.
476 error = VERIFY_ERROR_BAD_CLASS_SOFT;
477 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700478 // If we fail again at runtime, mark that this instruction would throw and force this
479 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700480 have_pending_runtime_throw_failure_ = true;
481 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700482 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 // Indication that verification should be retried at runtime.
484 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700485 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700486 have_pending_hard_failure_ = true;
487 }
488 break;
jeffhaod5347e02012-03-22 17:25:05 -0700489 // Hard verification failures at compile time will still fail at runtime, so the class is
490 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 case VERIFY_ERROR_BAD_CLASS_HARD: {
492 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700493 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000494 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800495 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700496 have_pending_hard_failure_ = true;
497 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800498 }
499 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700500 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800501 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700502 work_insn_idx_));
503 std::ostringstream* failure_message = new std::ostringstream(location);
504 failure_messages_.push_back(failure_message);
505 return *failure_message;
506}
507
Ian Rogers576ca0c2014-06-06 15:58:22 -0700508std::ostream& MethodVerifier::LogVerifyInfo() {
509 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
510 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
511}
512
Ian Rogersad0b3a32012-04-16 14:50:24 -0700513void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
514 size_t failure_num = failure_messages_.size();
515 DCHECK_NE(failure_num, 0U);
516 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
517 prepend += last_fail_message->str();
518 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
519 delete last_fail_message;
520}
521
522void MethodVerifier::AppendToLastFailMessage(std::string append) {
523 size_t failure_num = failure_messages_.size();
524 DCHECK_NE(failure_num, 0U);
525 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
526 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800527}
528
Ian Rogers776ac1f2012-04-13 23:36:36 -0700529bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700530 const uint16_t* insns = code_item_->insns_;
531 size_t insns_size = code_item_->insns_size_in_code_units_;
532 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700533 size_t new_instance_count = 0;
534 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700536
Ian Rogersd81871c2011-10-03 13:57:23 -0700537 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700538 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700539 switch (opcode) {
540 case Instruction::APUT_OBJECT:
541 case Instruction::CHECK_CAST:
542 has_check_casts_ = true;
543 break;
544 case Instruction::INVOKE_VIRTUAL:
545 case Instruction::INVOKE_VIRTUAL_RANGE:
546 case Instruction::INVOKE_INTERFACE:
547 case Instruction::INVOKE_INTERFACE_RANGE:
548 has_virtual_or_interface_invokes_ = true;
549 break;
550 case Instruction::MONITOR_ENTER:
551 monitor_enter_count++;
552 break;
553 case Instruction::NEW_INSTANCE:
554 new_instance_count++;
555 break;
556 default:
557 break;
jeffhaobdb76512011-09-07 11:43:16 -0700558 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 size_t inst_size = inst->SizeInCodeUnits();
560 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
561 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700562 inst = inst->Next();
563 }
564
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
567 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700568 return false;
569 }
570
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 new_instance_count_ = new_instance_count;
572 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700573 return true;
574}
575
Ian Rogers776ac1f2012-04-13 23:36:36 -0700576bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700577 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700578 if (tries_size == 0) {
579 return true;
580 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700582 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700583
584 for (uint32_t idx = 0; idx < tries_size; idx++) {
585 const DexFile::TryItem* try_item = &tries[idx];
586 uint32_t start = try_item->start_addr_;
587 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700588 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
590 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700591 return false;
592 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700594 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
595 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700596 return false;
597 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 for (uint32_t dex_pc = start; dex_pc < end;
599 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
600 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700601 }
602 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800603 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700604 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700605 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700606 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700607 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700608 CatchHandlerIterator iterator(handlers_ptr);
609 for (; iterator.HasNext(); iterator.Next()) {
610 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700611 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700612 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
613 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700614 return false;
615 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700617 // Ensure exception types are resolved so that they don't need resolution to be delivered,
618 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700619 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
621 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700622 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700623 if (exception_type == NULL) {
624 DCHECK(Thread::Current()->IsExceptionPending());
625 Thread::Current()->ClearException();
626 }
627 }
jeffhaobdb76512011-09-07 11:43:16 -0700628 }
Ian Rogers0571d352011-11-03 19:51:38 -0700629 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700630 }
jeffhaobdb76512011-09-07 11:43:16 -0700631 return true;
632}
633
Ian Rogers776ac1f2012-04-13 23:36:36 -0700634bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700636
Ian Rogers0c7abda2012-09-19 13:33:42 -0700637 /* 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 -0700638 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700639 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700640
641 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700642 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700644 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 return false;
646 }
647 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700648 // All invoke points are marked as "Throw" points already.
649 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000650 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700651 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000652 } else if (inst->IsReturn()) {
653 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 }
655 dex_pc += inst->SizeInCodeUnits();
656 inst = inst->Next();
657 }
658 return true;
659}
660
Ian Rogers776ac1f2012-04-13 23:36:36 -0700661bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 bool result = true;
663 switch (inst->GetVerifyTypeArgumentA()) {
664 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700665 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700668 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 }
671 switch (inst->GetVerifyTypeArgumentB()) {
672 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700673 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700676 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700679 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700682 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700685 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700688 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700691 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 break;
693 }
694 switch (inst->GetVerifyTypeArgumentC()) {
695 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700696 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700699 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 break;
701 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700702 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 break;
704 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700705 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 break;
707 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700708 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 break;
710 }
711 switch (inst->GetVerifyExtraFlags()) {
712 case Instruction::kVerifyArrayData:
713 result = result && CheckArrayData(code_offset);
714 break;
715 case Instruction::kVerifyBranchTarget:
716 result = result && CheckBranchTarget(code_offset);
717 break;
718 case Instruction::kVerifySwitchTargets:
719 result = result && CheckSwitchTargets(code_offset);
720 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700721 case Instruction::kVerifyVarArgNonZero:
722 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700723 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700724 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
726 "non-range invoke";
727 return false;
728 }
Ian Rogers29a26482014-05-02 15:27:29 -0700729 uint32_t args[Instruction::kMaxVarArgRegs];
730 inst->GetVarArgs(args);
731 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700733 }
Andreas Gampec3314312014-06-19 18:13:29 -0700734 case Instruction::kVerifyVarArgRangeNonZero:
735 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700737 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
738 inst->VRegA() <= 0) {
739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
740 "range invoke";
741 return false;
742 }
Ian Rogers29a26482014-05-02 15:27:29 -0700743 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 break;
745 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 result = false;
748 break;
749 }
Ian Rogers5fb22a92014-06-13 10:31:28 -0700750 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsCompiler()) {
751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
752 result = false;
753 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 return result;
755}
756
Ian Rogers776ac1f2012-04-13 23:36:36 -0700757bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
760 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 return true;
764}
765
Ian Rogers776ac1f2012-04-13 23:36:36 -0700766bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
769 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 }
772 return true;
773}
774
Ian Rogers776ac1f2012-04-13 23:36:36 -0700775bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
778 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return false;
780 }
781 return true;
782}
783
Ian Rogers776ac1f2012-04-13 23:36:36 -0700784bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700785 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
787 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
796 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700800 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700801 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 return false;
804 }
805 return true;
806}
807
Ian Rogers776ac1f2012-04-13 23:36:36 -0700808bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
811 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return false;
813 }
814 return true;
815}
816
Ian Rogers776ac1f2012-04-13 23:36:36 -0700817bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700818 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
820 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 return false;
822 }
823 return true;
824}
825
Ian Rogers776ac1f2012-04-13 23:36:36 -0700826bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
829 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
832 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700833 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 const char* cp = descriptor;
835 while (*cp++ == '[') {
836 bracket_count++;
837 }
838 if (bracket_count == 0) {
839 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700840 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
841 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 return false;
843 } else if (bracket_count > 255) {
844 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700845 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
846 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 return false;
848 }
849 return true;
850}
851
Ian Rogers776ac1f2012-04-13 23:36:36 -0700852bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
854 const uint16_t* insns = code_item_->insns_ + cur_offset;
855 const uint16_t* array_data;
856 int32_t array_data_offset;
857
858 DCHECK_LT(cur_offset, insn_count);
859 /* make sure the start of the array data table is in range */
860 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
861 if ((int32_t) cur_offset + array_data_offset < 0 ||
862 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700864 << ", data offset " << array_data_offset
865 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 return false;
867 }
868 /* offset to array data table is a relative branch-style offset */
869 array_data = insns + array_data_offset;
870 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800871 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
873 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 return false;
875 }
876 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700877 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
879 /* make sure the end of the switch is in range */
880 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
882 << ", data offset " << array_data_offset << ", end "
883 << cur_offset + array_data_offset + table_size
884 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 return false;
886 }
887 return true;
888}
889
Ian Rogers776ac1f2012-04-13 23:36:36 -0700890bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 int32_t offset;
892 bool isConditional, selfOkay;
893 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
894 return false;
895 }
896 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
898 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 return false;
900 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700901 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
902 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
905 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 return false;
907 }
908 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
909 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700910 if (abs_offset < 0 ||
911 (uint32_t) abs_offset >= insn_count ||
912 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700914 << reinterpret_cast<void*>(abs_offset) << ") at "
915 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 return false;
917 }
918 insn_flags_[abs_offset].SetBranchTarget();
919 return true;
920}
921
Ian Rogers776ac1f2012-04-13 23:36:36 -0700922bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 bool* selfOkay) {
924 const uint16_t* insns = code_item_->insns_ + cur_offset;
925 *pConditional = false;
926 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 switch (*insns & 0xff) {
928 case Instruction::GOTO:
929 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 break;
931 case Instruction::GOTO_32:
932 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700933 *selfOkay = true;
934 break;
935 case Instruction::GOTO_16:
936 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 break;
938 case Instruction::IF_EQ:
939 case Instruction::IF_NE:
940 case Instruction::IF_LT:
941 case Instruction::IF_GE:
942 case Instruction::IF_GT:
943 case Instruction::IF_LE:
944 case Instruction::IF_EQZ:
945 case Instruction::IF_NEZ:
946 case Instruction::IF_LTZ:
947 case Instruction::IF_GEZ:
948 case Instruction::IF_GTZ:
949 case Instruction::IF_LEZ:
950 *pOffset = (int16_t) insns[1];
951 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 break;
953 default:
954 return false;
955 break;
956 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700957 return true;
958}
959
Ian Rogers776ac1f2012-04-13 23:36:36 -0700960bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700962 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
966 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700968 << ", switch offset " << switch_offset
969 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 return false;
971 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700974 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -0800975 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
977 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700978 return false;
979 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 uint32_t switch_count = switch_insns[1];
981 int32_t keys_offset, targets_offset;
982 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700983 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
984 /* 0=sig, 1=count, 2/3=firstKey */
985 targets_offset = 4;
986 keys_offset = -1;
987 expected_signature = Instruction::kPackedSwitchSignature;
988 } else {
989 /* 0=sig, 1=count, 2..count*2 = keys */
990 keys_offset = 2;
991 targets_offset = 2 + 2 * switch_count;
992 expected_signature = Instruction::kSparseSwitchSignature;
993 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700996 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
997 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
998 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700999 return false;
1000 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001001 /* make sure the end of the switch is in range */
1002 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1004 << ", switch offset " << switch_offset
1005 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001006 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 return false;
1008 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 /* for a sparse switch, verify the keys are in ascending order */
1010 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1012 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001013 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1014 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1015 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1017 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001018 return false;
1019 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001020 last_key = key;
1021 }
1022 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001023 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 for (uint32_t targ = 0; targ < switch_count; targ++) {
1025 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1026 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1027 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001028 if (abs_offset < 0 ||
1029 abs_offset >= (int32_t) insn_count ||
1030 !insn_flags_[abs_offset].IsOpcode()) {
1031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1032 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1033 << reinterpret_cast<void*>(cur_offset)
1034 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001035 return false;
1036 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 insn_flags_[abs_offset].SetBranchTarget();
1038 }
1039 return true;
1040}
1041
Ian Rogers776ac1f2012-04-13 23:36:36 -07001042bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001043 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 return false;
1046 }
1047 uint16_t registers_size = code_item_->registers_size_;
1048 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001049 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1051 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
1053 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001054 }
1055
1056 return true;
1057}
1058
Ian Rogers776ac1f2012-04-13 23:36:36 -07001059bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 uint16_t registers_size = code_item_->registers_size_;
1061 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1062 // integer overflow when adding them here.
1063 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1065 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001066 return false;
1067 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001068 return true;
1069}
1070
Ian Rogers776ac1f2012-04-13 23:36:36 -07001071bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 uint16_t registers_size = code_item_->registers_size_;
1073 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001074
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001076 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1077 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 }
1079 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001080 reg_table_.Init(kTrackCompilerInterestPoints,
1081 insn_flags_.get(),
1082 insns_size,
1083 registers_size,
1084 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001085
jeffhaobdb76512011-09-07 11:43:16 -07001086
Ian Rogersd0fbd852013-09-24 18:17:04 -07001087 work_line_.reset(RegisterLine::Create(registers_size, this));
1088 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001089
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 /* Initialize register types of method arguments. */
1091 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001092 DCHECK_NE(failures_.size(), 0U);
1093 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001094 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001095 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 return false;
1097 }
1098 /* Perform code flow verification. */
1099 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
jeffhaobdb76512011-09-07 11:43:16 -07001103 return true;
1104}
1105
Ian Rogersad0b3a32012-04-16 14:50:24 -07001106std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1107 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001108 for (size_t i = 0; i < failures_.size(); ++i) {
1109 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001110 }
1111 return os;
1112}
1113
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001116 v->Dump(std::cerr);
1117}
1118
Ian Rogers776ac1f2012-04-13 23:36:36 -07001119void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001120 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001121 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 return;
jeffhaobdb76512011-09-07 11:43:16 -07001123 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001124 {
1125 os << "Register Types:\n";
1126 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1127 std::ostream indent_os(&indent_filter);
1128 reg_types_.Dump(indent_os);
1129 }
Ian Rogersb4903572012-10-11 11:52:56 -07001130 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001131 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1132 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 const Instruction* inst = Instruction::At(code_item_->insns_);
1134 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1135 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1137 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001138 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001139 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001140 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001141 const bool kDumpHexOfInstruction = false;
1142 if (kDumpHexOfInstruction) {
1143 indent_os << inst->DumpHex(5) << " ";
1144 }
1145 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 inst = inst->Next();
1147 }
jeffhaobdb76512011-09-07 11:43:16 -07001148}
1149
Ian Rogersd81871c2011-10-03 13:57:23 -07001150static bool IsPrimitiveDescriptor(char descriptor) {
1151 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001152 case 'I':
1153 case 'C':
1154 case 'S':
1155 case 'B':
1156 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001157 case 'F':
1158 case 'D':
1159 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001161 default:
1162 return false;
1163 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001164}
1165
Ian Rogers776ac1f2012-04-13 23:36:36 -07001166bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 RegisterLine* reg_line = reg_table_.GetLine(0);
1168 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1169 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001170
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001172 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001174 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1176 // argument as uninitialized. This restricts field access until the superclass constructor is
1177 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001178 const RegType& declaring_class = GetDeclaringClass();
1179 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 reg_line->SetRegisterType(arg_start + cur_arg,
1181 reg_types_.UninitializedThisArgument(declaring_class));
1182 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001183 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001184 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001186 }
1187
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001188 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001189 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001190 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001191
1192 for (; iterator.HasNext(); iterator.Next()) {
1193 const char* descriptor = iterator.GetDescriptor();
1194 if (descriptor == NULL) {
1195 LOG(FATAL) << "Null descriptor";
1196 }
1197 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1199 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 return false;
1201 }
1202 switch (descriptor[0]) {
1203 case 'L':
1204 case '[':
1205 // We assume that reference arguments are initialized. The only way it could be otherwise
1206 // (assuming the caller was verified) is if the current method is <init>, but in that case
1207 // it's effectively considered initialized the instant we reach here (in the sense that we
1208 // can return without doing anything or call virtual methods).
1209 {
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001210 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
1211 if (!reg_type.IsNonZeroReferenceTypes()) {
1212 DCHECK(HasFailures());
1213 return false;
1214 }
Ian Rogers84fa0742011-10-25 18:13:30 -07001215 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 }
1217 break;
1218 case 'Z':
1219 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1220 break;
1221 case 'C':
1222 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1223 break;
1224 case 'B':
1225 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1226 break;
1227 case 'I':
1228 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1229 break;
1230 case 'S':
1231 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1232 break;
1233 case 'F':
1234 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1235 break;
1236 case 'J':
1237 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001238 if (cur_arg + 1 >= expected_args) {
1239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1240 << " args, found more (" << descriptor << ")";
1241 return false;
1242 }
1243
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001244 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1245 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1246 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 cur_arg++;
1248 break;
1249 }
1250 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1252 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
1255 cur_arg++;
1256 }
1257 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1259 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 return false;
1261 }
1262 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1263 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1264 // format. Only major difference from the method argument format is that 'V' is supported.
1265 bool result;
1266 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1267 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001268 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 size_t i = 0;
1270 do {
1271 i++;
1272 } while (descriptor[i] == '['); // process leading [
1273 if (descriptor[i] == 'L') { // object array
1274 do {
1275 i++; // find closing ;
1276 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1277 result = descriptor[i] == ';';
1278 } else { // primitive array
1279 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1280 }
1281 } else if (descriptor[0] == 'L') {
1282 // could be more thorough here, but shouldn't be required
1283 size_t i = 0;
1284 do {
1285 i++;
1286 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1287 result = descriptor[i] == ';';
1288 } else {
1289 result = false;
1290 }
1291 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1293 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 }
1295 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001296}
1297
Ian Rogers776ac1f2012-04-13 23:36:36 -07001298bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 const uint16_t* insns = code_item_->insns_;
1300 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001301
jeffhaobdb76512011-09-07 11:43:16 -07001302 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 insn_flags_[0].SetChanged();
1304 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001305
jeffhaobdb76512011-09-07 11:43:16 -07001306 /* Continue until no instructions are marked "changed". */
1307 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1309 uint32_t insn_idx = start_guess;
1310 for (; insn_idx < insns_size; insn_idx++) {
1311 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001312 break;
1313 }
jeffhaobdb76512011-09-07 11:43:16 -07001314 if (insn_idx == insns_size) {
1315 if (start_guess != 0) {
1316 /* try again, starting from the top */
1317 start_guess = 0;
1318 continue;
1319 } else {
1320 /* all flags are clear */
1321 break;
1322 }
1323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 // We carry the working set of registers from instruction to instruction. If this address can
1325 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1326 // "changed" flags, we need to load the set of registers from the table.
1327 // Because we always prefer to continue on to the next instruction, we should never have a
1328 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1329 // target.
1330 work_insn_idx_ = insn_idx;
1331 if (insn_flags_[insn_idx].IsBranchTarget()) {
1332 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001333 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001334 /*
1335 * Sanity check: retrieve the stored register line (assuming
1336 * a full table) and make sure it actually matches.
1337 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1339 if (register_line != NULL) {
1340 if (work_line_->CompareLine(register_line) != 0) {
1341 Dump(std::cout);
1342 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001344 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1345 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001346 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001347 }
jeffhaobdb76512011-09-07 11:43:16 -07001348 }
jeffhaobdb76512011-09-07 11:43:16 -07001349 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001352 prepend += " failed to verify: ";
1353 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 return false;
1355 }
jeffhaobdb76512011-09-07 11:43:16 -07001356 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 insn_flags_[insn_idx].SetVisited();
1358 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001359 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001360
Ian Rogers1c849e52012-06-28 14:00:33 -07001361 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001362 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001363 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001364 * (besides the wasted space), but it indicates a flaw somewhere
1365 * down the line, possibly in the verifier.
1366 *
1367 * If we've substituted "always throw" instructions into the stream,
1368 * we are almost certainly going to have some dead code.
1369 */
1370 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 uint32_t insn_idx = 0;
1372 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001373 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001374 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001375 * may or may not be preceded by a padding NOP (for alignment).
1376 */
1377 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1378 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1379 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001380 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001381 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1382 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1383 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001385 }
1386
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001388 if (dead_start < 0)
1389 dead_start = insn_idx;
1390 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001391 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1392 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001393 dead_start = -1;
1394 }
1395 }
1396 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001397 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1398 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001400 // To dump the state of the verify after a method, do something like:
1401 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1402 // "boolean java.lang.String.equals(java.lang.Object)") {
1403 // LOG(INFO) << info_messages_.str();
1404 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001405 }
jeffhaobdb76512011-09-07 11:43:16 -07001406 return true;
1407}
1408
Ian Rogers776ac1f2012-04-13 23:36:36 -07001409bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001410 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1411 // We want the state _before_ the instruction, for the case where the dex pc we're
1412 // interested in is itself a monitor-enter instruction (which is a likely place
1413 // for a thread to be suspended).
1414 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001415 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001416 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1417 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1418 }
1419 }
1420
jeffhaobdb76512011-09-07 11:43:16 -07001421 /*
1422 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001423 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001424 * control to another statement:
1425 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001426 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001427 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001428 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001429 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001430 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001431 * throw an exception that is handled by an encompassing "try"
1432 * block.
1433 *
1434 * We can also return, in which case there is no successor instruction
1435 * from this point.
1436 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001437 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001438 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1440 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001441 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001442
jeffhaobdb76512011-09-07 11:43:16 -07001443 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001444 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001445 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001447 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1448 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 }
jeffhaobdb76512011-09-07 11:43:16 -07001450
1451 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001452 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001453 * can throw an exception, we will copy/merge this into the "catch"
1454 * address rather than work_line, because we don't want the result
1455 * from the "successful" code path (e.g. a check-cast that "improves"
1456 * a type) to be visible to the exception handler.
1457 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001458 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001460 } else {
1461#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001463#endif
1464 }
1465
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001466
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001467 // We need to ensure the work line is consistent while performing validation. When we spot a
1468 // peephole pattern we compute a new line for either the fallthrough instruction or the
1469 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001470 std::unique_ptr<RegisterLine> branch_line;
1471 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001472
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::NOP:
1475 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001476 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001477 * a signature that looks like a NOP; if we see one of these in
1478 * the course of executing code then we have a problem.
1479 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001482 }
1483 break;
1484
1485 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001486 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1487 break;
jeffhaobdb76512011-09-07 11:43:16 -07001488 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1490 break;
jeffhaobdb76512011-09-07 11:43:16 -07001491 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001493 break;
1494 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1496 break;
jeffhaobdb76512011-09-07 11:43:16 -07001497 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1499 break;
jeffhaobdb76512011-09-07 11:43:16 -07001500 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001501 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001502 break;
1503 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001504 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1505 break;
jeffhaobdb76512011-09-07 11:43:16 -07001506 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001507 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1508 break;
jeffhaobdb76512011-09-07 11:43:16 -07001509 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001510 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001511 break;
1512
1513 /*
1514 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001515 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001516 * might want to hold the result in an actual CPU register, so the
1517 * Dalvik spec requires that these only appear immediately after an
1518 * invoke or filled-new-array.
1519 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001520 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001521 * redundant with the reset done below, but it can make the debug info
1522 * easier to read in some cases.)
1523 */
1524 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001525 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001526 break;
1527 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001529 break;
1530 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001532 break;
1533
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001535 /*
jeffhao60f83e32012-02-13 17:16:30 -08001536 * This statement can only appear as the first instruction in an exception handler. We verify
1537 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001538 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001539 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001541 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001544 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1545 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 }
jeffhaobdb76512011-09-07 11:43:16 -07001548 }
1549 break;
1550 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001551 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001552 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 const RegType& return_type = GetMethodReturnType();
1554 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1556 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 } else {
1558 // Compilers may generate synthetic functions that write byte values into boolean fields.
1559 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 const uint32_t vregA = inst->VRegA_11x();
1561 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1563 ((return_type.IsBoolean() || return_type.IsByte() ||
1564 return_type.IsShort() || return_type.IsChar()) &&
1565 src_type.IsInteger()));
1566 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001567 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001569 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 }
jeffhaobdb76512011-09-07 11:43:16 -07001572 }
1573 }
1574 break;
1575 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001576 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001577 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 const RegType& return_type = GetMethodReturnType();
1579 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 } else {
1582 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001583 const uint32_t vregA = inst->VRegA_11x();
1584 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
jeffhaobdb76512011-09-07 11:43:16 -07001588 }
1589 }
1590 break;
1591 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001592 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 const RegType& return_type = GetMethodReturnType();
1594 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 } else {
1597 /* return_type is the *expected* return type, not register value */
1598 DCHECK(!return_type.IsZero());
1599 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001600 const uint32_t vregA = inst->VRegA_11x();
1601 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001602 // Disallow returning uninitialized values and verify that the reference in vAA is an
1603 // instance of the "return_type"
1604 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001605 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1606 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001607 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001608 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1609 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1610 << "' or '" << reg_type << "'";
1611 } else {
1612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1613 << "', but expected from declaration '" << return_type << "'";
1614 }
jeffhaobdb76512011-09-07 11:43:16 -07001615 }
1616 }
1617 }
1618 break;
1619
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001620 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 case Instruction::CONST_4: {
1622 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001623 work_line_->SetRegisterType(inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001624 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001625 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 }
1627 case Instruction::CONST_16: {
1628 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Sebastien Hertz849600b2013-12-20 10:28:08 +01001629 work_line_->SetRegisterType(inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001630 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001631 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001633 case Instruction::CONST: {
1634 int32_t val = inst->VRegB_31i();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 work_line_->SetRegisterType(inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001636 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001637 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001638 }
1639 case Instruction::CONST_HIGH16: {
1640 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 work_line_->SetRegisterType(inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001642 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001644 }
jeffhaobdb76512011-09-07 11:43:16 -07001645 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001648 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1649 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001651 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 }
1653 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1656 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 break;
1659 }
1660 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001662 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1663 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001665 break;
1666 }
1667 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001668 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001669 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1670 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 break;
1673 }
jeffhaobdb76512011-09-07 11:43:16 -07001674 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1676 break;
jeffhaobdb76512011-09-07 11:43:16 -07001677 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001679 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001681 // Get type from instruction if unresolved then we need an access check
1682 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001683 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001684 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001685 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001686 res_type.IsConflict() ? res_type
1687 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
1693 case Instruction::MONITOR_EXIT:
1694 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001695 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001696 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001697 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001698 * to the need to handle asynchronous exceptions, a now-deprecated
1699 * feature that Dalvik doesn't support.)
1700 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001701 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001702 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001703 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001704 * structured locking checks are working, the former would have
1705 * failed on the -enter instruction, and the latter is impossible.
1706 *
1707 * This is fortunate, because issue 3221411 prevents us from
1708 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001709 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001710 * some catch blocks (which will show up as "dead" code when
1711 * we skip them here); if we can't, then the code path could be
1712 * "live" so we still need to check it.
1713 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001716 break;
1717
Ian Rogers28ad40d2011-10-27 15:19:26 -07001718 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001720 /*
1721 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1722 * could be a "upcast" -- not expected, so we don't try to address it.)
1723 *
1724 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001726 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1728 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1729 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001730 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001731 // If this is a primitive type, fail HARD.
1732 mirror::Class* klass = (*dex_cache_)->GetResolvedType(type_idx);
1733 if (klass != nullptr && klass->IsPrimitive()) {
1734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1735 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1736 << GetDeclaringClass();
1737 break;
1738 }
1739
Ian Rogersad0b3a32012-04-16 14:50:24 -07001740 DCHECK_NE(failures_.size(), 0U);
1741 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001743 }
1744 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001745 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001746 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001747 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1748 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001749 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001750 if (is_checkcast) {
1751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1752 } else {
1753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1754 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001755 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001756 if (is_checkcast) {
1757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1758 } else {
1759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1760 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001761 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001762 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001766 }
jeffhaobdb76512011-09-07 11:43:16 -07001767 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001768 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 }
1770 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001771 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001772 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001773 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001775 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 }
1778 }
1779 break;
1780 }
1781 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001782 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001783 if (res_type.IsConflict()) {
1784 DCHECK_NE(failures_.size(), 0U);
1785 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001786 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001787 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1788 // can't create an instance of an interface or abstract class */
1789 if (!res_type.IsInstantiableTypes()) {
1790 Fail(VERIFY_ERROR_INSTANTIATION)
1791 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001792 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001793 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001794 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1795 // Any registers holding previous allocations from this address that have not yet been
1796 // initialized must be marked invalid.
1797 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1798 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 break;
1801 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001802 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001804 break;
1805 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001807 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001808 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001809 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001811 just_set_result = true; // Filled new array range sets result register
1812 break;
jeffhaobdb76512011-09-07 11:43:16 -07001813 case Instruction::CMPL_FLOAT:
1814 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001816 break;
1817 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001819 break;
1820 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001821 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823 case Instruction::CMPL_DOUBLE:
1824 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001826 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001827 break;
1828 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001830 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001831 break;
1832 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001833 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001834 break;
1835 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001837 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001838 break;
1839 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001840 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001842 break;
1843 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001847 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001848 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001849 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1850 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001851 }
1852 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 case Instruction::GOTO:
1855 case Instruction::GOTO_16:
1856 case Instruction::GOTO_32:
1857 /* no effect on or use of registers */
1858 break;
1859
1860 case Instruction::PACKED_SWITCH:
1861 case Instruction::SPARSE_SWITCH:
1862 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001863 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 case Instruction::FILL_ARRAY_DATA: {
1867 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001868 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001869 /* array_type can be null if the reg type is Zero */
1870 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001871 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1873 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001874 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001875 const RegType& component_type = reg_types_.GetComponentType(array_type,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001876 class_loader_->Get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001877 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001878 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1880 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 } else {
jeffhao457cc512012-02-02 16:55:13 -08001882 // Now verify if the element width in the table matches the element width declared in
1883 // the array
1884 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1885 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001887 } else {
1888 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1889 // Since we don't compress the data in Dex, expect to see equal width of data stored
1890 // in the table and expected from the array class.
1891 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1893 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001894 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 }
1896 }
jeffhaobdb76512011-09-07 11:43:16 -07001897 }
1898 }
1899 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 }
jeffhaobdb76512011-09-07 11:43:16 -07001901 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1904 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 bool mismatch = false;
1906 if (reg_type1.IsZero()) { // zero then integral or reference expected
1907 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1908 } else if (reg_type1.IsReferenceTypes()) { // both references?
1909 mismatch = !reg_type2.IsReferenceTypes();
1910 } else { // both integral?
1911 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1912 }
1913 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1915 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001916 }
1917 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001918 }
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::IF_LT:
1920 case Instruction::IF_GE:
1921 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001923 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1924 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001925 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1927 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001928 }
1929 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001932 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1936 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001937 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001938
1939 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001940 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001941 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001942 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001943 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001944 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001945 }
Ian Rogers9b360392013-06-06 14:45:07 -07001946 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001947 } else {
1948 break;
1949 }
1950
Ian Rogers9b360392013-06-06 14:45:07 -07001951 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001952
1953 /* Check for peep-hole pattern of:
1954 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001955 * instance-of vX, vY, T;
1956 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001957 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001958 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001959 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001960 * and sharpen the type of vY to be type T.
1961 * Note, this pattern can't be if:
1962 * - if there are other branches to this branch,
1963 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001964 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001965 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001966 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1967 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1968 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07001969 // Check the type of the instance-of is different than that of registers type, as if they
1970 // are the same there is no work to be done here. Check that the conversion is not to or
1971 // from an unresolved type as type information is imprecise. If the instance-of is to an
1972 // interface then ignore the type information as interfaces can only be treated as Objects
1973 // and we don't want to disallow field and other operations on the object. If the value
1974 // being instance-of checked against is known null (zero) then allow the optimization as
1975 // we didn't have type information. If the merge of the instance-of type with the original
1976 // type is assignable to the original then allow optimization. This check is performed to
1977 // ensure that subsequent merges don't lose type information - such as becoming an
1978 // interface from a class that would lose information relevant to field checks.
Jeff Haoc642ec82013-09-04 16:11:55 -07001979 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001980 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001981
Ian Rogersebbdd872014-07-07 23:53:08 -07001982 if (!orig_type.Equals(cast_type) &&
1983 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07001984 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07001985 !cast_type.GetClass()->IsInterface() &&
1986 (orig_type.IsZero() ||
1987 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001988 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001989 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001990 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001991 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001992 branch_line.reset(update_line);
1993 }
1994 update_line->CopyFromLine(work_line_.get());
1995 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1996 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1997 // See if instance-of was preceded by a move-object operation, common due to the small
1998 // register encoding space of instance-of, and propagate type information to the source
1999 // of the move-object.
2000 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002001 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002002 move_idx--;
2003 }
2004 CHECK(insn_flags_[move_idx].IsOpcode());
2005 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2006 switch (move_inst->Opcode()) {
2007 case Instruction::MOVE_OBJECT:
2008 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
2009 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
2010 }
2011 break;
2012 case Instruction::MOVE_OBJECT_FROM16:
2013 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
2014 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
2015 }
2016 break;
2017 case Instruction::MOVE_OBJECT_16:
2018 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2019 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2020 }
2021 break;
2022 default:
2023 break;
2024 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002025 }
2026 }
2027 }
2028
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 }
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::IF_LTZ:
2032 case Instruction::IF_GEZ:
2033 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2038 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 }
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 break;
jeffhaobdb76512011-09-07 11:43:16 -07002045 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 break;
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 break;
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
2060 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
2067 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 break;
2070 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 break;
2073 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
2076 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002081 break;
2082 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002084 break;
2085
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 break;
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
jeffhaobdb76512011-09-07 11:43:16 -07002092 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 break;
2098 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002100 break;
2101 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
2104 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
jeffhaobdb76512011-09-07 11:43:16 -07002107
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 break;
2114 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 break;
2117 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
2123 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002125 break;
jeffhaobdb76512011-09-07 11:43:16 -07002126 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
2129
jeffhaobdb76512011-09-07 11:43:16 -07002130 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002132 break;
jeffhaobdb76512011-09-07 11:43:16 -07002133 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002135 break;
jeffhaobdb76512011-09-07 11:43:16 -07002136 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 break;
jeffhaobdb76512011-09-07 11:43:16 -07002139 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 break;
2142 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002147 break;
2148 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 break;
2151
2152 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002153 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002154 break;
2155 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002156 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002157 break;
2158 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 break;
2161 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
2164 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002166 break;
2167 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173
2174 case Instruction::INVOKE_VIRTUAL:
2175 case Instruction::INVOKE_VIRTUAL_RANGE:
2176 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002177 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2179 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002180 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2181 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002182 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2183 is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002184 const RegType* return_type = nullptr;
2185 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002186 Thread* self = Thread::Current();
2187 StackHandleScope<1> hs(self);
2188 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
2189 MethodHelper mh(h_called_method);
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002190 mirror::Class* return_type_class = mh.GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002191 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002192 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2193 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002194 return_type_class->CannotBeAssignedFromOtherTypes());
2195 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08002196 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002197 self->ClearException();
2198 }
2199 }
2200 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002202 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2203 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002204 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002205 return_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002206 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002207 if (!return_type->IsLowHalf()) {
2208 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002210 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 }
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002218 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002219 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002220 const char* return_type_descriptor;
2221 bool is_constructor;
2222 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002224 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002225 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002226 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2227 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2228 } else {
2229 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002230 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers46685432012-06-03 22:26:43 -07002231 }
2232 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002233 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 * Some additional checks when calling a constructor. We know from the invocation arg check
2235 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2236 * that to require that called_method->klass is the same as this->klass or this->super,
2237 * allowing the latter only if the "this" argument is the same as the "this" argument to
2238 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002239 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002241 if (this_type.IsConflict()) // failure.
2242 break;
jeffhaobdb76512011-09-07 11:43:16 -07002243
jeffhaob57e9522012-04-26 18:08:21 -07002244 /* no null refs allowed (?) */
2245 if (this_type.IsZero()) {
2246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2247 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002248 }
jeffhaob57e9522012-04-26 18:08:21 -07002249
2250 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002251 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2252 // TODO: re-enable constructor type verification
2253 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002254 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002255 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2256 // break;
2257 // }
jeffhaob57e9522012-04-26 18:08:21 -07002258
2259 /* arg must be an uninitialized reference */
2260 if (!this_type.IsUninitializedTypes()) {
2261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2262 << this_type;
2263 break;
2264 }
2265
2266 /*
2267 * Replace the uninitialized reference with an initialized one. We need to do this for all
2268 * registers that have the same object instance in them, not just the "this" register.
2269 */
2270 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002272 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(),
Mathieu Chartier590fee92013-09-13 13:46:47 -07002273 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 if (!return_type.IsLowHalf()) {
2275 work_line_->SetResultRegisterType(return_type);
2276 } else {
2277 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2278 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002279 just_set_result = true;
2280 break;
2281 }
2282 case Instruction::INVOKE_STATIC:
2283 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002285 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002286 METHOD_STATIC,
2287 is_range,
2288 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002289 const char* descriptor;
2290 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002292 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2293 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002294 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002295 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002296 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002297 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002298 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
2299 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 if (!return_type.IsLowHalf()) {
2301 work_line_->SetResultRegisterType(return_type);
2302 } else {
2303 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2304 }
jeffhaobdb76512011-09-07 11:43:16 -07002305 just_set_result = true;
2306 }
2307 break;
jeffhaobdb76512011-09-07 11:43:16 -07002308 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002311 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002312 METHOD_INTERFACE,
2313 is_range,
2314 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002315 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002316 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002317 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2318 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2319 << PrettyMethod(abs_method) << "'";
2320 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002321 }
Ian Rogers0d604842012-04-16 14:50:24 -07002322 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002323 /* Get the type of the "this" arg, which should either be a sub-interface of called
2324 * interface or Object (see comments in RegType::JoinClass).
2325 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002327 if (this_type.IsZero()) {
2328 /* null pointer always passes (and always fails at runtime) */
2329 } else {
2330 if (this_type.IsUninitializedTypes()) {
2331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2332 << this_type;
2333 break;
2334 }
2335 // In the past we have tried to assert that "called_interface" is assignable
2336 // from "this_type.GetClass()", however, as we do an imprecise Join
2337 // (RegType::JoinClass) we don't have full information on what interfaces are
2338 // implemented by "this_type". For example, two classes may implement the same
2339 // interfaces and have a common parent that doesn't implement the interface. The
2340 // join will set "this_type" to the parent class and a test that this implements
2341 // the interface will incorrectly fail.
2342 }
2343 /*
2344 * We don't have an object instance, so we can't find the concrete method. However, all of
2345 * the type information is in the abstract method, so we're good.
2346 */
2347 const char* descriptor;
2348 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002350 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2351 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2352 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2353 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002354 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002355 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002356 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002357 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 if (!return_type.IsLowHalf()) {
2359 work_line_->SetResultRegisterType(return_type);
2360 } else {
2361 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2362 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002363 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002365 }
jeffhaobdb76512011-09-07 11:43:16 -07002366 case Instruction::NEG_INT:
2367 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::NEG_LONG:
2371 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002372 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002373 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002374 break;
2375 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002377 break;
2378 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002388 break;
2389 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002403 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002413 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002414 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
2416 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002418 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002421 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002422 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002425 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002426 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002427 break;
2428 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002429 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002432 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437
2438 case Instruction::ADD_INT:
2439 case Instruction::SUB_INT:
2440 case Instruction::MUL_INT:
2441 case Instruction::REM_INT:
2442 case Instruction::DIV_INT:
2443 case Instruction::SHL_INT:
2444 case Instruction::SHR_INT:
2445 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002447 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::AND_INT:
2450 case Instruction::OR_INT:
2451 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002452 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002453 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002454 break;
2455 case Instruction::ADD_LONG:
2456 case Instruction::SUB_LONG:
2457 case Instruction::MUL_LONG:
2458 case Instruction::DIV_LONG:
2459 case Instruction::REM_LONG:
2460 case Instruction::AND_LONG:
2461 case Instruction::OR_LONG:
2462 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002463 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002464 reg_types_.LongLo(), reg_types_.LongHi(),
2465 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
2467 case Instruction::SHL_LONG:
2468 case Instruction::SHR_LONG:
2469 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002471 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002472 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::ADD_FLOAT:
2475 case Instruction::SUB_FLOAT:
2476 case Instruction::MUL_FLOAT:
2477 case Instruction::DIV_FLOAT:
2478 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002479 work_line_->CheckBinaryOp(inst,
2480 reg_types_.Float(),
2481 reg_types_.Float(),
2482 reg_types_.Float(),
2483 false);
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::ADD_DOUBLE:
2486 case Instruction::SUB_DOUBLE:
2487 case Instruction::MUL_DOUBLE:
2488 case Instruction::DIV_DOUBLE:
2489 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002491 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2492 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002493 break;
2494 case Instruction::ADD_INT_2ADDR:
2495 case Instruction::SUB_INT_2ADDR:
2496 case Instruction::MUL_INT_2ADDR:
2497 case Instruction::REM_INT_2ADDR:
2498 case Instruction::SHL_INT_2ADDR:
2499 case Instruction::SHR_INT_2ADDR:
2500 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002501 work_line_->CheckBinaryOp2addr(inst,
2502 reg_types_.Integer(),
2503 reg_types_.Integer(),
2504 reg_types_.Integer(),
2505 false);
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::AND_INT_2ADDR:
2508 case Instruction::OR_INT_2ADDR:
2509 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002510 work_line_->CheckBinaryOp2addr(inst,
2511 reg_types_.Integer(),
2512 reg_types_.Integer(),
2513 reg_types_.Integer(),
2514 true);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002517 work_line_->CheckBinaryOp2addr(inst,
2518 reg_types_.Integer(),
2519 reg_types_.Integer(),
2520 reg_types_.Integer(),
2521 false);
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523 case Instruction::ADD_LONG_2ADDR:
2524 case Instruction::SUB_LONG_2ADDR:
2525 case Instruction::MUL_LONG_2ADDR:
2526 case Instruction::DIV_LONG_2ADDR:
2527 case Instruction::REM_LONG_2ADDR:
2528 case Instruction::AND_LONG_2ADDR:
2529 case Instruction::OR_LONG_2ADDR:
2530 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002531 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002532 reg_types_.LongLo(), reg_types_.LongHi(),
2533 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002534 break;
2535 case Instruction::SHL_LONG_2ADDR:
2536 case Instruction::SHR_LONG_2ADDR:
2537 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002539 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::ADD_FLOAT_2ADDR:
2542 case Instruction::SUB_FLOAT_2ADDR:
2543 case Instruction::MUL_FLOAT_2ADDR:
2544 case Instruction::DIV_FLOAT_2ADDR:
2545 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002546 work_line_->CheckBinaryOp2addr(inst,
2547 reg_types_.Float(),
2548 reg_types_.Float(),
2549 reg_types_.Float(),
2550 false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552 case Instruction::ADD_DOUBLE_2ADDR:
2553 case Instruction::SUB_DOUBLE_2ADDR:
2554 case Instruction::MUL_DOUBLE_2ADDR:
2555 case Instruction::DIV_DOUBLE_2ADDR:
2556 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002557 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2559 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002560 break;
2561 case Instruction::ADD_INT_LIT16:
2562 case Instruction::RSUB_INT:
2563 case Instruction::MUL_INT_LIT16:
2564 case Instruction::DIV_INT_LIT16:
2565 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002566 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568 case Instruction::AND_INT_LIT16:
2569 case Instruction::OR_INT_LIT16:
2570 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002572 break;
2573 case Instruction::ADD_INT_LIT8:
2574 case Instruction::RSUB_INT_LIT8:
2575 case Instruction::MUL_INT_LIT8:
2576 case Instruction::DIV_INT_LIT8:
2577 case Instruction::REM_INT_LIT8:
2578 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002580 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002581 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::AND_INT_LIT8:
2584 case Instruction::OR_INT_LIT8:
2585 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002589 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002590 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002591 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002592 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2593 }
2594 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002595 // Note: the following instructions encode offsets derived from class linking.
2596 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2597 // meaning if the class linking and resolution were successful.
2598 case Instruction::IGET_QUICK:
2599 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2600 break;
2601 case Instruction::IGET_WIDE_QUICK:
2602 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2603 break;
2604 case Instruction::IGET_OBJECT_QUICK:
2605 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2606 break;
2607 case Instruction::IPUT_QUICK:
2608 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2609 break;
2610 case Instruction::IPUT_WIDE_QUICK:
2611 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2612 break;
2613 case Instruction::IPUT_OBJECT_QUICK:
2614 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2615 break;
2616 case Instruction::INVOKE_VIRTUAL_QUICK:
2617 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2618 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002619 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002620 if (called_method != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002621 const char* descriptor = called_method->GetReturnTypeDescriptor();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002622 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor,
Mathieu Chartier590fee92013-09-13 13:46:47 -07002623 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002624 if (!return_type.IsLowHalf()) {
2625 work_line_->SetResultRegisterType(return_type);
2626 } else {
2627 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2628 }
2629 just_set_result = true;
2630 }
2631 break;
2632 }
2633
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002635 case Instruction::UNUSED_3E:
2636 case Instruction::UNUSED_3F:
2637 case Instruction::UNUSED_40:
2638 case Instruction::UNUSED_41:
2639 case Instruction::UNUSED_42:
2640 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002641 case Instruction::UNUSED_79:
2642 case Instruction::UNUSED_7A:
2643 case Instruction::UNUSED_EB:
2644 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002645 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002646 case Instruction::UNUSED_EE:
2647 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002648 case Instruction::UNUSED_F0:
2649 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002650 case Instruction::UNUSED_F2:
2651 case Instruction::UNUSED_F3:
2652 case Instruction::UNUSED_F4:
2653 case Instruction::UNUSED_F5:
2654 case Instruction::UNUSED_F6:
2655 case Instruction::UNUSED_F7:
2656 case Instruction::UNUSED_F8:
2657 case Instruction::UNUSED_F9:
2658 case Instruction::UNUSED_FA:
2659 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002660 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002661 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002662 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002663 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002665 break;
2666
2667 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002668 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002669 * complain if an instruction is missing (which is desirable).
2670 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002671 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002672
Ian Rogersad0b3a32012-04-16 14:50:24 -07002673 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002674 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002675 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002676 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002677 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002678 /* immediate failure, reject class */
2679 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2680 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002681 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002682 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002683 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002684 }
jeffhaobdb76512011-09-07 11:43:16 -07002685 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 * If we didn't just set the result register, clear it out. This ensures that you can only use
2687 * "move-result" immediately after the result is set. (We could check this statically, but it's
2688 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002689 */
2690 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002692 }
2693
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002694
jeffhaobdb76512011-09-07 11:43:16 -07002695
2696 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002697 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002698 *
2699 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002700 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002701 * somebody could get a reference field, check it for zero, and if the
2702 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002703 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002704 * that, and will reject the code.
2705 *
2706 * TODO: avoid re-fetching the branch target
2707 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002708 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002709 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002711 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002713 return false;
2714 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002715 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002716 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002717 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 }
jeffhaobdb76512011-09-07 11:43:16 -07002719 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002720 if (NULL != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002721 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002722 return false;
2723 }
2724 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002725 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002726 return false;
2727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 }
jeffhaobdb76512011-09-07 11:43:16 -07002729 }
2730
2731 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002732 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002733 *
2734 * We've already verified that the table is structurally sound, so we
2735 * just need to walk through and tag the targets.
2736 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002737 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002738 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2739 const uint16_t* switch_insns = insns + offset_to_switch;
2740 int switch_count = switch_insns[1];
2741 int offset_to_targets, targ;
2742
2743 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2744 /* 0 = sig, 1 = count, 2/3 = first key */
2745 offset_to_targets = 4;
2746 } else {
2747 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002748 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002749 offset_to_targets = 2 + 2 * switch_count;
2750 }
2751
2752 /* verify each switch target */
2753 for (targ = 0; targ < switch_count; targ++) {
2754 int offset;
2755 uint32_t abs_offset;
2756
2757 /* offsets are 32-bit, and only partly endian-swapped */
2758 offset = switch_insns[offset_to_targets + targ * 2] |
2759 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 abs_offset = work_insn_idx_ + offset;
2761 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002762 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002763 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002765 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002766 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002767 }
jeffhaobdb76512011-09-07 11:43:16 -07002768 }
2769 }
2770
2771 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2773 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002774 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002775 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002776 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002777 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002778
Andreas Gampef91baf12014-07-18 15:41:00 -07002779 // Need the linker to try and resolve the handled class to check if it's Throwable.
2780 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2781
Ian Rogers0571d352011-11-03 19:51:38 -07002782 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002783 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2784 if (handler_type_idx == DexFile::kDexNoIndex16) {
2785 has_catch_all_handler = true;
2786 } else {
2787 // It is also a catch-all if it is java.lang.Throwable.
2788 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, *dex_cache_,
2789 *class_loader_);
2790 if (klass != nullptr) {
2791 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
2792 has_catch_all_handler = true;
2793 }
2794 } else {
2795 // Clear exception.
2796 Thread* self = Thread::Current();
2797 DCHECK(self->IsExceptionPending());
2798 self->ClearException();
2799 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 }
jeffhaobdb76512011-09-07 11:43:16 -07002801 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002802 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2803 * "work_regs", because at runtime the exception will be thrown before the instruction
2804 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002805 */
Ian Rogersebbdd872014-07-07 23:53:08 -07002806 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002807 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 }
jeffhaobdb76512011-09-07 11:43:16 -07002809 }
2810
2811 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2813 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002814 */
Andreas Gampef91baf12014-07-18 15:41:00 -07002815 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07002816 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 * The state in work_line reflects the post-execution state. If the current instruction is a
2818 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002819 * it will do so before grabbing the lock).
2820 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002821 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002822 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002824 return false;
2825 }
2826 }
2827 }
2828
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002829 /* Handle "continue". Tag the next consecutive instruction.
2830 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2831 * because it changes work_line_ when performing peephole optimization
2832 * and this change should not be used in those cases.
2833 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002834 if ((opcode_flags & Instruction::kContinue) != 0) {
2835 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2836 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2838 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002839 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002840 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2841 // next instruction isn't one.
2842 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2843 return false;
2844 }
2845 if (NULL != fallthrough_line.get()) {
2846 // Make workline consistent with fallthrough computed from peephole optimization.
2847 work_line_->CopyFromLine(fallthrough_line.get());
2848 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002849 if (insn_flags_[next_insn_idx].IsReturn()) {
2850 // For returns we only care about the operand to the return, all other registers are dead.
2851 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2852 Instruction::Code opcode = ret_inst->Opcode();
2853 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2854 work_line_->MarkAllRegistersAsConflicts();
2855 } else {
2856 if (opcode == Instruction::RETURN_WIDE) {
2857 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2858 } else {
2859 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2860 }
2861 }
2862 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002863 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2864 if (next_line != NULL) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002865 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2866 // needed. If the merge changes the state of the registers then the work line will be
2867 // updated.
2868 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07002869 return false;
2870 }
2871 } else {
2872 /*
2873 * We're not recording register data for the next instruction, so we don't know what the
2874 * prior state was. We have to assume that something has changed and re-evaluate it.
2875 */
2876 insn_flags_[next_insn_idx].SetChanged();
2877 }
2878 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002879
jeffhaod1f0fde2011-09-08 17:25:33 -07002880 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002881 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002882 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002883 return false;
2884 }
jeffhaobdb76512011-09-07 11:43:16 -07002885 }
2886
2887 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002888 * Update start_guess. Advance to the next instruction of that's
2889 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002890 * neither of those exists we're in a return or throw; leave start_guess
2891 * alone and let the caller sort it out.
2892 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002893 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002895 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002896 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002898 }
2899
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2901 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002902
2903 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002904} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002905
Ian Rogers776ac1f2012-04-13 23:36:36 -07002906const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002907 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002908 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002909 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002910 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002911 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2912 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002913 : reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 if (result.IsConflict()) {
2915 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2916 << "' in " << referrer;
2917 return result;
2918 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002919 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002920 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002921 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002923 // check at runtime if access is allowed and so pass here. If result is
2924 // primitive, skip the access check.
2925 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2926 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002927 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002928 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002929 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002931}
2932
Ian Rogers776ac1f2012-04-13 23:36:36 -07002933const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002934 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002936 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2938 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002939 CatchHandlerIterator iterator(handlers_ptr);
2940 for (; iterator.HasNext(); iterator.Next()) {
2941 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2942 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002943 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002945 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07002946 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002947 if (exception.IsUnresolvedTypes()) {
2948 // We don't know enough about the type. Fail here and let runtime handle it.
2949 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2950 return exception;
2951 } else {
2952 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2953 return reg_types_.Conflict();
2954 }
Jeff Haob878f212014-04-24 16:25:36 -07002955 } else if (common_super == nullptr) {
2956 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002957 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002958 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002960 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002961 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 }
2963 }
2964 }
2965 }
Ian Rogers0571d352011-11-03 19:51:38 -07002966 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002967 }
2968 }
2969 if (common_super == NULL) {
2970 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002971 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002972 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002974 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002975}
2976
Brian Carlstromea46f952013-07-30 01:26:50 -07002977mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002978 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002979 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002980 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 if (klass_type.IsConflict()) {
2982 std::string append(" in attempt to access method ");
2983 append += dex_file_->GetMethodName(method_id);
2984 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002985 return NULL;
2986 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002987 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002988 return NULL; // Can't resolve Class so no more to do here
2989 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002990 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002991 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002992 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002993 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002994 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002995 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002996
2997 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002998 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002999 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003000 res_method = klass->FindInterfaceMethod(name, signature);
3001 } else {
3002 res_method = klass->FindVirtualMethod(name, signature);
3003 }
3004 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003005 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003007 // If a virtual or interface method wasn't found with the expected type, look in
3008 // the direct methods. This can happen when the wrong invoke type is used or when
3009 // a class has changed, and will be flagged as an error in later checks.
3010 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3011 res_method = klass->FindDirectMethod(name, signature);
3012 }
3013 if (res_method == NULL) {
3014 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3015 << PrettyDescriptor(klass) << "." << name
3016 << " " << signature;
3017 return NULL;
3018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 }
3020 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3022 // enforce them here.
3023 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3025 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 return NULL;
3027 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003028 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003029 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3031 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08003032 return NULL;
3033 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003034 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003035 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003036 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003038 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003039 }
jeffhaode0d9c92012-02-27 13:58:13 -08003040 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3041 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3043 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003044 return NULL;
3045 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003046 // Check that interface methods match interface classes.
3047 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3048 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3049 << " is in an interface class " << PrettyClass(klass);
3050 return NULL;
3051 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3052 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3053 << " is in a non-interface class " << PrettyClass(klass);
3054 return NULL;
3055 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 // See if the method type implied by the invoke instruction matches the access flags for the
3057 // target method.
3058 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3059 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3060 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3061 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003062 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3063 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return NULL;
3065 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003066 return res_method;
3067}
3068
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003069template <class T>
3070mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3071 MethodType method_type,
3072 bool is_range,
3073 mirror::ArtMethod* res_method) {
3074 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3075 // match the call to the signature. Also, we might be calling through an abstract method
3076 // definition (which doesn't have register count values).
3077 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3078 /* caught by static verifier */
3079 DCHECK(is_range || expected_args <= 5);
3080 if (expected_args > code_item_->outs_size_) {
3081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3082 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3083 return nullptr;
3084 }
3085
3086 uint32_t arg[5];
3087 if (!is_range) {
3088 inst->GetVarArgs(arg);
3089 }
3090 uint32_t sig_registers = 0;
3091
3092 /*
3093 * Check the "this" argument, which must be an instance of the class that declared the method.
3094 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3095 * rigorous check here (which is okay since we have to do it at runtime).
3096 */
3097 if (method_type != METHOD_STATIC) {
3098 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3099 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3100 CHECK(have_pending_hard_failure_);
3101 return nullptr;
3102 }
3103 if (actual_arg_type.IsUninitializedReference()) {
3104 if (res_method) {
3105 if (!res_method->IsConstructor()) {
3106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3107 return nullptr;
3108 }
3109 } else {
3110 // Check whether the name of the called method is "<init>"
3111 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3112 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "init") != 0) {
3113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3114 return nullptr;
3115 }
3116 }
3117 }
3118 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
3119 const RegType* res_method_class;
3120 if (res_method != nullptr) {
3121 mirror::Class* klass = res_method->GetDeclaringClass();
3122 res_method_class = &reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
3123 klass->CannotBeAssignedFromOtherTypes());
3124 } else {
3125 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3126 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3127 res_method_class = &reg_types_.FromDescriptor(class_loader_->Get(),
3128 dex_file_->StringByTypeIdx(class_idx),
3129 false);
3130 }
3131 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3132 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3133 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3134 << "' not instance of '" << *res_method_class << "'";
3135 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3136 // the compiler.
3137 if (have_pending_hard_failure_) {
3138 return nullptr;
3139 }
3140 }
3141 }
3142 sig_registers = 1;
3143 }
3144
3145 for ( ; it->HasNext(); it->Next()) {
3146 if (sig_registers >= expected_args) {
3147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3148 " arguments, found " << sig_registers << " or more.";
3149 return nullptr;
3150 }
3151
3152 const char* param_descriptor = it->GetDescriptor();
3153
3154 if (param_descriptor == nullptr) {
3155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3156 "component";
3157 return nullptr;
3158 }
3159
3160 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), param_descriptor,
3161 false);
3162 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3163 arg[sig_registers];
3164 if (reg_type.IsIntegralTypes()) {
3165 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3166 if (!src_type.IsIntegralTypes()) {
3167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3168 << " but expected " << reg_type;
3169 return res_method;
3170 }
3171 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3172 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3173 // compiler.
3174 if (have_pending_hard_failure_) {
3175 return res_method;
3176 }
3177 }
3178 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3179 }
3180 if (expected_args != sig_registers) {
3181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3182 " arguments, found " << sig_registers;
3183 return nullptr;
3184 }
3185 return res_method;
3186}
3187
3188void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3189 MethodType method_type,
3190 bool is_range) {
3191 // As the method may not have been resolved, make this static check against what we expect.
3192 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3193 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3194 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3195 DexFileParameterIterator it(*dex_file_,
3196 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3197 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3198 nullptr);
3199}
3200
3201class MethodParamListDescriptorIterator {
3202 public:
3203 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3204 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3205 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3206 }
3207
3208 bool HasNext() {
3209 return pos_ < params_size_;
3210 }
3211
3212 void Next() {
3213 ++pos_;
3214 }
3215
3216 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3217 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3218 }
3219
3220 private:
3221 mirror::ArtMethod* res_method_;
3222 size_t pos_;
3223 const DexFile::TypeList* params_;
3224 const size_t params_size_;
3225};
3226
Brian Carlstromea46f952013-07-30 01:26:50 -07003227mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003228 MethodType method_type,
3229 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003230 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003231 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3232 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003233 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003234
Brian Carlstromea46f952013-07-30 01:26:50 -07003235 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003236 if (res_method == NULL) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003237 // Check what we can statically.
3238 if (!have_pending_hard_failure_) {
3239 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3240 }
3241 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003242 }
3243
Ian Rogersd81871c2011-10-03 13:57:23 -07003244 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3245 // has a vtable entry for the target method.
3246 if (is_super) {
3247 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003248 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003249 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003250 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003251 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003252 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003253 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003254 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003255 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003256 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003257 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003258 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003259 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003260 << "." << res_method->GetName()
3261 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003262 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 }
3264 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003265
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003266 // Process the target method's signature. This signature may or may not
3267 MethodParamListDescriptorIterator it(res_method);
3268 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3269 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003270}
3271
Brian Carlstromea46f952013-07-30 01:26:50 -07003272mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003273 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003274 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3275 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3276 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Ian Rogers9bc54402014-04-17 16:40:01 -07003277 if (!actual_arg_type.HasClass()) {
3278 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003279 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003280 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003281 mirror::ObjectArray<mirror::ArtMethod>* vtable = nullptr;
3282 mirror::Class* klass = actual_arg_type.GetClass();
3283 if (klass->IsInterface()) {
3284 // Derive Object.class from Class.class.getSuperclass().
3285 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
3286 CHECK(object_klass->IsObjectClass());
3287 vtable = object_klass->GetVTable();
3288 } else {
3289 vtable = klass->GetVTable();
3290 }
3291 CHECK(vtable != nullptr) << PrettyDescriptor(klass);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003292 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003293 CHECK_LT(static_cast<int32_t>(vtable_index), vtable->GetLength()) << PrettyDescriptor(klass);
Brian Carlstromea46f952013-07-30 01:26:50 -07003294 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003295 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003296 return res_method;
3297}
3298
Brian Carlstromea46f952013-07-30 01:26:50 -07003299mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003300 bool is_range) {
3301 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003302 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003303 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003304 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003306 return NULL;
3307 }
3308 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3309
3310 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3311 // match the call to the signature. Also, we might be calling through an abstract method
3312 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003313 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3314 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3315 return NULL;
3316 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003317 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3318 /* caught by static verifier */
3319 DCHECK(is_range || expected_args <= 5);
3320 if (expected_args > code_item_->outs_size_) {
3321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3322 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3323 return NULL;
3324 }
3325
3326 /*
3327 * Check the "this" argument, which must be an instance of the class that declared the method.
3328 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3329 * rigorous check here (which is okay since we have to do it at runtime).
3330 */
3331 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3333 return NULL;
3334 }
3335 if (!actual_arg_type.IsZero()) {
3336 mirror::Class* klass = res_method->GetDeclaringClass();
3337 const RegType& res_method_class =
Mathieu Chartierf8322842014-05-16 10:59:25 -07003338 reg_types_.FromClass(klass->GetDescriptor().c_str(), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003339 klass->CannotBeAssignedFromOtherTypes());
3340 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003341 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3342 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003343 << "' not instance of '" << res_method_class << "'";
3344 return NULL;
3345 }
3346 }
3347 /*
3348 * Process the target method's signature. This signature may or may not
3349 * have been verified, so we can't assume it's properly formed.
3350 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003351 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003352 size_t params_size = params == NULL ? 0 : params->Size();
3353 uint32_t arg[5];
3354 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003355 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003356 }
3357 size_t actual_args = 1;
3358 for (size_t param_index = 0; param_index < params_size; param_index++) {
3359 if (actual_args >= expected_args) {
3360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003361 << "'. Expected " << expected_args
3362 << " arguments, processing argument " << actual_args
3363 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003364 return NULL;
3365 }
3366 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003367 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003368 if (descriptor == NULL) {
3369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003370 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003371 return NULL;
3372 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003373 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003374 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3375 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3376 return res_method;
3377 }
3378 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3379 }
3380 if (actual_args != expected_args) {
3381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3382 << " expected " << expected_args << " arguments, found " << actual_args;
3383 return NULL;
3384 } else {
3385 return res_method;
3386 }
3387}
3388
Ian Rogers62342ec2013-06-11 10:26:37 -07003389void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003390 uint32_t type_idx;
3391 if (!is_filled) {
3392 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3393 type_idx = inst->VRegC_22c();
3394 } else if (!is_range) {
3395 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3396 type_idx = inst->VRegB_35c();
3397 } else {
3398 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3399 type_idx = inst->VRegB_3rc();
3400 }
3401 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003402 if (res_type.IsConflict()) { // bad class
3403 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003404 } else {
3405 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3406 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003408 } else if (!is_filled) {
3409 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003410 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003411 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003412 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3413 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003414 } else {
3415 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3416 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003417 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->Get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003418 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3419 uint32_t arg[5];
3420 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003421 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003422 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003423 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003424 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003425 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003426 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003427 return;
3428 }
3429 }
3430 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003431 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3432 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003433 }
3434 }
3435}
3436
Sebastien Hertz5243e912013-05-21 10:55:07 +02003437void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003438 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003439 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003440 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003443 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003444 if (array_type.IsZero()) {
3445 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3446 // instruction type. TODO: have a proper notion of bottom here.
3447 if (!is_primitive || insn_type.IsCategory1Types()) {
3448 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003449 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003451 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003452 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003453 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003454 }
jeffhaofc3144e2012-02-01 17:21:15 -08003455 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003457 } else {
3458 /* verify the class */
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003459 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
jeffhaofc3144e2012-02-01 17:21:15 -08003460 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003462 << " source for aget-object";
3463 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003465 << " source for category 1 aget";
3466 } else if (is_primitive && !insn_type.Equals(component_type) &&
3467 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003468 (insn_type.IsLong() && component_type.IsDouble()))) {
3469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3470 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003471 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003472 // Use knowledge of the field type which is stronger than the type inferred from the
3473 // instruction, which can't differentiate object types and ints from floats, longs from
3474 // doubles.
3475 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003476 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003477 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003478 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003479 component_type.HighHalf(&reg_types_));
3480 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 }
3482 }
3483 }
3484}
3485
Jeff Haofe1f7c82013-08-01 14:50:24 -07003486void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3487 const uint32_t vregA) {
3488 // Primitive assignability rules are weaker than regular assignability rules.
3489 bool instruction_compatible;
3490 bool value_compatible;
3491 const RegType& value_type = work_line_->GetRegisterType(vregA);
3492 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003493 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003494 value_compatible = value_type.IsIntegralTypes();
3495 } else if (target_type.IsFloat()) {
3496 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3497 value_compatible = value_type.IsFloatTypes();
3498 } else if (target_type.IsLong()) {
3499 instruction_compatible = insn_type.IsLong();
3500 value_compatible = value_type.IsLongTypes();
3501 } else if (target_type.IsDouble()) {
3502 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3503 value_compatible = value_type.IsDoubleTypes();
3504 } else {
3505 instruction_compatible = false; // reference with primitive store
3506 value_compatible = false; // unused
3507 }
3508 if (!instruction_compatible) {
3509 // This is a global failure rather than a class change failure as the instructions and
3510 // the descriptors for the type should have been consistent within the same file at
3511 // compile time.
3512 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3513 << "' but expected type '" << target_type << "'";
3514 return;
3515 }
3516 if (!value_compatible) {
3517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3518 << " of type " << value_type << " but expected " << target_type << " for put";
3519 return;
3520 }
3521}
3522
Sebastien Hertz5243e912013-05-21 10:55:07 +02003523void MethodVerifier::VerifyAPut(const Instruction* inst,
Sebastien Hertz757b3042014-03-28 14:34:28 +01003524 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003525 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003526 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003527 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003528 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003529 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003530 if (array_type.IsZero()) {
3531 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3532 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003533 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003535 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003536 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->Get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003537 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003538 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003539 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003540 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003541 if (!component_type.IsReferenceTypes()) {
3542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3543 << " source for aput-object";
3544 } else {
3545 // The instruction agrees with the type of array, confirm the value to be stored does too
3546 // Note: we use the instruction type (rather than the component type) for aput-object as
3547 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003548 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003549 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 }
3551 }
3552 }
3553}
3554
Brian Carlstromea46f952013-07-30 01:26:50 -07003555mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003556 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3557 // Check access to class
3558 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003559 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003560 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3561 field_idx, dex_file_->GetFieldName(field_id),
3562 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003563 return NULL;
3564 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003565 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003566 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003567 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003568 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3569 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3570 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003572 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003573 << dex_file_->GetFieldName(field_id) << ") in "
3574 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003575 DCHECK(Thread::Current()->IsExceptionPending());
3576 Thread::Current()->ClearException();
3577 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003578 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3579 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003580 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003581 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003582 return NULL;
3583 } else if (!field->IsStatic()) {
3584 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3585 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003587 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003588}
3589
Brian Carlstromea46f952013-07-30 01:26:50 -07003590mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003591 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3592 // Check access to class
3593 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003594 if (klass_type.IsConflict()) {
3595 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3596 field_idx, dex_file_->GetFieldName(field_id),
3597 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003598 return NULL;
3599 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003600 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003601 return NULL; // Can't resolve Class so no more to do here
3602 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003603 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3604 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3605 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003606 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003607 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003608 << dex_file_->GetFieldName(field_id) << ") in "
3609 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003610 DCHECK(Thread::Current()->IsExceptionPending());
3611 Thread::Current()->ClearException();
3612 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003613 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3614 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003616 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 return NULL;
3618 } else if (field->IsStatic()) {
3619 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3620 << " to not be static";
3621 return NULL;
3622 } else if (obj_type.IsZero()) {
3623 // Cannot infer and check type, however, access will cause null pointer exception
3624 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003625 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003626 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003627 const RegType& field_klass =
3628 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003629 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003630 if (obj_type.IsUninitializedTypes() &&
3631 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3632 !field_klass.Equals(GetDeclaringClass()))) {
3633 // Field accesses through uninitialized references are only allowable for constructors where
3634 // the field is declared in this class
3635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003636 << " of a not fully initialized object within the context"
3637 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003638 return NULL;
3639 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3640 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3641 // of C1. For resolution to occur the declared class of the field must be compatible with
3642 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3643 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3644 << " from object of type " << obj_type;
3645 return NULL;
3646 } else {
3647 return field;
3648 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003649 }
3650}
3651
Sebastien Hertz5243e912013-05-21 10:55:07 +02003652void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3653 bool is_primitive, bool is_static) {
3654 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003655 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003656 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003657 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003658 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003659 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003660 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003661 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003662 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003663 if (field != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003664 Thread* self = Thread::Current();
3665 mirror::Class* field_type_class;
3666 {
3667 StackHandleScope<1> hs(self);
3668 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3669 field_type_class = FieldHelper(h_field).GetType(can_load_classes_);
3670 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003671 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003672 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003673 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003674 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003675 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3676 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003677 }
Ian Rogers0d604842012-04-16 14:50:24 -07003678 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003679 if (field_type == nullptr) {
3680 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3681 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003682 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003683 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003684 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003685 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003686 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003687 if (field_type->Equals(insn_type) ||
3688 (field_type->IsFloat() && insn_type.IsInteger()) ||
3689 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003690 // expected that read is of the correct primitive type or that int reads are reading
3691 // floats or long reads are reading doubles
3692 } else {
3693 // This is a global failure rather than a class change failure as the instructions and
3694 // the descriptors for the type should have been consistent within the same file at
3695 // compile time
3696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3697 << " to be of type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003698 << "' but found type '" << *field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003699 return;
3700 }
3701 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003702 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003703 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3704 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003705 << "' but found type '" << *field_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003706 << "' in Get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003707 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003708 return;
3709 }
3710 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003711 if (!field_type->IsLowHalf()) {
3712 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003713 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003714 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003716}
3717
Sebastien Hertz5243e912013-05-21 10:55:07 +02003718void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3719 bool is_primitive, bool is_static) {
3720 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003721 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003722 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003723 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003724 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003725 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003726 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003727 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003728 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003729 if (field != NULL) {
3730 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3731 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3732 << " from other class " << GetDeclaringClass();
3733 return;
3734 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003735 mirror::Class* field_type_class;
3736 {
3737 StackHandleScope<1> hs(Thread::Current());
3738 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3739 FieldHelper fh(h_field);
3740 field_type_class = fh.GetType(can_load_classes_);
3741 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003742 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003743 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003744 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003745 } else {
3746 Thread* self = Thread::Current();
3747 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3748 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003749 }
3750 }
3751 if (field_type == nullptr) {
3752 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3753 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003754 field_type = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003755 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003756 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003757 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003758 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003759 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003760 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003761 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003762 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3763 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003764 << "' but found type '" << *field_type
Ian Rogersad0b3a32012-04-16 14:50:24 -07003765 << "' in put-object";
3766 return;
3767 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003768 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003769 }
3770}
3771
Brian Carlstromea46f952013-07-30 01:26:50 -07003772mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08003773 RegisterLine* reg_line) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003774 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3775 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3776 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3777 inst->Opcode() == Instruction::IPUT_QUICK ||
3778 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3779 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3780 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07003781 if (!object_type.HasClass()) {
3782 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
3783 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003784 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003785 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02003786 mirror::ArtField* f = mirror::ArtField::FindInstanceFieldWithOffset(object_type.GetClass(),
3787 field_offset);
3788 if (f == nullptr) {
3789 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
3790 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
3791 }
3792 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003793}
3794
3795void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3796 bool is_primitive) {
3797 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003798 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003799 if (field == NULL) {
3800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3801 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003802 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003803 mirror::Class* field_type_class;
3804 {
3805 StackHandleScope<1> hs(Thread::Current());
3806 HandleWrapper<mirror::ArtField> h_field(hs.NewHandleWrapper(&field));
3807 FieldHelper fh(h_field);
3808 field_type_class = fh.GetType(can_load_classes_);
3809 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003810 const RegType* field_type;
3811 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003812 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003813 field_type_class->CannotBeAssignedFromOtherTypes());
3814 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003815 Thread* self = Thread::Current();
3816 DCHECK(!can_load_classes_ || self->IsExceptionPending());
3817 self->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003818 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003819 field->GetTypeDescriptor(), false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003820 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003821 DCHECK(field_type != nullptr);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003822 const uint32_t vregA = inst->VRegA_22c();
3823 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003824 if (field_type->Equals(insn_type) ||
3825 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3826 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003827 // expected that read is of the correct primitive type or that int reads are reading
3828 // floats or long reads are reading doubles
3829 } else {
3830 // This is a global failure rather than a class change failure as the instructions and
3831 // the descriptors for the type should have been consistent within the same file at
3832 // compile time
3833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003834 << " to be of type '" << insn_type
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07003835 << "' but found type '" << *field_type << "' in Get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003836 return;
3837 }
3838 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003839 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003840 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003841 << " to be compatible with type '" << insn_type
Sebastien Hertz757b3042014-03-28 14:34:28 +01003842 << "' but found type '" << *field_type
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003843 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003844 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3845 return;
3846 }
3847 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003848 if (!field_type->IsLowHalf()) {
3849 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003850 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003851 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003852 }
3853}
3854
3855void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3856 bool is_primitive) {
3857 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003858 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003859 if (field == NULL) {
3860 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3861 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003862 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003863 const char* descriptor = field->GetTypeDescriptor();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003864 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3865 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3866 if (field != NULL) {
3867 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3868 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003869 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003870 return;
3871 }
3872 }
3873 const uint32_t vregA = inst->VRegA_22c();
3874 if (is_primitive) {
3875 // Primitive field assignability rules are weaker than regular assignability rules
3876 bool instruction_compatible;
3877 bool value_compatible;
3878 const RegType& value_type = work_line_->GetRegisterType(vregA);
3879 if (field_type.IsIntegralTypes()) {
3880 instruction_compatible = insn_type.IsIntegralTypes();
3881 value_compatible = value_type.IsIntegralTypes();
3882 } else if (field_type.IsFloat()) {
3883 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3884 value_compatible = value_type.IsFloatTypes();
3885 } else if (field_type.IsLong()) {
3886 instruction_compatible = insn_type.IsLong();
3887 value_compatible = value_type.IsLongTypes();
3888 } else if (field_type.IsDouble()) {
3889 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3890 value_compatible = value_type.IsDoubleTypes();
3891 } else {
3892 instruction_compatible = false; // reference field with primitive store
3893 value_compatible = false; // unused
3894 }
3895 if (!instruction_compatible) {
3896 // This is a global failure rather than a class change failure as the instructions and
3897 // the descriptors for the type should have been consistent within the same file at
3898 // compile time
3899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003900 << " to be of type '" << insn_type
3901 << "' but found type '" << field_type
3902 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003903 return;
3904 }
3905 if (!value_compatible) {
3906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3907 << " of type " << value_type
3908 << " but expected " << field_type
3909 << " for store to " << PrettyField(field) << " in put";
3910 return;
3911 }
3912 } else {
3913 if (!insn_type.IsAssignableFrom(field_type)) {
3914 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003915 << " to be compatible with type '" << insn_type
3916 << "' but found type '" << field_type
3917 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 return;
3919 }
3920 work_line_->VerifyRegisterType(vregA, field_type);
3921 }
3922}
3923
Ian Rogers776ac1f2012-04-13 23:36:36 -07003924bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003927 return false;
3928 }
3929 return true;
3930}
3931
Ian Rogersebbdd872014-07-07 23:53:08 -07003932bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
3933 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003934 bool changed = true;
3935 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3936 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003937 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003938 * We haven't processed this instruction before, and we haven't touched the registers here, so
3939 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3940 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003941 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003942 if (!insn_flags_[next_insn].IsReturn()) {
3943 target_line->CopyFromLine(merge_line);
3944 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003945 // Verify that the monitor stack is empty on return.
3946 if (!merge_line->VerifyMonitorStackEmpty()) {
3947 return false;
3948 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003949 // For returns we only care about the operand to the return, all other registers are dead.
3950 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3951 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3952 Instruction::Code opcode = ret_inst->Opcode();
3953 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3954 target_line->MarkAllRegistersAsConflicts();
3955 } else {
3956 target_line->CopyFromLine(merge_line);
3957 if (opcode == Instruction::RETURN_WIDE) {
3958 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3959 } else {
3960 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3961 }
3962 }
3963 }
jeffhaobdb76512011-09-07 11:43:16 -07003964 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07003965 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003966 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003967 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003968 if (gDebugVerify) {
3969 copy->CopyFromLine(target_line);
3970 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003971 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003972 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003973 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003974 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003975 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003976 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003977 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3978 << *copy.get() << " MERGE\n"
3979 << *merge_line << " ==\n"
3980 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003981 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003982 if (update_merge_line && changed) {
3983 merge_line->CopyFromLine(target_line);
3984 }
jeffhaobdb76512011-09-07 11:43:16 -07003985 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 if (changed) {
3987 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003988 }
3989 return true;
3990}
3991
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003992InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003993 return &insn_flags_[work_insn_idx_];
3994}
3995
Ian Rogersad0b3a32012-04-16 14:50:24 -07003996const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003997 if (return_type_ == nullptr) {
3998 if (mirror_method_ != NULL) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003999 Thread* self = Thread::Current();
4000 StackHandleScope<1> hs(self);
4001 mirror::Class* return_type_class;
4002 {
4003 HandleWrapper<mirror::ArtMethod> h_mirror_method(hs.NewHandleWrapper(&mirror_method_));
4004 return_type_class = MethodHelper(h_mirror_method).GetReturnType(can_load_classes_);
4005 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004006 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004007 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4008 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004009 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004010 } else {
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004011 DCHECK(!can_load_classes_ || self->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004012 self->ClearException();
4013 }
4014 }
4015 if (return_type_ == nullptr) {
4016 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4017 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4018 uint16_t return_type_idx = proto_id.return_type_idx_;
4019 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004020 return_type_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004021 }
4022 }
4023 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004024}
4025
4026const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004027 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004028 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004029 const char* descriptor
4030 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07004031 if (mirror_method_ != NULL) {
4032 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004033 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4034 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004035 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07004036 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->Get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004037 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004038 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004039 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004040}
4041
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004042std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4043 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004044 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004045 std::vector<int32_t> result;
4046 for (size_t i = 0; i < line->NumRegs(); ++i) {
4047 const RegType& type = line->GetRegisterType(i);
4048 if (type.IsConstant()) {
4049 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4050 result.push_back(type.ConstantValue());
4051 } else if (type.IsConstantLo()) {
4052 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4053 result.push_back(type.ConstantValueLo());
4054 } else if (type.IsConstantHi()) {
4055 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4056 result.push_back(type.ConstantValueHi());
4057 } else if (type.IsIntegralTypes()) {
4058 result.push_back(kIntVReg);
4059 result.push_back(0);
4060 } else if (type.IsFloat()) {
4061 result.push_back(kFloatVReg);
4062 result.push_back(0);
4063 } else if (type.IsLong()) {
4064 result.push_back(kLongLoVReg);
4065 result.push_back(0);
4066 result.push_back(kLongHiVReg);
4067 result.push_back(0);
4068 ++i;
4069 } else if (type.IsDouble()) {
4070 result.push_back(kDoubleLoVReg);
4071 result.push_back(0);
4072 result.push_back(kDoubleHiVReg);
4073 result.push_back(0);
4074 ++i;
4075 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4076 result.push_back(kUndefined);
4077 result.push_back(0);
4078 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004079 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004080 result.push_back(kReferenceVReg);
4081 result.push_back(0);
4082 }
4083 }
4084 return result;
4085}
4086
Sebastien Hertz849600b2013-12-20 10:28:08 +01004087const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
4088 if (precise) {
4089 // Precise constant type.
4090 return reg_types_.FromCat1Const(value, true);
4091 } else {
4092 // Imprecise constant type.
4093 if (value < -32768) {
4094 return reg_types_.IntConstant();
4095 } else if (value < -128) {
4096 return reg_types_.ShortConstant();
4097 } else if (value < 0) {
4098 return reg_types_.ByteConstant();
4099 } else if (value == 0) {
4100 return reg_types_.Zero();
4101 } else if (value == 1) {
4102 return reg_types_.One();
4103 } else if (value < 128) {
4104 return reg_types_.PosByteConstant();
4105 } else if (value < 32768) {
4106 return reg_types_.PosShortConstant();
4107 } else if (value < 65536) {
4108 return reg_types_.CharConstant();
4109 } else {
4110 return reg_types_.IntConstant();
4111 }
4112 }
4113}
4114
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004115void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004116 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004117}
4118
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004119void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004120 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004121}
jeffhaod1224c72012-02-29 13:43:08 -08004122
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08004123void MethodVerifier::VisitRoots(RootCallback* callback, void* arg) {
4124 reg_types_.VisitRoots(callback, arg);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004125}
4126
Ian Rogersd81871c2011-10-03 13:57:23 -07004127} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004128} // namespace art