blob: e1d4160aac00655a64795911ac592253caa27007 [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 Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000028#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070030#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070033#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080035#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070036#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070037#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class.h"
39#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070043#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070044#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070045#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010047#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080049#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
51namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070052namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070054static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070055static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070056// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070057
Andreas Gampeebf850c2015-08-14 15:37:35 -070058// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
59static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
60
Mathieu Chartierde40d472015-10-15 17:47:48 -070061PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
62 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
63
Ian Rogers7b3ddd22013-02-21 15:19:52 -080064void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070065 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070066 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070067 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070068 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070069 for (uint32_t i = 0; i < insns_size; i++) {
70 bool interesting = false;
71 switch (mode) {
72 case kTrackRegsAll:
73 interesting = flags[i].IsOpcode();
74 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070075 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070076 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070077 break;
78 case kTrackRegsBranches:
79 interesting = flags[i].IsBranchTarget();
80 break;
81 default:
82 break;
83 }
84 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070085 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070086 }
87 }
88}
89
Mathieu Chartierde40d472015-10-15 17:47:48 -070090PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070091
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700110 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 mirror::Class* klass = method->GetDeclaringClass();
121 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
122 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 return VerifyClass(
164 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700165}
166
Ian Rogers7b078e82014-09-10 14:44:24 -0700167MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
168 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700169 Handle<mirror::DexCache> dex_cache,
170 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700171 const DexFile::ClassDef* class_def,
172 bool allow_soft_failures,
173 std::string* error) {
174 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700175
176 // A class must not be abstract and final.
177 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
178 *error = "Verifier rejected class ";
179 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
180 *error += ": class is abstract and final.";
181 return kHardFailure;
182 }
183
Ian Rogers13735952014-10-08 12:43:28 -0700184 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700185 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700187 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 }
jeffhaof56197c2012-03-05 18:01:54 -0800189 ClassDataItemIterator it(*dex_file, class_data);
190 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
191 it.Next();
192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700194 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700196 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800197 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700198 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800199 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700200 if (method_idx == previous_direct_method_idx) {
201 // smali can create dex files with two encoded_methods sharing the same method_idx
202 // http://code.google.com/p/smali/issues/detail?id=119
203 it.Next();
204 continue;
205 }
206 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 ArtMethod* method = linker->ResolveMethod(
209 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700211 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700213 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 } else {
215 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700218 MethodVerifier::FailureKind result = VerifyMethod(self,
219 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700220 dex_file,
221 dex_cache,
222 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700224 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (result != kNoFailure) {
227 if (result == kHardFailure) {
228 hard_fail = true;
229 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700230 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700232 *error = "Verifier rejected class ";
233 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
234 *error += " due to bad method ";
235 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700237 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
239 it.Next();
240 }
jeffhao9b0b1882012-10-01 16:51:22 -0700241 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800242 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700243 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800244 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700245 if (method_idx == previous_virtual_method_idx) {
246 // smali can create dex files with two encoded_methods sharing the same method_idx
247 // http://code.google.com/p/smali/issues/detail?id=119
248 it.Next();
249 continue;
250 }
251 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700252 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 ArtMethod* method = linker->ResolveMethod(
254 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700255 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700256 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700257 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700258 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700260 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700261 MethodVerifier::FailureKind result = VerifyMethod(self,
262 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700263 dex_file,
264 dex_cache,
265 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700267 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += " due to bad method ";
278 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700291static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700292 if (code_item == nullptr) {
293 return false;
294 }
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296 uint16_t registers_size = code_item->registers_size_;
297 uint32_t insns_size = code_item->insns_size_in_code_units_;
298
299 return registers_size * insns_size > 4*1024*1024;
300}
301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 Handle<mirror::DexCache> dex_cache,
305 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700309 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700310 bool allow_soft_failures,
311 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700314
Ian Rogers7b078e82014-09-10 14:44:24 -0700315 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700316 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800317 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 // Verification completed, however failures may be pending that didn't cause the verification
320 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 CHECK(!verifier.have_pending_hard_failure_);
322 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700323 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
326 }
Ian Rogersc8982582012-09-07 16:53:25 -0700327 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800328 }
329 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700332
333 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
334 // Failed due to being forced into interpreter. This is ok because
335 // we just want to skip verification.
336 result = kSoftFailure;
337 } else {
338 CHECK(verifier.have_pending_hard_failure_);
339 verifier.DumpFailures(LOG(INFO) << "Verification error in "
340 << PrettyMethod(method_idx, *dex_file) << "\n");
341 result = kHardFailure;
342 }
jeffhaof56197c2012-03-05 18:01:54 -0800343 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700344 std::cout << "\n" << verifier.info_messages_.str();
345 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800346 }
jeffhaof56197c2012-03-05 18:01:54 -0800347 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700348 if (kTimeVerifyMethod) {
349 uint64_t duration_ns = NanoTime() - start_ns;
350 if (duration_ns > MsToNs(100)) {
351 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700352 << " took " << PrettyDuration(duration_ns)
353 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700354 }
Ian Rogersc8982582012-09-07 16:53:25 -0700355 }
356 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800357}
358
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100359MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
360 VariableIndentationOutputStream* vios,
361 uint32_t dex_method_idx,
362 const DexFile* dex_file,
363 Handle<mirror::DexCache> dex_cache,
364 Handle<mirror::ClassLoader> class_loader,
365 const DexFile::ClassDef* class_def,
366 const DexFile::CodeItem* code_item,
367 ArtMethod* method,
368 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700369 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
370 class_def, code_item, dex_method_idx, method,
371 method_access_flags, true, true, true, true);
372 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100373 verifier->DumpFailures(vios->Stream());
374 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700375 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
376 // and querying any info is dangerous/can abort.
377 if (verifier->have_pending_hard_failure_) {
378 delete verifier;
379 return nullptr;
380 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100381 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700382 return verifier;
383 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800384}
385
Ian Rogers7b078e82014-09-10 14:44:24 -0700386MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700387 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
388 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700389 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700392 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800393 bool need_precise_constants, bool verify_to_dump,
394 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700395 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700396 arena_stack_(Runtime::Current()->GetArenaPool()),
397 arena_(&arena_stack_),
398 reg_types_(can_load_classes, arena_),
399 reg_table_(arena_),
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);
Vladimir Marko78568352015-09-21 12:00:16 +0100550 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
551 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800552}
553
554SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
555 Verify();
556 return GetStringInitPcRegMap();
557}
558
Ian Rogersad0b3a32012-04-16 14:50:24 -0700559bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700560 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
561 // the name.
Alex Lighteb7c1442015-08-31 13:17:42 -0700562 Runtime* runtime = Runtime::Current();
Andreas Gampee6215c02015-08-31 18:54:38 -0700563 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
564 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
565 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
566 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
567 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
568 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
569 if ((method_access_flags_ & kAccConstructor) != 0) {
570 if (!constructor_by_name) {
571 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
572 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700573 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700574 }
575 is_constructor_ = true;
576 } else if (constructor_by_name) {
577 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
578 << " not marked as constructor.";
579 is_constructor_ = true;
580 }
581 // If it's a constructor, check whether IsStatic() matches the name.
582 // This should have been rejected by the dex file verifier. Only do in debug build.
583 if (kIsDebugBuild) {
584 if (IsConstructor()) {
585 if (IsStatic() ^ static_constructor_by_name) {
586 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
587 << "constructor name doesn't match static flag";
588 return false;
589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
jeffhaobdb76512011-09-07 11:43:16 -0700591 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700592
593 // Methods may only have one of public/protected/private.
594 // This should have been rejected by the dex file verifier. Only do in debug build.
595 if (kIsDebugBuild) {
596 size_t access_mod_count =
597 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
598 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
599 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
600 if (access_mod_count > 1) {
601 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
602 return false;
603 }
604 }
605
606 // If there aren't any instructions, make sure that's expected, then exit successfully.
607 if (code_item_ == nullptr) {
608 // This should have been rejected by the dex file verifier. Only do in debug build.
609 if (kIsDebugBuild) {
610 // Only native or abstract methods may not have code.
611 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
613 return false;
614 }
615 if ((method_access_flags_ & kAccAbstract) != 0) {
616 // Abstract methods are not allowed to have the following flags.
617 static constexpr uint32_t kForbidden =
618 kAccPrivate |
619 kAccStatic |
620 kAccFinal |
621 kAccNative |
622 kAccStrict |
623 kAccSynchronized;
624 if ((method_access_flags_ & kForbidden) != 0) {
625 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
626 << "method can't be abstract and private/static/final/native/strict/synchronized";
627 return false;
628 }
629 }
630 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700631 // Interface methods must be public and abstract (if default methods are disabled).
632 bool default_methods_supported =
633 runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods);
634 uint32_t kRequired = kAccPublic | (default_methods_supported ? 0 : kAccAbstract);
635 if ((method_access_flags_ & kRequired) != kRequired) {
636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"
637 << (default_methods_supported ? "" : " and abstract");
Andreas Gampee6215c02015-08-31 18:54:38 -0700638 return false;
639 }
640 // In addition to the above, interface methods must not be protected.
641 static constexpr uint32_t kForbidden = kAccProtected;
642 if ((method_access_flags_ & kForbidden) != 0) {
643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
644 return false;
645 }
646 }
647 // We also don't allow constructors to be abstract or native.
648 if (IsConstructor()) {
649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
650 return false;
651 }
652 }
653 return true;
654 }
655
656 // This should have been rejected by the dex file verifier. Only do in debug build.
657 if (kIsDebugBuild) {
658 // When there's code, the method must not be native or abstract.
659 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
661 return false;
662 }
663
Andreas Gampee6215c02015-08-31 18:54:38 -0700664 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700665 // Interfaces may always have static initializers for their fields. If we are running with
666 // default methods enabled we also allow other public, static, non-final methods to have code.
667 // Otherwise that is the only type of method allowed.
668 if (runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods)) {
669 if (IsInstanceConstructor()) {
670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
671 return false;
672 } else if (method_access_flags_ & kAccFinal) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
674 return false;
675 } else if (!(method_access_flags_ & kAccPublic)) {
676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
677 return false;
678 }
679 } else if (!IsConstructor() || !IsStatic()) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
681 return false;
682 }
683 }
684
685 // Instance constructors must not be synchronized.
686 if (IsInstanceConstructor()) {
687 static constexpr uint32_t kForbidden = kAccSynchronized;
688 if ((method_access_flags_ & kForbidden) != 0) {
689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
690 return false;
691 }
692 }
693 }
694
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
696 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
698 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700700 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700701
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700703 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
704 DCHECK(insn_flags_ != nullptr);
705 std::uninitialized_fill_n(insn_flags_.get(),
706 code_item_->insns_size_in_code_units_,
707 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 // Run through the instructions and see if the width checks out.
709 bool result = ComputeWidthsAndCountOps();
710 // Flag instructions guarded by a "try" block and check exception handlers.
711 result = result && ScanTryCatchBlocks();
712 // Perform static instruction verification.
713 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700714 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000715 result = result && VerifyCodeFlow();
716 // Compute information for compiler.
Alex Lighteb7c1442015-08-31 13:17:42 -0700717 if (result && runtime->IsCompiler()) {
718 result = runtime->GetCompilerCallbacks()->MethodVerified(this);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000719 }
720 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700721}
722
Ian Rogers776ac1f2012-04-13 23:36:36 -0700723std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700724 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700725 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700726
Ian Rogersad0b3a32012-04-16 14:50:24 -0700727 switch (error) {
728 case VERIFY_ERROR_NO_CLASS:
729 case VERIFY_ERROR_NO_FIELD:
730 case VERIFY_ERROR_NO_METHOD:
731 case VERIFY_ERROR_ACCESS_CLASS:
732 case VERIFY_ERROR_ACCESS_FIELD:
733 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700734 case VERIFY_ERROR_INSTANTIATION:
735 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700736 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700737 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800738 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700739 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
740 // class change and instantiation errors into soft verification errors so that we re-verify
741 // at runtime. We may fail to find or to agree on access because of not yet available class
742 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
743 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
744 // paths" that dynamically perform the verification and cause the behavior to be that akin
745 // to an interpreter.
746 error = VERIFY_ERROR_BAD_CLASS_SOFT;
747 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700748 // If we fail again at runtime, mark that this instruction would throw and force this
749 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700750 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700751
752 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
753 // try to merge garbage.
754 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700755 // Note: this can fail before we touch any instruction, for the signature of a method. So
756 // add a check.
757 if (work_insn_idx_ < DexFile::kDexNoIndex) {
758 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
759 const Instruction* inst = Instruction::At(insns);
760 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700761
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700762 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
763 saved_line_->CopyFromLine(work_line_.get());
764 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700765 }
jeffhaofaf459e2012-08-31 15:32:47 -0700766 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700767 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700768
Ian Rogersad0b3a32012-04-16 14:50:24 -0700769 // Indication that verification should be retried at runtime.
770 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700771 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700772 have_pending_hard_failure_ = true;
773 }
774 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700775
jeffhaod5347e02012-03-22 17:25:05 -0700776 // Hard verification failures at compile time will still fail at runtime, so the class is
777 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700778 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800779 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700780 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000781 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800782 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700783 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700784 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
785 ScopedObjectAccess soa(Thread::Current());
786 std::ostringstream oss;
787 Dump(oss);
788 LOG(ERROR) << oss.str();
789 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700790 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800791 }
792 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700793 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700794 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700795 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700796 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700797 failure_messages_.push_back(failure_message);
798 return *failure_message;
799}
800
Ian Rogers576ca0c2014-06-06 15:58:22 -0700801std::ostream& MethodVerifier::LogVerifyInfo() {
802 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
803 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
804}
805
Ian Rogersad0b3a32012-04-16 14:50:24 -0700806void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
807 size_t failure_num = failure_messages_.size();
808 DCHECK_NE(failure_num, 0U);
809 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
810 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700811 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700812 delete last_fail_message;
813}
814
815void MethodVerifier::AppendToLastFailMessage(std::string append) {
816 size_t failure_num = failure_messages_.size();
817 DCHECK_NE(failure_num, 0U);
818 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
819 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800820}
821
Ian Rogers776ac1f2012-04-13 23:36:36 -0700822bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 const uint16_t* insns = code_item_->insns_;
824 size_t insns_size = code_item_->insns_size_in_code_units_;
825 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700826 size_t new_instance_count = 0;
827 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700829
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700831 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700832 switch (opcode) {
833 case Instruction::APUT_OBJECT:
834 case Instruction::CHECK_CAST:
835 has_check_casts_ = true;
836 break;
837 case Instruction::INVOKE_VIRTUAL:
838 case Instruction::INVOKE_VIRTUAL_RANGE:
839 case Instruction::INVOKE_INTERFACE:
840 case Instruction::INVOKE_INTERFACE_RANGE:
841 has_virtual_or_interface_invokes_ = true;
842 break;
843 case Instruction::MONITOR_ENTER:
844 monitor_enter_count++;
845 break;
846 case Instruction::NEW_INSTANCE:
847 new_instance_count++;
848 break;
849 default:
850 break;
jeffhaobdb76512011-09-07 11:43:16 -0700851 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700853 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700855 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700856 }
857
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
860 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700861 return false;
862 }
863
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 new_instance_count_ = new_instance_count;
865 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700866 return true;
867}
868
Ian Rogers776ac1f2012-04-13 23:36:36 -0700869bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700871 if (tries_size == 0) {
872 return true;
873 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700874 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700875 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700876
877 for (uint32_t idx = 0; idx < tries_size; idx++) {
878 const DexFile::TryItem* try_item = &tries[idx];
879 uint32_t start = try_item->start_addr_;
880 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700881 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700882 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
883 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700884 return false;
885 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700886 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700887 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
888 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700889 return false;
890 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700891 uint32_t dex_pc = start;
892 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
893 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700894 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700895 size_t insn_size = inst->SizeInCodeUnits();
896 dex_pc += insn_size;
897 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700898 }
899 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800900 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700901 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700902 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700903 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700904 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700905 CatchHandlerIterator iterator(handlers_ptr);
906 for (; iterator.HasNext(); iterator.Next()) {
907 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700908 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700909 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
910 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700911 return false;
912 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100913 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
914 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
915 << "exception handler begins with move-result* (" << dex_pc << ")";
916 return false;
917 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700918 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700919 // Ensure exception types are resolved so that they don't need resolution to be delivered,
920 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700921 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800922 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
923 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700924 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700925 if (exception_type == nullptr) {
926 DCHECK(self_->IsExceptionPending());
927 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700928 }
929 }
jeffhaobdb76512011-09-07 11:43:16 -0700930 }
Ian Rogers0571d352011-11-03 19:51:38 -0700931 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700932 }
jeffhaobdb76512011-09-07 11:43:16 -0700933 return true;
934}
935
Ian Rogers776ac1f2012-04-13 23:36:36 -0700936bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700937 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700938
Ian Rogers0c7abda2012-09-19 13:33:42 -0700939 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Mathieu Chartierde40d472015-10-15 17:47:48 -0700940 GetInstructionFlags(0).SetBranchTarget();
941 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700942
943 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700944 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700946 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 return false;
948 }
949 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700950 // All invoke points are marked as "Throw" points already.
951 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000952 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700953 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000954 // The compiler also needs safepoints for fall-through to loop heads.
955 // Such a loop head must be a target of a branch.
956 int32_t offset = 0;
957 bool cond, self_ok;
958 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
959 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700960 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000961 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700962 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000963 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700964 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 }
966 dex_pc += inst->SizeInCodeUnits();
967 inst = inst->Next();
968 }
969 return true;
970}
971
Ian Rogers776ac1f2012-04-13 23:36:36 -0700972bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700973 if (UNLIKELY(inst->IsExperimental())) {
974 // Experimental instructions don't yet have verifier support implementation.
975 // While it is possible to use them by themselves, when we try to use stable instructions
976 // with a virtual register that was created by an experimental instruction,
977 // the data flow analysis will fail.
978 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
979 << "experimental instruction is not supported by verifier; skipping verification";
980 have_pending_experimental_failure_ = true;
981 return false;
982 }
983
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 bool result = true;
985 switch (inst->GetVerifyTypeArgumentA()) {
986 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700987 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 break;
989 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700990 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 break;
992 }
993 switch (inst->GetVerifyTypeArgumentB()) {
994 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700995 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 break;
997 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700998 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700999 break;
1000 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001001 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 break;
1003 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001004 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 break;
1006 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001007 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001008 break;
1009 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001010 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 break;
1012 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001013 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 break;
1015 }
1016 switch (inst->GetVerifyTypeArgumentC()) {
1017 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001018 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 break;
1020 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001021 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 break;
1023 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001024 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 break;
1026 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001027 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 break;
1029 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001030 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001032 case Instruction::kVerifyRegCString:
1033 result = result && CheckStringIndex(inst->VRegC());
1034 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 }
1036 switch (inst->GetVerifyExtraFlags()) {
1037 case Instruction::kVerifyArrayData:
1038 result = result && CheckArrayData(code_offset);
1039 break;
1040 case Instruction::kVerifyBranchTarget:
1041 result = result && CheckBranchTarget(code_offset);
1042 break;
1043 case Instruction::kVerifySwitchTargets:
1044 result = result && CheckSwitchTargets(code_offset);
1045 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001046 case Instruction::kVerifyVarArgNonZero:
1047 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001048 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001049 // Instructions that can actually return a negative value shouldn't have this flag.
1050 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1051 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1052 v_a > Instruction::kMaxVarArgRegs) {
1053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001054 "non-range invoke";
1055 return false;
1056 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001057
Ian Rogers29a26482014-05-02 15:27:29 -07001058 uint32_t args[Instruction::kMaxVarArgRegs];
1059 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001060 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001062 }
Andreas Gampec3314312014-06-19 18:13:29 -07001063 case Instruction::kVerifyVarArgRangeNonZero:
1064 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001066 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1067 inst->VRegA() <= 0) {
1068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1069 "range invoke";
1070 return false;
1071 }
Ian Rogers29a26482014-05-02 15:27:29 -07001072 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001073 break;
1074 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001076 result = false;
1077 break;
1078 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001079 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1081 result = false;
1082 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 return result;
1084}
1085
Ian Rogers7b078e82014-09-10 14:44:24 -07001086inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1089 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001090 return false;
1091 }
1092 return true;
1093}
1094
Ian Rogers7b078e82014-09-10 14:44:24 -07001095inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1098 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 return false;
1100 }
1101 return true;
1102}
1103
Ian Rogers7b078e82014-09-10 14:44:24 -07001104inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1107 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 return true;
1111}
1112
Ian Rogers7b078e82014-09-10 14:44:24 -07001113inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1116 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 return false;
1118 }
1119 return true;
1120}
1121
Ian Rogers7b078e82014-09-10 14:44:24 -07001122inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1125 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return false;
1127 }
1128 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001129 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 return false;
1133 }
1134 return true;
1135}
1136
Ian Rogers7b078e82014-09-10 14:44:24 -07001137inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1140 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 return false;
1142 }
1143 return true;
1144}
1145
Ian Rogers7b078e82014-09-10 14:44:24 -07001146inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001148 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1149 << dex_file_->GetHeader().type_ids_size_ << ")";
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::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1158 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 return false;
1160 }
1161 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001162 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 const char* cp = descriptor;
1164 while (*cp++ == '[') {
1165 bracket_count++;
1166 }
1167 if (bracket_count == 0) {
1168 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1170 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 return false;
1172 } else if (bracket_count > 255) {
1173 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001174 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1175 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return false;
1177 }
1178 return true;
1179}
1180
Ian Rogers776ac1f2012-04-13 23:36:36 -07001181bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1183 const uint16_t* insns = code_item_->insns_ + cur_offset;
1184 const uint16_t* array_data;
1185 int32_t array_data_offset;
1186
1187 DCHECK_LT(cur_offset, insn_count);
1188 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001189 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1190 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001193 << ", data offset " << array_data_offset
1194 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 return false;
1196 }
1197 /* offset to array data table is a relative branch-style offset */
1198 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001199 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1200 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001201 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1202 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 return false;
1204 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001205 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1206 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001207 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001208 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1209 << ", data offset " << array_data_offset
1210 << " not correctly visited, probably bad padding.";
1211 return false;
1212 }
1213
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001215 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1217 /* make sure the end of the switch is in range */
1218 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1220 << ", data offset " << array_data_offset << ", end "
1221 << cur_offset + array_data_offset + table_size
1222 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 return true;
1226}
1227
Ian Rogers776ac1f2012-04-13 23:36:36 -07001228bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 int32_t offset;
1230 bool isConditional, selfOkay;
1231 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1232 return false;
1233 }
1234 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1236 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001239 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1240 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1243 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return false;
1245 }
1246 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1247 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001248 if (abs_offset < 0 ||
1249 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001250 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001252 << reinterpret_cast<void*>(abs_offset) << ") at "
1253 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 return false;
1255 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001256 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 return true;
1258}
1259
Ian Rogers776ac1f2012-04-13 23:36:36 -07001260bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001261 bool* selfOkay) {
1262 const uint16_t* insns = code_item_->insns_ + cur_offset;
1263 *pConditional = false;
1264 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001265 switch (*insns & 0xff) {
1266 case Instruction::GOTO:
1267 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 break;
1269 case Instruction::GOTO_32:
1270 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001271 *selfOkay = true;
1272 break;
1273 case Instruction::GOTO_16:
1274 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001275 break;
1276 case Instruction::IF_EQ:
1277 case Instruction::IF_NE:
1278 case Instruction::IF_LT:
1279 case Instruction::IF_GE:
1280 case Instruction::IF_GT:
1281 case Instruction::IF_LE:
1282 case Instruction::IF_EQZ:
1283 case Instruction::IF_NEZ:
1284 case Instruction::IF_LTZ:
1285 case Instruction::IF_GEZ:
1286 case Instruction::IF_GTZ:
1287 case Instruction::IF_LEZ:
1288 *pOffset = (int16_t) insns[1];
1289 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001290 break;
1291 default:
1292 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001293 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001294 return true;
1295}
1296
Ian Rogers776ac1f2012-04-13 23:36:36 -07001297bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001299 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001301 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001302 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1303 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1304 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001305 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001306 << ", switch offset " << switch_offset
1307 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 return false;
1309 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001310 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001312 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1313 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1315 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 return false;
1317 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001318 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1319 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001320 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1322 << ", switch offset " << switch_offset
1323 << " not correctly visited, probably bad padding.";
1324 return false;
1325 }
1326
David Brazdil5469d342015-09-25 16:57:53 +01001327 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1328
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001330 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001332 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 /* 0=sig, 1=count, 2/3=firstKey */
1334 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001335 expected_signature = Instruction::kPackedSwitchSignature;
1336 } else {
1337 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001338 targets_offset = 2 + 2 * switch_count;
1339 expected_signature = Instruction::kSparseSwitchSignature;
1340 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001343 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1344 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1345 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 return false;
1347 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001348 /* make sure the end of the switch is in range */
1349 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1351 << ", switch offset " << switch_offset
1352 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001353 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 return false;
1355 }
David Brazdil5469d342015-09-25 16:57:53 +01001356
1357 constexpr int32_t keys_offset = 2;
1358 if (switch_count > 1) {
1359 if (is_packed_switch) {
1360 /* for a packed switch, verify that keys do not overflow int32 */
1361 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1362 int32_t max_first_key =
1363 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1364 if (first_key > max_first_key) {
1365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1366 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001367 return false;
1368 }
David Brazdil5469d342015-09-25 16:57:53 +01001369 } else {
1370 /* for a sparse switch, verify the keys are in ascending order */
1371 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1372 for (uint32_t targ = 1; targ < switch_count; targ++) {
1373 int32_t key =
1374 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1375 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1376 if (key <= last_key) {
1377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1378 << ", this=" << key;
1379 return false;
1380 }
1381 last_key = key;
1382 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 }
1384 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001386 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001387 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1388 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001389 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001390 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001391 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001392 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1394 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1395 << reinterpret_cast<void*>(cur_offset)
1396 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 return false;
1398 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001399 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001400 }
1401 return true;
1402}
1403
Ian Rogers776ac1f2012-04-13 23:36:36 -07001404bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 uint16_t registers_size = code_item_->registers_size_;
1406 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001407 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001408 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1409 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 return false;
1411 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 }
1413
1414 return true;
1415}
1416
Ian Rogers776ac1f2012-04-13 23:36:36 -07001417bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 uint16_t registers_size = code_item_->registers_size_;
1419 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1420 // integer overflow when adding them here.
1421 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1423 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 return false;
1425 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001426 return true;
1427}
1428
Ian Rogers776ac1f2012-04-13 23:36:36 -07001429bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 uint16_t registers_size = code_item_->registers_size_;
1431 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001432
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001434 reg_table_.Init(kTrackCompilerInterestPoints,
1435 insn_flags_.get(),
1436 insns_size,
1437 registers_size,
1438 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001439
Ian Rogersd0fbd852013-09-24 18:17:04 -07001440 work_line_.reset(RegisterLine::Create(registers_size, this));
1441 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001442
Ian Rogersd81871c2011-10-03 13:57:23 -07001443 /* Initialize register types of method arguments. */
1444 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001445 DCHECK_NE(failures_.size(), 0U);
1446 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001448 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001449 return false;
1450 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001451 // We may have a runtime failure here, clear.
1452 have_pending_runtime_throw_failure_ = false;
1453
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 /* Perform code flow verification. */
1455 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001456 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001458 }
jeffhaobdb76512011-09-07 11:43:16 -07001459 return true;
1460}
1461
Ian Rogersad0b3a32012-04-16 14:50:24 -07001462std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1463 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001464 for (size_t i = 0; i < failures_.size(); ++i) {
1465 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001466 }
1467 return os;
1468}
1469
Ian Rogers776ac1f2012-04-13 23:36:36 -07001470void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001471 VariableIndentationOutputStream vios(&os);
1472 Dump(&vios);
1473}
1474
1475void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001476 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 return;
jeffhaobdb76512011-09-07 11:43:16 -07001479 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001480 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001481 vios->Stream() << "Register Types:\n";
1482 ScopedIndentation indent1(vios);
1483 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001485 vios->Stream() << "Dumping instructions and register lines:\n";
1486 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 const Instruction* inst = Instruction::At(code_item_->insns_);
1488 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001489 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001491 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001494 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001495 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001496 const bool kDumpHexOfInstruction = false;
1497 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001501 }
jeffhaobdb76512011-09-07 11:43:16 -07001502}
1503
Ian Rogersd81871c2011-10-03 13:57:23 -07001504static bool IsPrimitiveDescriptor(char descriptor) {
1505 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001506 case 'I':
1507 case 'C':
1508 case 'S':
1509 case 'B':
1510 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001511 case 'F':
1512 case 'D':
1513 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001514 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001515 default:
1516 return false;
1517 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001518}
1519
Ian Rogers776ac1f2012-04-13 23:36:36 -07001520bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001522
1523 // Should have been verified earlier.
1524 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1525
1526 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001528
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001529 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001531 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001532 if (expected_args == 0) {
1533 // Expect at least a receiver.
1534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1535 return false;
1536 }
1537
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1539 // argument as uninitialized. This restricts field access until the superclass constructor is
1540 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001541 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001542 if (IsConstructor()) {
1543 if (declaring_class.IsJavaLangObject()) {
1544 // "this" is implicitly initialized.
1545 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001546 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001547 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001548 reg_line->SetRegisterType<LockOp::kClear>(
1549 this,
1550 arg_start + cur_arg,
1551 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001552 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001554 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001555 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001557 }
1558
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001559 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001561 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001562
1563 for (; iterator.HasNext(); iterator.Next()) {
1564 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001565 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 LOG(FATAL) << "Null descriptor";
1567 }
1568 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1570 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 return false;
1572 }
1573 switch (descriptor[0]) {
1574 case 'L':
1575 case '[':
1576 // We assume that reference arguments are initialized. The only way it could be otherwise
1577 // (assuming the caller was verified) is if the current method is <init>, but in that case
1578 // it's effectively considered initialized the instant we reach here (in the sense that we
1579 // can return without doing anything or call virtual methods).
1580 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001581 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001582 if (!reg_type.IsNonZeroReferenceTypes()) {
1583 DCHECK(HasFailures());
1584 return false;
1585 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001586 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 }
1588 break;
1589 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001590 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 break;
1592 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001593 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 break;
1595 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001596 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 break;
1598 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001599 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 break;
1601 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001602 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001603 break;
1604 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001605 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 break;
1607 case 'J':
1608 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001609 if (cur_arg + 1 >= expected_args) {
1610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1611 << " args, found more (" << descriptor << ")";
1612 return false;
1613 }
1614
Ian Rogers7b078e82014-09-10 14:44:24 -07001615 const RegType* lo_half;
1616 const RegType* hi_half;
1617 if (descriptor[0] == 'J') {
1618 lo_half = &reg_types_.LongLo();
1619 hi_half = &reg_types_.LongHi();
1620 } else {
1621 lo_half = &reg_types_.DoubleLo();
1622 hi_half = &reg_types_.DoubleHi();
1623 }
1624 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001625 cur_arg++;
1626 break;
1627 }
1628 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1630 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001631 return false;
1632 }
1633 cur_arg++;
1634 }
1635 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1637 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 return false;
1639 }
1640 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1641 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1642 // format. Only major difference from the method argument format is that 'V' is supported.
1643 bool result;
1644 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1645 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001646 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 size_t i = 0;
1648 do {
1649 i++;
1650 } while (descriptor[i] == '['); // process leading [
1651 if (descriptor[i] == 'L') { // object array
1652 do {
1653 i++; // find closing ;
1654 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1655 result = descriptor[i] == ';';
1656 } else { // primitive array
1657 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1658 }
1659 } else if (descriptor[0] == 'L') {
1660 // could be more thorough here, but shouldn't be required
1661 size_t i = 0;
1662 do {
1663 i++;
1664 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1665 result = descriptor[i] == ';';
1666 } else {
1667 result = false;
1668 }
1669 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1671 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001672 }
1673 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001674}
1675
Ian Rogers776ac1f2012-04-13 23:36:36 -07001676bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001677 const uint16_t* insns = code_item_->insns_;
1678 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001679
jeffhaobdb76512011-09-07 11:43:16 -07001680 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001681 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001683
jeffhaobdb76512011-09-07 11:43:16 -07001684 /* Continue until no instructions are marked "changed". */
1685 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001686 if (allow_thread_suspension_) {
1687 self_->AllowThreadSuspension();
1688 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1690 uint32_t insn_idx = start_guess;
1691 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001692 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001693 break;
1694 }
jeffhaobdb76512011-09-07 11:43:16 -07001695 if (insn_idx == insns_size) {
1696 if (start_guess != 0) {
1697 /* try again, starting from the top */
1698 start_guess = 0;
1699 continue;
1700 } else {
1701 /* all flags are clear */
1702 break;
1703 }
1704 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 // We carry the working set of registers from instruction to instruction. If this address can
1706 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1707 // "changed" flags, we need to load the set of registers from the table.
1708 // Because we always prefer to continue on to the next instruction, we should never have a
1709 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1710 // target.
1711 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001712 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001714 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001715 /*
1716 * Sanity check: retrieve the stored register line (assuming
1717 * a full table) and make sure it actually matches.
1718 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 if (work_line_->CompareLine(register_line) != 0) {
1722 Dump(std::cout);
1723 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001724 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001725 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001726 << " work_line=" << work_line_->Dump(this) << "\n"
1727 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
jeffhaobdb76512011-09-07 11:43:16 -07001730 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001733 prepend += " failed to verify: ";
1734 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001735 return false;
1736 }
jeffhaobdb76512011-09-07 11:43:16 -07001737 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001738 GetInstructionFlags(insn_idx).SetVisited();
1739 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001741
Ian Rogers1c849e52012-06-28 14:00:33 -07001742 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001743 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001744 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001745 * (besides the wasted space), but it indicates a flaw somewhere
1746 * down the line, possibly in the verifier.
1747 *
1748 * If we've substituted "always throw" instructions into the stream,
1749 * we are almost certainly going to have some dead code.
1750 */
1751 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 for (; insn_idx < insns_size;
1754 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001755 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001756 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001757 * may or may not be preceded by a padding NOP (for alignment).
1758 */
1759 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1760 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1761 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001762 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001763 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1764 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1765 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001766 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001767 }
1768
Mathieu Chartierde40d472015-10-15 17:47:48 -07001769 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001770 if (dead_start < 0)
1771 dead_start = insn_idx;
1772 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001773 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1774 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001775 dead_start = -1;
1776 }
1777 }
1778 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001779 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1780 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001781 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001782 // To dump the state of the verify after a method, do something like:
1783 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1784 // "boolean java.lang.String.equals(java.lang.Object)") {
1785 // LOG(INFO) << info_messages_.str();
1786 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 return true;
1789}
1790
Andreas Gampe68df3202015-06-22 11:35:46 -07001791// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1792// is no such field.
1793static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1794 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1795 DCHECK(class_def != nullptr);
1796 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1797 DCHECK(class_data != nullptr);
1798 ClassDataItemIterator it(dex_file, class_data);
1799 // Skip static fields.
1800 while (it.HasNextStaticField()) {
1801 it.Next();
1802 }
1803 while (it.HasNextInstanceField()) {
1804 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1805 return it.GetMemberIndex();
1806 }
1807 it.Next();
1808 }
1809 return DexFile::kDexNoIndex;
1810}
1811
Andreas Gampea727e372015-08-25 09:22:37 -07001812// Setup a register line for the given return instruction.
1813static void AdjustReturnLine(MethodVerifier* verifier,
1814 const Instruction* ret_inst,
1815 RegisterLine* line) {
1816 Instruction::Code opcode = ret_inst->Opcode();
1817
1818 switch (opcode) {
1819 case Instruction::RETURN_VOID:
1820 case Instruction::RETURN_VOID_NO_BARRIER:
1821 SafelyMarkAllRegistersAsConflicts(verifier, line);
1822 break;
1823
1824 case Instruction::RETURN:
1825 case Instruction::RETURN_OBJECT:
1826 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1827 break;
1828
1829 case Instruction::RETURN_WIDE:
1830 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1831 break;
1832
1833 default:
1834 LOG(FATAL) << "Unknown return opcode " << opcode;
1835 UNREACHABLE();
1836 }
1837}
1838
Ian Rogers776ac1f2012-04-13 23:36:36 -07001839bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001840 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1841 // We want the state _before_ the instruction, for the case where the dex pc we're
1842 // interested in is itself a monitor-enter instruction (which is a likely place
1843 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001844 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001845 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001846 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1847 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1848 }
1849 }
1850
jeffhaobdb76512011-09-07 11:43:16 -07001851 /*
1852 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001853 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001854 * control to another statement:
1855 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001856 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001857 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001858 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001859 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001860 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001861 * throw an exception that is handled by an encompassing "try"
1862 * block.
1863 *
1864 * We can also return, in which case there is no successor instruction
1865 * from this point.
1866 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001867 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001868 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1870 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001871 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001872
jeffhaobdb76512011-09-07 11:43:16 -07001873 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001874 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001875 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001877 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001878 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 }
jeffhaobdb76512011-09-07 11:43:16 -07001880
1881 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001882 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001883 * can throw an exception, we will copy/merge this into the "catch"
1884 * address rather than work_line, because we don't want the result
1885 * from the "successful" code path (e.g. a check-cast that "improves"
1886 * a type) to be visible to the exception handler.
1887 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001888 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001890 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001892 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001893 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001894
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001895
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001896 // We need to ensure the work line is consistent while performing validation. When we spot a
1897 // peephole pattern we compute a new line for either the fallthrough instruction or the
1898 // branch target.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001899 ArenaUniquePtr<RegisterLine> branch_line;
1900 ArenaUniquePtr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001901
Sebastien Hertz5243e912013-05-21 10:55:07 +02001902 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001903 case Instruction::NOP:
1904 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001905 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001906 * a signature that looks like a NOP; if we see one of these in
1907 * the course of executing code then we have a problem.
1908 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001911 }
1912 break;
1913
1914 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001915 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001916 break;
jeffhaobdb76512011-09-07 11:43:16 -07001917 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 break;
jeffhaobdb76512011-09-07 11:43:16 -07001920 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001921 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001922 break;
1923 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 break;
jeffhaobdb76512011-09-07 11:43:16 -07001926 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001927 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 break;
jeffhaobdb76512011-09-07 11:43:16 -07001929 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001931 break;
1932 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001933 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001934 break;
jeffhaobdb76512011-09-07 11:43:16 -07001935 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001936 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 break;
jeffhaobdb76512011-09-07 11:43:16 -07001938 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001939 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001940 break;
1941
1942 /*
1943 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001944 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001945 * might want to hold the result in an actual CPU register, so the
1946 * Dalvik spec requires that these only appear immediately after an
1947 * invoke or filled-new-array.
1948 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001949 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001950 * redundant with the reset done below, but it can make the debug info
1951 * easier to read in some cases.)
1952 */
1953 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001954 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001955 break;
1956 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001957 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001958 break;
1959 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001960 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001961 break;
1962
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001964 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1965 // where one entrypoint to the catch block is not actually an exception path.
1966 if (work_insn_idx_ == 0) {
1967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1968 break;
1969 }
jeffhaobdb76512011-09-07 11:43:16 -07001970 /*
jeffhao60f83e32012-02-13 17:16:30 -08001971 * This statement can only appear as the first instruction in an exception handler. We verify
1972 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001973 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001974 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001975 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001976 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 }
jeffhaobdb76512011-09-07 11:43:16 -07001978 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001979 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001980 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 }
jeffhaobdb76512011-09-07 11:43:16 -07001983 }
1984 break;
1985 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001986 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001987 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001988 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1991 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 } else {
1993 // Compilers may generate synthetic functions that write byte values into boolean fields.
1994 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1998 ((return_type.IsBoolean() || return_type.IsByte() ||
1999 return_type.IsShort() || return_type.IsChar()) &&
2000 src_type.IsInteger()));
2001 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002002 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002004 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 }
jeffhaobdb76512011-09-07 11:43:16 -07002007 }
2008 }
2009 break;
2010 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002011 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002012 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002013 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 } else {
2017 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002019 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002020 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023 }
2024 }
2025 break;
2026 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002027 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002028 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002030 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 } else {
2032 /* return_type is the *expected* return type, not register value */
2033 DCHECK(!return_type.IsZero());
2034 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002036 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002037 // Disallow returning undefined, conflict & uninitialized values and verify that the
2038 // reference in vAA is an instance of the "return_type."
2039 if (reg_type.IsUndefined()) {
2040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2041 } else if (reg_type.IsConflict()) {
2042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2043 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002044 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2045 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002046 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002047 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2048 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2049 << "' or '" << reg_type << "'";
2050 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002051 bool soft_error = false;
2052 // Check whether arrays are involved. They will show a valid class status, even
2053 // if their components are erroneous.
2054 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2055 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2056 if (soft_error) {
2057 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2058 << reg_type << " vs " << return_type;
2059 }
2060 }
2061
2062 if (!soft_error) {
2063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2064 << "', but expected from declaration '" << return_type << "'";
2065 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002066 }
jeffhaobdb76512011-09-07 11:43:16 -07002067 }
2068 }
2069 }
2070 break;
2071
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002072 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 case Instruction::CONST_4: {
2074 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002075 work_line_->SetRegisterType<LockOp::kClear>(
2076 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002077 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 }
2079 case Instruction::CONST_16: {
2080 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002081 work_line_->SetRegisterType<LockOp::kClear>(
2082 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002083 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002085 case Instruction::CONST: {
2086 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002087 work_line_->SetRegisterType<LockOp::kClear>(
2088 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002090 }
2091 case Instruction::CONST_HIGH16: {
2092 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002093 work_line_->SetRegisterType<LockOp::kClear>(
2094 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002098 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002100 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2101 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002102 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002104 }
2105 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002107 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2108 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002109 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002110 break;
2111 }
2112 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002114 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2115 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002117 break;
2118 }
2119 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002121 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2122 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002124 break;
2125 }
jeffhaobdb76512011-09-07 11:43:16 -07002126 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002127 work_line_->SetRegisterType<LockOp::kClear>(
2128 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 break;
jeffhaobdb76512011-09-07 11:43:16 -07002130 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002131 work_line_->SetRegisterType<LockOp::kClear>(
2132 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002134 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002135 // Get type from instruction if unresolved then we need an access check
2136 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002137 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002138 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002139 work_line_->SetRegisterType<LockOp::kClear>(
2140 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2141 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 }
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002145 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002146 // Check whether the previous instruction is a move-object with vAA as a source, creating
2147 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002148 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002149 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002150 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002151 prev_idx--;
2152 }
2153 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2154 switch (prev_inst->Opcode()) {
2155 case Instruction::MOVE_OBJECT:
2156 case Instruction::MOVE_OBJECT_16:
2157 case Instruction::MOVE_OBJECT_FROM16:
2158 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2159 // Redo the copy. This won't change the register types, but update the lock status
2160 // for the aliased register.
2161 work_line_->CopyRegister1(this,
2162 prev_inst->VRegA(),
2163 prev_inst->VRegB(),
2164 kTypeCategoryRef);
2165 }
2166 break;
2167
2168 default: // Other instruction types ignored.
2169 break;
2170 }
2171 }
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
2173 case Instruction::MONITOR_EXIT:
2174 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002175 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002176 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002177 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002178 * to the need to handle asynchronous exceptions, a now-deprecated
2179 * feature that Dalvik doesn't support.)
2180 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002181 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002182 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002183 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002184 * structured locking checks are working, the former would have
2185 * failed on the -enter instruction, and the latter is impossible.
2186 *
2187 * This is fortunate, because issue 3221411 prevents us from
2188 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002189 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002190 * some catch blocks (which will show up as "dead" code when
2191 * we skip them here); if we can't, then the code path could be
2192 * "live" so we still need to check it.
2193 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002194 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002195 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002196 break;
2197
Ian Rogers28ad40d2011-10-27 15:19:26 -07002198 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002199 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002200 /*
2201 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2202 * could be a "upcast" -- not expected, so we don't try to address it.)
2203 *
2204 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002205 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002206 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002207 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2208 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002209 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002210 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002211 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002212 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002213 if (klass != nullptr && klass->IsPrimitive()) {
2214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2215 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2216 << GetDeclaringClass();
2217 break;
2218 }
2219
Ian Rogersad0b3a32012-04-16 14:50:24 -07002220 DCHECK_NE(failures_.size(), 0U);
2221 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002222 work_line_->SetRegisterType<LockOp::kClear>(this,
2223 inst->VRegA_22c(),
2224 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002225 }
2226 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002227 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002228 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002229 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002230 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002231 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 if (is_checkcast) {
2233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2234 } else {
2235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2236 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 if (is_checkcast) {
2239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2240 } else {
2241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2242 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002243 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002244 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002245 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002246 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002247 work_line_->SetRegisterType<LockOp::kClear>(this,
2248 inst->VRegA_22c(),
2249 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002250 }
jeffhaobdb76512011-09-07 11:43:16 -07002251 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002252 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 }
2254 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002255 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002256 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002257 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002259 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002260 work_line_->SetRegisterType<LockOp::kClear>(this,
2261 inst->VRegA_12x(),
2262 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002264 } else {
2265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 }
2267 break;
2268 }
2269 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002270 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002271 if (res_type.IsConflict()) {
2272 DCHECK_NE(failures_.size(), 0U);
2273 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002274 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002275 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2276 // can't create an instance of an interface or abstract class */
2277 if (!res_type.IsInstantiableTypes()) {
2278 Fail(VERIFY_ERROR_INSTANTIATION)
2279 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002280 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002281 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002282 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002283 // Any registers holding previous allocations from this address that have not yet been
2284 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002286 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002287 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
2289 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002290 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002292 break;
2293 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002294 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002295 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002297 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002298 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002299 just_set_result = true; // Filled new array range sets result register
2300 break;
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::CMPL_FLOAT:
2302 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002303 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002304 break;
2305 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002306 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002307 break;
2308 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002309 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
2311 case Instruction::CMPL_DOUBLE:
2312 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002313 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002315 break;
2316 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002317 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002319 break;
2320 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002321 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002324 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002326 break;
2327 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002328 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002330 break;
2331 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002332 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002335 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002336 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002337 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2338 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002339 }
2340 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 }
jeffhaobdb76512011-09-07 11:43:16 -07002342 case Instruction::GOTO:
2343 case Instruction::GOTO_16:
2344 case Instruction::GOTO_32:
2345 /* no effect on or use of registers */
2346 break;
2347
2348 case Instruction::PACKED_SWITCH:
2349 case Instruction::SPARSE_SWITCH:
2350 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002351 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 case Instruction::FILL_ARRAY_DATA: {
2355 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002356 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002357 /* array_type can be null if the reg type is Zero */
2358 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002359 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2361 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002362 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002363 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002364 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002365 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2367 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002368 } else {
jeffhao457cc512012-02-02 16:55:13 -08002369 // Now verify if the element width in the table matches the element width declared in
2370 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002371 const uint16_t* array_data =
2372 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002373 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002375 } else {
2376 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2377 // Since we don't compress the data in Dex, expect to see equal width of data stored
2378 // in the table and expected from the array class.
2379 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2381 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002382 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002383 }
2384 }
jeffhaobdb76512011-09-07 11:43:16 -07002385 }
2386 }
2387 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002391 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2392 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002393 bool mismatch = false;
2394 if (reg_type1.IsZero()) { // zero then integral or reference expected
2395 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2396 } else if (reg_type1.IsReferenceTypes()) { // both references?
2397 mismatch = !reg_type2.IsReferenceTypes();
2398 } else { // both integral?
2399 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2400 }
2401 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2403 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002404 }
2405 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 }
jeffhaobdb76512011-09-07 11:43:16 -07002407 case Instruction::IF_LT:
2408 case Instruction::IF_GE:
2409 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002411 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2412 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2415 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002416 }
2417 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 }
jeffhaobdb76512011-09-07 11:43:16 -07002419 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002421 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002423 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2424 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002425 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002426
2427 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002428 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002429 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002430 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002431 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002432 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002433 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002434 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002435 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2436 work_insn_idx_)) {
2437 break;
2438 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002439 } else {
2440 break;
2441 }
2442
Ian Rogers9b360392013-06-06 14:45:07 -07002443 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002444
2445 /* Check for peep-hole pattern of:
2446 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002447 * instance-of vX, vY, T;
2448 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002449 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002450 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002451 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002452 * and sharpen the type of vY to be type T.
2453 * Note, this pattern can't be if:
2454 * - if there are other branches to this branch,
2455 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002456 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002457 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002458 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2459 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2460 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002461 // Check the type of the instance-of is different than that of registers type, as if they
2462 // are the same there is no work to be done here. Check that the conversion is not to or
2463 // from an unresolved type as type information is imprecise. If the instance-of is to an
2464 // interface then ignore the type information as interfaces can only be treated as Objects
2465 // and we don't want to disallow field and other operations on the object. If the value
2466 // being instance-of checked against is known null (zero) then allow the optimization as
2467 // we didn't have type information. If the merge of the instance-of type with the original
2468 // type is assignable to the original then allow optimization. This check is performed to
2469 // ensure that subsequent merges don't lose type information - such as becoming an
2470 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002472 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002473
Ian Rogersebbdd872014-07-07 23:53:08 -07002474 if (!orig_type.Equals(cast_type) &&
2475 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002476 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002477 !cast_type.GetClass()->IsInterface() &&
2478 (orig_type.IsZero() ||
2479 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002480 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002481 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002482 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002483 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002484 branch_line.reset(update_line);
2485 }
2486 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002487 update_line->SetRegisterType<LockOp::kKeep>(this,
2488 instance_of_inst->VRegB_22c(),
2489 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002490 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002491 // See if instance-of was preceded by a move-object operation, common due to the small
2492 // register encoding space of instance-of, and propagate type information to the source
2493 // of the move-object.
2494 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002495 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002496 move_idx--;
2497 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002498 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002499 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2500 work_insn_idx_)) {
2501 break;
2502 }
Ian Rogers9b360392013-06-06 14:45:07 -07002503 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2504 switch (move_inst->Opcode()) {
2505 case Instruction::MOVE_OBJECT:
2506 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002507 update_line->SetRegisterType<LockOp::kKeep>(this,
2508 move_inst->VRegB_12x(),
2509 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002510 }
2511 break;
2512 case Instruction::MOVE_OBJECT_FROM16:
2513 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002514 update_line->SetRegisterType<LockOp::kKeep>(this,
2515 move_inst->VRegB_22x(),
2516 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002517 }
2518 break;
2519 case Instruction::MOVE_OBJECT_16:
2520 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002521 update_line->SetRegisterType<LockOp::kKeep>(this,
2522 move_inst->VRegB_32x(),
2523 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002524 }
2525 break;
2526 default:
2527 break;
2528 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002529 }
2530 }
2531 }
2532
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002534 }
jeffhaobdb76512011-09-07 11:43:16 -07002535 case Instruction::IF_LTZ:
2536 case Instruction::IF_GEZ:
2537 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2542 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002543 }
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 }
jeffhaobdb76512011-09-07 11:43:16 -07002546 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002547 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002548 break;
jeffhaobdb76512011-09-07 11:43:16 -07002549 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002550 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002551 break;
jeffhaobdb76512011-09-07 11:43:16 -07002552 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002553 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 break;
jeffhaobdb76512011-09-07 11:43:16 -07002555 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002556 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002557 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002559 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 break;
jeffhaobdb76512011-09-07 11:43:16 -07002561 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002562 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 break;
2564 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002565 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002566 break;
2567
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002569 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 break;
2571 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002572 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 break;
2574 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002575 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002576 break;
2577 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002578 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002579 break;
2580 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002581 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002582 break;
2583 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002584 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002587 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
2589
jeffhaobdb76512011-09-07 11:43:16 -07002590 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002591 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 break;
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002594 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 break;
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002597 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 break;
jeffhaobdb76512011-09-07 11:43:16 -07002599 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002600 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 break;
2602 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002603 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002606 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002609 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2610 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 break;
jeffhaobdb76512011-09-07 11:43:16 -07002612
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002614 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 break;
2616 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002617 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 break;
2619 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002620 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 break;
2622 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002623 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002624 break;
2625 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002626 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002629 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002630 break;
jeffhaobdb76512011-09-07 11:43:16 -07002631 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002632 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2633 false);
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635
jeffhaobdb76512011-09-07 11:43:16 -07002636 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002637 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002638 break;
jeffhaobdb76512011-09-07 11:43:16 -07002639 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002640 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002643 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 break;
jeffhaobdb76512011-09-07 11:43:16 -07002645 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002646 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 break;
2648 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002649 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002650 break;
2651 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002652 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002653 break;
2654 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002655 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2656 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 break;
2658
2659 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002660 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 break;
2662 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002663 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 break;
2665 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002666 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 break;
2668 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002669 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002670 break;
2671 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002672 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002675 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
2677 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002678 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2679 true);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681
2682 case Instruction::INVOKE_VIRTUAL:
2683 case Instruction::INVOKE_VIRTUAL_RANGE:
2684 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002685 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002686 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2687 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002688 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2689 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002690 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002691 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002692 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002693 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2694 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2695 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002696 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002697 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2698 return_type_class,
2699 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002700 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002701 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2702 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002703 }
2704 }
2705 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002706 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002707 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2708 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002709 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002710 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002711 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002712 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002713 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002714 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002715 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002716 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002717 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 }
jeffhaobdb76512011-09-07 11:43:16 -07002720 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002722 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002723 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002724 const char* return_type_descriptor;
2725 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002726 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002727 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002728 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002729 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002730 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002731 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2732 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2733 } else {
2734 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002735 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002736 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2737 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2738 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002739 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002740 return_type = &FromClass(return_type_descriptor,
2741 return_type_class,
2742 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002743 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002744 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2745 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002746 }
Ian Rogers46685432012-06-03 22:26:43 -07002747 }
2748 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002749 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 * Some additional checks when calling a constructor. We know from the invocation arg check
2751 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2752 * that to require that called_method->klass is the same as this->klass or this->super,
2753 * allowing the latter only if the "this" argument is the same as the "this" argument to
2754 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002755 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002756 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002757 if (this_type.IsConflict()) // failure.
2758 break;
jeffhaobdb76512011-09-07 11:43:16 -07002759
jeffhaob57e9522012-04-26 18:08:21 -07002760 /* no null refs allowed (?) */
2761 if (this_type.IsZero()) {
2762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2763 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002764 }
jeffhaob57e9522012-04-26 18:08:21 -07002765
2766 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002767 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002768 // TODO: re-enable constructor type verification
2769 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002770 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002771 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2772 // break;
2773 // }
jeffhaob57e9522012-04-26 18:08:21 -07002774
2775 /* arg must be an uninitialized reference */
2776 if (!this_type.IsUninitializedTypes()) {
2777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2778 << this_type;
2779 break;
2780 }
2781
2782 /*
2783 * Replace the uninitialized reference with an initialized one. We need to do this for all
2784 * registers that have the same object instance in them, not just the "this" register.
2785 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002786 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2787 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002788 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002789 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002790 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002791 }
2792 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002794 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002795 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002797 just_set_result = true;
2798 break;
2799 }
2800 case Instruction::INVOKE_STATIC:
2801 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002802 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002803 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002804 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002805 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002807 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2808 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002809 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002810 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002811 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002812 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002813 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002814 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002815 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002816 } else {
2817 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2818 }
jeffhaobdb76512011-09-07 11:43:16 -07002819 just_set_result = true;
2820 }
2821 break;
jeffhaobdb76512011-09-07 11:43:16 -07002822 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002824 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002825 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002826 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002827 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002828 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2829 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2830 << PrettyMethod(abs_method) << "'";
2831 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002832 }
Ian Rogers0d604842012-04-16 14:50:24 -07002833 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 /* Get the type of the "this" arg, which should either be a sub-interface of called
2835 * interface or Object (see comments in RegType::JoinClass).
2836 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002837 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 if (this_type.IsZero()) {
2839 /* null pointer always passes (and always fails at runtime) */
2840 } else {
2841 if (this_type.IsUninitializedTypes()) {
2842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2843 << this_type;
2844 break;
2845 }
2846 // In the past we have tried to assert that "called_interface" is assignable
2847 // from "this_type.GetClass()", however, as we do an imprecise Join
2848 // (RegType::JoinClass) we don't have full information on what interfaces are
2849 // implemented by "this_type". For example, two classes may implement the same
2850 // interfaces and have a common parent that doesn't implement the interface. The
2851 // join will set "this_type" to the parent class and a test that this implements
2852 // the interface will incorrectly fail.
2853 }
2854 /*
2855 * We don't have an object instance, so we can't find the concrete method. However, all of
2856 * the type information is in the abstract method, so we're good.
2857 */
2858 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002860 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2862 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002863 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002865 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002867 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002869 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002870 } else {
2871 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2872 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002873 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002874 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 }
jeffhaobdb76512011-09-07 11:43:16 -07002876 case Instruction::NEG_INT:
2877 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::NEG_LONG:
2881 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002883 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002886 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002887 break;
2888 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002889 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
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::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002901 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002905 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002909 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002913 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002916 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002917 break;
2918 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002920 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002924 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
2926 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002928 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002929 break;
2930 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002931 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002932 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002933 break;
2934 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002936 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002937 break;
2938 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002939 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002940 break;
2941 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002942 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002943 break;
2944 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002945 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002946 break;
2947
2948 case Instruction::ADD_INT:
2949 case Instruction::SUB_INT:
2950 case Instruction::MUL_INT:
2951 case Instruction::REM_INT:
2952 case Instruction::DIV_INT:
2953 case Instruction::SHL_INT:
2954 case Instruction::SHR_INT:
2955 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002957 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002958 break;
2959 case Instruction::AND_INT:
2960 case Instruction::OR_INT:
2961 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002963 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
2965 case Instruction::ADD_LONG:
2966 case Instruction::SUB_LONG:
2967 case Instruction::MUL_LONG:
2968 case Instruction::DIV_LONG:
2969 case Instruction::REM_LONG:
2970 case Instruction::AND_LONG:
2971 case Instruction::OR_LONG:
2972 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002974 reg_types_.LongLo(), reg_types_.LongHi(),
2975 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002976 break;
2977 case Instruction::SHL_LONG:
2978 case Instruction::SHR_LONG:
2979 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002982 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002983 break;
2984 case Instruction::ADD_FLOAT:
2985 case Instruction::SUB_FLOAT:
2986 case Instruction::MUL_FLOAT:
2987 case Instruction::DIV_FLOAT:
2988 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2990 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992 case Instruction::ADD_DOUBLE:
2993 case Instruction::SUB_DOUBLE:
2994 case Instruction::MUL_DOUBLE:
2995 case Instruction::DIV_DOUBLE:
2996 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002997 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002998 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2999 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003000 break;
3001 case Instruction::ADD_INT_2ADDR:
3002 case Instruction::SUB_INT_2ADDR:
3003 case Instruction::MUL_INT_2ADDR:
3004 case Instruction::REM_INT_2ADDR:
3005 case Instruction::SHL_INT_2ADDR:
3006 case Instruction::SHR_INT_2ADDR:
3007 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003008 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3009 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011 case Instruction::AND_INT_2ADDR:
3012 case Instruction::OR_INT_2ADDR:
3013 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3015 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003016 break;
3017 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3019 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003020 break;
3021 case Instruction::ADD_LONG_2ADDR:
3022 case Instruction::SUB_LONG_2ADDR:
3023 case Instruction::MUL_LONG_2ADDR:
3024 case Instruction::DIV_LONG_2ADDR:
3025 case Instruction::REM_LONG_2ADDR:
3026 case Instruction::AND_LONG_2ADDR:
3027 case Instruction::OR_LONG_2ADDR:
3028 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003030 reg_types_.LongLo(), reg_types_.LongHi(),
3031 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003032 break;
3033 case Instruction::SHL_LONG_2ADDR:
3034 case Instruction::SHR_LONG_2ADDR:
3035 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::ADD_FLOAT_2ADDR:
3040 case Instruction::SUB_FLOAT_2ADDR:
3041 case Instruction::MUL_FLOAT_2ADDR:
3042 case Instruction::DIV_FLOAT_2ADDR:
3043 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3045 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::ADD_DOUBLE_2ADDR:
3048 case Instruction::SUB_DOUBLE_2ADDR:
3049 case Instruction::MUL_DOUBLE_2ADDR:
3050 case Instruction::DIV_DOUBLE_2ADDR:
3051 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003053 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3054 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003055 break;
3056 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003057 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003058 case Instruction::MUL_INT_LIT16:
3059 case Instruction::DIV_INT_LIT16:
3060 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3062 true);
jeffhaobdb76512011-09-07 11:43:16 -07003063 break;
3064 case Instruction::AND_INT_LIT16:
3065 case Instruction::OR_INT_LIT16:
3066 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3068 true);
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
3070 case Instruction::ADD_INT_LIT8:
3071 case Instruction::RSUB_INT_LIT8:
3072 case Instruction::MUL_INT_LIT8:
3073 case Instruction::DIV_INT_LIT8:
3074 case Instruction::REM_INT_LIT8:
3075 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003076 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003077 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3079 false);
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::AND_INT_LIT8:
3082 case Instruction::OR_INT_LIT8:
3083 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3085 false);
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003088 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003089 case Instruction::RETURN_VOID_NO_BARRIER:
3090 if (IsConstructor() && !IsStatic()) {
3091 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003092 if (declaring_class.IsUnresolvedReference()) {
3093 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3094 // manually over the underlying dex file.
3095 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3096 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3097 if (first_index != DexFile::kDexNoIndex) {
3098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3099 << first_index;
3100 }
3101 break;
3102 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003103 auto* klass = declaring_class.GetClass();
3104 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3105 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3107 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003108 break;
3109 }
3110 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003111 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003112 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3113 // quickened opcodes (otherwise this could be a fall-through).
3114 if (!IsConstructor()) {
3115 if (!GetMethodReturnType().IsConflict()) {
3116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3117 }
3118 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003119 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003120 // Note: the following instructions encode offsets derived from class linking.
3121 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3122 // meaning if the class linking and resolution were successful.
3123 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003124 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 break;
3126 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003127 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 break;
3129 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003130 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003132 case Instruction::IGET_BOOLEAN_QUICK:
3133 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3134 break;
3135 case Instruction::IGET_BYTE_QUICK:
3136 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3137 break;
3138 case Instruction::IGET_CHAR_QUICK:
3139 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3140 break;
3141 case Instruction::IGET_SHORT_QUICK:
3142 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3143 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003144 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003145 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003147 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003148 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003149 break;
3150 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003151 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003152 break;
3153 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003154 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003155 break;
3156 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003157 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003158 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003159 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003160 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 break;
3162 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003163 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003164 break;
3165 case Instruction::INVOKE_VIRTUAL_QUICK:
3166 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3167 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003168 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003170 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003171 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003172 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 } else {
3175 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3176 }
3177 just_set_result = true;
3178 }
3179 break;
3180 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003181 case Instruction::INVOKE_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 invoke-lambda verification
3186 break;
3187 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003188 case Instruction::CAPTURE_VARIABLE: {
3189 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3190 // If the code would've normally hard-failed, then the interpreter will throw the
3191 // appropriate verification errors at runtime.
3192 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3193 break;
3194 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003195 case Instruction::CREATE_LAMBDA: {
3196 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3197 // If the code would've normally hard-failed, then the interpreter will throw the
3198 // appropriate verification errors at runtime.
3199 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3200 break;
3201 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003202 case Instruction::LIBERATE_VARIABLE: {
3203 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3204 // If the code would've normally hard-failed, then the interpreter will throw the
3205 // appropriate verification errors at runtime.
3206 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3207 break;
3208 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003209
Igor Murashkin6918bf12015-09-27 19:19:06 -07003210 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003211 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003212 // Conservatively fail verification on release builds.
3213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3214 break;
3215 }
3216
3217 case Instruction::BOX_LAMBDA: {
3218 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3219 // If the code would've normally hard-failed, then the interpreter will throw the
3220 // appropriate verification errors at runtime.
3221 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003222
3223 // Partial verification. Sets the resulting type to always be an object, which
3224 // is good enough for some other verification to occur without hard-failing.
3225 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3226 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003227 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003228 break;
3229 }
3230
3231 case Instruction::UNBOX_LAMBDA: {
3232 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3233 // If the code would've normally hard-failed, then the interpreter will throw the
3234 // appropriate verification errors at runtime.
3235 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3236 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003237 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003238
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003240 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003241 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 case Instruction::UNUSED_79:
3243 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003245 break;
3246
3247 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003248 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003249 * complain if an instruction is missing (which is desirable).
3250 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003251 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003252
Ian Rogersad0b3a32012-04-16 14:50:24 -07003253 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003254 if (Runtime::Current()->IsAotCompiler()) {
3255 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003256 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3257 LOG(ERROR) << "Pending failures:";
3258 for (auto& error : failures_) {
3259 LOG(ERROR) << error;
3260 }
3261 for (auto& error_msg : failure_messages_) {
3262 LOG(ERROR) << error_msg->str();
3263 }
3264 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3265 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003266 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267 /* immediate failure, reject class */
3268 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3269 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003270 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003271 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003272 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003273 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3274 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3275 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003276 }
jeffhaobdb76512011-09-07 11:43:16 -07003277 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 * If we didn't just set the result register, clear it out. This ensures that you can only use
3279 * "move-result" immediately after the result is set. (We could check this statically, but it's
3280 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003281 */
3282 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003284 }
3285
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003286
jeffhaobdb76512011-09-07 11:43:16 -07003287
3288 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003289 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003290 *
3291 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003292 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003293 * somebody could get a reference field, check it for zero, and if the
3294 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003295 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003296 * that, and will reject the code.
3297 *
3298 * TODO: avoid re-fetching the branch target
3299 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003300 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003301 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003303 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003305 return false;
3306 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003307 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003308 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003309 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 }
jeffhaobdb76512011-09-07 11:43:16 -07003311 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003312 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003313 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003314 return false;
3315 }
3316 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003317 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003318 return false;
3319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
jeffhaobdb76512011-09-07 11:43:16 -07003321 }
3322
3323 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003324 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003325 *
3326 * We've already verified that the table is structurally sound, so we
3327 * just need to walk through and tag the targets.
3328 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003329 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003330 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003331 const uint16_t* switch_insns = insns + offset_to_switch;
3332 int switch_count = switch_insns[1];
3333 int offset_to_targets, targ;
3334
3335 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3336 /* 0 = sig, 1 = count, 2/3 = first key */
3337 offset_to_targets = 4;
3338 } else {
3339 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003340 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003341 offset_to_targets = 2 + 2 * switch_count;
3342 }
3343
3344 /* verify each switch target */
3345 for (targ = 0; targ < switch_count; targ++) {
3346 int offset;
3347 uint32_t abs_offset;
3348
3349 /* offsets are 32-bit, and only partly endian-swapped */
3350 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003351 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 abs_offset = work_insn_idx_ + offset;
3353 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003354 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003355 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003357 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003358 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003359 }
jeffhaobdb76512011-09-07 11:43:16 -07003360 }
3361 }
3362
3363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3365 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003366 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003367 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003368 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003369 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003370
Andreas Gampef91baf12014-07-18 15:41:00 -07003371 // Need the linker to try and resolve the handled class to check if it's Throwable.
3372 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3373
Ian Rogers0571d352011-11-03 19:51:38 -07003374 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003375 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3376 if (handler_type_idx == DexFile::kDexNoIndex16) {
3377 has_catch_all_handler = true;
3378 } else {
3379 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003380 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3381 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003382 if (klass != nullptr) {
3383 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3384 has_catch_all_handler = true;
3385 }
3386 } else {
3387 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003388 DCHECK(self_->IsExceptionPending());
3389 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003390 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 }
jeffhaobdb76512011-09-07 11:43:16 -07003392 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3394 * "work_regs", because at runtime the exception will be thrown before the instruction
3395 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003396 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003397 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003398 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 }
jeffhaobdb76512011-09-07 11:43:16 -07003400 }
3401
3402 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3404 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003405 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003406 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003407 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 * The state in work_line reflects the post-execution state. If the current instruction is a
3409 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003410 * it will do so before grabbing the lock).
3411 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003412 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003413 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003415 return false;
3416 }
3417 }
3418 }
3419
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003420 /* Handle "continue". Tag the next consecutive instruction.
3421 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3422 * because it changes work_line_ when performing peephole optimization
3423 * and this change should not be used in those cases.
3424 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003425 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3427 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003428 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3430 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003431 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003432 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3433 // next instruction isn't one.
3434 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3435 return false;
3436 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003437 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003438 // Make workline consistent with fallthrough computed from peephole optimization.
3439 work_line_->CopyFromLine(fallthrough_line.get());
3440 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003441 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003442 // For returns we only care about the operand to the return, all other registers are dead.
3443 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003444 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003445 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003446 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003447 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003448 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3449 // needed. If the merge changes the state of the registers then the work line will be
3450 // updated.
3451 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003452 return false;
3453 }
3454 } else {
3455 /*
3456 * We're not recording register data for the next instruction, so we don't know what the
3457 * prior state was. We have to assume that something has changed and re-evaluate it.
3458 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003459 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003460 }
3461 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003462
jeffhaod1f0fde2011-09-08 17:25:33 -07003463 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003464 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003465 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003466 }
3467
3468 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003469 * Update start_guess. Advance to the next instruction of that's
3470 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003471 * neither of those exists we're in a return or throw; leave start_guess
3472 * alone and let the caller sort it out.
3473 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003474 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003475 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3476 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003477 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003478 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003480 }
3481
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003483 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003484
Andreas Gampea727e372015-08-25 09:22:37 -07003485 if (have_pending_runtime_throw_failure_) {
3486 have_any_pending_runtime_throw_failure_ = true;
3487 // Reset the pending_runtime_throw flag now.
3488 have_pending_runtime_throw_failure_ = false;
3489 }
3490
jeffhaobdb76512011-09-07 11:43:16 -07003491 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003492} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003493
Mathieu Chartierde40d472015-10-15 17:47:48 -07003494void MethodVerifier::UninstantiableError(const char* descriptor) {
3495 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3496 << "non-instantiable klass " << descriptor;
3497}
3498
3499inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3500 return klass->IsInstantiable() || klass->IsPrimitive();
3501}
3502
Ian Rogersd8f69b02014-09-10 21:43:52 +00003503const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003504 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003505 const RegType* result = nullptr;
3506 if (klass != nullptr) {
3507 bool precise = klass->CannotBeAssignedFromOtherTypes();
3508 if (precise && !IsInstantiableOrPrimitive(klass)) {
3509 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3510 UninstantiableError(descriptor);
3511 precise = false;
3512 }
3513 result = reg_types_.FindClass(klass, precise);
3514 if (result == nullptr) {
3515 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3516 result = reg_types_.InsertClass(descriptor, klass, precise);
3517 }
3518 } else {
3519 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3520 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003521 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003522 DCHECK(result != nullptr);
3523 if (result->IsConflict()) {
3524 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3525 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3526 << "' in " << GetDeclaringClass();
3527 return *result;
3528 }
3529 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3530 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003531 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003532 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003533 // check at runtime if access is allowed and so pass here. If result is
3534 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003535 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3536 const RegType& referrer = GetDeclaringClass();
3537 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3538 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3539 << referrer << "' -> '" << result << "'";
3540 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003541 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003542 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003543}
3544
Ian Rogersd8f69b02014-09-10 21:43:52 +00003545const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003546 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003548 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3550 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003551 CatchHandlerIterator iterator(handlers_ptr);
3552 for (; iterator.HasNext(); iterator.Next()) {
3553 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3554 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003555 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003557 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003558 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003559 if (exception.IsUnresolvedTypes()) {
3560 // We don't know enough about the type. Fail here and let runtime handle it.
3561 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3562 return exception;
3563 } else {
3564 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3565 return reg_types_.Conflict();
3566 }
Jeff Haob878f212014-04-24 16:25:36 -07003567 } else if (common_super == nullptr) {
3568 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003569 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003570 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003571 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003572 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003573 if (FailOrAbort(this,
3574 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3575 "java.lang.Throwable is not assignable-from common_super at ",
3576 work_insn_idx_)) {
3577 break;
3578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 }
3580 }
3581 }
3582 }
Ian Rogers0571d352011-11-03 19:51:38 -07003583 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003584 }
3585 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003586 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003587 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003588 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003589 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003590 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003591 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003592}
3593
Mathieu Chartiere401d142015-04-22 13:56:20 -07003594ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3595 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003596 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003597 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 if (klass_type.IsConflict()) {
3599 std::string append(" in attempt to access method ");
3600 append += dex_file_->GetMethodName(method_id);
3601 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003602 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003603 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003604 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003605 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003606 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003607 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003608 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003609 auto* cl = Runtime::Current()->GetClassLinker();
3610 auto pointer_size = cl->GetImagePointerSize();
3611 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003612 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003613 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003614 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003615
3616 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003617 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003618 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003619 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003621 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003624 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003626 // If a virtual or interface method wasn't found with the expected type, look in
3627 // the direct methods. This can happen when the wrong invoke type is used or when
3628 // a class has changed, and will be flagged as an error in later checks.
3629 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003630 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003631 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003632 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003633 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3634 << PrettyDescriptor(klass) << "." << name
3635 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003636 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003637 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 }
3639 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3641 // enforce them here.
3642 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3644 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003645 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003647 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003648 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3650 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003651 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003652 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003653 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003654 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003655 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003656 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003657 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003658 }
jeffhaode0d9c92012-02-27 13:58:13 -08003659 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3660 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3662 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003663 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003664 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003665 // Check that interface methods match interface classes.
3666 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3667 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3668 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003670 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3671 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3672 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003673 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003674 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003675 // See if the method type implied by the invoke instruction matches the access flags for the
3676 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003677 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003678 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3679 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3680 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003681 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3682 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003683 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003685 return res_method;
3686}
3687
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003688template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003689ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3690 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003691 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3692 // match the call to the signature. Also, we might be calling through an abstract method
3693 // definition (which doesn't have register count values).
3694 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3695 /* caught by static verifier */
3696 DCHECK(is_range || expected_args <= 5);
3697 if (expected_args > code_item_->outs_size_) {
3698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3699 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3700 return nullptr;
3701 }
3702
3703 uint32_t arg[5];
3704 if (!is_range) {
3705 inst->GetVarArgs(arg);
3706 }
3707 uint32_t sig_registers = 0;
3708
3709 /*
3710 * Check the "this" argument, which must be an instance of the class that declared the method.
3711 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3712 * rigorous check here (which is okay since we have to do it at runtime).
3713 */
3714 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003715 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003716 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3717 CHECK(have_pending_hard_failure_);
3718 return nullptr;
3719 }
3720 if (actual_arg_type.IsUninitializedReference()) {
3721 if (res_method) {
3722 if (!res_method->IsConstructor()) {
3723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3724 return nullptr;
3725 }
3726 } else {
3727 // Check whether the name of the called method is "<init>"
3728 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003729 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3731 return nullptr;
3732 }
3733 }
3734 }
3735 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003736 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003737 // Miranda methods have the declaring interface as their declaring class, not the abstract
3738 // class. It would be wrong to use this for the type check (interface type checks are
3739 // postponed to runtime).
3740 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003741 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003742 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003743 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3744 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003745 } else {
3746 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3747 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003748 res_method_class = &reg_types_.FromDescriptor(
3749 GetClassLoader(),
3750 dex_file_->StringByTypeIdx(class_idx),
3751 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003752 }
3753 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3754 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3755 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3756 << "' not instance of '" << *res_method_class << "'";
3757 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3758 // the compiler.
3759 if (have_pending_hard_failure_) {
3760 return nullptr;
3761 }
3762 }
3763 }
3764 sig_registers = 1;
3765 }
3766
3767 for ( ; it->HasNext(); it->Next()) {
3768 if (sig_registers >= expected_args) {
3769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3770 " arguments, found " << sig_registers << " or more.";
3771 return nullptr;
3772 }
3773
3774 const char* param_descriptor = it->GetDescriptor();
3775
3776 if (param_descriptor == nullptr) {
3777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3778 "component";
3779 return nullptr;
3780 }
3781
Ian Rogersd8f69b02014-09-10 21:43:52 +00003782 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003783 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3784 arg[sig_registers];
3785 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003786 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003787 if (!src_type.IsIntegralTypes()) {
3788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3789 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003790 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003791 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003792 } else {
3793 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3794 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3795 // the compiler.
3796 if (have_pending_hard_failure_) {
3797 return nullptr;
3798 }
3799 } else if (reg_type.IsLongOrDoubleTypes()) {
3800 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3801 // instructions not specifying register pairs by the first component, but require them
3802 // nonetheless. Only check when there's an actual register in the parameters. If there's
3803 // none, this will fail below.
3804 if (!is_range && sig_registers + 1 < expected_args) {
3805 uint32_t second_reg = arg[sig_registers + 1];
3806 if (second_reg != get_reg + 1) {
3807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3808 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3809 << second_reg << ".";
3810 return nullptr;
3811 }
3812 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003813 }
3814 }
3815 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3816 }
3817 if (expected_args != sig_registers) {
3818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3819 " arguments, found " << sig_registers;
3820 return nullptr;
3821 }
3822 return res_method;
3823}
3824
3825void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3826 MethodType method_type,
3827 bool is_range) {
3828 // As the method may not have been resolved, make this static check against what we expect.
3829 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3830 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3831 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3832 DexFileParameterIterator it(*dex_file_,
3833 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3834 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3835 nullptr);
3836}
3837
3838class MethodParamListDescriptorIterator {
3839 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003840 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003841 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3842 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3843 }
3844
3845 bool HasNext() {
3846 return pos_ < params_size_;
3847 }
3848
3849 void Next() {
3850 ++pos_;
3851 }
3852
Mathieu Chartier90443472015-07-16 20:32:27 -07003853 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003854 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3855 }
3856
3857 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003858 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003859 size_t pos_;
3860 const DexFile::TypeList* params_;
3861 const size_t params_size_;
3862};
3863
Mathieu Chartiere401d142015-04-22 13:56:20 -07003864ArtMethod* MethodVerifier::VerifyInvocationArgs(
3865 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003866 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3867 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003868 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003869
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003871 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003872 // Check what we can statically.
3873 if (!have_pending_hard_failure_) {
3874 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3875 }
3876 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003877 }
3878
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3880 // has a vtable entry for the target method.
3881 if (is_super) {
3882 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003883 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003884 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003885 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003886 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003887 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003888 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003889 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003890 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003891 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003892 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003893 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003894 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003895 << "." << res_method->GetName()
3896 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003897 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 }
3899 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003900
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003901 // Process the target method's signature. This signature may or may not
3902 MethodParamListDescriptorIterator it(res_method);
3903 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3904 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003905}
3906
Mathieu Chartiere401d142015-04-22 13:56:20 -07003907ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3908 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003909 if (is_range) {
3910 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3911 } else {
3912 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3913 }
3914 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003915 if (!actual_arg_type.HasClass()) {
3916 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003917 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003918 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003919 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003920 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003921 if (klass->IsInterface()) {
3922 // Derive Object.class from Class.class.getSuperclass().
3923 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003924 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003925 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003926 return nullptr;
3927 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003928 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003929 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003930 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003931 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003932 if (!dispatch_class->HasVTable()) {
3933 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3934 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003935 return nullptr;
3936 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003937 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003938 auto* cl = Runtime::Current()->GetClassLinker();
3939 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003940 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3941 FailOrAbort(this, allow_failure,
3942 "Receiver class has not enough vtable slots for quickened invoke at ",
3943 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003944 return nullptr;
3945 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003946 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003947 if (self_->IsExceptionPending()) {
3948 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3949 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003950 return nullptr;
3951 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003952 return res_method;
3953}
3954
Mathieu Chartiere401d142015-04-22 13:56:20 -07003955ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003956 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3957 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3958
Mathieu Chartiere401d142015-04-22 13:56:20 -07003959 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003960 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003963 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003964 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3965 work_insn_idx_)) {
3966 return nullptr;
3967 }
3968 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3969 work_insn_idx_)) {
3970 return nullptr;
3971 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003972
3973 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3974 // match the call to the signature. Also, we might be calling through an abstract method
3975 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003976 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003977 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003978 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003979 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003980 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3981 /* caught by static verifier */
3982 DCHECK(is_range || expected_args <= 5);
3983 if (expected_args > code_item_->outs_size_) {
3984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3985 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003986 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003987 }
3988
3989 /*
3990 * Check the "this" argument, which must be an instance of the class that declared the method.
3991 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3992 * rigorous check here (which is okay since we have to do it at runtime).
3993 */
3994 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003996 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003997 }
3998 if (!actual_arg_type.IsZero()) {
3999 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004000 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004001 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004002 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004003 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07004004 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
4005 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004007 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004008 }
4009 }
4010 /*
4011 * Process the target method's signature. This signature may or may not
4012 * have been verified, so we can't assume it's properly formed.
4013 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004014 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004015 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004016 uint32_t arg[5];
4017 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004018 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004019 }
4020 size_t actual_args = 1;
4021 for (size_t param_index = 0; param_index < params_size; param_index++) {
4022 if (actual_args >= expected_args) {
4023 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004024 << "'. Expected " << expected_args
4025 << " arguments, processing argument " << actual_args
4026 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004027 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004028 }
4029 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004030 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004031 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004033 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004034 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004035 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004036 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004037 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004038 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004039 return res_method;
4040 }
4041 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4042 }
4043 if (actual_args != expected_args) {
4044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4045 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004046 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004047 } else {
4048 return res_method;
4049 }
4050}
4051
Ian Rogers62342ec2013-06-11 10:26:37 -07004052void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004053 uint32_t type_idx;
4054 if (!is_filled) {
4055 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4056 type_idx = inst->VRegC_22c();
4057 } else if (!is_range) {
4058 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4059 type_idx = inst->VRegB_35c();
4060 } else {
4061 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4062 type_idx = inst->VRegB_3rc();
4063 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004064 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004065 if (res_type.IsConflict()) { // bad class
4066 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004067 } else {
4068 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4069 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004071 } else if (!is_filled) {
4072 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004073 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004074 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004075 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004076 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004077 } else {
4078 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4079 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004080 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004081 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4082 uint32_t arg[5];
4083 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004084 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004085 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004086 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004087 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004088 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4089 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004090 return;
4091 }
4092 }
4093 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004094 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004095 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004096 }
4097 }
4098}
4099
Sebastien Hertz5243e912013-05-21 10:55:07 +02004100void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004101 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004103 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004105 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004106 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004107 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004108 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004109 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4110 // instruction type. TODO: have a proper notion of bottom here.
4111 if (!is_primitive || insn_type.IsCategory1Types()) {
4112 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004113 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004114 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004115 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004116 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4117 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004118 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004119 }
jeffhaofc3144e2012-02-01 17:21:15 -08004120 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004121 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004122 } else {
4123 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004124 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004125 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004127 << " source for aget-object";
4128 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004130 << " source for category 1 aget";
4131 } else if (is_primitive && !insn_type.Equals(component_type) &&
4132 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004133 (insn_type.IsLong() && component_type.IsDouble()))) {
4134 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4135 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004136 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004137 // Use knowledge of the field type which is stronger than the type inferred from the
4138 // instruction, which can't differentiate object types and ints from floats, longs from
4139 // doubles.
4140 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004141 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004142 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004143 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004144 component_type.HighHalf(&reg_types_));
4145 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004146 }
4147 }
4148 }
4149}
4150
Ian Rogersd8f69b02014-09-10 21:43:52 +00004151void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004152 const uint32_t vregA) {
4153 // Primitive assignability rules are weaker than regular assignability rules.
4154 bool instruction_compatible;
4155 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004156 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004157 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004158 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004159 value_compatible = value_type.IsIntegralTypes();
4160 } else if (target_type.IsFloat()) {
4161 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4162 value_compatible = value_type.IsFloatTypes();
4163 } else if (target_type.IsLong()) {
4164 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004165 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4166 // as target_type depends on the resolved type of the field.
4167 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004168 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004169 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4170 } else {
4171 value_compatible = false;
4172 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004173 } else if (target_type.IsDouble()) {
4174 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004175 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4176 // as target_type depends on the resolved type of the field.
4177 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004178 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004179 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4180 } else {
4181 value_compatible = false;
4182 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004183 } else {
4184 instruction_compatible = false; // reference with primitive store
4185 value_compatible = false; // unused
4186 }
4187 if (!instruction_compatible) {
4188 // This is a global failure rather than a class change failure as the instructions and
4189 // the descriptors for the type should have been consistent within the same file at
4190 // compile time.
4191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4192 << "' but expected type '" << target_type << "'";
4193 return;
4194 }
4195 if (!value_compatible) {
4196 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4197 << " of type " << value_type << " but expected " << target_type << " for put";
4198 return;
4199 }
4200}
4201
Sebastien Hertz5243e912013-05-21 10:55:07 +02004202void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004203 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004204 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004205 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004207 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004208 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004209 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004210 // Null array type; this code path will fail at runtime.
4211 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004212 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4213 // and fits multiple register types.
4214 const RegType* modified_reg_type = &insn_type;
4215 if ((modified_reg_type == &reg_types_.Integer()) ||
4216 (modified_reg_type == &reg_types_.LongLo())) {
4217 // May be integer or float | long or double. Overwrite insn_type accordingly.
4218 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4219 if (modified_reg_type == &reg_types_.Integer()) {
4220 if (&value_type == &reg_types_.Float()) {
4221 modified_reg_type = &value_type;
4222 }
4223 } else {
4224 if (&value_type == &reg_types_.DoubleLo()) {
4225 modified_reg_type = &value_type;
4226 }
4227 }
4228 }
4229 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004230 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004232 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004233 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004234 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004235 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004236 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004237 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004238 if (!component_type.IsReferenceTypes()) {
4239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4240 << " source for aput-object";
4241 } else {
4242 // The instruction agrees with the type of array, confirm the value to be stored does too
4243 // Note: we use the instruction type (rather than the component type) for aput-object as
4244 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004246 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004247 }
4248 }
4249 }
4250}
4251
Mathieu Chartierc7853442015-03-27 14:35:38 -07004252ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004253 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4254 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004255 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004256 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004257 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4258 field_idx, dex_file_->GetFieldName(field_id),
4259 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004260 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004261 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004262 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004263 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004264 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004265 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004266 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4267 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004268 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004269 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004270 << dex_file_->GetFieldName(field_id) << ") in "
4271 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 DCHECK(self_->IsExceptionPending());
4273 self_->ClearException();
4274 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004275 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4276 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004277 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004278 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004279 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004280 } else if (!field->IsStatic()) {
4281 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004282 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004283 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004284 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004285}
4286
Mathieu Chartierc7853442015-03-27 14:35:38 -07004287ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004288 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4289 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004290 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004291 if (klass_type.IsConflict()) {
4292 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4293 field_idx, dex_file_->GetFieldName(field_id),
4294 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004295 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004296 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004297 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004298 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004299 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004300 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004301 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4302 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004303 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004304 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004305 << dex_file_->GetFieldName(field_id) << ") in "
4306 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004307 DCHECK(self_->IsExceptionPending());
4308 self_->ClearException();
4309 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004310 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4311 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004312 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004313 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004314 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004315 } else if (field->IsStatic()) {
4316 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4317 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004318 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004319 } else if (obj_type.IsZero()) {
4320 // Cannot infer and check type, however, access will cause null pointer exception
4321 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004322 } else if (!obj_type.IsReferenceTypes()) {
4323 // Trying to read a field from something that isn't a reference
4324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4325 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004326 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004327 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004328 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004329 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004330 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4331 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004332 if (obj_type.IsUninitializedTypes() &&
4333 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4334 !field_klass.Equals(GetDeclaringClass()))) {
4335 // Field accesses through uninitialized references are only allowable for constructors where
4336 // the field is declared in this class
4337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004338 << " of a not fully initialized object within the context"
4339 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004341 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4342 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4343 // of C1. For resolution to occur the declared class of the field must be compatible with
4344 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4345 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4346 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004347 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004348 } else {
4349 return field;
4350 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004351 }
4352}
4353
Andreas Gampe896df402014-10-20 22:25:29 -07004354template <MethodVerifier::FieldAccessType kAccType>
4355void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4356 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004357 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004358 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004359 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004360 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004361 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004362 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004363 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004364 if (UNLIKELY(have_pending_hard_failure_)) {
4365 return;
4366 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004367 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004368 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004369 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004370 if (kAccType == FieldAccessType::kAccPut) {
4371 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4372 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4373 << " from other class " << GetDeclaringClass();
4374 return;
4375 }
4376 }
4377
Mathieu Chartierc7853442015-03-27 14:35:38 -07004378 mirror::Class* field_type_class =
4379 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004380 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004381 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4382 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004383 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004384 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4385 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004386 }
Ian Rogers0d604842012-04-16 14:50:24 -07004387 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004388 if (field_type == nullptr) {
4389 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4390 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004391 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004392 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004393 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004394 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004395 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4396 "Unexpected third access type");
4397 if (kAccType == FieldAccessType::kAccPut) {
4398 // sput or iput.
4399 if (is_primitive) {
4400 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004401 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004402 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004403 // If the field type is not a reference, this is a global failure rather than
4404 // a class change failure as the instructions and the descriptors for the type
4405 // should have been consistent within the same file at compile time.
4406 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4407 : VERIFY_ERROR_BAD_CLASS_HARD;
4408 Fail(error) << "expected field " << PrettyField(field)
4409 << " to be compatible with type '" << insn_type
4410 << "' but found type '" << *field_type
4411 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004412 return;
4413 }
4414 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004415 }
Andreas Gampe896df402014-10-20 22:25:29 -07004416 } else if (kAccType == FieldAccessType::kAccGet) {
4417 // sget or iget.
4418 if (is_primitive) {
4419 if (field_type->Equals(insn_type) ||
4420 (field_type->IsFloat() && insn_type.IsInteger()) ||
4421 (field_type->IsDouble() && insn_type.IsLong())) {
4422 // expected that read is of the correct primitive type or that int reads are reading
4423 // floats or long reads are reading doubles
4424 } else {
4425 // This is a global failure rather than a class change failure as the instructions and
4426 // the descriptors for the type should have been consistent within the same file at
4427 // compile time
4428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4429 << " to be of type '" << insn_type
4430 << "' but found type '" << *field_type << "' in get";
4431 return;
4432 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004433 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004434 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004435 // If the field type is not a reference, this is a global failure rather than
4436 // a class change failure as the instructions and the descriptors for the type
4437 // should have been consistent within the same file at compile time.
4438 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4439 : VERIFY_ERROR_BAD_CLASS_HARD;
4440 Fail(error) << "expected field " << PrettyField(field)
4441 << " to be compatible with type '" << insn_type
4442 << "' but found type '" << *field_type
4443 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004444 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004445 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004446 }
Andreas Gampe896df402014-10-20 22:25:29 -07004447 return;
4448 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004449 }
Andreas Gampe896df402014-10-20 22:25:29 -07004450 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004451 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004452 } else {
4453 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004455 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004456 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004457 }
4458}
4459
Mathieu Chartierc7853442015-03-27 14:35:38 -07004460ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004461 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004462 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004463 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004464 if (!object_type.HasClass()) {
4465 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4466 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004467 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004468 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004469 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004470 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004471 if (f == nullptr) {
4472 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4473 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4474 }
4475 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004476}
4477
Andreas Gampe896df402014-10-20 22:25:29 -07004478template <MethodVerifier::FieldAccessType kAccType>
4479void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4480 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004481 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004482
Mathieu Chartierc7853442015-03-27 14:35:38 -07004483 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004484 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004485 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4486 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004487 }
Andreas Gampe896df402014-10-20 22:25:29 -07004488
4489 // For an IPUT_QUICK, we now test for final flag of the field.
4490 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004491 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4492 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004493 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004494 return;
4495 }
4496 }
Andreas Gampe896df402014-10-20 22:25:29 -07004497
4498 // Get the field type.
4499 const RegType* field_type;
4500 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004501 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4502 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004503
4504 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004505 field_type = &FromClass(field->GetTypeDescriptor(),
4506 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004507 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004508 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004509 Thread* self = Thread::Current();
4510 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4511 self->ClearException();
4512 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004513 field->GetTypeDescriptor(),
4514 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004515 }
Andreas Gampe896df402014-10-20 22:25:29 -07004516 if (field_type == nullptr) {
4517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004518 return;
4519 }
Andreas Gampe896df402014-10-20 22:25:29 -07004520 }
4521
4522 const uint32_t vregA = inst->VRegA_22c();
4523 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4524 "Unexpected third access type");
4525 if (kAccType == FieldAccessType::kAccPut) {
4526 if (is_primitive) {
4527 // Primitive field assignability rules are weaker than regular assignability rules
4528 bool instruction_compatible;
4529 bool value_compatible;
4530 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4531 if (field_type->IsIntegralTypes()) {
4532 instruction_compatible = insn_type.IsIntegralTypes();
4533 value_compatible = value_type.IsIntegralTypes();
4534 } else if (field_type->IsFloat()) {
4535 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4536 value_compatible = value_type.IsFloatTypes();
4537 } else if (field_type->IsLong()) {
4538 instruction_compatible = insn_type.IsLong();
4539 value_compatible = value_type.IsLongTypes();
4540 } else if (field_type->IsDouble()) {
4541 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4542 value_compatible = value_type.IsDoubleTypes();
4543 } else {
4544 instruction_compatible = false; // reference field with primitive store
4545 value_compatible = false; // unused
4546 }
4547 if (!instruction_compatible) {
4548 // This is a global failure rather than a class change failure as the instructions and
4549 // the descriptors for the type should have been consistent within the same file at
4550 // compile time
4551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4552 << " to be of type '" << insn_type
4553 << "' but found type '" << *field_type
4554 << "' in put";
4555 return;
4556 }
4557 if (!value_compatible) {
4558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4559 << " of type " << value_type
4560 << " but expected " << *field_type
4561 << " for store to " << PrettyField(field) << " in put";
4562 return;
4563 }
4564 } else {
4565 if (!insn_type.IsAssignableFrom(*field_type)) {
4566 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4567 << " to be compatible with type '" << insn_type
4568 << "' but found type '" << *field_type
4569 << "' in put-object";
4570 return;
4571 }
4572 work_line_->VerifyRegisterType(this, vregA, *field_type);
4573 }
4574 } else if (kAccType == FieldAccessType::kAccGet) {
4575 if (is_primitive) {
4576 if (field_type->Equals(insn_type) ||
4577 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4578 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4579 // expected that read is of the correct primitive type or that int reads are reading
4580 // floats or long reads are reading doubles
4581 } else {
4582 // This is a global failure rather than a class change failure as the instructions and
4583 // the descriptors for the type should have been consistent within the same file at
4584 // compile time
4585 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4586 << " to be of type '" << insn_type
4587 << "' but found type '" << *field_type << "' in Get";
4588 return;
4589 }
4590 } else {
4591 if (!insn_type.IsAssignableFrom(*field_type)) {
4592 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4593 << " to be compatible with type '" << insn_type
4594 << "' but found type '" << *field_type
4595 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004596 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004597 return;
4598 }
4599 }
4600 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004601 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004602 } else {
4603 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004604 }
4605 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004606 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004607 }
4608}
4609
Ian Rogers776ac1f2012-04-13 23:36:36 -07004610bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004611 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004613 return false;
4614 }
4615 return true;
4616}
4617
Stephen Kyle9bc61992014-09-22 13:53:15 +01004618bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4619 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4620 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4622 return false;
4623 }
4624 return true;
4625}
4626
4627bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4628 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4629}
4630
Ian Rogersebbdd872014-07-07 23:53:08 -07004631bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4632 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004633 bool changed = true;
4634 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004635 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004636 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004637 * We haven't processed this instruction before, and we haven't touched the registers here, so
4638 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4639 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004640 */
Andreas Gampea727e372015-08-25 09:22:37 -07004641 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004642 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004643 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004644 merge_line->VerifyMonitorStackEmpty(this);
4645
Ian Rogersb8c78592013-07-25 23:52:52 +00004646 // For returns we only care about the operand to the return, all other registers are dead.
4647 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4648 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004649 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004650 }
jeffhaobdb76512011-09-07 11:43:16 -07004651 } else {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004652 ArenaUniquePtr<RegisterLine> copy;
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004653 if (gDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004654 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004655 copy->CopyFromLine(target_line);
4656 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004657 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004658 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004659 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004660 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004661 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004662 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004663 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004664 << copy->Dump(this) << " MERGE\n"
4665 << merge_line->Dump(this) << " ==\n"
4666 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004667 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004668 if (update_merge_line && changed) {
4669 merge_line->CopyFromLine(target_line);
4670 }
jeffhaobdb76512011-09-07 11:43:16 -07004671 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004672 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004673 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004674 }
4675 return true;
4676}
4677
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004678InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004679 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004680}
4681
Ian Rogersd8f69b02014-09-10 21:43:52 +00004682const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004683 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004684 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004685 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4686 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4687 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004688 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004689 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4690 return_type_class,
4691 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004692 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004693 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4694 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004695 }
4696 }
4697 if (return_type_ == nullptr) {
4698 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4699 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4700 uint16_t return_type_idx = proto_id.return_type_idx_;
4701 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004702 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004703 }
4704 }
4705 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004706}
4707
Ian Rogersd8f69b02014-09-10 21:43:52 +00004708const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004709 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004710 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004711 const char* descriptor
4712 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004713 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004714 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004715 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004716 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004717 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004718 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004719 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004720 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004721}
4722
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004723std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4724 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004725 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004726 std::vector<int32_t> result;
4727 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004728 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004729 if (type.IsConstant()) {
4730 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004731 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4732 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004733 } else if (type.IsConstantLo()) {
4734 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004735 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4736 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004737 } else if (type.IsConstantHi()) {
4738 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004739 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4740 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004741 } else if (type.IsIntegralTypes()) {
4742 result.push_back(kIntVReg);
4743 result.push_back(0);
4744 } else if (type.IsFloat()) {
4745 result.push_back(kFloatVReg);
4746 result.push_back(0);
4747 } else if (type.IsLong()) {
4748 result.push_back(kLongLoVReg);
4749 result.push_back(0);
4750 result.push_back(kLongHiVReg);
4751 result.push_back(0);
4752 ++i;
4753 } else if (type.IsDouble()) {
4754 result.push_back(kDoubleLoVReg);
4755 result.push_back(0);
4756 result.push_back(kDoubleHiVReg);
4757 result.push_back(0);
4758 ++i;
4759 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4760 result.push_back(kUndefined);
4761 result.push_back(0);
4762 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004763 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004764 result.push_back(kReferenceVReg);
4765 result.push_back(0);
4766 }
4767 }
4768 return result;
4769}
4770
Ian Rogersd8f69b02014-09-10 21:43:52 +00004771const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004772 if (precise) {
4773 // Precise constant type.
4774 return reg_types_.FromCat1Const(value, true);
4775 } else {
4776 // Imprecise constant type.
4777 if (value < -32768) {
4778 return reg_types_.IntConstant();
4779 } else if (value < -128) {
4780 return reg_types_.ShortConstant();
4781 } else if (value < 0) {
4782 return reg_types_.ByteConstant();
4783 } else if (value == 0) {
4784 return reg_types_.Zero();
4785 } else if (value == 1) {
4786 return reg_types_.One();
4787 } else if (value < 128) {
4788 return reg_types_.PosByteConstant();
4789 } else if (value < 32768) {
4790 return reg_types_.PosShortConstant();
4791 } else if (value < 65536) {
4792 return reg_types_.CharConstant();
4793 } else {
4794 return reg_types_.IntConstant();
4795 }
4796 }
4797}
4798
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004799void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004800 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004801}
4802
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004803void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004804 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004805}
jeffhaod1224c72012-02-29 13:43:08 -08004806
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004807void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4808 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004809}
4810
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004811void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4812 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004813}
4814
Andreas Gampef23f33d2015-06-23 14:18:17 -07004815const RegType& MethodVerifier::FromClass(const char* descriptor,
4816 mirror::Class* klass,
4817 bool precise) {
4818 DCHECK(klass != nullptr);
4819 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4820 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4821 << "non-instantiable klass " << descriptor;
4822 precise = false;
4823 }
4824 return reg_types_.FromClass(descriptor, klass, precise);
4825}
4826
Ian Rogersd81871c2011-10-03 13:57:23 -07004827} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004828} // namespace art