blob: 1ed6980c967abe6444e0234988778ca36c264250 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070026#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000027#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070029#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070034#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070035#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Andreas Gampeebf850c2015-08-14 15:37:35 -070056// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
57static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
58
Ian Rogers7b3ddd22013-02-21 15:19:52 -080059void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070060 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070061 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070062 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070063 register_lines_.reset(new RegisterLine*[insns_size]());
64 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070065 for (uint32_t i = 0; i < insns_size; i++) {
66 bool interesting = false;
67 switch (mode) {
68 case kTrackRegsAll:
69 interesting = flags[i].IsOpcode();
70 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070071 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070072 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070073 break;
74 case kTrackRegsBranches:
75 interesting = flags[i].IsBranchTarget();
76 break;
77 default:
78 break;
79 }
80 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070081 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
82 }
83 }
84}
85
86PcToRegisterLineTable::~PcToRegisterLineTable() {
87 for (size_t i = 0; i < size_; i++) {
88 delete register_lines_[i];
89 if (kIsDebugBuild) {
90 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070091 }
92 }
93}
94
Andreas Gampe7c038102014-10-27 20:08:46 -070095// Note: returns true on failure.
96ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
97 const char* error_msg, uint32_t work_insn_idx) {
98 if (kIsDebugBuild) {
99 // In a debug build, abort if the error condition is wrong.
100 DCHECK(condition) << error_msg << work_insn_idx;
101 } else {
102 // In a non-debug build, just fail the class.
103 if (!condition) {
104 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
105 return true;
106 }
107 }
108
109 return false;
110}
111
Stephen Kyle7e541c92014-12-17 17:10:02 +0000112static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700113 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
115 reg_line->CheckConstructorReturn(verifier);
116 }
117 reg_line->MarkAllRegistersAsConflicts(verifier);
118}
119
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
122 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 mirror::Class* klass = method->GetDeclaringClass();
124 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
125 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
127 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 method->GetAccessFlags(), allow_soft_failures, false);
129}
130
131
Ian Rogers7b078e82014-09-10 14:44:24 -0700132MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
133 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700134 bool allow_soft_failures,
135 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700136 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700137 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700138 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800139 bool early_failure = false;
140 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141 const DexFile& dex_file = klass->GetDexFile();
142 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700144 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700151 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800152 early_failure = true;
153 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
154 }
155 if (early_failure) {
156 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800157 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800158 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000159 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700161 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700162 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700164 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 return VerifyClass(
167 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700168}
169
Ian Rogers7b078e82014-09-10 14:44:24 -0700170MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
171 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700172 Handle<mirror::DexCache> dex_cache,
173 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 const DexFile::ClassDef* class_def,
175 bool allow_soft_failures,
176 std::string* error) {
177 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700178
179 // A class must not be abstract and final.
180 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
181 *error = "Verifier rejected class ";
182 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
183 *error += ": class is abstract and final.";
184 return kHardFailure;
185 }
186
Ian Rogers13735952014-10-08 12:43:28 -0700187 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700190 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 }
jeffhaof56197c2012-03-05 18:01:54 -0800192 ClassDataItemIterator it(*dex_file, class_data);
193 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
194 it.Next();
195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700199 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800200 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800202 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700203 if (method_idx == previous_direct_method_idx) {
204 // smali can create dex files with two encoded_methods sharing the same method_idx
205 // http://code.google.com/p/smali/issues/detail?id=119
206 it.Next();
207 continue;
208 }
209 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700210 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700211 ArtMethod* method = linker->ResolveMethod(
212 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700213 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700214 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700217 } else {
218 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700220 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700221 MethodVerifier::FailureKind result = VerifyMethod(self,
222 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700223 dex_file,
224 dex_cache,
225 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700226 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700227 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700228 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700229 if (result != kNoFailure) {
230 if (result == kHardFailure) {
231 hard_fail = true;
232 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700233 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700234 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error = "Verifier rejected class ";
236 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
237 *error += " due to bad method ";
238 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700240 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800241 }
242 it.Next();
243 }
jeffhao9b0b1882012-10-01 16:51:22 -0700244 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800245 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700246 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800247 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700248 if (method_idx == previous_virtual_method_idx) {
249 // smali can create dex files with two encoded_methods sharing the same method_idx
250 // http://code.google.com/p/smali/issues/detail?id=119
251 it.Next();
252 continue;
253 }
254 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 ArtMethod* method = linker->ResolveMethod(
257 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700259 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700261 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700264 MethodVerifier::FailureKind result = VerifyMethod(self,
265 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700266 dex_file,
267 dex_cache,
268 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700269 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700270 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 if (result != kNoFailure) {
273 if (result == kHardFailure) {
274 hard_fail = true;
275 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700276 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700277 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 *error = "Verifier rejected class ";
279 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
280 *error += " due to bad method ";
281 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800284 }
285 it.Next();
286 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700287 if (error_count == 0) {
288 return kNoFailure;
289 } else {
290 return hard_fail ? kHardFailure : kSoftFailure;
291 }
jeffhaof56197c2012-03-05 18:01:54 -0800292}
293
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700294static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700295 if (code_item == nullptr) {
296 return false;
297 }
298
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700299 uint16_t registers_size = code_item->registers_size_;
300 uint32_t insns_size = code_item->insns_size_in_code_units_;
301
302 return registers_size * insns_size > 4*1024*1024;
303}
304
Ian Rogers7b078e82014-09-10 14:44:24 -0700305MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700307 Handle<mirror::DexCache> dex_cache,
308 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700312 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700313 bool allow_soft_failures,
314 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700315 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700316 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700317
Ian Rogers7b078e82014-09-10 14:44:24 -0700318 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700319 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800320 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700322 // Verification completed, however failures may be pending that didn't cause the verification
323 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 CHECK(!verifier.have_pending_hard_failure_);
325 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700326 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700327 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700328 << PrettyMethod(method_idx, *dex_file) << "\n");
329 }
Ian Rogersc8982582012-09-07 16:53:25 -0700330 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800331 }
332 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700334 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700335
336 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
337 // Failed due to being forced into interpreter. This is ok because
338 // we just want to skip verification.
339 result = kSoftFailure;
340 } else {
341 CHECK(verifier.have_pending_hard_failure_);
342 verifier.DumpFailures(LOG(INFO) << "Verification error in "
343 << PrettyMethod(method_idx, *dex_file) << "\n");
344 result = kHardFailure;
345 }
jeffhaof56197c2012-03-05 18:01:54 -0800346 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700347 std::cout << "\n" << verifier.info_messages_.str();
348 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800349 }
jeffhaof56197c2012-03-05 18:01:54 -0800350 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700351 if (kTimeVerifyMethod) {
352 uint64_t duration_ns = NanoTime() - start_ns;
353 if (duration_ns > MsToNs(100)) {
354 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700355 << " took " << PrettyDuration(duration_ns)
356 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700357 }
Ian Rogersc8982582012-09-07 16:53:25 -0700358 }
359 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800360}
361
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100362MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
363 VariableIndentationOutputStream* vios,
364 uint32_t dex_method_idx,
365 const DexFile* dex_file,
366 Handle<mirror::DexCache> dex_cache,
367 Handle<mirror::ClassLoader> class_loader,
368 const DexFile::ClassDef* class_def,
369 const DexFile::CodeItem* code_item,
370 ArtMethod* method,
371 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700372 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
373 class_def, code_item, dex_method_idx, method,
374 method_access_flags, true, true, true, true);
375 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100376 verifier->DumpFailures(vios->Stream());
377 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700378 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
379 // and querying any info is dangerous/can abort.
380 if (verifier->have_pending_hard_failure_) {
381 delete verifier;
382 return nullptr;
383 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100384 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700385 return verifier;
386 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387}
388
Ian Rogers7b078e82014-09-10 14:44:24 -0700389MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700390 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
391 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700392 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700394 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700395 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800396 bool need_precise_constants, bool verify_to_dump,
397 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 : self_(self),
399 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700400 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800401 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700402 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700403 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700404 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800405 dex_file_(dex_file),
406 dex_cache_(dex_cache),
407 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700408 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800409 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700410 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700411 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700412 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700414 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700416 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800417 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800418 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700419 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700420 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200421 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700422 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200423 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700424 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800425 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700426 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700427 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700428 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700429 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700430 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800431}
432
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700434 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700435 STLDeleteElements(&failure_messages_);
436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700439 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700441 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
442 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
444 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800445 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700447 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700448 verifier.FindLocksAtDexPc();
449}
450
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700451static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
452 const Instruction* inst = Instruction::At(code_item->insns_);
453
454 uint32_t insns_size = code_item->insns_size_in_code_units_;
455 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
456 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
457 return true;
458 }
459
460 dex_pc += inst->SizeInCodeUnits();
461 inst = inst->Next();
462 }
463
464 return false;
465}
466
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700467void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700468 CHECK(monitor_enter_dex_pcs_ != nullptr);
469 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700470
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700471 // Quick check whether there are any monitor_enter instructions at all.
472 if (!HasMonitorEnterInstructions(code_item_)) {
473 return;
474 }
475
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700476 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
477 // verification. In practice, the phase we want relies on data structures set up by all the
478 // earlier passes, so we just run the full method verification and bail out early when we've
479 // got what we wanted.
480 Verify();
481}
482
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
484 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
486 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
488 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
489 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200490 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200491}
492
Mathieu Chartierc7853442015-03-27 14:35:38 -0700493ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700494 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200495
496 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
497 // verification. In practice, the phase we want relies on data structures set up by all the
498 // earlier passes, so we just run the full method verification and bail out early when we've
499 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200500 bool success = Verify();
501 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700502 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200503 }
504 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700505 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700506 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200507 }
508 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
509 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200510}
511
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
513 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700514 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
515 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
517 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
518 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200519 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200520}
521
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700523 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200524
525 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
526 // verification. In practice, the phase we want relies on data structures set up by all the
527 // earlier passes, so we just run the full method verification and bail out early when we've
528 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200529 bool success = Verify();
530 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700531 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200532 }
533 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700534 if (register_line == nullptr) {
535 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200536 }
537 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
538 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800539 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200540}
541
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800543 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
546 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700548 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800549 true, true, false, true);
550 return verifier.FindStringInitMap();
551}
552
553SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
554 Verify();
555 return GetStringInitPcRegMap();
556}
557
Ian Rogersad0b3a32012-04-16 14:50:24 -0700558bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700559 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
560 // the name.
561 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
562 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
563 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
564 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
565 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
566 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
567 if ((method_access_flags_ & kAccConstructor) != 0) {
568 if (!constructor_by_name) {
569 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
570 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700571 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700572 }
573 is_constructor_ = true;
574 } else if (constructor_by_name) {
575 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
576 << " not marked as constructor.";
577 is_constructor_ = true;
578 }
579 // If it's a constructor, check whether IsStatic() matches the name.
580 // This should have been rejected by the dex file verifier. Only do in debug build.
581 if (kIsDebugBuild) {
582 if (IsConstructor()) {
583 if (IsStatic() ^ static_constructor_by_name) {
584 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
585 << "constructor name doesn't match static flag";
586 return false;
587 }
jeffhaobdb76512011-09-07 11:43:16 -0700588 }
jeffhaobdb76512011-09-07 11:43:16 -0700589 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700590
591 // Methods may only have one of public/protected/private.
592 // This should have been rejected by the dex file verifier. Only do in debug build.
593 if (kIsDebugBuild) {
594 size_t access_mod_count =
595 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
596 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
597 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
598 if (access_mod_count > 1) {
599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
600 return false;
601 }
602 }
603
604 // If there aren't any instructions, make sure that's expected, then exit successfully.
605 if (code_item_ == nullptr) {
606 // This should have been rejected by the dex file verifier. Only do in debug build.
607 if (kIsDebugBuild) {
608 // Only native or abstract methods may not have code.
609 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
611 return false;
612 }
613 if ((method_access_flags_ & kAccAbstract) != 0) {
614 // Abstract methods are not allowed to have the following flags.
615 static constexpr uint32_t kForbidden =
616 kAccPrivate |
617 kAccStatic |
618 kAccFinal |
619 kAccNative |
620 kAccStrict |
621 kAccSynchronized;
622 if ((method_access_flags_ & kForbidden) != 0) {
623 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
624 << "method can't be abstract and private/static/final/native/strict/synchronized";
625 return false;
626 }
627 }
628 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
629 // Interface methods must be public and abstract.
630 if ((method_access_flags_ & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public and abstract";
632 return false;
633 }
634 // In addition to the above, interface methods must not be protected.
635 static constexpr uint32_t kForbidden = kAccProtected;
636 if ((method_access_flags_ & kForbidden) != 0) {
637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
638 return false;
639 }
640 }
641 // We also don't allow constructors to be abstract or native.
642 if (IsConstructor()) {
643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
644 return false;
645 }
646 }
647 return true;
648 }
649
650 // This should have been rejected by the dex file verifier. Only do in debug build.
651 if (kIsDebugBuild) {
652 // When there's code, the method must not be native or abstract.
653 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
655 return false;
656 }
657
658 // Only the static initializer may have code in an interface.
659 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
660 // Interfaces may have static initializers for their fields.
661 if (!IsConstructor() || !IsStatic()) {
662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
663 return false;
664 }
665 }
666
667 // Instance constructors must not be synchronized.
668 if (IsInstanceConstructor()) {
669 static constexpr uint32_t kForbidden = kAccSynchronized;
670 if ((method_access_flags_ & kForbidden) != 0) {
671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
672 return false;
673 }
674 }
675 }
676
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
678 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
680 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700682 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800684 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 // Run through the instructions and see if the width checks out.
686 bool result = ComputeWidthsAndCountOps();
687 // Flag instructions guarded by a "try" block and check exception handlers.
688 result = result && ScanTryCatchBlocks();
689 // Perform static instruction verification.
690 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700691 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000692 result = result && VerifyCodeFlow();
693 // Compute information for compiler.
694 if (result && Runtime::Current()->IsCompiler()) {
695 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
696 }
697 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700698}
699
Ian Rogers776ac1f2012-04-13 23:36:36 -0700700std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700701 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700702 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700703
Ian Rogersad0b3a32012-04-16 14:50:24 -0700704 switch (error) {
705 case VERIFY_ERROR_NO_CLASS:
706 case VERIFY_ERROR_NO_FIELD:
707 case VERIFY_ERROR_NO_METHOD:
708 case VERIFY_ERROR_ACCESS_CLASS:
709 case VERIFY_ERROR_ACCESS_FIELD:
710 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700711 case VERIFY_ERROR_INSTANTIATION:
712 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700713 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700714 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800715 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700716 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
717 // class change and instantiation errors into soft verification errors so that we re-verify
718 // at runtime. We may fail to find or to agree on access because of not yet available class
719 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
720 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
721 // paths" that dynamically perform the verification and cause the behavior to be that akin
722 // to an interpreter.
723 error = VERIFY_ERROR_BAD_CLASS_SOFT;
724 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700725 // If we fail again at runtime, mark that this instruction would throw and force this
726 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700727 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700728
729 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
730 // try to merge garbage.
731 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700732 // Note: this can fail before we touch any instruction, for the signature of a method. So
733 // add a check.
734 if (work_insn_idx_ < DexFile::kDexNoIndex) {
735 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
736 const Instruction* inst = Instruction::At(insns);
737 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700738
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700739 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
740 saved_line_->CopyFromLine(work_line_.get());
741 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700742 }
jeffhaofaf459e2012-08-31 15:32:47 -0700743 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700744 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700745
Ian Rogersad0b3a32012-04-16 14:50:24 -0700746 // Indication that verification should be retried at runtime.
747 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700748 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700749 have_pending_hard_failure_ = true;
750 }
751 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700752
jeffhaod5347e02012-03-22 17:25:05 -0700753 // Hard verification failures at compile time will still fail at runtime, so the class is
754 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700755 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800756 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700757 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000758 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800759 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700760 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700761 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
762 ScopedObjectAccess soa(Thread::Current());
763 std::ostringstream oss;
764 Dump(oss);
765 LOG(ERROR) << oss.str();
766 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700767 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800768 }
769 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700770 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700771 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700773 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 failure_messages_.push_back(failure_message);
775 return *failure_message;
776}
777
Ian Rogers576ca0c2014-06-06 15:58:22 -0700778std::ostream& MethodVerifier::LogVerifyInfo() {
779 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
780 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
781}
782
Ian Rogersad0b3a32012-04-16 14:50:24 -0700783void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
784 size_t failure_num = failure_messages_.size();
785 DCHECK_NE(failure_num, 0U);
786 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
787 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700788 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700789 delete last_fail_message;
790}
791
792void MethodVerifier::AppendToLastFailMessage(std::string append) {
793 size_t failure_num = failure_messages_.size();
794 DCHECK_NE(failure_num, 0U);
795 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
796 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800797}
798
Ian Rogers776ac1f2012-04-13 23:36:36 -0700799bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700800 const uint16_t* insns = code_item_->insns_;
801 size_t insns_size = code_item_->insns_size_in_code_units_;
802 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700803 size_t new_instance_count = 0;
804 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700806
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700808 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700809 switch (opcode) {
810 case Instruction::APUT_OBJECT:
811 case Instruction::CHECK_CAST:
812 has_check_casts_ = true;
813 break;
814 case Instruction::INVOKE_VIRTUAL:
815 case Instruction::INVOKE_VIRTUAL_RANGE:
816 case Instruction::INVOKE_INTERFACE:
817 case Instruction::INVOKE_INTERFACE_RANGE:
818 has_virtual_or_interface_invokes_ = true;
819 break;
820 case Instruction::MONITOR_ENTER:
821 monitor_enter_count++;
822 break;
823 case Instruction::NEW_INSTANCE:
824 new_instance_count++;
825 break;
826 default:
827 break;
jeffhaobdb76512011-09-07 11:43:16 -0700828 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700830 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700832 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700833 }
834
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
837 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700838 return false;
839 }
840
Ian Rogersd81871c2011-10-03 13:57:23 -0700841 new_instance_count_ = new_instance_count;
842 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700843 return true;
844}
845
Ian Rogers776ac1f2012-04-13 23:36:36 -0700846bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700848 if (tries_size == 0) {
849 return true;
850 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700852 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700853
854 for (uint32_t idx = 0; idx < tries_size; idx++) {
855 const DexFile::TryItem* try_item = &tries[idx];
856 uint32_t start = try_item->start_addr_;
857 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700858 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
860 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700861 return false;
862 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
865 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700866 return false;
867 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700868 uint32_t dex_pc = start;
869 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
870 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700871 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700872 size_t insn_size = inst->SizeInCodeUnits();
873 dex_pc += insn_size;
874 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700875 }
876 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800877 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700878 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700879 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700880 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700881 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700882 CatchHandlerIterator iterator(handlers_ptr);
883 for (; iterator.HasNext(); iterator.Next()) {
884 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700886 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
887 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700888 return false;
889 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100890 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
891 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
892 << "exception handler begins with move-result* (" << dex_pc << ")";
893 return false;
894 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700896 // Ensure exception types are resolved so that they don't need resolution to be delivered,
897 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700898 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800899 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
900 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700901 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700902 if (exception_type == nullptr) {
903 DCHECK(self_->IsExceptionPending());
904 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700905 }
906 }
jeffhaobdb76512011-09-07 11:43:16 -0700907 }
Ian Rogers0571d352011-11-03 19:51:38 -0700908 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700909 }
jeffhaobdb76512011-09-07 11:43:16 -0700910 return true;
911}
912
Ian Rogers776ac1f2012-04-13 23:36:36 -0700913bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700915
Ian Rogers0c7abda2012-09-19 13:33:42 -0700916 /* 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 -0700917 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700918 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700919
920 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700921 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700923 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 return false;
925 }
926 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700927 // All invoke points are marked as "Throw" points already.
928 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000929 if (inst->IsBranch()) {
930 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
931 // The compiler also needs safepoints for fall-through to loop heads.
932 // Such a loop head must be a target of a branch.
933 int32_t offset = 0;
934 bool cond, self_ok;
935 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
936 DCHECK(target_ok);
937 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
938 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700939 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000940 } else if (inst->IsReturn()) {
941 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 }
943 dex_pc += inst->SizeInCodeUnits();
944 inst = inst->Next();
945 }
946 return true;
947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700950 if (UNLIKELY(inst->IsExperimental())) {
951 // Experimental instructions don't yet have verifier support implementation.
952 // While it is possible to use them by themselves, when we try to use stable instructions
953 // with a virtual register that was created by an experimental instruction,
954 // the data flow analysis will fail.
955 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
956 << "experimental instruction is not supported by verifier; skipping verification";
957 have_pending_experimental_failure_ = true;
958 return false;
959 }
960
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 bool result = true;
962 switch (inst->GetVerifyTypeArgumentA()) {
963 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700964 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 break;
966 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700967 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 break;
969 }
970 switch (inst->GetVerifyTypeArgumentB()) {
971 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700972 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 break;
974 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700975 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 break;
977 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700978 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 break;
980 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700981 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 break;
983 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700984 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 break;
986 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700987 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 break;
989 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700990 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 break;
992 }
993 switch (inst->GetVerifyTypeArgumentC()) {
994 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700995 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 break;
997 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700998 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 break;
1000 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001001 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 break;
1003 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001004 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 break;
1006 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001007 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 break;
1009 }
1010 switch (inst->GetVerifyExtraFlags()) {
1011 case Instruction::kVerifyArrayData:
1012 result = result && CheckArrayData(code_offset);
1013 break;
1014 case Instruction::kVerifyBranchTarget:
1015 result = result && CheckBranchTarget(code_offset);
1016 break;
1017 case Instruction::kVerifySwitchTargets:
1018 result = result && CheckSwitchTargets(code_offset);
1019 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001020 case Instruction::kVerifyVarArgNonZero:
1021 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001022 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001023 // Instructions that can actually return a negative value shouldn't have this flag.
1024 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1025 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1026 v_a > Instruction::kMaxVarArgRegs) {
1027 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001028 "non-range invoke";
1029 return false;
1030 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001031
Ian Rogers29a26482014-05-02 15:27:29 -07001032 uint32_t args[Instruction::kMaxVarArgRegs];
1033 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001034 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001036 }
Andreas Gampec3314312014-06-19 18:13:29 -07001037 case Instruction::kVerifyVarArgRangeNonZero:
1038 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001040 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1041 inst->VRegA() <= 0) {
1042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1043 "range invoke";
1044 return false;
1045 }
Ian Rogers29a26482014-05-02 15:27:29 -07001046 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 break;
1048 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001049 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001050 result = false;
1051 break;
1052 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001053 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1055 result = false;
1056 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 return result;
1058}
1059
Ian Rogers7b078e82014-09-10 14:44:24 -07001060inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1063 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
1066 return true;
1067}
1068
Ian Rogers7b078e82014-09-10 14:44:24 -07001069inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1072 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 return false;
1074 }
1075 return true;
1076}
1077
Ian Rogers7b078e82014-09-10 14:44:24 -07001078inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001079 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1081 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return false;
1083 }
1084 return true;
1085}
1086
Ian Rogers7b078e82014-09-10 14:44:24 -07001087inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1090 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 return false;
1092 }
1093 return true;
1094}
1095
Ian Rogers7b078e82014-09-10 14:44:24 -07001096inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1099 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 return false;
1101 }
1102 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001103 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 return false;
1107 }
1108 return true;
1109}
1110
Ian Rogers7b078e82014-09-10 14:44:24 -07001111inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1114 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 return false;
1116 }
1117 return true;
1118}
1119
Ian Rogers7b078e82014-09-10 14:44:24 -07001120inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1123 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 return false;
1125 }
1126 return true;
1127}
1128
Ian Rogers776ac1f2012-04-13 23:36:36 -07001129bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1132 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 return false;
1134 }
1135 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001136 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 const char* cp = descriptor;
1138 while (*cp++ == '[') {
1139 bracket_count++;
1140 }
1141 if (bracket_count == 0) {
1142 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1144 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 return false;
1146 } else if (bracket_count > 255) {
1147 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1149 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 return false;
1151 }
1152 return true;
1153}
1154
Ian Rogers776ac1f2012-04-13 23:36:36 -07001155bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1157 const uint16_t* insns = code_item_->insns_ + cur_offset;
1158 const uint16_t* array_data;
1159 int32_t array_data_offset;
1160
1161 DCHECK_LT(cur_offset, insn_count);
1162 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001163 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1164 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001167 << ", data offset " << array_data_offset
1168 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 return false;
1170 }
1171 /* offset to array data table is a relative branch-style offset */
1172 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001173 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1174 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1176 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 return false;
1178 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001179 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1180 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1181 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1183 << ", data offset " << array_data_offset
1184 << " not correctly visited, probably bad padding.";
1185 return false;
1186 }
1187
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001189 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1191 /* make sure the end of the switch is in range */
1192 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1194 << ", data offset " << array_data_offset << ", end "
1195 << cur_offset + array_data_offset + table_size
1196 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 }
1199 return true;
1200}
1201
Ian Rogers776ac1f2012-04-13 23:36:36 -07001202bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 int32_t offset;
1204 bool isConditional, selfOkay;
1205 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1206 return false;
1207 }
1208 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1210 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 return false;
1212 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001213 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1214 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1217 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 return false;
1219 }
1220 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1221 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001222 if (abs_offset < 0 ||
1223 (uint32_t) abs_offset >= insn_count ||
1224 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001226 << reinterpret_cast<void*>(abs_offset) << ") at "
1227 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 return false;
1229 }
1230 insn_flags_[abs_offset].SetBranchTarget();
1231 return true;
1232}
1233
Ian Rogers776ac1f2012-04-13 23:36:36 -07001234bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 bool* selfOkay) {
1236 const uint16_t* insns = code_item_->insns_ + cur_offset;
1237 *pConditional = false;
1238 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001239 switch (*insns & 0xff) {
1240 case Instruction::GOTO:
1241 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001242 break;
1243 case Instruction::GOTO_32:
1244 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001245 *selfOkay = true;
1246 break;
1247 case Instruction::GOTO_16:
1248 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001249 break;
1250 case Instruction::IF_EQ:
1251 case Instruction::IF_NE:
1252 case Instruction::IF_LT:
1253 case Instruction::IF_GE:
1254 case Instruction::IF_GT:
1255 case Instruction::IF_LE:
1256 case Instruction::IF_EQZ:
1257 case Instruction::IF_NEZ:
1258 case Instruction::IF_LTZ:
1259 case Instruction::IF_GEZ:
1260 case Instruction::IF_GTZ:
1261 case Instruction::IF_LEZ:
1262 *pOffset = (int16_t) insns[1];
1263 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001264 break;
1265 default:
1266 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 return true;
1269}
1270
Ian Rogers776ac1f2012-04-13 23:36:36 -07001271bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001273 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001275 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001276 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1277 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1278 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001280 << ", switch offset " << switch_offset
1281 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001282 return false;
1283 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001284 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001286 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1287 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1289 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001290 return false;
1291 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001292 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1293 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1294 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1296 << ", switch offset " << switch_offset
1297 << " not correctly visited, probably bad padding.";
1298 return false;
1299 }
1300
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 uint32_t switch_count = switch_insns[1];
1302 int32_t keys_offset, targets_offset;
1303 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001304 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1305 /* 0=sig, 1=count, 2/3=firstKey */
1306 targets_offset = 4;
1307 keys_offset = -1;
1308 expected_signature = Instruction::kPackedSwitchSignature;
1309 } else {
1310 /* 0=sig, 1=count, 2..count*2 = keys */
1311 keys_offset = 2;
1312 targets_offset = 2 + 2 * switch_count;
1313 expected_signature = Instruction::kSparseSwitchSignature;
1314 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001317 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1318 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1319 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 return false;
1321 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001322 /* make sure the end of the switch is in range */
1323 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1325 << ", switch offset " << switch_offset
1326 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001327 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001328 return false;
1329 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001330 /* for a sparse switch, verify the keys are in ascending order */
1331 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1333 for (uint32_t targ = 1; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001334 int32_t key =
1335 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1336 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1339 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 return false;
1341 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 last_key = key;
1343 }
1344 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001347 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1348 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001350 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001351 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001352 !insn_flags_[abs_offset].IsOpcode()) {
1353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1354 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1355 << reinterpret_cast<void*>(cur_offset)
1356 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 return false;
1358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 insn_flags_[abs_offset].SetBranchTarget();
1360 }
1361 return true;
1362}
1363
Ian Rogers776ac1f2012-04-13 23:36:36 -07001364bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 uint16_t registers_size = code_item_->registers_size_;
1366 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001367 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1369 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 return false;
1371 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001372 }
1373
1374 return true;
1375}
1376
Ian Rogers776ac1f2012-04-13 23:36:36 -07001377bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 uint16_t registers_size = code_item_->registers_size_;
1379 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1380 // integer overflow when adding them here.
1381 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1383 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001384 return false;
1385 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001386 return true;
1387}
1388
Ian Rogers776ac1f2012-04-13 23:36:36 -07001389bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001390 uint16_t registers_size = code_item_->registers_size_;
1391 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001392
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001394 reg_table_.Init(kTrackCompilerInterestPoints,
1395 insn_flags_.get(),
1396 insns_size,
1397 registers_size,
1398 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001399
jeffhaobdb76512011-09-07 11:43:16 -07001400
Ian Rogersd0fbd852013-09-24 18:17:04 -07001401 work_line_.reset(RegisterLine::Create(registers_size, this));
1402 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001403
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 /* Initialize register types of method arguments. */
1405 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001406 DCHECK_NE(failures_.size(), 0U);
1407 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001408 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001409 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 return false;
1411 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001412 // We may have a runtime failure here, clear.
1413 have_pending_runtime_throw_failure_ = false;
1414
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 /* Perform code flow verification. */
1416 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001417 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001419 }
jeffhaobdb76512011-09-07 11:43:16 -07001420 return true;
1421}
1422
Ian Rogersad0b3a32012-04-16 14:50:24 -07001423std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1424 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001425 for (size_t i = 0; i < failures_.size(); ++i) {
1426 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001427 }
1428 return os;
1429}
1430
Ian Rogers776ac1f2012-04-13 23:36:36 -07001431void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001432 VariableIndentationOutputStream vios(&os);
1433 Dump(&vios);
1434}
1435
1436void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001437 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001438 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 return;
jeffhaobdb76512011-09-07 11:43:16 -07001440 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001441 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001442 vios->Stream() << "Register Types:\n";
1443 ScopedIndentation indent1(vios);
1444 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001445 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001446 vios->Stream() << "Dumping instructions and register lines:\n";
1447 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 const Instruction* inst = Instruction::At(code_item_->insns_);
1449 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001450 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001452 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001453 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001454 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001455 vios->Stream()
1456 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 const bool kDumpHexOfInstruction = false;
1458 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001459 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 }
jeffhaobdb76512011-09-07 11:43:16 -07001463}
1464
Ian Rogersd81871c2011-10-03 13:57:23 -07001465static bool IsPrimitiveDescriptor(char descriptor) {
1466 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001467 case 'I':
1468 case 'C':
1469 case 'S':
1470 case 'B':
1471 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001472 case 'F':
1473 case 'D':
1474 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001476 default:
1477 return false;
1478 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001479}
1480
Ian Rogers776ac1f2012-04-13 23:36:36 -07001481bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001483
1484 // Should have been verified earlier.
1485 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1486
1487 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001489
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001490 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001491 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001492 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001493 if (expected_args == 0) {
1494 // Expect at least a receiver.
1495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1496 return false;
1497 }
1498
Ian Rogersd81871c2011-10-03 13:57:23 -07001499 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1500 // argument as uninitialized. This restricts field access until the superclass constructor is
1501 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001502 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001503 if (IsConstructor()) {
1504 if (declaring_class.IsJavaLangObject()) {
1505 // "this" is implicitly initialized.
1506 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001507 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001508 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001509 reg_line->SetRegisterType<LockOp::kClear>(
1510 this,
1511 arg_start + cur_arg,
1512 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001513 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001515 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001516 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001518 }
1519
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001520 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001522 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001523
1524 for (; iterator.HasNext(); iterator.Next()) {
1525 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001526 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 LOG(FATAL) << "Null descriptor";
1528 }
1529 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1531 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 return false;
1533 }
1534 switch (descriptor[0]) {
1535 case 'L':
1536 case '[':
1537 // We assume that reference arguments are initialized. The only way it could be otherwise
1538 // (assuming the caller was verified) is if the current method is <init>, but in that case
1539 // it's effectively considered initialized the instant we reach here (in the sense that we
1540 // can return without doing anything or call virtual methods).
1541 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001542 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001543 if (!reg_type.IsNonZeroReferenceTypes()) {
1544 DCHECK(HasFailures());
1545 return false;
1546 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001547 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
1549 break;
1550 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001551 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 break;
1553 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001554 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 break;
1556 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001557 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 break;
1559 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001560 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 break;
1562 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001563 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 break;
1565 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001566 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 break;
1568 case 'J':
1569 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001570 if (cur_arg + 1 >= expected_args) {
1571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1572 << " args, found more (" << descriptor << ")";
1573 return false;
1574 }
1575
Ian Rogers7b078e82014-09-10 14:44:24 -07001576 const RegType* lo_half;
1577 const RegType* hi_half;
1578 if (descriptor[0] == 'J') {
1579 lo_half = &reg_types_.LongLo();
1580 hi_half = &reg_types_.LongHi();
1581 } else {
1582 lo_half = &reg_types_.DoubleLo();
1583 hi_half = &reg_types_.DoubleHi();
1584 }
1585 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 cur_arg++;
1587 break;
1588 }
1589 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1591 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 return false;
1593 }
1594 cur_arg++;
1595 }
1596 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001597 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1598 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 return false;
1600 }
1601 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1602 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1603 // format. Only major difference from the method argument format is that 'V' is supported.
1604 bool result;
1605 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1606 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001607 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 size_t i = 0;
1609 do {
1610 i++;
1611 } while (descriptor[i] == '['); // process leading [
1612 if (descriptor[i] == 'L') { // object array
1613 do {
1614 i++; // find closing ;
1615 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1616 result = descriptor[i] == ';';
1617 } else { // primitive array
1618 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1619 }
1620 } else if (descriptor[0] == 'L') {
1621 // could be more thorough here, but shouldn't be required
1622 size_t i = 0;
1623 do {
1624 i++;
1625 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1626 result = descriptor[i] == ';';
1627 } else {
1628 result = false;
1629 }
1630 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1632 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 }
1634 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001635}
1636
Ian Rogers776ac1f2012-04-13 23:36:36 -07001637bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 const uint16_t* insns = code_item_->insns_;
1639 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001640
jeffhaobdb76512011-09-07 11:43:16 -07001641 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 insn_flags_[0].SetChanged();
1643 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001644
jeffhaobdb76512011-09-07 11:43:16 -07001645 /* Continue until no instructions are marked "changed". */
1646 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001647 if (allow_thread_suspension_) {
1648 self_->AllowThreadSuspension();
1649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001650 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1651 uint32_t insn_idx = start_guess;
1652 for (; insn_idx < insns_size; insn_idx++) {
1653 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001654 break;
1655 }
jeffhaobdb76512011-09-07 11:43:16 -07001656 if (insn_idx == insns_size) {
1657 if (start_guess != 0) {
1658 /* try again, starting from the top */
1659 start_guess = 0;
1660 continue;
1661 } else {
1662 /* all flags are clear */
1663 break;
1664 }
1665 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 // We carry the working set of registers from instruction to instruction. If this address can
1667 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1668 // "changed" flags, we need to load the set of registers from the table.
1669 // Because we always prefer to continue on to the next instruction, we should never have a
1670 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1671 // target.
1672 work_insn_idx_ = insn_idx;
1673 if (insn_flags_[insn_idx].IsBranchTarget()) {
1674 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001675 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001676 /*
1677 * Sanity check: retrieve the stored register line (assuming
1678 * a full table) and make sure it actually matches.
1679 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001681 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 if (work_line_->CompareLine(register_line) != 0) {
1683 Dump(std::cout);
1684 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001685 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001686 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 << " work_line=" << work_line_->Dump(this) << "\n"
1688 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 }
jeffhaobdb76512011-09-07 11:43:16 -07001690 }
jeffhaobdb76512011-09-07 11:43:16 -07001691 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001693 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001694 prepend += " failed to verify: ";
1695 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001696 return false;
1697 }
jeffhaobdb76512011-09-07 11:43:16 -07001698 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 insn_flags_[insn_idx].SetVisited();
1700 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001701 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001702
Ian Rogers1c849e52012-06-28 14:00:33 -07001703 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001704 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001705 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001706 * (besides the wasted space), but it indicates a flaw somewhere
1707 * down the line, possibly in the verifier.
1708 *
1709 * If we've substituted "always throw" instructions into the stream,
1710 * we are almost certainly going to have some dead code.
1711 */
1712 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001714 for (; insn_idx < insns_size;
1715 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001716 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001717 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001718 * may or may not be preceded by a padding NOP (for alignment).
1719 */
1720 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1721 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1722 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001723 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001724 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1725 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1726 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
1729
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001731 if (dead_start < 0)
1732 dead_start = insn_idx;
1733 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001734 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1735 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001736 dead_start = -1;
1737 }
1738 }
1739 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001740 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1741 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001742 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001743 // To dump the state of the verify after a method, do something like:
1744 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1745 // "boolean java.lang.String.equals(java.lang.Object)") {
1746 // LOG(INFO) << info_messages_.str();
1747 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001748 }
jeffhaobdb76512011-09-07 11:43:16 -07001749 return true;
1750}
1751
Andreas Gampe68df3202015-06-22 11:35:46 -07001752// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1753// is no such field.
1754static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1755 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1756 DCHECK(class_def != nullptr);
1757 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1758 DCHECK(class_data != nullptr);
1759 ClassDataItemIterator it(dex_file, class_data);
1760 // Skip static fields.
1761 while (it.HasNextStaticField()) {
1762 it.Next();
1763 }
1764 while (it.HasNextInstanceField()) {
1765 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1766 return it.GetMemberIndex();
1767 }
1768 it.Next();
1769 }
1770 return DexFile::kDexNoIndex;
1771}
1772
Andreas Gampea727e372015-08-25 09:22:37 -07001773// Setup a register line for the given return instruction.
1774static void AdjustReturnLine(MethodVerifier* verifier,
1775 const Instruction* ret_inst,
1776 RegisterLine* line) {
1777 Instruction::Code opcode = ret_inst->Opcode();
1778
1779 switch (opcode) {
1780 case Instruction::RETURN_VOID:
1781 case Instruction::RETURN_VOID_NO_BARRIER:
1782 SafelyMarkAllRegistersAsConflicts(verifier, line);
1783 break;
1784
1785 case Instruction::RETURN:
1786 case Instruction::RETURN_OBJECT:
1787 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1788 break;
1789
1790 case Instruction::RETURN_WIDE:
1791 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unknown return opcode " << opcode;
1796 UNREACHABLE();
1797 }
1798}
1799
Ian Rogers776ac1f2012-04-13 23:36:36 -07001800bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001801 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1802 // We want the state _before_ the instruction, for the case where the dex pc we're
1803 // interested in is itself a monitor-enter instruction (which is a likely place
1804 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001806 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001807 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1808 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1809 }
1810 }
1811
jeffhaobdb76512011-09-07 11:43:16 -07001812 /*
1813 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001814 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001815 * control to another statement:
1816 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001817 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001818 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001819 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001820 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001821 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001822 * throw an exception that is handled by an encompassing "try"
1823 * block.
1824 *
1825 * We can also return, in which case there is no successor instruction
1826 * from this point.
1827 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001828 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001829 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1831 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001832 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001833
jeffhaobdb76512011-09-07 11:43:16 -07001834 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001835 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001836 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001838 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 }
jeffhaobdb76512011-09-07 11:43:16 -07001841
1842 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001843 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001844 * can throw an exception, we will copy/merge this into the "catch"
1845 * address rather than work_line, because we don't want the result
1846 * from the "successful" code path (e.g. a check-cast that "improves"
1847 * a type) to be visible to the exception handler.
1848 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001849 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001851 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001853 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001854 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001855
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001856
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001857 // We need to ensure the work line is consistent while performing validation. When we spot a
1858 // peephole pattern we compute a new line for either the fallthrough instruction or the
1859 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001860 std::unique_ptr<RegisterLine> branch_line;
1861 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001862
Sebastien Hertz5243e912013-05-21 10:55:07 +02001863 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001864 case Instruction::NOP:
1865 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001866 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001867 * a signature that looks like a NOP; if we see one of these in
1868 * the course of executing code then we have a problem.
1869 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001870 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001872 }
1873 break;
1874
1875 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001876 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001877 break;
jeffhaobdb76512011-09-07 11:43:16 -07001878 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001880 break;
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001882 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001883 break;
1884 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001885 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 break;
jeffhaobdb76512011-09-07 11:43:16 -07001887 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001888 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001889 break;
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001891 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001894 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 break;
jeffhaobdb76512011-09-07 11:43:16 -07001896 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001898 break;
jeffhaobdb76512011-09-07 11:43:16 -07001899 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001900 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
1902
1903 /*
1904 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001906 * might want to hold the result in an actual CPU register, so the
1907 * Dalvik spec requires that these only appear immediately after an
1908 * invoke or filled-new-array.
1909 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001910 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001911 * redundant with the reset done below, but it can make the debug info
1912 * easier to read in some cases.)
1913 */
1914 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001915 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001916 break;
1917 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001919 break;
1920 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
1923
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001925 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1926 // where one entrypoint to the catch block is not actually an exception path.
1927 if (work_insn_idx_ == 0) {
1928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1929 break;
1930 }
jeffhaobdb76512011-09-07 11:43:16 -07001931 /*
jeffhao60f83e32012-02-13 17:16:30 -08001932 * This statement can only appear as the first instruction in an exception handler. We verify
1933 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001934 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001935 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001936 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001937 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 }
jeffhaobdb76512011-09-07 11:43:16 -07001939 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001940 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001941 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001942 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 }
jeffhaobdb76512011-09-07 11:43:16 -07001944 }
1945 break;
1946 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001947 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001948 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001949 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001950 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1952 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 } else {
1954 // Compilers may generate synthetic functions that write byte values into boolean fields.
1955 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001956 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001957 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1959 ((return_type.IsBoolean() || return_type.IsByte() ||
1960 return_type.IsShort() || return_type.IsChar()) &&
1961 src_type.IsInteger()));
1962 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001963 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001964 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001965 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 }
jeffhaobdb76512011-09-07 11:43:16 -07001968 }
1969 }
1970 break;
1971 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001972 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001973 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001974 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 } else {
1978 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001979 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001980 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001981 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
jeffhaobdb76512011-09-07 11:43:16 -07001984 }
1985 }
1986 break;
1987 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001988 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001989 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 } else {
1993 /* return_type is the *expected* return type, not register value */
1994 DCHECK(!return_type.IsZero());
1995 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001997 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07001998 // Disallow returning undefined, conflict & uninitialized values and verify that the
1999 // reference in vAA is an instance of the "return_type."
2000 if (reg_type.IsUndefined()) {
2001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2002 } else if (reg_type.IsConflict()) {
2003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2004 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002005 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2006 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002007 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002008 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2009 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2010 << "' or '" << reg_type << "'";
2011 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002012 bool soft_error = false;
2013 // Check whether arrays are involved. They will show a valid class status, even
2014 // if their components are erroneous.
2015 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2016 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2017 if (soft_error) {
2018 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2019 << reg_type << " vs " << return_type;
2020 }
2021 }
2022
2023 if (!soft_error) {
2024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2025 << "', but expected from declaration '" << return_type << "'";
2026 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002027 }
jeffhaobdb76512011-09-07 11:43:16 -07002028 }
2029 }
2030 }
2031 break;
2032
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002033 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 case Instruction::CONST_4: {
2035 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002036 work_line_->SetRegisterType<LockOp::kClear>(
2037 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002038 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 }
2040 case Instruction::CONST_16: {
2041 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002042 work_line_->SetRegisterType<LockOp::kClear>(
2043 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002044 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002046 case Instruction::CONST: {
2047 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002048 work_line_->SetRegisterType<LockOp::kClear>(
2049 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002051 }
2052 case Instruction::CONST_HIGH16: {
2053 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002054 work_line_->SetRegisterType<LockOp::kClear>(
2055 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002057 }
jeffhaobdb76512011-09-07 11:43:16 -07002058 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002059 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002061 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2062 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002065 }
2066 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002068 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2069 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002070 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002071 break;
2072 }
2073 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002075 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2076 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002077 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002078 break;
2079 }
2080 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002082 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2083 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002085 break;
2086 }
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002088 work_line_->SetRegisterType<LockOp::kClear>(
2089 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 break;
jeffhaobdb76512011-09-07 11:43:16 -07002091 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002092 work_line_->SetRegisterType<LockOp::kClear>(
2093 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002096 // Get type from instruction if unresolved then we need an access check
2097 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002098 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002099 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002100 work_line_->SetRegisterType<LockOp::kClear>(
2101 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2102 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002107 // Check whether the previous instruction is a move-object with vAA as a source, creating
2108 // untracked lock aliasing.
2109 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2110 uint32_t prev_idx = work_insn_idx_ - 1;
2111 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2112 prev_idx--;
2113 }
2114 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2115 switch (prev_inst->Opcode()) {
2116 case Instruction::MOVE_OBJECT:
2117 case Instruction::MOVE_OBJECT_16:
2118 case Instruction::MOVE_OBJECT_FROM16:
2119 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2120 // Redo the copy. This won't change the register types, but update the lock status
2121 // for the aliased register.
2122 work_line_->CopyRegister1(this,
2123 prev_inst->VRegA(),
2124 prev_inst->VRegB(),
2125 kTypeCategoryRef);
2126 }
2127 break;
2128
2129 default: // Other instruction types ignored.
2130 break;
2131 }
2132 }
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134 case Instruction::MONITOR_EXIT:
2135 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002136 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002137 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002138 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002139 * to the need to handle asynchronous exceptions, a now-deprecated
2140 * feature that Dalvik doesn't support.)
2141 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002142 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002143 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002144 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002145 * structured locking checks are working, the former would have
2146 * failed on the -enter instruction, and the latter is impossible.
2147 *
2148 * This is fortunate, because issue 3221411 prevents us from
2149 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002150 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002151 * some catch blocks (which will show up as "dead" code when
2152 * we skip them here); if we can't, then the code path could be
2153 * "live" so we still need to check it.
2154 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002155 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002156 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
2158
Ian Rogers28ad40d2011-10-27 15:19:26 -07002159 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002161 /*
2162 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2163 * could be a "upcast" -- not expected, so we don't try to address it.)
2164 *
2165 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002166 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002167 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2169 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002170 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002171 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002172 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002173 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002174 if (klass != nullptr && klass->IsPrimitive()) {
2175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2176 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2177 << GetDeclaringClass();
2178 break;
2179 }
2180
Ian Rogersad0b3a32012-04-16 14:50:24 -07002181 DCHECK_NE(failures_.size(), 0U);
2182 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002183 work_line_->SetRegisterType<LockOp::kClear>(this,
2184 inst->VRegA_22c(),
2185 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002186 }
2187 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002188 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002189 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002190 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002191 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002192 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002193 if (is_checkcast) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2195 } else {
2196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2197 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002198 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 if (is_checkcast) {
2200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2201 } else {
2202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2203 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002204 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002205 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002206 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002207 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002208 work_line_->SetRegisterType<LockOp::kClear>(this,
2209 inst->VRegA_22c(),
2210 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002211 }
jeffhaobdb76512011-09-07 11:43:16 -07002212 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 }
2215 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002216 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002217 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002218 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002220 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002221 work_line_->SetRegisterType<LockOp::kClear>(this,
2222 inst->VRegA_12x(),
2223 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002225 } else {
2226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 }
2228 break;
2229 }
2230 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002231 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002232 if (res_type.IsConflict()) {
2233 DCHECK_NE(failures_.size(), 0U);
2234 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002235 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2237 // can't create an instance of an interface or abstract class */
2238 if (!res_type.IsInstantiableTypes()) {
2239 Fail(VERIFY_ERROR_INSTANTIATION)
2240 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002241 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002242 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002243 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002244 // Any registers holding previous allocations from this address that have not yet been
2245 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002246 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002247 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002248 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 break;
2250 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002251 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002253 break;
2254 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002256 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002257 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002258 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002260 just_set_result = true; // Filled new array range sets result register
2261 break;
jeffhaobdb76512011-09-07 11:43:16 -07002262 case Instruction::CMPL_FLOAT:
2263 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002264 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002265 break;
2266 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002267 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002268 break;
2269 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002270 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002271 break;
2272 case Instruction::CMPL_DOUBLE:
2273 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002274 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002275 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002276 break;
2277 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002278 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002279 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002280 break;
2281 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002282 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002283 break;
2284 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002286 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002287 break;
2288 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002289 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002290 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002291 break;
2292 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002293 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002295 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002296 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002297 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002298 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2299 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002300 }
2301 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 }
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::GOTO:
2304 case Instruction::GOTO_16:
2305 case Instruction::GOTO_32:
2306 /* no effect on or use of registers */
2307 break;
2308
2309 case Instruction::PACKED_SWITCH:
2310 case Instruction::SPARSE_SWITCH:
2311 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002313 break;
2314
Ian Rogersd81871c2011-10-03 13:57:23 -07002315 case Instruction::FILL_ARRAY_DATA: {
2316 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002318 /* array_type can be null if the reg type is Zero */
2319 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002320 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2322 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002323 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002324 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002325 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002326 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2328 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 } else {
jeffhao457cc512012-02-02 16:55:13 -08002330 // Now verify if the element width in the table matches the element width declared in
2331 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002332 const uint16_t* array_data =
2333 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002334 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002336 } else {
2337 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2338 // Since we don't compress the data in Dex, expect to see equal width of data stored
2339 // in the table and expected from the array class.
2340 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2342 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002343 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 }
2345 }
jeffhaobdb76512011-09-07 11:43:16 -07002346 }
2347 }
2348 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002349 }
jeffhaobdb76512011-09-07 11:43:16 -07002350 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002352 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2353 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 bool mismatch = false;
2355 if (reg_type1.IsZero()) { // zero then integral or reference expected
2356 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2357 } else if (reg_type1.IsReferenceTypes()) { // both references?
2358 mismatch = !reg_type2.IsReferenceTypes();
2359 } else { // both integral?
2360 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2361 }
2362 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2364 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002365 }
2366 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 }
jeffhaobdb76512011-09-07 11:43:16 -07002368 case Instruction::IF_LT:
2369 case Instruction::IF_GE:
2370 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002372 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2373 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002374 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2376 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002377 }
2378 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 }
jeffhaobdb76512011-09-07 11:43:16 -07002380 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002381 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002382 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2385 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002387
2388 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002389 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002390 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002391 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002392 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002393 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002394 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002395 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2396 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2397 work_insn_idx_)) {
2398 break;
2399 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002400 } else {
2401 break;
2402 }
2403
Ian Rogers9b360392013-06-06 14:45:07 -07002404 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002405
2406 /* Check for peep-hole pattern of:
2407 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002408 * instance-of vX, vY, T;
2409 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002410 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002411 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002412 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002413 * and sharpen the type of vY to be type T.
2414 * Note, this pattern can't be if:
2415 * - if there are other branches to this branch,
2416 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002417 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002418 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002419 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2420 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2421 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002422 // Check the type of the instance-of is different than that of registers type, as if they
2423 // are the same there is no work to be done here. Check that the conversion is not to or
2424 // from an unresolved type as type information is imprecise. If the instance-of is to an
2425 // interface then ignore the type information as interfaces can only be treated as Objects
2426 // and we don't want to disallow field and other operations on the object. If the value
2427 // being instance-of checked against is known null (zero) then allow the optimization as
2428 // we didn't have type information. If the merge of the instance-of type with the original
2429 // type is assignable to the original then allow optimization. This check is performed to
2430 // ensure that subsequent merges don't lose type information - such as becoming an
2431 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002433 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002434
Ian Rogersebbdd872014-07-07 23:53:08 -07002435 if (!orig_type.Equals(cast_type) &&
2436 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002437 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002438 !cast_type.GetClass()->IsInterface() &&
2439 (orig_type.IsZero() ||
2440 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002441 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002442 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002443 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002444 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002445 branch_line.reset(update_line);
2446 }
2447 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002448 update_line->SetRegisterType<LockOp::kKeep>(this,
2449 instance_of_inst->VRegB_22c(),
2450 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002451 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2452 // See if instance-of was preceded by a move-object operation, common due to the small
2453 // register encoding space of instance-of, and propagate type information to the source
2454 // of the move-object.
2455 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002456 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002457 move_idx--;
2458 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002459 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2460 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2461 work_insn_idx_)) {
2462 break;
2463 }
Ian Rogers9b360392013-06-06 14:45:07 -07002464 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2465 switch (move_inst->Opcode()) {
2466 case Instruction::MOVE_OBJECT:
2467 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002468 update_line->SetRegisterType<LockOp::kKeep>(this,
2469 move_inst->VRegB_12x(),
2470 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002471 }
2472 break;
2473 case Instruction::MOVE_OBJECT_FROM16:
2474 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002475 update_line->SetRegisterType<LockOp::kKeep>(this,
2476 move_inst->VRegB_22x(),
2477 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002478 }
2479 break;
2480 case Instruction::MOVE_OBJECT_16:
2481 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002482 update_line->SetRegisterType<LockOp::kKeep>(this,
2483 move_inst->VRegB_32x(),
2484 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002485 }
2486 break;
2487 default:
2488 break;
2489 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002490 }
2491 }
2492 }
2493
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002495 }
jeffhaobdb76512011-09-07 11:43:16 -07002496 case Instruction::IF_LTZ:
2497 case Instruction::IF_GEZ:
2498 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002500 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2503 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 }
jeffhaobdb76512011-09-07 11:43:16 -07002505 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 }
jeffhaobdb76512011-09-07 11:43:16 -07002507 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002508 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002509 break;
jeffhaobdb76512011-09-07 11:43:16 -07002510 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002512 break;
jeffhaobdb76512011-09-07 11:43:16 -07002513 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 break;
jeffhaobdb76512011-09-07 11:43:16 -07002516 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002520 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002521 break;
jeffhaobdb76512011-09-07 11:43:16 -07002522 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002523 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 break;
2525 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002526 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002527 break;
2528
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 break;
2532 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002533 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 break;
2535 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002536 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 break;
2538 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002539 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002545 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002548 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002549 break;
2550
jeffhaobdb76512011-09-07 11:43:16 -07002551 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002552 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
jeffhaobdb76512011-09-07 11:43:16 -07002554 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002555 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 break;
jeffhaobdb76512011-09-07 11:43:16 -07002557 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002558 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 break;
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002561 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
2563 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002564 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002567 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002570 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2571 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
jeffhaobdb76512011-09-07 11:43:16 -07002573
Ian Rogersd81871c2011-10-03 13:57:23 -07002574 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002575 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 break;
2577 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002578 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 break;
2580 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002581 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002582 break;
2583 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002584 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002587 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
2589 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002590 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 break;
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002593 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2594 false);
jeffhaobdb76512011-09-07 11:43:16 -07002595 break;
2596
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002598 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002599 break;
jeffhaobdb76512011-09-07 11:43:16 -07002600 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002601 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 break;
jeffhaobdb76512011-09-07 11:43:16 -07002603 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002604 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002605 break;
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002607 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 break;
2609 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002610 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002613 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
2615 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002616 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2617 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
2619
2620 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002621 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 break;
2623 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002624 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 break;
2626 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002627 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 break;
2629 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002630 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002631 break;
2632 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002633 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002636 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002639 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2640 true);
jeffhaobdb76512011-09-07 11:43:16 -07002641 break;
2642
2643 case Instruction::INVOKE_VIRTUAL:
2644 case Instruction::INVOKE_VIRTUAL_RANGE:
2645 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002647 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2648 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002649 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2650 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002651 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002652 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002653 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002654 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2655 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2656 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002657 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002658 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2659 return_type_class,
2660 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002661 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002662 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2663 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002664 }
2665 }
2666 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002667 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002668 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2669 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002670 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002671 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002672 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002673 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002674 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002675 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002676 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002677 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002678 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 }
jeffhaobdb76512011-09-07 11:43:16 -07002681 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002683 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002684 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002685 const char* return_type_descriptor;
2686 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002687 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002688 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002689 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002690 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002691 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002692 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2693 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2694 } else {
2695 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002696 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002697 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2698 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2699 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002700 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002701 return_type = &FromClass(return_type_descriptor,
2702 return_type_class,
2703 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002704 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002705 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2706 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002707 }
Ian Rogers46685432012-06-03 22:26:43 -07002708 }
2709 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002710 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 * Some additional checks when calling a constructor. We know from the invocation arg check
2712 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2713 * that to require that called_method->klass is the same as this->klass or this->super,
2714 * allowing the latter only if the "this" argument is the same as the "this" argument to
2715 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002716 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002717 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002718 if (this_type.IsConflict()) // failure.
2719 break;
jeffhaobdb76512011-09-07 11:43:16 -07002720
jeffhaob57e9522012-04-26 18:08:21 -07002721 /* no null refs allowed (?) */
2722 if (this_type.IsZero()) {
2723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2724 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002725 }
jeffhaob57e9522012-04-26 18:08:21 -07002726
2727 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002728 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002729 // TODO: re-enable constructor type verification
2730 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002731 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002732 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2733 // break;
2734 // }
jeffhaob57e9522012-04-26 18:08:21 -07002735
2736 /* arg must be an uninitialized reference */
2737 if (!this_type.IsUninitializedTypes()) {
2738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2739 << this_type;
2740 break;
2741 }
2742
2743 /*
2744 * Replace the uninitialized reference with an initialized one. We need to do this for all
2745 * registers that have the same object instance in them, not just the "this" register.
2746 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002747 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2748 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002749 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002750 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002751 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2752 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002753 }
2754 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002756 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002757 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002758 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002759 just_set_result = true;
2760 break;
2761 }
2762 case Instruction::INVOKE_STATIC:
2763 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002764 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002765 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002766 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002767 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002768 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002769 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2770 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002771 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002772 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002773 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002774 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002775 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002776 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002777 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002778 } else {
2779 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2780 }
jeffhaobdb76512011-09-07 11:43:16 -07002781 just_set_result = true;
2782 }
2783 break;
jeffhaobdb76512011-09-07 11:43:16 -07002784 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002786 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002787 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002788 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002789 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002790 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2791 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2792 << PrettyMethod(abs_method) << "'";
2793 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002794 }
Ian Rogers0d604842012-04-16 14:50:24 -07002795 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002796 /* Get the type of the "this" arg, which should either be a sub-interface of called
2797 * interface or Object (see comments in RegType::JoinClass).
2798 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002799 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002800 if (this_type.IsZero()) {
2801 /* null pointer always passes (and always fails at runtime) */
2802 } else {
2803 if (this_type.IsUninitializedTypes()) {
2804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2805 << this_type;
2806 break;
2807 }
2808 // In the past we have tried to assert that "called_interface" is assignable
2809 // from "this_type.GetClass()", however, as we do an imprecise Join
2810 // (RegType::JoinClass) we don't have full information on what interfaces are
2811 // implemented by "this_type". For example, two classes may implement the same
2812 // interfaces and have a common parent that doesn't implement the interface. The
2813 // join will set "this_type" to the parent class and a test that this implements
2814 // the interface will incorrectly fail.
2815 }
2816 /*
2817 * We don't have an object instance, so we can't find the concrete method. However, all of
2818 * the type information is in the abstract method, so we're good.
2819 */
2820 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002821 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2824 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2825 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2826 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002827 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002828 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002829 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002830 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002831 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002832 } else {
2833 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2834 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002836 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002837 }
jeffhaobdb76512011-09-07 11:43:16 -07002838 case Instruction::NEG_INT:
2839 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002840 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002841 break;
2842 case Instruction::NEG_LONG:
2843 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002844 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002845 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002846 break;
2847 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002848 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002849 break;
2850 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002851 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002852 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002853 break;
2854 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002855 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002856 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002857 break;
2858 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002860 break;
2861 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002862 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002863 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002864 break;
2865 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002866 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002867 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002868 break;
2869 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002870 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002871 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002872 break;
2873 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002874 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002875 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
2877 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002882 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002883 break;
2884 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002885 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002886 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002887 break;
2888 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002889 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002890 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002891 break;
2892 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002899 break;
2900 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002901 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002905 break;
2906 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002907 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002908 break;
2909
2910 case Instruction::ADD_INT:
2911 case Instruction::SUB_INT:
2912 case Instruction::MUL_INT:
2913 case Instruction::REM_INT:
2914 case Instruction::DIV_INT:
2915 case Instruction::SHL_INT:
2916 case Instruction::SHR_INT:
2917 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002918 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002919 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002920 break;
2921 case Instruction::AND_INT:
2922 case Instruction::OR_INT:
2923 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002924 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002925 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002926 break;
2927 case Instruction::ADD_LONG:
2928 case Instruction::SUB_LONG:
2929 case Instruction::MUL_LONG:
2930 case Instruction::DIV_LONG:
2931 case Instruction::REM_LONG:
2932 case Instruction::AND_LONG:
2933 case Instruction::OR_LONG:
2934 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002936 reg_types_.LongLo(), reg_types_.LongHi(),
2937 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002938 break;
2939 case Instruction::SHL_LONG:
2940 case Instruction::SHR_LONG:
2941 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002942 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002943 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002944 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002945 break;
2946 case Instruction::ADD_FLOAT:
2947 case Instruction::SUB_FLOAT:
2948 case Instruction::MUL_FLOAT:
2949 case Instruction::DIV_FLOAT:
2950 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2952 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002953 break;
2954 case Instruction::ADD_DOUBLE:
2955 case Instruction::SUB_DOUBLE:
2956 case Instruction::MUL_DOUBLE:
2957 case Instruction::DIV_DOUBLE:
2958 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002960 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2961 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002962 break;
2963 case Instruction::ADD_INT_2ADDR:
2964 case Instruction::SUB_INT_2ADDR:
2965 case Instruction::MUL_INT_2ADDR:
2966 case Instruction::REM_INT_2ADDR:
2967 case Instruction::SHL_INT_2ADDR:
2968 case Instruction::SHR_INT_2ADDR:
2969 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002970 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2971 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002972 break;
2973 case Instruction::AND_INT_2ADDR:
2974 case Instruction::OR_INT_2ADDR:
2975 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002976 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2977 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002978 break;
2979 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002980 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2981 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002982 break;
2983 case Instruction::ADD_LONG_2ADDR:
2984 case Instruction::SUB_LONG_2ADDR:
2985 case Instruction::MUL_LONG_2ADDR:
2986 case Instruction::DIV_LONG_2ADDR:
2987 case Instruction::REM_LONG_2ADDR:
2988 case Instruction::AND_LONG_2ADDR:
2989 case Instruction::OR_LONG_2ADDR:
2990 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002991 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002992 reg_types_.LongLo(), reg_types_.LongHi(),
2993 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002994 break;
2995 case Instruction::SHL_LONG_2ADDR:
2996 case Instruction::SHR_LONG_2ADDR:
2997 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002999 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003000 break;
3001 case Instruction::ADD_FLOAT_2ADDR:
3002 case Instruction::SUB_FLOAT_2ADDR:
3003 case Instruction::MUL_FLOAT_2ADDR:
3004 case Instruction::DIV_FLOAT_2ADDR:
3005 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003006 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3007 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003008 break;
3009 case Instruction::ADD_DOUBLE_2ADDR:
3010 case Instruction::SUB_DOUBLE_2ADDR:
3011 case Instruction::MUL_DOUBLE_2ADDR:
3012 case Instruction::DIV_DOUBLE_2ADDR:
3013 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003015 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3016 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003017 break;
3018 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003019 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003020 case Instruction::MUL_INT_LIT16:
3021 case Instruction::DIV_INT_LIT16:
3022 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003023 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3024 true);
jeffhaobdb76512011-09-07 11:43:16 -07003025 break;
3026 case Instruction::AND_INT_LIT16:
3027 case Instruction::OR_INT_LIT16:
3028 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3030 true);
jeffhaobdb76512011-09-07 11:43:16 -07003031 break;
3032 case Instruction::ADD_INT_LIT8:
3033 case Instruction::RSUB_INT_LIT8:
3034 case Instruction::MUL_INT_LIT8:
3035 case Instruction::DIV_INT_LIT8:
3036 case Instruction::REM_INT_LIT8:
3037 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003038 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003039 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3041 false);
jeffhaobdb76512011-09-07 11:43:16 -07003042 break;
3043 case Instruction::AND_INT_LIT8:
3044 case Instruction::OR_INT_LIT8:
3045 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3047 false);
jeffhaobdb76512011-09-07 11:43:16 -07003048 break;
3049
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003050 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003051 case Instruction::RETURN_VOID_NO_BARRIER:
3052 if (IsConstructor() && !IsStatic()) {
3053 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003054 if (declaring_class.IsUnresolvedReference()) {
3055 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3056 // manually over the underlying dex file.
3057 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3058 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3059 if (first_index != DexFile::kDexNoIndex) {
3060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3061 << first_index;
3062 }
3063 break;
3064 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003065 auto* klass = declaring_class.GetClass();
3066 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3067 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3069 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003070 break;
3071 }
3072 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003073 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003074 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3075 // quickened opcodes (otherwise this could be a fall-through).
3076 if (!IsConstructor()) {
3077 if (!GetMethodReturnType().IsConflict()) {
3078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3079 }
3080 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003081 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003082 // Note: the following instructions encode offsets derived from class linking.
3083 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3084 // meaning if the class linking and resolution were successful.
3085 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003086 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003087 break;
3088 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003089 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003090 break;
3091 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003092 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003093 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003094 case Instruction::IGET_BOOLEAN_QUICK:
3095 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3096 break;
3097 case Instruction::IGET_BYTE_QUICK:
3098 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3099 break;
3100 case Instruction::IGET_CHAR_QUICK:
3101 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3102 break;
3103 case Instruction::IGET_SHORT_QUICK:
3104 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3105 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003106 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003107 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003108 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003109 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003110 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003111 break;
3112 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003113 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003114 break;
3115 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003116 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003117 break;
3118 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003119 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003120 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003121 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003122 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003123 break;
3124 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003125 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003126 break;
3127 case Instruction::INVOKE_VIRTUAL_QUICK:
3128 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3129 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003130 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003131 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003132 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003133 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003134 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003135 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003136 } else {
3137 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3138 }
3139 just_set_result = true;
3140 }
3141 break;
3142 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003143 case Instruction::INVOKE_LAMBDA: {
3144 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3145 // If the code would've normally hard-failed, then the interpreter will throw the
3146 // appropriate verification errors at runtime.
3147 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3148 break;
3149 }
3150 case Instruction::CREATE_LAMBDA: {
3151 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3152 // If the code would've normally hard-failed, then the interpreter will throw the
3153 // appropriate verification errors at runtime.
3154 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3155 break;
3156 }
3157
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003158 case Instruction::UNUSED_F4:
3159 case Instruction::UNUSED_F5:
3160 case Instruction::UNUSED_F7: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003161 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003162 // Conservatively fail verification on release builds.
3163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3164 break;
3165 }
3166
3167 case Instruction::BOX_LAMBDA: {
3168 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3169 // If the code would've normally hard-failed, then the interpreter will throw the
3170 // appropriate verification errors at runtime.
3171 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003172
3173 // Partial verification. Sets the resulting type to always be an object, which
3174 // is good enough for some other verification to occur without hard-failing.
3175 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3176 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003177 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003178 break;
3179 }
3180
3181 case Instruction::UNBOX_LAMBDA: {
3182 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3183 // If the code would've normally hard-failed, then the interpreter will throw the
3184 // appropriate verification errors at runtime.
3185 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3186 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003187 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003188
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003190 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003191 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003192 case Instruction::UNUSED_79:
3193 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003195 break;
3196
3197 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003198 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003199 * complain if an instruction is missing (which is desirable).
3200 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003201 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003202
Ian Rogersad0b3a32012-04-16 14:50:24 -07003203 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003204 if (Runtime::Current()->IsAotCompiler()) {
3205 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003206 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3207 LOG(ERROR) << "Pending failures:";
3208 for (auto& error : failures_) {
3209 LOG(ERROR) << error;
3210 }
3211 for (auto& error_msg : failure_messages_) {
3212 LOG(ERROR) << error_msg->str();
3213 }
3214 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3215 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003216 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003217 /* immediate failure, reject class */
3218 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3219 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003220 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003221 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003222 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003223 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3224 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3225 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003226 }
jeffhaobdb76512011-09-07 11:43:16 -07003227 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003228 * If we didn't just set the result register, clear it out. This ensures that you can only use
3229 * "move-result" immediately after the result is set. (We could check this statically, but it's
3230 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003231 */
3232 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003234 }
3235
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003236
jeffhaobdb76512011-09-07 11:43:16 -07003237
3238 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003239 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003240 *
3241 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003242 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003243 * somebody could get a reference field, check it for zero, and if the
3244 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003245 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003246 * that, and will reject the code.
3247 *
3248 * TODO: avoid re-fetching the branch target
3249 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003250 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003251 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003252 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003253 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003255 return false;
3256 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003257 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003258 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003259 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003260 }
jeffhaobdb76512011-09-07 11:43:16 -07003261 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003262 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003263 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003264 return false;
3265 }
3266 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003267 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003268 return false;
3269 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 }
jeffhaobdb76512011-09-07 11:43:16 -07003271 }
3272
3273 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003274 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003275 *
3276 * We've already verified that the table is structurally sound, so we
3277 * just need to walk through and tag the targets.
3278 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003279 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003280 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003281 const uint16_t* switch_insns = insns + offset_to_switch;
3282 int switch_count = switch_insns[1];
3283 int offset_to_targets, targ;
3284
3285 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3286 /* 0 = sig, 1 = count, 2/3 = first key */
3287 offset_to_targets = 4;
3288 } else {
3289 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003290 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003291 offset_to_targets = 2 + 2 * switch_count;
3292 }
3293
3294 /* verify each switch target */
3295 for (targ = 0; targ < switch_count; targ++) {
3296 int offset;
3297 uint32_t abs_offset;
3298
3299 /* offsets are 32-bit, and only partly endian-swapped */
3300 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003301 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 abs_offset = work_insn_idx_ + offset;
3303 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003304 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003305 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003307 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003308 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003309 }
jeffhaobdb76512011-09-07 11:43:16 -07003310 }
3311 }
3312
3313 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003314 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3315 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003316 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003317 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003318 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003319 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003320
Andreas Gampef91baf12014-07-18 15:41:00 -07003321 // Need the linker to try and resolve the handled class to check if it's Throwable.
3322 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3323
Ian Rogers0571d352011-11-03 19:51:38 -07003324 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003325 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3326 if (handler_type_idx == DexFile::kDexNoIndex16) {
3327 has_catch_all_handler = true;
3328 } else {
3329 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003330 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3331 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003332 if (klass != nullptr) {
3333 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3334 has_catch_all_handler = true;
3335 }
3336 } else {
3337 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003338 DCHECK(self_->IsExceptionPending());
3339 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003340 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003341 }
jeffhaobdb76512011-09-07 11:43:16 -07003342 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003343 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3344 * "work_regs", because at runtime the exception will be thrown before the instruction
3345 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003346 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003347 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003348 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 }
jeffhaobdb76512011-09-07 11:43:16 -07003350 }
3351
3352 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3354 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003355 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003356 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003357 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 * The state in work_line reflects the post-execution state. If the current instruction is a
3359 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003360 * it will do so before grabbing the lock).
3361 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003362 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003363 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003365 return false;
3366 }
3367 }
3368 }
3369
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003370 /* Handle "continue". Tag the next consecutive instruction.
3371 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3372 * because it changes work_line_ when performing peephole optimization
3373 * and this change should not be used in those cases.
3374 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003375 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003376 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3377 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003378 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3380 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003381 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003382 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3383 // next instruction isn't one.
3384 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3385 return false;
3386 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003387 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003388 // Make workline consistent with fallthrough computed from peephole optimization.
3389 work_line_->CopyFromLine(fallthrough_line.get());
3390 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003391 if (insn_flags_[next_insn_idx].IsReturn()) {
3392 // For returns we only care about the operand to the return, all other registers are dead.
3393 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003394 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003395 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003396 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003397 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003398 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3399 // needed. If the merge changes the state of the registers then the work line will be
3400 // updated.
3401 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003402 return false;
3403 }
3404 } else {
3405 /*
3406 * We're not recording register data for the next instruction, so we don't know what the
3407 * prior state was. We have to assume that something has changed and re-evaluate it.
3408 */
3409 insn_flags_[next_insn_idx].SetChanged();
3410 }
3411 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003412
jeffhaod1f0fde2011-09-08 17:25:33 -07003413 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003414 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003415 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003416 }
3417
3418 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003419 * Update start_guess. Advance to the next instruction of that's
3420 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003421 * neither of those exists we're in a return or throw; leave start_guess
3422 * alone and let the caller sort it out.
3423 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003424 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003425 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3426 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003427 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003428 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003430 }
3431
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3433 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003434
Andreas Gampea727e372015-08-25 09:22:37 -07003435 if (have_pending_runtime_throw_failure_) {
3436 have_any_pending_runtime_throw_failure_ = true;
3437 // Reset the pending_runtime_throw flag now.
3438 have_pending_runtime_throw_failure_ = false;
3439 }
3440
jeffhaobdb76512011-09-07 11:43:16 -07003441 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003442} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003443
Ian Rogersd8f69b02014-09-10 21:43:52 +00003444const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003445 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003446 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003447 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003448 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003449 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003450 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003451 if (result.IsConflict()) {
3452 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3453 << "' in " << referrer;
3454 return result;
3455 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003457 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003458 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003460 // check at runtime if access is allowed and so pass here. If result is
3461 // primitive, skip the access check.
3462 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3463 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003464 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003466 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003468}
3469
Ian Rogersd8f69b02014-09-10 21:43:52 +00003470const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003471 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003472 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003473 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3475 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003476 CatchHandlerIterator iterator(handlers_ptr);
3477 for (; iterator.HasNext(); iterator.Next()) {
3478 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3479 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003480 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003482 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003483 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003484 if (exception.IsUnresolvedTypes()) {
3485 // We don't know enough about the type. Fail here and let runtime handle it.
3486 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3487 return exception;
3488 } else {
3489 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3490 return reg_types_.Conflict();
3491 }
Jeff Haob878f212014-04-24 16:25:36 -07003492 } else if (common_super == nullptr) {
3493 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003494 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003495 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003497 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003498 if (FailOrAbort(this,
3499 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3500 "java.lang.Throwable is not assignable-from common_super at ",
3501 work_insn_idx_)) {
3502 break;
3503 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 }
3505 }
3506 }
3507 }
Ian Rogers0571d352011-11-03 19:51:38 -07003508 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 }
3510 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003511 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003513 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003514 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003515 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003516 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003517}
3518
Mathieu Chartiere401d142015-04-22 13:56:20 -07003519ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3520 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003521 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003522 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 if (klass_type.IsConflict()) {
3524 std::string append(" in attempt to access method ");
3525 append += dex_file_->GetMethodName(method_id);
3526 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003527 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003528 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003529 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003530 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003531 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003532 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003533 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003534 auto* cl = Runtime::Current()->GetClassLinker();
3535 auto pointer_size = cl->GetImagePointerSize();
3536 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003537 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003538 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003539 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003540
3541 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003542 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003543 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003544 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003545 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003546 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003548 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003549 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003551 // If a virtual or interface method wasn't found with the expected type, look in
3552 // the direct methods. This can happen when the wrong invoke type is used or when
3553 // a class has changed, and will be flagged as an error in later checks.
3554 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003555 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003556 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003557 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003558 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3559 << PrettyDescriptor(klass) << "." << name
3560 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003562 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003563 }
3564 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3566 // enforce them here.
3567 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3569 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003570 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003572 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003573 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3575 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003576 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003577 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003578 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003579 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003580 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003581 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003582 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003583 }
jeffhaode0d9c92012-02-27 13:58:13 -08003584 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3585 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003586 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3587 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003588 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003589 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003590 // Check that interface methods match interface classes.
3591 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3592 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3593 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003594 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003595 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3596 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3597 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003598 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003599 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 // See if the method type implied by the invoke instruction matches the access flags for the
3601 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003602 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003603 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3604 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3605 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003606 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3607 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003608 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003610 return res_method;
3611}
3612
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003613template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003614ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3615 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003616 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3617 // match the call to the signature. Also, we might be calling through an abstract method
3618 // definition (which doesn't have register count values).
3619 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3620 /* caught by static verifier */
3621 DCHECK(is_range || expected_args <= 5);
3622 if (expected_args > code_item_->outs_size_) {
3623 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3624 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3625 return nullptr;
3626 }
3627
3628 uint32_t arg[5];
3629 if (!is_range) {
3630 inst->GetVarArgs(arg);
3631 }
3632 uint32_t sig_registers = 0;
3633
3634 /*
3635 * Check the "this" argument, which must be an instance of the class that declared the method.
3636 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3637 * rigorous check here (which is okay since we have to do it at runtime).
3638 */
3639 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003640 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003641 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3642 CHECK(have_pending_hard_failure_);
3643 return nullptr;
3644 }
3645 if (actual_arg_type.IsUninitializedReference()) {
3646 if (res_method) {
3647 if (!res_method->IsConstructor()) {
3648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3649 return nullptr;
3650 }
3651 } else {
3652 // Check whether the name of the called method is "<init>"
3653 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003654 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3656 return nullptr;
3657 }
3658 }
3659 }
3660 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003661 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003662 // Miranda methods have the declaring interface as their declaring class, not the abstract
3663 // class. It would be wrong to use this for the type check (interface type checks are
3664 // postponed to runtime).
3665 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003666 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003667 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003668 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3669 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003670 } else {
3671 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3672 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003673 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003674 dex_file_->StringByTypeIdx(class_idx),
3675 false);
3676 }
3677 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3678 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3679 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3680 << "' not instance of '" << *res_method_class << "'";
3681 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3682 // the compiler.
3683 if (have_pending_hard_failure_) {
3684 return nullptr;
3685 }
3686 }
3687 }
3688 sig_registers = 1;
3689 }
3690
3691 for ( ; it->HasNext(); it->Next()) {
3692 if (sig_registers >= expected_args) {
3693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3694 " arguments, found " << sig_registers << " or more.";
3695 return nullptr;
3696 }
3697
3698 const char* param_descriptor = it->GetDescriptor();
3699
3700 if (param_descriptor == nullptr) {
3701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3702 "component";
3703 return nullptr;
3704 }
3705
Ian Rogersd8f69b02014-09-10 21:43:52 +00003706 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003707 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3708 arg[sig_registers];
3709 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003711 if (!src_type.IsIntegralTypes()) {
3712 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3713 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003714 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003715 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003716 } else {
3717 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3718 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3719 // the compiler.
3720 if (have_pending_hard_failure_) {
3721 return nullptr;
3722 }
3723 } else if (reg_type.IsLongOrDoubleTypes()) {
3724 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3725 // instructions not specifying register pairs by the first component, but require them
3726 // nonetheless. Only check when there's an actual register in the parameters. If there's
3727 // none, this will fail below.
3728 if (!is_range && sig_registers + 1 < expected_args) {
3729 uint32_t second_reg = arg[sig_registers + 1];
3730 if (second_reg != get_reg + 1) {
3731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3732 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3733 << second_reg << ".";
3734 return nullptr;
3735 }
3736 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003737 }
3738 }
3739 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3740 }
3741 if (expected_args != sig_registers) {
3742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3743 " arguments, found " << sig_registers;
3744 return nullptr;
3745 }
3746 return res_method;
3747}
3748
3749void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3750 MethodType method_type,
3751 bool is_range) {
3752 // As the method may not have been resolved, make this static check against what we expect.
3753 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3754 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3755 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3756 DexFileParameterIterator it(*dex_file_,
3757 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3758 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3759 nullptr);
3760}
3761
3762class MethodParamListDescriptorIterator {
3763 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003764 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003765 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3766 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3767 }
3768
3769 bool HasNext() {
3770 return pos_ < params_size_;
3771 }
3772
3773 void Next() {
3774 ++pos_;
3775 }
3776
Mathieu Chartier90443472015-07-16 20:32:27 -07003777 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003778 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3779 }
3780
3781 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003782 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003783 size_t pos_;
3784 const DexFile::TypeList* params_;
3785 const size_t params_size_;
3786};
3787
Mathieu Chartiere401d142015-04-22 13:56:20 -07003788ArtMethod* MethodVerifier::VerifyInvocationArgs(
3789 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003790 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3791 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003792 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003793
Mathieu Chartiere401d142015-04-22 13:56:20 -07003794 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003795 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003796 // Check what we can statically.
3797 if (!have_pending_hard_failure_) {
3798 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3799 }
3800 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003801 }
3802
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3804 // has a vtable entry for the target method.
3805 if (is_super) {
3806 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003807 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003808 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003809 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003810 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003811 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003812 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003813 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003814 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003815 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003816 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003817 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003818 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003819 << "." << res_method->GetName()
3820 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003821 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003822 }
3823 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003824
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003825 // Process the target method's signature. This signature may or may not
3826 MethodParamListDescriptorIterator it(res_method);
3827 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3828 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003829}
3830
Mathieu Chartiere401d142015-04-22 13:56:20 -07003831ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3832 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003833 if (is_range) {
3834 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3835 } else {
3836 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3837 }
3838 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003839 if (!actual_arg_type.HasClass()) {
3840 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003841 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003842 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003843 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003844 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003845 if (klass->IsInterface()) {
3846 // Derive Object.class from Class.class.getSuperclass().
3847 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003848 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003849 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003850 return nullptr;
3851 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003852 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003853 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003854 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003855 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003856 if (!dispatch_class->HasVTable()) {
3857 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3858 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003859 return nullptr;
3860 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003861 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003862 auto* cl = Runtime::Current()->GetClassLinker();
3863 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003864 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3865 FailOrAbort(this, allow_failure,
3866 "Receiver class has not enough vtable slots for quickened invoke at ",
3867 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003868 return nullptr;
3869 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003871 if (self_->IsExceptionPending()) {
3872 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3873 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003874 return nullptr;
3875 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003876 return res_method;
3877}
3878
Mathieu Chartiere401d142015-04-22 13:56:20 -07003879ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003880 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3881 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3882
Mathieu Chartiere401d142015-04-22 13:56:20 -07003883 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003886 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003887 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003888 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3889 work_insn_idx_)) {
3890 return nullptr;
3891 }
3892 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3893 work_insn_idx_)) {
3894 return nullptr;
3895 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003896
3897 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3898 // match the call to the signature. Also, we might be calling through an abstract method
3899 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003900 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003901 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003902 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003903 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003904 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3905 /* caught by static verifier */
3906 DCHECK(is_range || expected_args <= 5);
3907 if (expected_args > code_item_->outs_size_) {
3908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3909 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003910 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003911 }
3912
3913 /*
3914 * Check the "this" argument, which must be an instance of the class that declared the method.
3915 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3916 * rigorous check here (which is okay since we have to do it at runtime).
3917 */
3918 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3919 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003920 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003921 }
3922 if (!actual_arg_type.IsZero()) {
3923 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003924 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003925 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003926 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003927 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003928 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3929 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003930 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003931 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003932 }
3933 }
3934 /*
3935 * Process the target method's signature. This signature may or may not
3936 * have been verified, so we can't assume it's properly formed.
3937 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003938 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003939 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003940 uint32_t arg[5];
3941 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003942 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003943 }
3944 size_t actual_args = 1;
3945 for (size_t param_index = 0; param_index < params_size; param_index++) {
3946 if (actual_args >= expected_args) {
3947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003948 << "'. Expected " << expected_args
3949 << " arguments, processing argument " << actual_args
3950 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003952 }
3953 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003954 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003955 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003957 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003958 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003959 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003960 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003961 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003963 return res_method;
3964 }
3965 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3966 }
3967 if (actual_args != expected_args) {
3968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3969 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003970 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003971 } else {
3972 return res_method;
3973 }
3974}
3975
Ian Rogers62342ec2013-06-11 10:26:37 -07003976void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003977 uint32_t type_idx;
3978 if (!is_filled) {
3979 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3980 type_idx = inst->VRegC_22c();
3981 } else if (!is_range) {
3982 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3983 type_idx = inst->VRegB_35c();
3984 } else {
3985 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3986 type_idx = inst->VRegB_3rc();
3987 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003988 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003989 if (res_type.IsConflict()) { // bad class
3990 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003991 } else {
3992 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3993 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003995 } else if (!is_filled) {
3996 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003998 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003999 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004000 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004001 } else {
4002 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4003 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004004 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004005 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4006 uint32_t arg[5];
4007 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004008 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004009 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004010 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004011 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4013 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004014 return;
4015 }
4016 }
4017 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004018 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004019 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004020 }
4021 }
4022}
4023
Sebastien Hertz5243e912013-05-21 10:55:07 +02004024void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004025 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004026 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004027 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004028 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004029 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004030 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004031 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004032 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004033 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4034 // instruction type. TODO: have a proper notion of bottom here.
4035 if (!is_primitive || insn_type.IsCategory1Types()) {
4036 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004037 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004039 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4041 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004042 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004043 }
jeffhaofc3144e2012-02-01 17:21:15 -08004044 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004046 } else {
4047 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004048 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004049 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004051 << " source for aget-object";
4052 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004054 << " source for category 1 aget";
4055 } else if (is_primitive && !insn_type.Equals(component_type) &&
4056 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004057 (insn_type.IsLong() && component_type.IsDouble()))) {
4058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4059 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004060 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004061 // Use knowledge of the field type which is stronger than the type inferred from the
4062 // instruction, which can't differentiate object types and ints from floats, longs from
4063 // doubles.
4064 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004065 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004066 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004067 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004068 component_type.HighHalf(&reg_types_));
4069 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004070 }
4071 }
4072 }
4073}
4074
Ian Rogersd8f69b02014-09-10 21:43:52 +00004075void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004076 const uint32_t vregA) {
4077 // Primitive assignability rules are weaker than regular assignability rules.
4078 bool instruction_compatible;
4079 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004080 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004081 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004082 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004083 value_compatible = value_type.IsIntegralTypes();
4084 } else if (target_type.IsFloat()) {
4085 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4086 value_compatible = value_type.IsFloatTypes();
4087 } else if (target_type.IsLong()) {
4088 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004089 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4090 // as target_type depends on the resolved type of the field.
4091 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004092 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004093 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4094 } else {
4095 value_compatible = false;
4096 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004097 } else if (target_type.IsDouble()) {
4098 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004099 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4100 // as target_type depends on the resolved type of the field.
4101 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004103 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4104 } else {
4105 value_compatible = false;
4106 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004107 } else {
4108 instruction_compatible = false; // reference with primitive store
4109 value_compatible = false; // unused
4110 }
4111 if (!instruction_compatible) {
4112 // This is a global failure rather than a class change failure as the instructions and
4113 // the descriptors for the type should have been consistent within the same file at
4114 // compile time.
4115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4116 << "' but expected type '" << target_type << "'";
4117 return;
4118 }
4119 if (!value_compatible) {
4120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4121 << " of type " << value_type << " but expected " << target_type << " for put";
4122 return;
4123 }
4124}
4125
Sebastien Hertz5243e912013-05-21 10:55:07 +02004126void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004127 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004128 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004131 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004133 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004134 // Null array type; this code path will fail at runtime.
4135 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004136 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4137 // and fits multiple register types.
4138 const RegType* modified_reg_type = &insn_type;
4139 if ((modified_reg_type == &reg_types_.Integer()) ||
4140 (modified_reg_type == &reg_types_.LongLo())) {
4141 // May be integer or float | long or double. Overwrite insn_type accordingly.
4142 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4143 if (modified_reg_type == &reg_types_.Integer()) {
4144 if (&value_type == &reg_types_.Float()) {
4145 modified_reg_type = &value_type;
4146 }
4147 } else {
4148 if (&value_type == &reg_types_.DoubleLo()) {
4149 modified_reg_type = &value_type;
4150 }
4151 }
4152 }
4153 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004154 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004156 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004157 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004158 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004159 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004160 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004161 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004162 if (!component_type.IsReferenceTypes()) {
4163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4164 << " source for aput-object";
4165 } else {
4166 // The instruction agrees with the type of array, confirm the value to be stored does too
4167 // Note: we use the instruction type (rather than the component type) for aput-object as
4168 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004169 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004170 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004171 }
4172 }
4173 }
4174}
4175
Mathieu Chartierc7853442015-03-27 14:35:38 -07004176ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004177 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4178 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004179 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004180 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004181 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4182 field_idx, dex_file_->GetFieldName(field_id),
4183 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004184 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004185 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004186 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004187 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004188 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004190 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4191 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004193 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004194 << dex_file_->GetFieldName(field_id) << ") in "
4195 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004196 DCHECK(self_->IsExceptionPending());
4197 self_->ClearException();
4198 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004199 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4200 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004201 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004202 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004203 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004204 } else if (!field->IsStatic()) {
4205 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004206 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004207 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004208 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004209}
4210
Mathieu Chartierc7853442015-03-27 14:35:38 -07004211ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004212 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4213 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004214 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004215 if (klass_type.IsConflict()) {
4216 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4217 field_idx, dex_file_->GetFieldName(field_id),
4218 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004220 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004221 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004222 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004223 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004224 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004225 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4226 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004227 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004228 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004229 << dex_file_->GetFieldName(field_id) << ") in "
4230 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004231 DCHECK(self_->IsExceptionPending());
4232 self_->ClearException();
4233 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004234 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4235 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004236 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004237 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004238 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004239 } else if (field->IsStatic()) {
4240 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4241 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004242 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004243 } else if (obj_type.IsZero()) {
4244 // Cannot infer and check type, however, access will cause null pointer exception
4245 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004246 } else if (!obj_type.IsReferenceTypes()) {
4247 // Trying to read a field from something that isn't a reference
4248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4249 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004250 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004251 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004252 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004253 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004254 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4255 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004256 if (obj_type.IsUninitializedTypes() &&
4257 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4258 !field_klass.Equals(GetDeclaringClass()))) {
4259 // Field accesses through uninitialized references are only allowable for constructors where
4260 // the field is declared in this class
4261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004262 << " of a not fully initialized object within the context"
4263 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004264 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004265 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4266 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4267 // of C1. For resolution to occur the declared class of the field must be compatible with
4268 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4269 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4270 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004271 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004272 } else {
4273 return field;
4274 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004275 }
4276}
4277
Andreas Gampe896df402014-10-20 22:25:29 -07004278template <MethodVerifier::FieldAccessType kAccType>
4279void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4280 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004281 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004282 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004283 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004284 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004285 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004286 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004287 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004288 if (UNLIKELY(have_pending_hard_failure_)) {
4289 return;
4290 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004291 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004292 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004293 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004294 if (kAccType == FieldAccessType::kAccPut) {
4295 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4296 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4297 << " from other class " << GetDeclaringClass();
4298 return;
4299 }
4300 }
4301
Mathieu Chartierc7853442015-03-27 14:35:38 -07004302 mirror::Class* field_type_class =
4303 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004304 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004305 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4306 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004307 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004308 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4309 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004310 }
Ian Rogers0d604842012-04-16 14:50:24 -07004311 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004312 if (field_type == nullptr) {
4313 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4314 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004315 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004316 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004317 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004318 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004319 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4320 "Unexpected third access type");
4321 if (kAccType == FieldAccessType::kAccPut) {
4322 // sput or iput.
4323 if (is_primitive) {
4324 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004325 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004326 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004327 // If the field type is not a reference, this is a global failure rather than
4328 // a class change failure as the instructions and the descriptors for the type
4329 // should have been consistent within the same file at compile time.
4330 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4331 : VERIFY_ERROR_BAD_CLASS_HARD;
4332 Fail(error) << "expected field " << PrettyField(field)
4333 << " to be compatible with type '" << insn_type
4334 << "' but found type '" << *field_type
4335 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004336 return;
4337 }
4338 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004339 }
Andreas Gampe896df402014-10-20 22:25:29 -07004340 } else if (kAccType == FieldAccessType::kAccGet) {
4341 // sget or iget.
4342 if (is_primitive) {
4343 if (field_type->Equals(insn_type) ||
4344 (field_type->IsFloat() && insn_type.IsInteger()) ||
4345 (field_type->IsDouble() && insn_type.IsLong())) {
4346 // expected that read is of the correct primitive type or that int reads are reading
4347 // floats or long reads are reading doubles
4348 } else {
4349 // This is a global failure rather than a class change failure as the instructions and
4350 // the descriptors for the type should have been consistent within the same file at
4351 // compile time
4352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4353 << " to be of type '" << insn_type
4354 << "' but found type '" << *field_type << "' in get";
4355 return;
4356 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004357 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004358 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004359 // If the field type is not a reference, this is a global failure rather than
4360 // a class change failure as the instructions and the descriptors for the type
4361 // should have been consistent within the same file at compile time.
4362 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4363 : VERIFY_ERROR_BAD_CLASS_HARD;
4364 Fail(error) << "expected field " << PrettyField(field)
4365 << " to be compatible with type '" << insn_type
4366 << "' but found type '" << *field_type
4367 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004368 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004369 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004370 }
Andreas Gampe896df402014-10-20 22:25:29 -07004371 return;
4372 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004373 }
Andreas Gampe896df402014-10-20 22:25:29 -07004374 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004375 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004376 } else {
4377 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4378 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004379 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004380 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004381 }
4382}
4383
Mathieu Chartierc7853442015-03-27 14:35:38 -07004384ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004385 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004386 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004387 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004388 if (!object_type.HasClass()) {
4389 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4390 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004391 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004392 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004393 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004394 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004395 if (f == nullptr) {
4396 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4397 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4398 }
4399 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004400}
4401
Andreas Gampe896df402014-10-20 22:25:29 -07004402template <MethodVerifier::FieldAccessType kAccType>
4403void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4404 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004405 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004406
Mathieu Chartierc7853442015-03-27 14:35:38 -07004407 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004408 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4410 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004411 }
Andreas Gampe896df402014-10-20 22:25:29 -07004412
4413 // For an IPUT_QUICK, we now test for final flag of the field.
4414 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004415 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4416 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004417 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004418 return;
4419 }
4420 }
Andreas Gampe896df402014-10-20 22:25:29 -07004421
4422 // Get the field type.
4423 const RegType* field_type;
4424 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004425 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4426 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004427
4428 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004429 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4430 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004431 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004432 Thread* self = Thread::Current();
4433 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4434 self->ClearException();
4435 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4436 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004437 }
Andreas Gampe896df402014-10-20 22:25:29 -07004438 if (field_type == nullptr) {
4439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004440 return;
4441 }
Andreas Gampe896df402014-10-20 22:25:29 -07004442 }
4443
4444 const uint32_t vregA = inst->VRegA_22c();
4445 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4446 "Unexpected third access type");
4447 if (kAccType == FieldAccessType::kAccPut) {
4448 if (is_primitive) {
4449 // Primitive field assignability rules are weaker than regular assignability rules
4450 bool instruction_compatible;
4451 bool value_compatible;
4452 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4453 if (field_type->IsIntegralTypes()) {
4454 instruction_compatible = insn_type.IsIntegralTypes();
4455 value_compatible = value_type.IsIntegralTypes();
4456 } else if (field_type->IsFloat()) {
4457 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4458 value_compatible = value_type.IsFloatTypes();
4459 } else if (field_type->IsLong()) {
4460 instruction_compatible = insn_type.IsLong();
4461 value_compatible = value_type.IsLongTypes();
4462 } else if (field_type->IsDouble()) {
4463 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4464 value_compatible = value_type.IsDoubleTypes();
4465 } else {
4466 instruction_compatible = false; // reference field with primitive store
4467 value_compatible = false; // unused
4468 }
4469 if (!instruction_compatible) {
4470 // This is a global failure rather than a class change failure as the instructions and
4471 // the descriptors for the type should have been consistent within the same file at
4472 // compile time
4473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4474 << " to be of type '" << insn_type
4475 << "' but found type '" << *field_type
4476 << "' in put";
4477 return;
4478 }
4479 if (!value_compatible) {
4480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4481 << " of type " << value_type
4482 << " but expected " << *field_type
4483 << " for store to " << PrettyField(field) << " in put";
4484 return;
4485 }
4486 } else {
4487 if (!insn_type.IsAssignableFrom(*field_type)) {
4488 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4489 << " to be compatible with type '" << insn_type
4490 << "' but found type '" << *field_type
4491 << "' in put-object";
4492 return;
4493 }
4494 work_line_->VerifyRegisterType(this, vregA, *field_type);
4495 }
4496 } else if (kAccType == FieldAccessType::kAccGet) {
4497 if (is_primitive) {
4498 if (field_type->Equals(insn_type) ||
4499 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4500 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4501 // expected that read is of the correct primitive type or that int reads are reading
4502 // floats or long reads are reading doubles
4503 } else {
4504 // This is a global failure rather than a class change failure as the instructions and
4505 // the descriptors for the type should have been consistent within the same file at
4506 // compile time
4507 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4508 << " to be of type '" << insn_type
4509 << "' but found type '" << *field_type << "' in Get";
4510 return;
4511 }
4512 } else {
4513 if (!insn_type.IsAssignableFrom(*field_type)) {
4514 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4515 << " to be compatible with type '" << insn_type
4516 << "' but found type '" << *field_type
4517 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004518 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004519 return;
4520 }
4521 }
4522 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004523 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004524 } else {
4525 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004526 }
4527 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004528 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004529 }
4530}
4531
Ian Rogers776ac1f2012-04-13 23:36:36 -07004532bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004533 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004535 return false;
4536 }
4537 return true;
4538}
4539
Stephen Kyle9bc61992014-09-22 13:53:15 +01004540bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4541 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4542 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4544 return false;
4545 }
4546 return true;
4547}
4548
4549bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4550 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4551}
4552
Ian Rogersebbdd872014-07-07 23:53:08 -07004553bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4554 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004555 bool changed = true;
4556 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4557 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004558 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004559 * We haven't processed this instruction before, and we haven't touched the registers here, so
4560 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4561 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004562 */
Andreas Gampea727e372015-08-25 09:22:37 -07004563 target_line->CopyFromLine(merge_line);
4564 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004565 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004566 merge_line->VerifyMonitorStackEmpty(this);
4567
Ian Rogersb8c78592013-07-25 23:52:52 +00004568 // For returns we only care about the operand to the return, all other registers are dead.
4569 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4570 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004571 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004572 }
jeffhaobdb76512011-09-07 11:43:16 -07004573 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004574 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004575 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004576 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004577 if (gDebugVerify) {
4578 copy->CopyFromLine(target_line);
4579 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004580 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004581 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004582 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004583 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004584 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004585 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004586 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004587 << copy->Dump(this) << " MERGE\n"
4588 << merge_line->Dump(this) << " ==\n"
4589 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004590 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004591 if (update_merge_line && changed) {
4592 merge_line->CopyFromLine(target_line);
4593 }
jeffhaobdb76512011-09-07 11:43:16 -07004594 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004595 if (changed) {
4596 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004597 }
4598 return true;
4599}
4600
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004601InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004602 return &insn_flags_[work_insn_idx_];
4603}
4604
Ian Rogersd8f69b02014-09-10 21:43:52 +00004605const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004606 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004607 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004608 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4609 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4610 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004611 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004612 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4613 return_type_class,
4614 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004615 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004616 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4617 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004618 }
4619 }
4620 if (return_type_ == nullptr) {
4621 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4622 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4623 uint16_t return_type_idx = proto_id.return_type_idx_;
4624 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004625 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004626 }
4627 }
4628 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004629}
4630
Ian Rogersd8f69b02014-09-10 21:43:52 +00004631const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004632 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004633 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004634 const char* descriptor
4635 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004636 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004637 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004638 declaring_class_ = &FromClass(descriptor, klass,
4639 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004640 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004641 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004642 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004643 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004644 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004645}
4646
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004647std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4648 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004649 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004650 std::vector<int32_t> result;
4651 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004652 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004653 if (type.IsConstant()) {
4654 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004655 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4656 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004657 } else if (type.IsConstantLo()) {
4658 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004659 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4660 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004661 } else if (type.IsConstantHi()) {
4662 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004663 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4664 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004665 } else if (type.IsIntegralTypes()) {
4666 result.push_back(kIntVReg);
4667 result.push_back(0);
4668 } else if (type.IsFloat()) {
4669 result.push_back(kFloatVReg);
4670 result.push_back(0);
4671 } else if (type.IsLong()) {
4672 result.push_back(kLongLoVReg);
4673 result.push_back(0);
4674 result.push_back(kLongHiVReg);
4675 result.push_back(0);
4676 ++i;
4677 } else if (type.IsDouble()) {
4678 result.push_back(kDoubleLoVReg);
4679 result.push_back(0);
4680 result.push_back(kDoubleHiVReg);
4681 result.push_back(0);
4682 ++i;
4683 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4684 result.push_back(kUndefined);
4685 result.push_back(0);
4686 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004687 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004688 result.push_back(kReferenceVReg);
4689 result.push_back(0);
4690 }
4691 }
4692 return result;
4693}
4694
Ian Rogersd8f69b02014-09-10 21:43:52 +00004695const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004696 if (precise) {
4697 // Precise constant type.
4698 return reg_types_.FromCat1Const(value, true);
4699 } else {
4700 // Imprecise constant type.
4701 if (value < -32768) {
4702 return reg_types_.IntConstant();
4703 } else if (value < -128) {
4704 return reg_types_.ShortConstant();
4705 } else if (value < 0) {
4706 return reg_types_.ByteConstant();
4707 } else if (value == 0) {
4708 return reg_types_.Zero();
4709 } else if (value == 1) {
4710 return reg_types_.One();
4711 } else if (value < 128) {
4712 return reg_types_.PosByteConstant();
4713 } else if (value < 32768) {
4714 return reg_types_.PosShortConstant();
4715 } else if (value < 65536) {
4716 return reg_types_.CharConstant();
4717 } else {
4718 return reg_types_.IntConstant();
4719 }
4720 }
4721}
4722
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004723void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004724 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004725}
4726
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004727void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004728 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004729}
jeffhaod1224c72012-02-29 13:43:08 -08004730
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004731void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4732 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004733}
4734
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004735void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4736 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004737}
4738
Andreas Gampef23f33d2015-06-23 14:18:17 -07004739const RegType& MethodVerifier::FromClass(const char* descriptor,
4740 mirror::Class* klass,
4741 bool precise) {
4742 DCHECK(klass != nullptr);
4743 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4744 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4745 << "non-instantiable klass " << descriptor;
4746 precise = false;
4747 }
4748 return reg_types_.FromClass(descriptor, klass, precise);
4749}
4750
Ian Rogersd81871c2011-10-03 13:57:23 -07004751} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004752} // namespace art