blob: 2db79ab229f8244975fc08a9899747872fde8df7 [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.
Alex Light0db36b32015-10-27 14:06:34 -0700668 if (!(IsConstructor() && IsStatic())) {
669 if (runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods)) {
670 if (IsInstanceConstructor()) {
671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
672 return false;
673 } else if (method_access_flags_ & kAccFinal) {
674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
675 return false;
676 } else if (!(method_access_flags_ & kAccPublic)) {
677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
678 return false;
679 }
680 } else {
681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
Alex Lighteb7c1442015-08-31 13:17:42 -0700682 return false;
683 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700684 }
685 }
686
687 // Instance constructors must not be synchronized.
688 if (IsInstanceConstructor()) {
689 static constexpr uint32_t kForbidden = kAccSynchronized;
690 if ((method_access_flags_ & kForbidden) != 0) {
691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
692 return false;
693 }
694 }
695 }
696
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
698 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
700 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700702 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700703
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700705 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
706 DCHECK(insn_flags_ != nullptr);
707 std::uninitialized_fill_n(insn_flags_.get(),
708 code_item_->insns_size_in_code_units_,
709 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 // Run through the instructions and see if the width checks out.
711 bool result = ComputeWidthsAndCountOps();
712 // Flag instructions guarded by a "try" block and check exception handlers.
713 result = result && ScanTryCatchBlocks();
714 // Perform static instruction verification.
715 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700716 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000717 result = result && VerifyCodeFlow();
718 // Compute information for compiler.
Alex Lighteb7c1442015-08-31 13:17:42 -0700719 if (result && runtime->IsCompiler()) {
720 result = runtime->GetCompilerCallbacks()->MethodVerified(this);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000721 }
722 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700723}
724
Ian Rogers776ac1f2012-04-13 23:36:36 -0700725std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700726 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700727 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700728
Ian Rogersad0b3a32012-04-16 14:50:24 -0700729 switch (error) {
730 case VERIFY_ERROR_NO_CLASS:
731 case VERIFY_ERROR_NO_FIELD:
732 case VERIFY_ERROR_NO_METHOD:
733 case VERIFY_ERROR_ACCESS_CLASS:
734 case VERIFY_ERROR_ACCESS_FIELD:
735 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700736 case VERIFY_ERROR_INSTANTIATION:
737 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700738 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700739 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800740 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700741 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
742 // class change and instantiation errors into soft verification errors so that we re-verify
743 // at runtime. We may fail to find or to agree on access because of not yet available class
744 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
745 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
746 // paths" that dynamically perform the verification and cause the behavior to be that akin
747 // to an interpreter.
748 error = VERIFY_ERROR_BAD_CLASS_SOFT;
749 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700750 // If we fail again at runtime, mark that this instruction would throw and force this
751 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700752 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700753
754 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
755 // try to merge garbage.
756 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700757 // Note: this can fail before we touch any instruction, for the signature of a method. So
758 // add a check.
759 if (work_insn_idx_ < DexFile::kDexNoIndex) {
760 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
761 const Instruction* inst = Instruction::At(insns);
762 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700763
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700764 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
765 saved_line_->CopyFromLine(work_line_.get());
766 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700767 }
jeffhaofaf459e2012-08-31 15:32:47 -0700768 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700769 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700770
Ian Rogersad0b3a32012-04-16 14:50:24 -0700771 // Indication that verification should be retried at runtime.
772 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700773 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700774 have_pending_hard_failure_ = true;
775 }
776 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700777
jeffhaod5347e02012-03-22 17:25:05 -0700778 // Hard verification failures at compile time will still fail at runtime, so the class is
779 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700780 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800781 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700782 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000783 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800784 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700785 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700786 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
787 ScopedObjectAccess soa(Thread::Current());
788 std::ostringstream oss;
789 Dump(oss);
790 LOG(ERROR) << oss.str();
791 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700792 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800793 }
794 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700795 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700796 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700797 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700798 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700799 failure_messages_.push_back(failure_message);
800 return *failure_message;
801}
802
Ian Rogers576ca0c2014-06-06 15:58:22 -0700803std::ostream& MethodVerifier::LogVerifyInfo() {
804 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
805 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
806}
807
Ian Rogersad0b3a32012-04-16 14:50:24 -0700808void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
809 size_t failure_num = failure_messages_.size();
810 DCHECK_NE(failure_num, 0U);
811 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
812 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700813 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700814 delete last_fail_message;
815}
816
817void MethodVerifier::AppendToLastFailMessage(std::string append) {
818 size_t failure_num = failure_messages_.size();
819 DCHECK_NE(failure_num, 0U);
820 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
821 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800822}
823
Ian Rogers776ac1f2012-04-13 23:36:36 -0700824bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 const uint16_t* insns = code_item_->insns_;
826 size_t insns_size = code_item_->insns_size_in_code_units_;
827 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700828 size_t new_instance_count = 0;
829 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700831
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700833 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700834 switch (opcode) {
835 case Instruction::APUT_OBJECT:
836 case Instruction::CHECK_CAST:
837 has_check_casts_ = true;
838 break;
839 case Instruction::INVOKE_VIRTUAL:
840 case Instruction::INVOKE_VIRTUAL_RANGE:
841 case Instruction::INVOKE_INTERFACE:
842 case Instruction::INVOKE_INTERFACE_RANGE:
843 has_virtual_or_interface_invokes_ = true;
844 break;
845 case Instruction::MONITOR_ENTER:
846 monitor_enter_count++;
847 break;
848 case Instruction::NEW_INSTANCE:
849 new_instance_count++;
850 break;
851 default:
852 break;
jeffhaobdb76512011-09-07 11:43:16 -0700853 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700855 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700857 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700858 }
859
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
862 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700863 return false;
864 }
865
Ian Rogersd81871c2011-10-03 13:57:23 -0700866 new_instance_count_ = new_instance_count;
867 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700868 return true;
869}
870
Ian Rogers776ac1f2012-04-13 23:36:36 -0700871bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700873 if (tries_size == 0) {
874 return true;
875 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700876 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700877 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700878
879 for (uint32_t idx = 0; idx < tries_size; idx++) {
880 const DexFile::TryItem* try_item = &tries[idx];
881 uint32_t start = try_item->start_addr_;
882 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700883 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
885 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700886 return false;
887 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700888 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700889 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
890 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700891 return false;
892 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700893 uint32_t dex_pc = start;
894 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
895 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700896 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700897 size_t insn_size = inst->SizeInCodeUnits();
898 dex_pc += insn_size;
899 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700900 }
901 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800902 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700903 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700904 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700905 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700906 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700907 CatchHandlerIterator iterator(handlers_ptr);
908 for (; iterator.HasNext(); iterator.Next()) {
909 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700910 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700911 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
912 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700913 return false;
914 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100915 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
916 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
917 << "exception handler begins with move-result* (" << dex_pc << ")";
918 return false;
919 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700920 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700921 // Ensure exception types are resolved so that they don't need resolution to be delivered,
922 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700923 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800924 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
925 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700926 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700927 if (exception_type == nullptr) {
928 DCHECK(self_->IsExceptionPending());
929 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700930 }
931 }
jeffhaobdb76512011-09-07 11:43:16 -0700932 }
Ian Rogers0571d352011-11-03 19:51:38 -0700933 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700934 }
jeffhaobdb76512011-09-07 11:43:16 -0700935 return true;
936}
937
Ian Rogers776ac1f2012-04-13 23:36:36 -0700938bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700940
Ian Rogers0c7abda2012-09-19 13:33:42 -0700941 /* 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 -0700942 GetInstructionFlags(0).SetBranchTarget();
943 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700944
945 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700946 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700948 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 return false;
950 }
951 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700952 // All invoke points are marked as "Throw" points already.
953 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000954 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700955 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000956 // The compiler also needs safepoints for fall-through to loop heads.
957 // Such a loop head must be a target of a branch.
958 int32_t offset = 0;
959 bool cond, self_ok;
960 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
961 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700962 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000963 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700964 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000965 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700966 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 }
968 dex_pc += inst->SizeInCodeUnits();
969 inst = inst->Next();
970 }
971 return true;
972}
973
Ian Rogers776ac1f2012-04-13 23:36:36 -0700974bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700975 if (UNLIKELY(inst->IsExperimental())) {
976 // Experimental instructions don't yet have verifier support implementation.
977 // While it is possible to use them by themselves, when we try to use stable instructions
978 // with a virtual register that was created by an experimental instruction,
979 // the data flow analysis will fail.
980 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
981 << "experimental instruction is not supported by verifier; skipping verification";
982 have_pending_experimental_failure_ = true;
983 return false;
984 }
985
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 bool result = true;
987 switch (inst->GetVerifyTypeArgumentA()) {
988 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700989 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 break;
991 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700992 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 break;
994 }
995 switch (inst->GetVerifyTypeArgumentB()) {
996 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700997 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 break;
999 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001000 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 break;
1002 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001003 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 break;
1005 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001006 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 break;
1008 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001009 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 break;
1011 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001012 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 break;
1014 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001015 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 break;
1017 }
1018 switch (inst->GetVerifyTypeArgumentC()) {
1019 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001020 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 break;
1022 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001023 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001024 break;
1025 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001026 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 break;
1028 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001029 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001030 break;
1031 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001032 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001034 case Instruction::kVerifyRegCString:
1035 result = result && CheckStringIndex(inst->VRegC());
1036 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 }
1038 switch (inst->GetVerifyExtraFlags()) {
1039 case Instruction::kVerifyArrayData:
1040 result = result && CheckArrayData(code_offset);
1041 break;
1042 case Instruction::kVerifyBranchTarget:
1043 result = result && CheckBranchTarget(code_offset);
1044 break;
1045 case Instruction::kVerifySwitchTargets:
1046 result = result && CheckSwitchTargets(code_offset);
1047 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001048 case Instruction::kVerifyVarArgNonZero:
1049 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001050 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001051 // Instructions that can actually return a negative value shouldn't have this flag.
1052 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1053 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1054 v_a > Instruction::kMaxVarArgRegs) {
1055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001056 "non-range invoke";
1057 return false;
1058 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001059
Ian Rogers29a26482014-05-02 15:27:29 -07001060 uint32_t args[Instruction::kMaxVarArgRegs];
1061 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001062 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001064 }
Andreas Gampec3314312014-06-19 18:13:29 -07001065 case Instruction::kVerifyVarArgRangeNonZero:
1066 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001068 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1069 inst->VRegA() <= 0) {
1070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1071 "range invoke";
1072 return false;
1073 }
Ian Rogers29a26482014-05-02 15:27:29 -07001074 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 break;
1076 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 result = false;
1079 break;
1080 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001081 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1083 result = false;
1084 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 return result;
1086}
1087
Ian Rogers7b078e82014-09-10 14:44:24 -07001088inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1091 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 return false;
1093 }
1094 return true;
1095}
1096
Ian Rogers7b078e82014-09-10 14:44:24 -07001097inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001098 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001099 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1100 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return false;
1102 }
1103 return true;
1104}
1105
Ian Rogers7b078e82014-09-10 14:44:24 -07001106inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1109 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 return false;
1111 }
1112 return true;
1113}
1114
Ian Rogers7b078e82014-09-10 14:44:24 -07001115inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1118 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 return false;
1120 }
1121 return true;
1122}
1123
Ian Rogers7b078e82014-09-10 14:44:24 -07001124inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1127 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 return false;
1129 }
1130 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001131 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return false;
1135 }
1136 return true;
1137}
1138
Ian Rogers7b078e82014-09-10 14:44:24 -07001139inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1142 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 return false;
1144 }
1145 return true;
1146}
1147
Ian Rogers7b078e82014-09-10 14:44:24 -07001148inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1151 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 return false;
1153 }
1154 return true;
1155}
1156
Ian Rogers776ac1f2012-04-13 23:36:36 -07001157bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1160 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 return false;
1162 }
1163 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001164 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 const char* cp = descriptor;
1166 while (*cp++ == '[') {
1167 bracket_count++;
1168 }
1169 if (bracket_count == 0) {
1170 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1172 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 return false;
1174 } else if (bracket_count > 255) {
1175 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001176 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1177 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 return false;
1179 }
1180 return true;
1181}
1182
Ian Rogers776ac1f2012-04-13 23:36:36 -07001183bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1185 const uint16_t* insns = code_item_->insns_ + cur_offset;
1186 const uint16_t* array_data;
1187 int32_t array_data_offset;
1188
1189 DCHECK_LT(cur_offset, insn_count);
1190 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001191 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1192 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001195 << ", data offset " << array_data_offset
1196 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 }
1199 /* offset to array data table is a relative branch-style offset */
1200 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001201 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1202 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1204 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 return false;
1206 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001207 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1208 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001209 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1211 << ", data offset " << array_data_offset
1212 << " not correctly visited, probably bad padding.";
1213 return false;
1214 }
1215
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001217 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1219 /* make sure the end of the switch is in range */
1220 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1222 << ", data offset " << array_data_offset << ", end "
1223 << cur_offset + array_data_offset + table_size
1224 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 return false;
1226 }
1227 return true;
1228}
1229
Ian Rogers776ac1f2012-04-13 23:36:36 -07001230bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 int32_t offset;
1232 bool isConditional, selfOkay;
1233 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1234 return false;
1235 }
1236 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1238 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return false;
1240 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001241 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1242 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001243 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1245 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 return false;
1247 }
1248 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1249 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001250 if (abs_offset < 0 ||
1251 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001252 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001254 << reinterpret_cast<void*>(abs_offset) << ") at "
1255 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 return false;
1257 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001258 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 return true;
1260}
1261
Ian Rogers776ac1f2012-04-13 23:36:36 -07001262bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 bool* selfOkay) {
1264 const uint16_t* insns = code_item_->insns_ + cur_offset;
1265 *pConditional = false;
1266 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001267 switch (*insns & 0xff) {
1268 case Instruction::GOTO:
1269 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001270 break;
1271 case Instruction::GOTO_32:
1272 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 *selfOkay = true;
1274 break;
1275 case Instruction::GOTO_16:
1276 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001277 break;
1278 case Instruction::IF_EQ:
1279 case Instruction::IF_NE:
1280 case Instruction::IF_LT:
1281 case Instruction::IF_GE:
1282 case Instruction::IF_GT:
1283 case Instruction::IF_LE:
1284 case Instruction::IF_EQZ:
1285 case Instruction::IF_NEZ:
1286 case Instruction::IF_LTZ:
1287 case Instruction::IF_GEZ:
1288 case Instruction::IF_GTZ:
1289 case Instruction::IF_LEZ:
1290 *pOffset = (int16_t) insns[1];
1291 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 break;
1293 default:
1294 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001295 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001296 return true;
1297}
1298
Ian Rogers776ac1f2012-04-13 23:36:36 -07001299bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001301 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001303 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001304 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1305 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1306 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001308 << ", switch offset " << switch_offset
1309 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001310 return false;
1311 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001312 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001314 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1315 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1317 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001318 return false;
1319 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001320 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1321 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001322 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1324 << ", switch offset " << switch_offset
1325 << " not correctly visited, probably bad padding.";
1326 return false;
1327 }
1328
David Brazdil5469d342015-09-25 16:57:53 +01001329 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1330
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001332 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001334 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001335 /* 0=sig, 1=count, 2/3=firstKey */
1336 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001337 expected_signature = Instruction::kPackedSwitchSignature;
1338 } else {
1339 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 targets_offset = 2 + 2 * switch_count;
1341 expected_signature = Instruction::kSparseSwitchSignature;
1342 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001344 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001345 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1346 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1347 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001348 return false;
1349 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001350 /* make sure the end of the switch is in range */
1351 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1353 << ", switch offset " << switch_offset
1354 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001355 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001356 return false;
1357 }
David Brazdil5469d342015-09-25 16:57:53 +01001358
1359 constexpr int32_t keys_offset = 2;
1360 if (switch_count > 1) {
1361 if (is_packed_switch) {
1362 /* for a packed switch, verify that keys do not overflow int32 */
1363 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1364 int32_t max_first_key =
1365 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1366 if (first_key > max_first_key) {
1367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1368 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001369 return false;
1370 }
David Brazdil5469d342015-09-25 16:57:53 +01001371 } else {
1372 /* for a sparse switch, verify the keys are in ascending order */
1373 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1374 for (uint32_t targ = 1; targ < switch_count; targ++) {
1375 int32_t key =
1376 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1377 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1378 if (key <= last_key) {
1379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1380 << ", this=" << key;
1381 return false;
1382 }
1383 last_key = key;
1384 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 }
1386 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001389 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1390 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001391 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001392 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001393 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001394 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1396 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1397 << reinterpret_cast<void*>(cur_offset)
1398 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 return false;
1400 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001401 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001402 }
1403 return true;
1404}
1405
Ian Rogers776ac1f2012-04-13 23:36:36 -07001406bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001407 uint16_t registers_size = code_item_->registers_size_;
1408 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001409 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1411 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 return false;
1413 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 }
1415
1416 return true;
1417}
1418
Ian Rogers776ac1f2012-04-13 23:36:36 -07001419bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 uint16_t registers_size = code_item_->registers_size_;
1421 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1422 // integer overflow when adding them here.
1423 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1425 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001426 return false;
1427 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001428 return true;
1429}
1430
Ian Rogers776ac1f2012-04-13 23:36:36 -07001431bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 uint16_t registers_size = code_item_->registers_size_;
1433 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001434
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001436 reg_table_.Init(kTrackCompilerInterestPoints,
1437 insn_flags_.get(),
1438 insns_size,
1439 registers_size,
1440 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001441
Ian Rogersd0fbd852013-09-24 18:17:04 -07001442 work_line_.reset(RegisterLine::Create(registers_size, this));
1443 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001444
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 /* Initialize register types of method arguments. */
1446 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001447 DCHECK_NE(failures_.size(), 0U);
1448 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001449 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001450 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 return false;
1452 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001453 // We may have a runtime failure here, clear.
1454 have_pending_runtime_throw_failure_ = false;
1455
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 /* Perform code flow verification. */
1457 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001458 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001460 }
jeffhaobdb76512011-09-07 11:43:16 -07001461 return true;
1462}
1463
Ian Rogersad0b3a32012-04-16 14:50:24 -07001464std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1465 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001466 for (size_t i = 0; i < failures_.size(); ++i) {
1467 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001468 }
1469 return os;
1470}
1471
Ian Rogers776ac1f2012-04-13 23:36:36 -07001472void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001473 VariableIndentationOutputStream vios(&os);
1474 Dump(&vios);
1475}
1476
1477void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001478 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001479 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 return;
jeffhaobdb76512011-09-07 11:43:16 -07001481 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001483 vios->Stream() << "Register Types:\n";
1484 ScopedIndentation indent1(vios);
1485 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 vios->Stream() << "Dumping instructions and register lines:\n";
1488 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 const Instruction* inst = Instruction::At(code_item_->insns_);
1490 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001491 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001493 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001494 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001495 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001496 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001497 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 const bool kDumpHexOfInstruction = false;
1499 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001501 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001502 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001503 }
jeffhaobdb76512011-09-07 11:43:16 -07001504}
1505
Ian Rogersd81871c2011-10-03 13:57:23 -07001506static bool IsPrimitiveDescriptor(char descriptor) {
1507 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001508 case 'I':
1509 case 'C':
1510 case 'S':
1511 case 'B':
1512 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001513 case 'F':
1514 case 'D':
1515 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001517 default:
1518 return false;
1519 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001520}
1521
Ian Rogers776ac1f2012-04-13 23:36:36 -07001522bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001524
1525 // Should have been verified earlier.
1526 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1527
1528 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001530
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001531 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001533 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001534 if (expected_args == 0) {
1535 // Expect at least a receiver.
1536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1537 return false;
1538 }
1539
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1541 // argument as uninitialized. This restricts field access until the superclass constructor is
1542 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001543 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001544 if (IsConstructor()) {
1545 if (declaring_class.IsJavaLangObject()) {
1546 // "this" is implicitly initialized.
1547 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001548 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001549 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001550 reg_line->SetRegisterType<LockOp::kClear>(
1551 this,
1552 arg_start + cur_arg,
1553 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001554 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001556 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001557 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
1560
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001561 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001563 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001564
1565 for (; iterator.HasNext(); iterator.Next()) {
1566 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001567 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 LOG(FATAL) << "Null descriptor";
1569 }
1570 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1572 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 return false;
1574 }
1575 switch (descriptor[0]) {
1576 case 'L':
1577 case '[':
1578 // We assume that reference arguments are initialized. The only way it could be otherwise
1579 // (assuming the caller was verified) is if the current method is <init>, but in that case
1580 // it's effectively considered initialized the instant we reach here (in the sense that we
1581 // can return without doing anything or call virtual methods).
1582 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001583 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001584 if (!reg_type.IsNonZeroReferenceTypes()) {
1585 DCHECK(HasFailures());
1586 return false;
1587 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001588 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 }
1590 break;
1591 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001592 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 break;
1594 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001595 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 break;
1597 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001598 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 break;
1600 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001601 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 break;
1603 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001604 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 break;
1606 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001607 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 break;
1609 case 'J':
1610 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001611 if (cur_arg + 1 >= expected_args) {
1612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1613 << " args, found more (" << descriptor << ")";
1614 return false;
1615 }
1616
Ian Rogers7b078e82014-09-10 14:44:24 -07001617 const RegType* lo_half;
1618 const RegType* hi_half;
1619 if (descriptor[0] == 'J') {
1620 lo_half = &reg_types_.LongLo();
1621 hi_half = &reg_types_.LongHi();
1622 } else {
1623 lo_half = &reg_types_.DoubleLo();
1624 hi_half = &reg_types_.DoubleHi();
1625 }
1626 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001627 cur_arg++;
1628 break;
1629 }
1630 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1632 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 return false;
1634 }
1635 cur_arg++;
1636 }
1637 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1639 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001640 return false;
1641 }
1642 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1643 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1644 // format. Only major difference from the method argument format is that 'V' is supported.
1645 bool result;
1646 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1647 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001648 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 size_t i = 0;
1650 do {
1651 i++;
1652 } while (descriptor[i] == '['); // process leading [
1653 if (descriptor[i] == 'L') { // object array
1654 do {
1655 i++; // find closing ;
1656 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1657 result = descriptor[i] == ';';
1658 } else { // primitive array
1659 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1660 }
1661 } else if (descriptor[0] == 'L') {
1662 // could be more thorough here, but shouldn't be required
1663 size_t i = 0;
1664 do {
1665 i++;
1666 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1667 result = descriptor[i] == ';';
1668 } else {
1669 result = false;
1670 }
1671 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1673 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001674 }
1675 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001676}
1677
Ian Rogers776ac1f2012-04-13 23:36:36 -07001678bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 const uint16_t* insns = code_item_->insns_;
1680 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001681
jeffhaobdb76512011-09-07 11:43:16 -07001682 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001683 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001685
jeffhaobdb76512011-09-07 11:43:16 -07001686 /* Continue until no instructions are marked "changed". */
1687 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001688 if (allow_thread_suspension_) {
1689 self_->AllowThreadSuspension();
1690 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1692 uint32_t insn_idx = start_guess;
1693 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001694 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
1696 }
jeffhaobdb76512011-09-07 11:43:16 -07001697 if (insn_idx == insns_size) {
1698 if (start_guess != 0) {
1699 /* try again, starting from the top */
1700 start_guess = 0;
1701 continue;
1702 } else {
1703 /* all flags are clear */
1704 break;
1705 }
1706 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 // We carry the working set of registers from instruction to instruction. If this address can
1708 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1709 // "changed" flags, we need to load the set of registers from the table.
1710 // Because we always prefer to continue on to the next instruction, we should never have a
1711 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1712 // target.
1713 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001714 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001716 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001717 /*
1718 * Sanity check: retrieve the stored register line (assuming
1719 * a full table) and make sure it actually matches.
1720 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001722 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 if (work_line_->CompareLine(register_line) != 0) {
1724 Dump(std::cout);
1725 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001726 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001727 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001728 << " work_line=" << work_line_->Dump(this) << "\n"
1729 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 }
jeffhaobdb76512011-09-07 11:43:16 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001734 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001735 prepend += " failed to verify: ";
1736 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001737 return false;
1738 }
jeffhaobdb76512011-09-07 11:43:16 -07001739 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001740 GetInstructionFlags(insn_idx).SetVisited();
1741 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001742 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001743
Ian Rogers1c849e52012-06-28 14:00:33 -07001744 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001745 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001746 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001747 * (besides the wasted space), but it indicates a flaw somewhere
1748 * down the line, possibly in the verifier.
1749 *
1750 * If we've substituted "always throw" instructions into the stream,
1751 * we are almost certainly going to have some dead code.
1752 */
1753 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 for (; insn_idx < insns_size;
1756 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001757 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001758 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001759 * may or may not be preceded by a padding NOP (for alignment).
1760 */
1761 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1762 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1763 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001764 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001765 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1766 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1767 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001768 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001769 }
1770
Mathieu Chartierde40d472015-10-15 17:47:48 -07001771 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001772 if (dead_start < 0)
1773 dead_start = insn_idx;
1774 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001775 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1776 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001777 dead_start = -1;
1778 }
1779 }
1780 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001781 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1782 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001783 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001784 // To dump the state of the verify after a method, do something like:
1785 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1786 // "boolean java.lang.String.equals(java.lang.Object)") {
1787 // LOG(INFO) << info_messages_.str();
1788 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001789 }
jeffhaobdb76512011-09-07 11:43:16 -07001790 return true;
1791}
1792
Andreas Gampe68df3202015-06-22 11:35:46 -07001793// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1794// is no such field.
1795static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1796 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1797 DCHECK(class_def != nullptr);
1798 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1799 DCHECK(class_data != nullptr);
1800 ClassDataItemIterator it(dex_file, class_data);
1801 // Skip static fields.
1802 while (it.HasNextStaticField()) {
1803 it.Next();
1804 }
1805 while (it.HasNextInstanceField()) {
1806 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1807 return it.GetMemberIndex();
1808 }
1809 it.Next();
1810 }
1811 return DexFile::kDexNoIndex;
1812}
1813
Andreas Gampea727e372015-08-25 09:22:37 -07001814// Setup a register line for the given return instruction.
1815static void AdjustReturnLine(MethodVerifier* verifier,
1816 const Instruction* ret_inst,
1817 RegisterLine* line) {
1818 Instruction::Code opcode = ret_inst->Opcode();
1819
1820 switch (opcode) {
1821 case Instruction::RETURN_VOID:
1822 case Instruction::RETURN_VOID_NO_BARRIER:
1823 SafelyMarkAllRegistersAsConflicts(verifier, line);
1824 break;
1825
1826 case Instruction::RETURN:
1827 case Instruction::RETURN_OBJECT:
1828 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1829 break;
1830
1831 case Instruction::RETURN_WIDE:
1832 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1833 break;
1834
1835 default:
1836 LOG(FATAL) << "Unknown return opcode " << opcode;
1837 UNREACHABLE();
1838 }
1839}
1840
Ian Rogers776ac1f2012-04-13 23:36:36 -07001841bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001842 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1843 // We want the state _before_ the instruction, for the case where the dex pc we're
1844 // interested in is itself a monitor-enter instruction (which is a likely place
1845 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001846 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001847 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001848 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1849 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1850 }
1851 }
1852
jeffhaobdb76512011-09-07 11:43:16 -07001853 /*
1854 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001855 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001856 * control to another statement:
1857 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001858 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001859 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001860 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001861 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001862 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001863 * throw an exception that is handled by an encompassing "try"
1864 * block.
1865 *
1866 * We can also return, in which case there is no successor instruction
1867 * from this point.
1868 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001869 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001870 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1872 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001873 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001874
jeffhaobdb76512011-09-07 11:43:16 -07001875 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001876 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001877 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001879 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001881 }
jeffhaobdb76512011-09-07 11:43:16 -07001882
1883 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001884 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001885 * can throw an exception, we will copy/merge this into the "catch"
1886 * address rather than work_line, because we don't want the result
1887 * from the "successful" code path (e.g. a check-cast that "improves"
1888 * a type) to be visible to the exception handler.
1889 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001890 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001892 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001894 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001895 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001896
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001897
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001898 // We need to ensure the work line is consistent while performing validation. When we spot a
1899 // peephole pattern we compute a new line for either the fallthrough instruction or the
1900 // branch target.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001901 ArenaUniquePtr<RegisterLine> branch_line;
1902 ArenaUniquePtr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001903
Sebastien Hertz5243e912013-05-21 10:55:07 +02001904 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001905 case Instruction::NOP:
1906 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001907 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001908 * a signature that looks like a NOP; if we see one of these in
1909 * the course of executing code then we have a problem.
1910 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001911 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001913 }
1914 break;
1915
1916 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 break;
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 break;
jeffhaobdb76512011-09-07 11:43:16 -07001922 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001924 break;
1925 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 break;
jeffhaobdb76512011-09-07 11:43:16 -07001928 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001930 break;
jeffhaobdb76512011-09-07 11:43:16 -07001931 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001932 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001933 break;
1934 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001935 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001936 break;
jeffhaobdb76512011-09-07 11:43:16 -07001937 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001939 break;
jeffhaobdb76512011-09-07 11:43:16 -07001940 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001941 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001942 break;
1943
1944 /*
1945 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001947 * might want to hold the result in an actual CPU register, so the
1948 * Dalvik spec requires that these only appear immediately after an
1949 * invoke or filled-new-array.
1950 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001951 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001952 * redundant with the reset done below, but it can make the debug info
1953 * easier to read in some cases.)
1954 */
1955 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001960 break;
1961 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001963 break;
1964
Ian Rogersd81871c2011-10-03 13:57:23 -07001965 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001966 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1967 // where one entrypoint to the catch block is not actually an exception path.
1968 if (work_insn_idx_ == 0) {
1969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1970 break;
1971 }
jeffhaobdb76512011-09-07 11:43:16 -07001972 /*
jeffhao60f83e32012-02-13 17:16:30 -08001973 * This statement can only appear as the first instruction in an exception handler. We verify
1974 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001975 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001976 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001977 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001978 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001979 }
jeffhaobdb76512011-09-07 11:43:16 -07001980 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001981 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001982 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 }
jeffhaobdb76512011-09-07 11:43:16 -07001985 }
1986 break;
1987 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001988 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001989 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001990 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1993 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 } else {
1995 // Compilers may generate synthetic functions that write byte values into boolean fields.
1996 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001998 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2000 ((return_type.IsBoolean() || return_type.IsByte() ||
2001 return_type.IsShort() || return_type.IsChar()) &&
2002 src_type.IsInteger()));
2003 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002004 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002006 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009 }
2010 }
2011 break;
2012 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002013 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002014 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002015 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 } else {
2019 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002021 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002022 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 }
jeffhaobdb76512011-09-07 11:43:16 -07002025 }
2026 }
2027 break;
2028 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002029 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002030 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 } else {
2034 /* return_type is the *expected* return type, not register value */
2035 DCHECK(!return_type.IsZero());
2036 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002038 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002039 // Disallow returning undefined, conflict & uninitialized values and verify that the
2040 // reference in vAA is an instance of the "return_type."
2041 if (reg_type.IsUndefined()) {
2042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2043 } else if (reg_type.IsConflict()) {
2044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2045 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002046 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2047 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002048 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002049 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2050 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2051 << "' or '" << reg_type << "'";
2052 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002053 bool soft_error = false;
2054 // Check whether arrays are involved. They will show a valid class status, even
2055 // if their components are erroneous.
2056 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2057 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2058 if (soft_error) {
2059 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2060 << reg_type << " vs " << return_type;
2061 }
2062 }
2063
2064 if (!soft_error) {
2065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2066 << "', but expected from declaration '" << return_type << "'";
2067 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002068 }
jeffhaobdb76512011-09-07 11:43:16 -07002069 }
2070 }
2071 }
2072 break;
2073
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002074 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 case Instruction::CONST_4: {
2076 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002077 work_line_->SetRegisterType<LockOp::kClear>(
2078 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002079 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 }
2081 case Instruction::CONST_16: {
2082 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002083 work_line_->SetRegisterType<LockOp::kClear>(
2084 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002085 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002087 case Instruction::CONST: {
2088 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002089 work_line_->SetRegisterType<LockOp::kClear>(
2090 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002092 }
2093 case Instruction::CONST_HIGH16: {
2094 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002095 work_line_->SetRegisterType<LockOp::kClear>(
2096 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002098 }
jeffhaobdb76512011-09-07 11:43:16 -07002099 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002100 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002102 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2103 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002104 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002106 }
2107 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002109 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2110 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002111 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002112 break;
2113 }
2114 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002116 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2117 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002118 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002119 break;
2120 }
2121 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002123 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2124 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002126 break;
2127 }
jeffhaobdb76512011-09-07 11:43:16 -07002128 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002129 work_line_->SetRegisterType<LockOp::kClear>(
2130 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 break;
jeffhaobdb76512011-09-07 11:43:16 -07002132 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002133 work_line_->SetRegisterType<LockOp::kClear>(
2134 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002137 // Get type from instruction if unresolved then we need an access check
2138 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002139 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002140 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002141 work_line_->SetRegisterType<LockOp::kClear>(
2142 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2143 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 }
jeffhaobdb76512011-09-07 11:43:16 -07002146 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002147 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002148 // Check whether the previous instruction is a move-object with vAA as a source, creating
2149 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002150 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002151 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002152 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002153 prev_idx--;
2154 }
2155 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2156 switch (prev_inst->Opcode()) {
2157 case Instruction::MOVE_OBJECT:
2158 case Instruction::MOVE_OBJECT_16:
2159 case Instruction::MOVE_OBJECT_FROM16:
2160 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2161 // Redo the copy. This won't change the register types, but update the lock status
2162 // for the aliased register.
2163 work_line_->CopyRegister1(this,
2164 prev_inst->VRegA(),
2165 prev_inst->VRegB(),
2166 kTypeCategoryRef);
2167 }
2168 break;
2169
2170 default: // Other instruction types ignored.
2171 break;
2172 }
2173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::MONITOR_EXIT:
2176 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002177 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002178 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002179 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002180 * to the need to handle asynchronous exceptions, a now-deprecated
2181 * feature that Dalvik doesn't support.)
2182 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002183 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002184 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002185 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002186 * structured locking checks are working, the former would have
2187 * failed on the -enter instruction, and the latter is impossible.
2188 *
2189 * This is fortunate, because issue 3221411 prevents us from
2190 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002191 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002192 * some catch blocks (which will show up as "dead" code when
2193 * we skip them here); if we can't, then the code path could be
2194 * "live" so we still need to check it.
2195 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002196 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002197 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002198 break;
2199
Ian Rogers28ad40d2011-10-27 15:19:26 -07002200 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002202 /*
2203 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2204 * could be a "upcast" -- not expected, so we don't try to address it.)
2205 *
2206 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002207 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002208 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2210 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002211 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002213 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002214 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002215 if (klass != nullptr && klass->IsPrimitive()) {
2216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2217 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2218 << GetDeclaringClass();
2219 break;
2220 }
2221
Ian Rogersad0b3a32012-04-16 14:50:24 -07002222 DCHECK_NE(failures_.size(), 0U);
2223 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002224 work_line_->SetRegisterType<LockOp::kClear>(this,
2225 inst->VRegA_22c(),
2226 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002227 }
2228 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002229 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002232 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002233 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 if (is_checkcast) {
2235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2236 } else {
2237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2238 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002239 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 if (is_checkcast) {
2241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2242 } else {
2243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2244 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002245 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002247 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002248 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002249 work_line_->SetRegisterType<LockOp::kClear>(this,
2250 inst->VRegA_22c(),
2251 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002252 }
jeffhaobdb76512011-09-07 11:43:16 -07002253 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002254 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 }
2256 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002257 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002258 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002259 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002260 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002261 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002262 work_line_->SetRegisterType<LockOp::kClear>(this,
2263 inst->VRegA_12x(),
2264 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002266 } else {
2267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002268 }
2269 break;
2270 }
2271 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002272 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002273 if (res_type.IsConflict()) {
2274 DCHECK_NE(failures_.size(), 0U);
2275 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002276 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002277 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2278 // can't create an instance of an interface or abstract class */
2279 if (!res_type.IsInstantiableTypes()) {
2280 Fail(VERIFY_ERROR_INSTANTIATION)
2281 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002282 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002284 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002285 // Any registers holding previous allocations from this address that have not yet been
2286 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002287 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002288 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002289 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002290 break;
2291 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002292 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002297 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002299 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002301 just_set_result = true; // Filled new array range sets result register
2302 break;
jeffhaobdb76512011-09-07 11:43:16 -07002303 case Instruction::CMPL_FLOAT:
2304 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002305 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002306 break;
2307 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002308 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002309 break;
2310 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002311 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::CMPL_DOUBLE:
2314 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002316 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002317 break;
2318 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002319 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002320 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002321 break;
2322 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002323 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002326 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002328 break;
2329 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002330 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002332 break;
2333 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002334 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002336 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002337 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002338 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002339 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2340 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002341 }
2342 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002343 }
jeffhaobdb76512011-09-07 11:43:16 -07002344 case Instruction::GOTO:
2345 case Instruction::GOTO_16:
2346 case Instruction::GOTO_32:
2347 /* no effect on or use of registers */
2348 break;
2349
2350 case Instruction::PACKED_SWITCH:
2351 case Instruction::SPARSE_SWITCH:
2352 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002353 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 case Instruction::FILL_ARRAY_DATA: {
2357 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002358 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002359 /* array_type can be null if the reg type is Zero */
2360 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002361 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2363 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002364 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002365 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002366 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002367 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2369 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002370 } else {
jeffhao457cc512012-02-02 16:55:13 -08002371 // Now verify if the element width in the table matches the element width declared in
2372 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002373 const uint16_t* array_data =
2374 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002375 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002377 } else {
2378 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2379 // Since we don't compress the data in Dex, expect to see equal width of data stored
2380 // in the table and expected from the array class.
2381 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002382 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2383 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002384 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 }
2386 }
jeffhaobdb76512011-09-07 11:43:16 -07002387 }
2388 }
2389 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002390 }
jeffhaobdb76512011-09-07 11:43:16 -07002391 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002393 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2394 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 bool mismatch = false;
2396 if (reg_type1.IsZero()) { // zero then integral or reference expected
2397 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2398 } else if (reg_type1.IsReferenceTypes()) { // both references?
2399 mismatch = !reg_type2.IsReferenceTypes();
2400 } else { // both integral?
2401 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2402 }
2403 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2405 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002406 }
2407 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002408 }
jeffhaobdb76512011-09-07 11:43:16 -07002409 case Instruction::IF_LT:
2410 case Instruction::IF_GE:
2411 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002413 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2414 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2417 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002418 }
2419 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 }
jeffhaobdb76512011-09-07 11:43:16 -07002421 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002425 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2426 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002428
2429 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002430 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002431 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002432 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002433 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002434 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002435 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002436 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002437 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2438 work_insn_idx_)) {
2439 break;
2440 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002441 } else {
2442 break;
2443 }
2444
Ian Rogers9b360392013-06-06 14:45:07 -07002445 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002446
2447 /* Check for peep-hole pattern of:
2448 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002449 * instance-of vX, vY, T;
2450 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002451 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002452 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002453 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002454 * and sharpen the type of vY to be type T.
2455 * Note, this pattern can't be if:
2456 * - if there are other branches to this branch,
2457 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002458 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002459 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002460 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2461 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2462 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002463 // Check the type of the instance-of is different than that of registers type, as if they
2464 // are the same there is no work to be done here. Check that the conversion is not to or
2465 // from an unresolved type as type information is imprecise. If the instance-of is to an
2466 // interface then ignore the type information as interfaces can only be treated as Objects
2467 // and we don't want to disallow field and other operations on the object. If the value
2468 // being instance-of checked against is known null (zero) then allow the optimization as
2469 // we didn't have type information. If the merge of the instance-of type with the original
2470 // type is assignable to the original then allow optimization. This check is performed to
2471 // ensure that subsequent merges don't lose type information - such as becoming an
2472 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002474 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002475
Ian Rogersebbdd872014-07-07 23:53:08 -07002476 if (!orig_type.Equals(cast_type) &&
2477 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002478 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002479 !cast_type.GetClass()->IsInterface() &&
2480 (orig_type.IsZero() ||
2481 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002482 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002483 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002484 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002485 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002486 branch_line.reset(update_line);
2487 }
2488 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002489 update_line->SetRegisterType<LockOp::kKeep>(this,
2490 instance_of_inst->VRegB_22c(),
2491 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002492 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002493 // See if instance-of was preceded by a move-object operation, common due to the small
2494 // register encoding space of instance-of, and propagate type information to the source
2495 // of the move-object.
2496 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002497 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002498 move_idx--;
2499 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002500 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002501 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2502 work_insn_idx_)) {
2503 break;
2504 }
Ian Rogers9b360392013-06-06 14:45:07 -07002505 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2506 switch (move_inst->Opcode()) {
2507 case Instruction::MOVE_OBJECT:
2508 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002509 update_line->SetRegisterType<LockOp::kKeep>(this,
2510 move_inst->VRegB_12x(),
2511 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002512 }
2513 break;
2514 case Instruction::MOVE_OBJECT_FROM16:
2515 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002516 update_line->SetRegisterType<LockOp::kKeep>(this,
2517 move_inst->VRegB_22x(),
2518 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002519 }
2520 break;
2521 case Instruction::MOVE_OBJECT_16:
2522 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002523 update_line->SetRegisterType<LockOp::kKeep>(this,
2524 move_inst->VRegB_32x(),
2525 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002526 }
2527 break;
2528 default:
2529 break;
2530 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002531 }
2532 }
2533 }
2534
jeffhaobdb76512011-09-07 11:43:16 -07002535 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::IF_LTZ:
2538 case Instruction::IF_GEZ:
2539 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002540 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002541 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2544 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002545 }
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 }
jeffhaobdb76512011-09-07 11:43:16 -07002548 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 break;
jeffhaobdb76512011-09-07 11:43:16 -07002551 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
jeffhaobdb76512011-09-07 11:43:16 -07002554 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 break;
jeffhaobdb76512011-09-07 11:43:16 -07002557 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002559 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
jeffhaobdb76512011-09-07 11:43:16 -07002563 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 break;
2566 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002567 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002568 break;
2569
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
2573 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 break;
2576 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002577 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
2579 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002580 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002590 break;
2591
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002593 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 break;
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002596 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 break;
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002599 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 break;
jeffhaobdb76512011-09-07 11:43:16 -07002601 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002602 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 break;
2604 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002605 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002608 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002611 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2612 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 break;
jeffhaobdb76512011-09-07 11:43:16 -07002614
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002616 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 break;
2618 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002619 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 break;
2621 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002622 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 break;
2624 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002625 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002628 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002631 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 break;
jeffhaobdb76512011-09-07 11:43:16 -07002633 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002634 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2635 false);
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637
jeffhaobdb76512011-09-07 11:43:16 -07002638 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002639 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 break;
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002642 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 break;
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002645 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 break;
jeffhaobdb76512011-09-07 11:43:16 -07002647 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002648 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 break;
2650 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002651 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002654 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002655 break;
2656 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002657 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2658 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 break;
2660
2661 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002662 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 break;
2664 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002665 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 break;
2667 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002668 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 break;
2670 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002671 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002674 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002677 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002678 break;
2679 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002680 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2681 true);
jeffhaobdb76512011-09-07 11:43:16 -07002682 break;
2683
2684 case Instruction::INVOKE_VIRTUAL:
2685 case Instruction::INVOKE_VIRTUAL_RANGE:
2686 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002688 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2689 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002690 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2691 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002692 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002693 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002694 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002695 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2696 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2697 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002698 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002699 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2700 return_type_class,
2701 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002702 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002703 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2704 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002705 }
2706 }
2707 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002708 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002709 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2710 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002711 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002712 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002713 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002714 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002715 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002716 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002717 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002718 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002720 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 }
jeffhaobdb76512011-09-07 11:43:16 -07002722 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002724 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002725 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002726 const char* return_type_descriptor;
2727 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002728 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002729 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002730 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002731 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002732 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002733 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2734 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2735 } else {
2736 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002737 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002738 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2739 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2740 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002741 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002742 return_type = &FromClass(return_type_descriptor,
2743 return_type_class,
2744 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002745 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002746 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2747 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002748 }
Ian Rogers46685432012-06-03 22:26:43 -07002749 }
2750 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002751 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 * Some additional checks when calling a constructor. We know from the invocation arg check
2753 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2754 * that to require that called_method->klass is the same as this->klass or this->super,
2755 * allowing the latter only if the "this" argument is the same as the "this" argument to
2756 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002757 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002758 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002759 if (this_type.IsConflict()) // failure.
2760 break;
jeffhaobdb76512011-09-07 11:43:16 -07002761
jeffhaob57e9522012-04-26 18:08:21 -07002762 /* no null refs allowed (?) */
2763 if (this_type.IsZero()) {
2764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2765 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002766 }
jeffhaob57e9522012-04-26 18:08:21 -07002767
2768 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002769 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002770 // TODO: re-enable constructor type verification
2771 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002772 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002773 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2774 // break;
2775 // }
jeffhaob57e9522012-04-26 18:08:21 -07002776
2777 /* arg must be an uninitialized reference */
2778 if (!this_type.IsUninitializedTypes()) {
2779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2780 << this_type;
2781 break;
2782 }
2783
2784 /*
2785 * Replace the uninitialized reference with an initialized one. We need to do this for all
2786 * registers that have the same object instance in them, not just the "this" register.
2787 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002788 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2789 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002790 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002791 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002792 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002793 }
2794 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002795 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002797 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002798 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002799 just_set_result = true;
2800 break;
2801 }
2802 case Instruction::INVOKE_STATIC:
2803 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002804 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002805 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002806 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002808 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002809 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2810 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002811 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002812 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002813 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002814 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002815 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002816 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002817 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002818 } else {
2819 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 just_set_result = true;
2822 }
2823 break;
jeffhaobdb76512011-09-07 11:43:16 -07002824 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002825 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002826 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002827 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002828 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002829 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002830 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2831 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2832 << PrettyMethod(abs_method) << "'";
2833 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002834 }
Ian Rogers0d604842012-04-16 14:50:24 -07002835 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002836 /* Get the type of the "this" arg, which should either be a sub-interface of called
2837 * interface or Object (see comments in RegType::JoinClass).
2838 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002839 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002840 if (this_type.IsZero()) {
2841 /* null pointer always passes (and always fails at runtime) */
2842 } else {
2843 if (this_type.IsUninitializedTypes()) {
2844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2845 << this_type;
2846 break;
2847 }
2848 // In the past we have tried to assert that "called_interface" is assignable
2849 // from "this_type.GetClass()", however, as we do an imprecise Join
2850 // (RegType::JoinClass) we don't have full information on what interfaces are
2851 // implemented by "this_type". For example, two classes may implement the same
2852 // interfaces and have a common parent that doesn't implement the interface. The
2853 // join will set "this_type" to the parent class and a test that this implements
2854 // the interface will incorrectly fail.
2855 }
2856 /*
2857 * We don't have an object instance, so we can't find the concrete method. However, all of
2858 * the type information is in the abstract method, so we're good.
2859 */
2860 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002861 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002862 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002863 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2864 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002865 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002867 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002869 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002870 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002871 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002872 } else {
2873 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2874 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002876 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 }
jeffhaobdb76512011-09-07 11:43:16 -07002878 case Instruction::NEG_INT:
2879 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002880 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002881 break;
2882 case Instruction::NEG_LONG:
2883 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002884 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002885 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002886 break;
2887 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002889 break;
2890 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002891 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002892 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002893 break;
2894 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002895 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002897 break;
2898 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002899 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002900 break;
2901 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002902 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002903 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
2905 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002907 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002908 break;
2909 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002910 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002911 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002912 break;
2913 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002914 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002915 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002916 break;
2917 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002918 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002919 break;
2920 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002921 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002922 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002923 break;
2924 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002925 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002926 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002927 break;
2928 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002930 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002931 break;
2932 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002933 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002934 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002935 break;
2936 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002938 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002939 break;
2940 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002942 break;
2943 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002944 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002945 break;
2946 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002947 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002948 break;
2949
2950 case Instruction::ADD_INT:
2951 case Instruction::SUB_INT:
2952 case Instruction::MUL_INT:
2953 case Instruction::REM_INT:
2954 case Instruction::DIV_INT:
2955 case Instruction::SHL_INT:
2956 case Instruction::SHR_INT:
2957 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002958 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002959 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002960 break;
2961 case Instruction::AND_INT:
2962 case Instruction::OR_INT:
2963 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002966 break;
2967 case Instruction::ADD_LONG:
2968 case Instruction::SUB_LONG:
2969 case Instruction::MUL_LONG:
2970 case Instruction::DIV_LONG:
2971 case Instruction::REM_LONG:
2972 case Instruction::AND_LONG:
2973 case Instruction::OR_LONG:
2974 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002976 reg_types_.LongLo(), reg_types_.LongHi(),
2977 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002978 break;
2979 case Instruction::SHL_LONG:
2980 case Instruction::SHR_LONG:
2981 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002983 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002984 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002985 break;
2986 case Instruction::ADD_FLOAT:
2987 case Instruction::SUB_FLOAT:
2988 case Instruction::MUL_FLOAT:
2989 case Instruction::DIV_FLOAT:
2990 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002991 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2992 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002993 break;
2994 case Instruction::ADD_DOUBLE:
2995 case Instruction::SUB_DOUBLE:
2996 case Instruction::MUL_DOUBLE:
2997 case Instruction::DIV_DOUBLE:
2998 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002999 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003000 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3001 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003002 break;
3003 case Instruction::ADD_INT_2ADDR:
3004 case Instruction::SUB_INT_2ADDR:
3005 case Instruction::MUL_INT_2ADDR:
3006 case Instruction::REM_INT_2ADDR:
3007 case Instruction::SHL_INT_2ADDR:
3008 case Instruction::SHR_INT_2ADDR:
3009 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003010 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3011 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003012 break;
3013 case Instruction::AND_INT_2ADDR:
3014 case Instruction::OR_INT_2ADDR:
3015 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003016 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3017 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003018 break;
3019 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003020 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3021 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
3023 case Instruction::ADD_LONG_2ADDR:
3024 case Instruction::SUB_LONG_2ADDR:
3025 case Instruction::MUL_LONG_2ADDR:
3026 case Instruction::DIV_LONG_2ADDR:
3027 case Instruction::REM_LONG_2ADDR:
3028 case Instruction::AND_LONG_2ADDR:
3029 case Instruction::OR_LONG_2ADDR:
3030 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003031 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003032 reg_types_.LongLo(), reg_types_.LongHi(),
3033 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::SHL_LONG_2ADDR:
3036 case Instruction::SHR_LONG_2ADDR:
3037 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003038 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003039 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003040 break;
3041 case Instruction::ADD_FLOAT_2ADDR:
3042 case Instruction::SUB_FLOAT_2ADDR:
3043 case Instruction::MUL_FLOAT_2ADDR:
3044 case Instruction::DIV_FLOAT_2ADDR:
3045 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003046 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3047 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003048 break;
3049 case Instruction::ADD_DOUBLE_2ADDR:
3050 case Instruction::SUB_DOUBLE_2ADDR:
3051 case Instruction::MUL_DOUBLE_2ADDR:
3052 case Instruction::DIV_DOUBLE_2ADDR:
3053 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003054 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003055 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3056 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003057 break;
3058 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003059 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003060 case Instruction::MUL_INT_LIT16:
3061 case Instruction::DIV_INT_LIT16:
3062 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003063 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3064 true);
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::AND_INT_LIT16:
3067 case Instruction::OR_INT_LIT16:
3068 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3070 true);
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
3072 case Instruction::ADD_INT_LIT8:
3073 case Instruction::RSUB_INT_LIT8:
3074 case Instruction::MUL_INT_LIT8:
3075 case Instruction::DIV_INT_LIT8:
3076 case Instruction::REM_INT_LIT8:
3077 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003078 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003079 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003080 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3081 false);
jeffhaobdb76512011-09-07 11:43:16 -07003082 break;
3083 case Instruction::AND_INT_LIT8:
3084 case Instruction::OR_INT_LIT8:
3085 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003086 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3087 false);
jeffhaobdb76512011-09-07 11:43:16 -07003088 break;
3089
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003090 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003091 case Instruction::RETURN_VOID_NO_BARRIER:
3092 if (IsConstructor() && !IsStatic()) {
3093 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003094 if (declaring_class.IsUnresolvedReference()) {
3095 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3096 // manually over the underlying dex file.
3097 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3098 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3099 if (first_index != DexFile::kDexNoIndex) {
3100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3101 << first_index;
3102 }
3103 break;
3104 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003105 auto* klass = declaring_class.GetClass();
3106 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3107 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3109 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003110 break;
3111 }
3112 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003113 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003114 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3115 // quickened opcodes (otherwise this could be a fall-through).
3116 if (!IsConstructor()) {
3117 if (!GetMethodReturnType().IsConflict()) {
3118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3119 }
3120 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003121 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003122 // Note: the following instructions encode offsets derived from class linking.
3123 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3124 // meaning if the class linking and resolution were successful.
3125 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003126 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003127 break;
3128 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003129 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003130 break;
3131 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003132 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003133 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003134 case Instruction::IGET_BOOLEAN_QUICK:
3135 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3136 break;
3137 case Instruction::IGET_BYTE_QUICK:
3138 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3139 break;
3140 case Instruction::IGET_CHAR_QUICK:
3141 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3142 break;
3143 case Instruction::IGET_SHORT_QUICK:
3144 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3145 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003147 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003148 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003149 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003150 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003151 break;
3152 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003153 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003154 break;
3155 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003156 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003157 break;
3158 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003159 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003160 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003162 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003163 break;
3164 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003165 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003166 break;
3167 case Instruction::INVOKE_VIRTUAL_QUICK:
3168 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3169 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003170 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003171 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003172 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003173 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003175 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 } else {
3177 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3178 }
3179 just_set_result = true;
3180 }
3181 break;
3182 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003183 case Instruction::INVOKE_LAMBDA: {
3184 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3185 // If the code would've normally hard-failed, then the interpreter will throw the
3186 // appropriate verification errors at runtime.
3187 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3188 break;
3189 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003190 case Instruction::CAPTURE_VARIABLE: {
3191 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3192 // If the code would've normally hard-failed, then the interpreter will throw the
3193 // appropriate verification errors at runtime.
3194 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3195 break;
3196 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003197 case Instruction::CREATE_LAMBDA: {
3198 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3199 // If the code would've normally hard-failed, then the interpreter will throw the
3200 // appropriate verification errors at runtime.
3201 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3202 break;
3203 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003204 case Instruction::LIBERATE_VARIABLE: {
3205 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3206 // If the code would've normally hard-failed, then the interpreter will throw the
3207 // appropriate verification errors at runtime.
3208 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3209 break;
3210 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003211
Igor Murashkin6918bf12015-09-27 19:19:06 -07003212 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003213 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003214 // Conservatively fail verification on release builds.
3215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3216 break;
3217 }
3218
3219 case Instruction::BOX_LAMBDA: {
3220 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3221 // If the code would've normally hard-failed, then the interpreter will throw the
3222 // appropriate verification errors at runtime.
3223 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003224
3225 // Partial verification. Sets the resulting type to always be an object, which
3226 // is good enough for some other verification to occur without hard-failing.
3227 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3228 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003229 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003230 break;
3231 }
3232
3233 case Instruction::UNBOX_LAMBDA: {
3234 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3235 // If the code would've normally hard-failed, then the interpreter will throw the
3236 // appropriate verification errors at runtime.
3237 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3238 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003239 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003240
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003242 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003243 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003244 case Instruction::UNUSED_79:
3245 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003247 break;
3248
3249 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003250 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003251 * complain if an instruction is missing (which is desirable).
3252 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003253 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003254
Ian Rogersad0b3a32012-04-16 14:50:24 -07003255 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003256 if (Runtime::Current()->IsAotCompiler()) {
3257 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003258 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3259 LOG(ERROR) << "Pending failures:";
3260 for (auto& error : failures_) {
3261 LOG(ERROR) << error;
3262 }
3263 for (auto& error_msg : failure_messages_) {
3264 LOG(ERROR) << error_msg->str();
3265 }
3266 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3267 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003268 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003269 /* immediate failure, reject class */
3270 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3271 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003272 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003273 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003274 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003275 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3276 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3277 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003278 }
jeffhaobdb76512011-09-07 11:43:16 -07003279 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003280 * If we didn't just set the result register, clear it out. This ensures that you can only use
3281 * "move-result" immediately after the result is set. (We could check this statically, but it's
3282 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003283 */
3284 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003285 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003286 }
3287
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003288
jeffhaobdb76512011-09-07 11:43:16 -07003289
3290 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003291 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003292 *
3293 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003294 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003295 * somebody could get a reference field, check it for zero, and if the
3296 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003297 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003298 * that, and will reject the code.
3299 *
3300 * TODO: avoid re-fetching the branch target
3301 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003302 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003303 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003304 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003305 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003307 return false;
3308 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003309 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003310 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003311 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003312 }
jeffhaobdb76512011-09-07 11:43:16 -07003313 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003314 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003315 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003316 return false;
3317 }
3318 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003319 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003320 return false;
3321 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003322 }
jeffhaobdb76512011-09-07 11:43:16 -07003323 }
3324
3325 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003326 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003327 *
3328 * We've already verified that the table is structurally sound, so we
3329 * just need to walk through and tag the targets.
3330 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003331 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003332 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003333 const uint16_t* switch_insns = insns + offset_to_switch;
3334 int switch_count = switch_insns[1];
3335 int offset_to_targets, targ;
3336
3337 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3338 /* 0 = sig, 1 = count, 2/3 = first key */
3339 offset_to_targets = 4;
3340 } else {
3341 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003342 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003343 offset_to_targets = 2 + 2 * switch_count;
3344 }
3345
3346 /* verify each switch target */
3347 for (targ = 0; targ < switch_count; targ++) {
3348 int offset;
3349 uint32_t abs_offset;
3350
3351 /* offsets are 32-bit, and only partly endian-swapped */
3352 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003353 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 abs_offset = work_insn_idx_ + offset;
3355 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003356 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003357 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003358 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003359 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003360 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003361 }
jeffhaobdb76512011-09-07 11:43:16 -07003362 }
3363 }
3364
3365 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3367 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003368 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003369 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003370 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003371 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003372
Andreas Gampef91baf12014-07-18 15:41:00 -07003373 // Need the linker to try and resolve the handled class to check if it's Throwable.
3374 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3375
Ian Rogers0571d352011-11-03 19:51:38 -07003376 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003377 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3378 if (handler_type_idx == DexFile::kDexNoIndex16) {
3379 has_catch_all_handler = true;
3380 } else {
3381 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003382 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3383 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003384 if (klass != nullptr) {
3385 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3386 has_catch_all_handler = true;
3387 }
3388 } else {
3389 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003390 DCHECK(self_->IsExceptionPending());
3391 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 }
jeffhaobdb76512011-09-07 11:43:16 -07003394 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3396 * "work_regs", because at runtime the exception will be thrown before the instruction
3397 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003398 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003399 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003400 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 }
jeffhaobdb76512011-09-07 11:43:16 -07003402 }
3403
3404 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3406 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003407 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003408 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003409 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003410 * The state in work_line reflects the post-execution state. If the current instruction is a
3411 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003412 * it will do so before grabbing the lock).
3413 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003414 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003415 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003416 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003417 return false;
3418 }
3419 }
3420 }
3421
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003422 /* Handle "continue". Tag the next consecutive instruction.
3423 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3424 * because it changes work_line_ when performing peephole optimization
3425 * and this change should not be used in those cases.
3426 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003427 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003428 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3429 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003430 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3431 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3432 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003433 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003434 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3435 // next instruction isn't one.
3436 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3437 return false;
3438 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003439 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003440 // Make workline consistent with fallthrough computed from peephole optimization.
3441 work_line_->CopyFromLine(fallthrough_line.get());
3442 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003443 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003444 // For returns we only care about the operand to the return, all other registers are dead.
3445 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003446 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003447 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003448 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003449 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003450 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3451 // needed. If the merge changes the state of the registers then the work line will be
3452 // updated.
3453 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003454 return false;
3455 }
3456 } else {
3457 /*
3458 * We're not recording register data for the next instruction, so we don't know what the
3459 * prior state was. We have to assume that something has changed and re-evaluate it.
3460 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003461 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003462 }
3463 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003464
jeffhaod1f0fde2011-09-08 17:25:33 -07003465 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003466 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003467 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003468 }
3469
3470 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003471 * Update start_guess. Advance to the next instruction of that's
3472 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003473 * neither of those exists we're in a return or throw; leave start_guess
3474 * alone and let the caller sort it out.
3475 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003476 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3478 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003479 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003480 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003482 }
3483
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003485 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003486
Andreas Gampea727e372015-08-25 09:22:37 -07003487 if (have_pending_runtime_throw_failure_) {
3488 have_any_pending_runtime_throw_failure_ = true;
3489 // Reset the pending_runtime_throw flag now.
3490 have_pending_runtime_throw_failure_ = false;
3491 }
3492
jeffhaobdb76512011-09-07 11:43:16 -07003493 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003494} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003495
Mathieu Chartierde40d472015-10-15 17:47:48 -07003496void MethodVerifier::UninstantiableError(const char* descriptor) {
3497 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3498 << "non-instantiable klass " << descriptor;
3499}
3500
3501inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3502 return klass->IsInstantiable() || klass->IsPrimitive();
3503}
3504
Ian Rogersd8f69b02014-09-10 21:43:52 +00003505const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003506 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003507 const RegType* result = nullptr;
3508 if (klass != nullptr) {
3509 bool precise = klass->CannotBeAssignedFromOtherTypes();
3510 if (precise && !IsInstantiableOrPrimitive(klass)) {
3511 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3512 UninstantiableError(descriptor);
3513 precise = false;
3514 }
3515 result = reg_types_.FindClass(klass, precise);
3516 if (result == nullptr) {
3517 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3518 result = reg_types_.InsertClass(descriptor, klass, precise);
3519 }
3520 } else {
3521 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3522 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003524 DCHECK(result != nullptr);
3525 if (result->IsConflict()) {
3526 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3527 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3528 << "' in " << GetDeclaringClass();
3529 return *result;
3530 }
3531 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3532 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003533 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003534 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003535 // check at runtime if access is allowed and so pass here. If result is
3536 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003537 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3538 const RegType& referrer = GetDeclaringClass();
3539 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3540 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3541 << referrer << "' -> '" << result << "'";
3542 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003543 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003544 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003545}
3546
Ian Rogersd8f69b02014-09-10 21:43:52 +00003547const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003548 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003549 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003550 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003551 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3552 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003553 CatchHandlerIterator iterator(handlers_ptr);
3554 for (; iterator.HasNext(); iterator.Next()) {
3555 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3556 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003557 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003558 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003559 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003560 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003561 if (exception.IsUnresolvedTypes()) {
3562 // We don't know enough about the type. Fail here and let runtime handle it.
3563 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3564 return exception;
3565 } else {
3566 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3567 return reg_types_.Conflict();
3568 }
Jeff Haob878f212014-04-24 16:25:36 -07003569 } else if (common_super == nullptr) {
3570 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003571 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003572 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003574 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003575 if (FailOrAbort(this,
3576 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3577 "java.lang.Throwable is not assignable-from common_super at ",
3578 work_insn_idx_)) {
3579 break;
3580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003581 }
3582 }
3583 }
3584 }
Ian Rogers0571d352011-11-03 19:51:38 -07003585 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 }
3587 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003588 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003589 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003590 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003591 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003592 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003593 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003594}
3595
Mathieu Chartiere401d142015-04-22 13:56:20 -07003596ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3597 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003599 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 if (klass_type.IsConflict()) {
3601 std::string append(" in attempt to access method ");
3602 append += dex_file_->GetMethodName(method_id);
3603 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003604 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003605 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003608 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003609 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003610 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003611 auto* cl = Runtime::Current()->GetClassLinker();
3612 auto pointer_size = cl->GetImagePointerSize();
3613 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003615 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003616 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003617
3618 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003619 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003620 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003621 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003623 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003624 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003625 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003626 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003628 // If a virtual or interface method wasn't found with the expected type, look in
3629 // the direct methods. This can happen when the wrong invoke type is used or when
3630 // a class has changed, and will be flagged as an error in later checks.
3631 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003632 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003633 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003634 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003635 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3636 << PrettyDescriptor(klass) << "." << name
3637 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003639 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003640 }
3641 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3643 // enforce them here.
3644 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3646 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003647 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003648 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003649 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003650 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3652 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003654 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003655 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003656 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003657 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003658 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003659 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003660 }
jeffhaode0d9c92012-02-27 13:58:13 -08003661 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3662 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3664 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003665 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003666 }
Alex Light0db36b32015-10-27 14:06:34 -07003667 // Check that interface methods are static or match interface classes.
3668 // We only allow statics if we don't have default methods enabled.
3669 Runtime* runtime = Runtime::Current();
3670 const bool default_methods_supported =
3671 runtime == nullptr ||
3672 runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods);
3673 if (klass->IsInterface() &&
3674 method_type != METHOD_INTERFACE &&
3675 (!default_methods_supported || method_type != METHOD_STATIC)) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003676 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3677 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003678 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003679 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3680 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3681 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003682 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003683 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003684 // See if the method type implied by the invoke instruction matches the access flags for the
3685 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003686 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3688 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3689 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003690 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3691 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003694 return res_method;
3695}
3696
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003697template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003698ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3699 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003700 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3701 // match the call to the signature. Also, we might be calling through an abstract method
3702 // definition (which doesn't have register count values).
3703 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3704 /* caught by static verifier */
3705 DCHECK(is_range || expected_args <= 5);
3706 if (expected_args > code_item_->outs_size_) {
3707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3708 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3709 return nullptr;
3710 }
3711
3712 uint32_t arg[5];
3713 if (!is_range) {
3714 inst->GetVarArgs(arg);
3715 }
3716 uint32_t sig_registers = 0;
3717
3718 /*
3719 * Check the "this" argument, which must be an instance of the class that declared the method.
3720 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3721 * rigorous check here (which is okay since we have to do it at runtime).
3722 */
3723 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003724 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003725 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3726 CHECK(have_pending_hard_failure_);
3727 return nullptr;
3728 }
3729 if (actual_arg_type.IsUninitializedReference()) {
3730 if (res_method) {
3731 if (!res_method->IsConstructor()) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3733 return nullptr;
3734 }
3735 } else {
3736 // Check whether the name of the called method is "<init>"
3737 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003738 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3740 return nullptr;
3741 }
3742 }
3743 }
3744 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003745 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003746 // Miranda methods have the declaring interface as their declaring class, not the abstract
3747 // class. It would be wrong to use this for the type check (interface type checks are
3748 // postponed to runtime).
3749 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003750 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003751 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003752 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3753 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003754 } else {
3755 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3756 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003757 res_method_class = &reg_types_.FromDescriptor(
3758 GetClassLoader(),
3759 dex_file_->StringByTypeIdx(class_idx),
3760 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003761 }
3762 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3763 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3764 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3765 << "' not instance of '" << *res_method_class << "'";
3766 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3767 // the compiler.
3768 if (have_pending_hard_failure_) {
3769 return nullptr;
3770 }
3771 }
3772 }
3773 sig_registers = 1;
3774 }
3775
3776 for ( ; it->HasNext(); it->Next()) {
3777 if (sig_registers >= expected_args) {
3778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3779 " arguments, found " << sig_registers << " or more.";
3780 return nullptr;
3781 }
3782
3783 const char* param_descriptor = it->GetDescriptor();
3784
3785 if (param_descriptor == nullptr) {
3786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3787 "component";
3788 return nullptr;
3789 }
3790
Ian Rogersd8f69b02014-09-10 21:43:52 +00003791 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003792 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3793 arg[sig_registers];
3794 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003795 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003796 if (!src_type.IsIntegralTypes()) {
3797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3798 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003799 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003800 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003801 } else {
3802 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3803 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3804 // the compiler.
3805 if (have_pending_hard_failure_) {
3806 return nullptr;
3807 }
3808 } else if (reg_type.IsLongOrDoubleTypes()) {
3809 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3810 // instructions not specifying register pairs by the first component, but require them
3811 // nonetheless. Only check when there's an actual register in the parameters. If there's
3812 // none, this will fail below.
3813 if (!is_range && sig_registers + 1 < expected_args) {
3814 uint32_t second_reg = arg[sig_registers + 1];
3815 if (second_reg != get_reg + 1) {
3816 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3817 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3818 << second_reg << ".";
3819 return nullptr;
3820 }
3821 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003822 }
3823 }
3824 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3825 }
3826 if (expected_args != sig_registers) {
3827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3828 " arguments, found " << sig_registers;
3829 return nullptr;
3830 }
3831 return res_method;
3832}
3833
3834void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3835 MethodType method_type,
3836 bool is_range) {
3837 // As the method may not have been resolved, make this static check against what we expect.
3838 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3839 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3840 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3841 DexFileParameterIterator it(*dex_file_,
3842 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3843 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3844 nullptr);
3845}
3846
3847class MethodParamListDescriptorIterator {
3848 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003849 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003850 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3851 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3852 }
3853
3854 bool HasNext() {
3855 return pos_ < params_size_;
3856 }
3857
3858 void Next() {
3859 ++pos_;
3860 }
3861
Mathieu Chartier90443472015-07-16 20:32:27 -07003862 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003863 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3864 }
3865
3866 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003867 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003868 size_t pos_;
3869 const DexFile::TypeList* params_;
3870 const size_t params_size_;
3871};
3872
Mathieu Chartiere401d142015-04-22 13:56:20 -07003873ArtMethod* MethodVerifier::VerifyInvocationArgs(
3874 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003875 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3876 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003877 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003878
Mathieu Chartiere401d142015-04-22 13:56:20 -07003879 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003880 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003881 // Check what we can statically.
3882 if (!have_pending_hard_failure_) {
3883 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3884 }
3885 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003886 }
3887
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3889 // has a vtable entry for the target method.
3890 if (is_super) {
3891 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003892 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003893 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003894 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003895 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003896 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003897 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003898 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003899 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003900 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003901 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003902 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003903 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003904 << "." << res_method->GetName()
3905 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003906 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003907 }
3908 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003909
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003910 // Process the target method's signature. This signature may or may not
3911 MethodParamListDescriptorIterator it(res_method);
3912 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3913 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003914}
3915
Mathieu Chartiere401d142015-04-22 13:56:20 -07003916ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3917 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003918 if (is_range) {
3919 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3920 } else {
3921 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3922 }
3923 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003924 if (!actual_arg_type.HasClass()) {
3925 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003926 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003927 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003928 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003929 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003930 if (klass->IsInterface()) {
3931 // Derive Object.class from Class.class.getSuperclass().
3932 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003933 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003934 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003935 return nullptr;
3936 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003937 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003938 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003939 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003940 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003941 if (!dispatch_class->HasVTable()) {
3942 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3943 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003944 return nullptr;
3945 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003946 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003947 auto* cl = Runtime::Current()->GetClassLinker();
3948 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003949 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3950 FailOrAbort(this, allow_failure,
3951 "Receiver class has not enough vtable slots for quickened invoke at ",
3952 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003953 return nullptr;
3954 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003955 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003956 if (self_->IsExceptionPending()) {
3957 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3958 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003959 return nullptr;
3960 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003961 return res_method;
3962}
3963
Mathieu Chartiere401d142015-04-22 13:56:20 -07003964ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003965 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3966 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3967
Mathieu Chartiere401d142015-04-22 13:56:20 -07003968 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003971 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003972 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003973 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3974 work_insn_idx_)) {
3975 return nullptr;
3976 }
3977 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3978 work_insn_idx_)) {
3979 return nullptr;
3980 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003981
3982 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3983 // match the call to the signature. Also, we might be calling through an abstract method
3984 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003985 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003986 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003987 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003988 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003989 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3990 /* caught by static verifier */
3991 DCHECK(is_range || expected_args <= 5);
3992 if (expected_args > code_item_->outs_size_) {
3993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3994 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003995 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003996 }
3997
3998 /*
3999 * Check the "this" argument, which must be an instance of the class that declared the method.
4000 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4001 * rigorous check here (which is okay since we have to do it at runtime).
4002 */
4003 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
4004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004005 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 }
4007 if (!actual_arg_type.IsZero()) {
4008 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004009 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004010 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004011 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004012 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07004013 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
4014 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004015 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004016 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004017 }
4018 }
4019 /*
4020 * Process the target method's signature. This signature may or may not
4021 * have been verified, so we can't assume it's properly formed.
4022 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004023 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004024 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004025 uint32_t arg[5];
4026 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004027 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004028 }
4029 size_t actual_args = 1;
4030 for (size_t param_index = 0; param_index < params_size; param_index++) {
4031 if (actual_args >= expected_args) {
4032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004033 << "'. Expected " << expected_args
4034 << " arguments, processing argument " << actual_args
4035 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004036 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004037 }
4038 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004039 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004040 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004042 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004043 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004044 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004045 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004046 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004048 return res_method;
4049 }
4050 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4051 }
4052 if (actual_args != expected_args) {
4053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4054 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004055 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004056 } else {
4057 return res_method;
4058 }
4059}
4060
Ian Rogers62342ec2013-06-11 10:26:37 -07004061void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004062 uint32_t type_idx;
4063 if (!is_filled) {
4064 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4065 type_idx = inst->VRegC_22c();
4066 } else if (!is_range) {
4067 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4068 type_idx = inst->VRegB_35c();
4069 } else {
4070 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4071 type_idx = inst->VRegB_3rc();
4072 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004073 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004074 if (res_type.IsConflict()) { // bad class
4075 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004076 } else {
4077 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4078 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004080 } else if (!is_filled) {
4081 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004082 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004083 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004084 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004085 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004086 } else {
4087 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4088 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004089 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004090 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4091 uint32_t arg[5];
4092 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004093 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004094 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004095 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004096 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004097 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4098 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004099 return;
4100 }
4101 }
4102 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004103 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004104 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004105 }
4106 }
4107}
4108
Sebastien Hertz5243e912013-05-21 10:55:07 +02004109void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004110 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004111 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004112 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004114 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004115 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004116 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004117 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004118 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4119 // instruction type. TODO: have a proper notion of bottom here.
4120 if (!is_primitive || insn_type.IsCategory1Types()) {
4121 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004122 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004123 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004124 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004125 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4126 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004127 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004128 }
jeffhaofc3144e2012-02-01 17:21:15 -08004129 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004131 } else {
4132 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004133 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004134 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004135 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004136 << " source for aget-object";
4137 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004139 << " source for category 1 aget";
4140 } else if (is_primitive && !insn_type.Equals(component_type) &&
4141 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004142 (insn_type.IsLong() && component_type.IsDouble()))) {
4143 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4144 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004145 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004146 // Use knowledge of the field type which is stronger than the type inferred from the
4147 // instruction, which can't differentiate object types and ints from floats, longs from
4148 // doubles.
4149 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004150 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004151 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004152 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004153 component_type.HighHalf(&reg_types_));
4154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004155 }
4156 }
4157 }
4158}
4159
Ian Rogersd8f69b02014-09-10 21:43:52 +00004160void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004161 const uint32_t vregA) {
4162 // Primitive assignability rules are weaker than regular assignability rules.
4163 bool instruction_compatible;
4164 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004165 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004166 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004167 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004168 value_compatible = value_type.IsIntegralTypes();
4169 } else if (target_type.IsFloat()) {
4170 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4171 value_compatible = value_type.IsFloatTypes();
4172 } else if (target_type.IsLong()) {
4173 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004174 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4175 // as target_type depends on the resolved type of the field.
4176 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004177 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004178 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4179 } else {
4180 value_compatible = false;
4181 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004182 } else if (target_type.IsDouble()) {
4183 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004184 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4185 // as target_type depends on the resolved type of the field.
4186 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004187 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004188 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4189 } else {
4190 value_compatible = false;
4191 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004192 } else {
4193 instruction_compatible = false; // reference with primitive store
4194 value_compatible = false; // unused
4195 }
4196 if (!instruction_compatible) {
4197 // This is a global failure rather than a class change failure as the instructions and
4198 // the descriptors for the type should have been consistent within the same file at
4199 // compile time.
4200 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4201 << "' but expected type '" << target_type << "'";
4202 return;
4203 }
4204 if (!value_compatible) {
4205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4206 << " of type " << value_type << " but expected " << target_type << " for put";
4207 return;
4208 }
4209}
4210
Sebastien Hertz5243e912013-05-21 10:55:07 +02004211void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004212 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004214 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004216 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004218 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004219 // Null array type; this code path will fail at runtime.
4220 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004221 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4222 // and fits multiple register types.
4223 const RegType* modified_reg_type = &insn_type;
4224 if ((modified_reg_type == &reg_types_.Integer()) ||
4225 (modified_reg_type == &reg_types_.LongLo())) {
4226 // May be integer or float | long or double. Overwrite insn_type accordingly.
4227 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4228 if (modified_reg_type == &reg_types_.Integer()) {
4229 if (&value_type == &reg_types_.Float()) {
4230 modified_reg_type = &value_type;
4231 }
4232 } else {
4233 if (&value_type == &reg_types_.DoubleLo()) {
4234 modified_reg_type = &value_type;
4235 }
4236 }
4237 }
4238 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004239 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004241 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004242 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004243 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004244 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004245 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004246 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004247 if (!component_type.IsReferenceTypes()) {
4248 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4249 << " source for aput-object";
4250 } else {
4251 // The instruction agrees with the type of array, confirm the value to be stored does too
4252 // Note: we use the instruction type (rather than the component type) for aput-object as
4253 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004254 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004255 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004256 }
4257 }
4258 }
4259}
4260
Mathieu Chartierc7853442015-03-27 14:35:38 -07004261ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004262 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4263 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004264 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004265 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004266 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4267 field_idx, dex_file_->GetFieldName(field_id),
4268 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004269 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004270 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004271 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004273 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004275 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4276 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004277 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004278 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004279 << dex_file_->GetFieldName(field_id) << ") in "
4280 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004281 DCHECK(self_->IsExceptionPending());
4282 self_->ClearException();
4283 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004284 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4285 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004286 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004287 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004289 } else if (!field->IsStatic()) {
4290 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004291 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004292 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004293 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004294}
4295
Mathieu Chartierc7853442015-03-27 14:35:38 -07004296ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004297 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4298 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004299 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004300 if (klass_type.IsConflict()) {
4301 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4302 field_idx, dex_file_->GetFieldName(field_id),
4303 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004304 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004305 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004306 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004307 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004308 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004309 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004310 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4311 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004312 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004313 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004314 << dex_file_->GetFieldName(field_id) << ") in "
4315 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004316 DCHECK(self_->IsExceptionPending());
4317 self_->ClearException();
4318 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004319 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4320 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004321 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004322 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004323 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004324 } else if (field->IsStatic()) {
4325 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4326 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004327 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004328 } else if (obj_type.IsZero()) {
4329 // Cannot infer and check type, however, access will cause null pointer exception
4330 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004331 } else if (!obj_type.IsReferenceTypes()) {
4332 // Trying to read a field from something that isn't a reference
4333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4334 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004335 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004336 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004337 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004338 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004339 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4340 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004341 if (obj_type.IsUninitializedTypes() &&
4342 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4343 !field_klass.Equals(GetDeclaringClass()))) {
4344 // Field accesses through uninitialized references are only allowable for constructors where
4345 // the field is declared in this class
4346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004347 << " of a not fully initialized object within the context"
4348 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004349 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004350 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4351 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4352 // of C1. For resolution to occur the declared class of the field must be compatible with
4353 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4354 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4355 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004356 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004357 } else {
4358 return field;
4359 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004360 }
4361}
4362
Andreas Gampe896df402014-10-20 22:25:29 -07004363template <MethodVerifier::FieldAccessType kAccType>
4364void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4365 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004366 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004367 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004368 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004369 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004370 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004371 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004372 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004373 if (UNLIKELY(have_pending_hard_failure_)) {
4374 return;
4375 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004376 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004377 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004378 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004379 if (kAccType == FieldAccessType::kAccPut) {
4380 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4381 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4382 << " from other class " << GetDeclaringClass();
4383 return;
4384 }
4385 }
4386
Mathieu Chartierc7853442015-03-27 14:35:38 -07004387 mirror::Class* field_type_class =
4388 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004389 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004390 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4391 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004392 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004393 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4394 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004395 }
Ian Rogers0d604842012-04-16 14:50:24 -07004396 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004397 if (field_type == nullptr) {
4398 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4399 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004400 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004401 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004402 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004403 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004404 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4405 "Unexpected third access type");
4406 if (kAccType == FieldAccessType::kAccPut) {
4407 // sput or iput.
4408 if (is_primitive) {
4409 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004410 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004411 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004412 // If the field type is not a reference, this is a global failure rather than
4413 // a class change failure as the instructions and the descriptors for the type
4414 // should have been consistent within the same file at compile time.
4415 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4416 : VERIFY_ERROR_BAD_CLASS_HARD;
4417 Fail(error) << "expected field " << PrettyField(field)
4418 << " to be compatible with type '" << insn_type
4419 << "' but found type '" << *field_type
4420 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004421 return;
4422 }
4423 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004424 }
Andreas Gampe896df402014-10-20 22:25:29 -07004425 } else if (kAccType == FieldAccessType::kAccGet) {
4426 // sget or iget.
4427 if (is_primitive) {
4428 if (field_type->Equals(insn_type) ||
4429 (field_type->IsFloat() && insn_type.IsInteger()) ||
4430 (field_type->IsDouble() && insn_type.IsLong())) {
4431 // expected that read is of the correct primitive type or that int reads are reading
4432 // floats or long reads are reading doubles
4433 } else {
4434 // This is a global failure rather than a class change failure as the instructions and
4435 // the descriptors for the type should have been consistent within the same file at
4436 // compile time
4437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4438 << " to be of type '" << insn_type
4439 << "' but found type '" << *field_type << "' in get";
4440 return;
4441 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004442 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004443 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004444 // If the field type is not a reference, this is a global failure rather than
4445 // a class change failure as the instructions and the descriptors for the type
4446 // should have been consistent within the same file at compile time.
4447 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4448 : VERIFY_ERROR_BAD_CLASS_HARD;
4449 Fail(error) << "expected field " << PrettyField(field)
4450 << " to be compatible with type '" << insn_type
4451 << "' but found type '" << *field_type
4452 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004453 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004454 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004455 }
Andreas Gampe896df402014-10-20 22:25:29 -07004456 return;
4457 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004458 }
Andreas Gampe896df402014-10-20 22:25:29 -07004459 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004460 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004461 } else {
4462 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004464 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004465 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004466 }
4467}
4468
Mathieu Chartierc7853442015-03-27 14:35:38 -07004469ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004470 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004471 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004472 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004473 if (!object_type.HasClass()) {
4474 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4475 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004476 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004477 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004478 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004479 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004480 if (f == nullptr) {
4481 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4482 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4483 }
4484 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004485}
4486
Andreas Gampe896df402014-10-20 22:25:29 -07004487template <MethodVerifier::FieldAccessType kAccType>
4488void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4489 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004490 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004491
Mathieu Chartierc7853442015-03-27 14:35:38 -07004492 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4495 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004496 }
Andreas Gampe896df402014-10-20 22:25:29 -07004497
4498 // For an IPUT_QUICK, we now test for final flag of the field.
4499 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004500 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4501 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004502 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004503 return;
4504 }
4505 }
Andreas Gampe896df402014-10-20 22:25:29 -07004506
4507 // Get the field type.
4508 const RegType* field_type;
4509 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004510 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4511 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004512
4513 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004514 field_type = &FromClass(field->GetTypeDescriptor(),
4515 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004516 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004517 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004518 Thread* self = Thread::Current();
4519 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4520 self->ClearException();
4521 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004522 field->GetTypeDescriptor(),
4523 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004524 }
Andreas Gampe896df402014-10-20 22:25:29 -07004525 if (field_type == nullptr) {
4526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004527 return;
4528 }
Andreas Gampe896df402014-10-20 22:25:29 -07004529 }
4530
4531 const uint32_t vregA = inst->VRegA_22c();
4532 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4533 "Unexpected third access type");
4534 if (kAccType == FieldAccessType::kAccPut) {
4535 if (is_primitive) {
4536 // Primitive field assignability rules are weaker than regular assignability rules
4537 bool instruction_compatible;
4538 bool value_compatible;
4539 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4540 if (field_type->IsIntegralTypes()) {
4541 instruction_compatible = insn_type.IsIntegralTypes();
4542 value_compatible = value_type.IsIntegralTypes();
4543 } else if (field_type->IsFloat()) {
4544 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4545 value_compatible = value_type.IsFloatTypes();
4546 } else if (field_type->IsLong()) {
4547 instruction_compatible = insn_type.IsLong();
4548 value_compatible = value_type.IsLongTypes();
4549 } else if (field_type->IsDouble()) {
4550 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4551 value_compatible = value_type.IsDoubleTypes();
4552 } else {
4553 instruction_compatible = false; // reference field with primitive store
4554 value_compatible = false; // unused
4555 }
4556 if (!instruction_compatible) {
4557 // This is a global failure rather than a class change failure as the instructions and
4558 // the descriptors for the type should have been consistent within the same file at
4559 // compile time
4560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4561 << " to be of type '" << insn_type
4562 << "' but found type '" << *field_type
4563 << "' in put";
4564 return;
4565 }
4566 if (!value_compatible) {
4567 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4568 << " of type " << value_type
4569 << " but expected " << *field_type
4570 << " for store to " << PrettyField(field) << " in put";
4571 return;
4572 }
4573 } else {
4574 if (!insn_type.IsAssignableFrom(*field_type)) {
4575 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4576 << " to be compatible with type '" << insn_type
4577 << "' but found type '" << *field_type
4578 << "' in put-object";
4579 return;
4580 }
4581 work_line_->VerifyRegisterType(this, vregA, *field_type);
4582 }
4583 } else if (kAccType == FieldAccessType::kAccGet) {
4584 if (is_primitive) {
4585 if (field_type->Equals(insn_type) ||
4586 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4587 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4588 // expected that read is of the correct primitive type or that int reads are reading
4589 // floats or long reads are reading doubles
4590 } else {
4591 // This is a global failure rather than a class change failure as the instructions and
4592 // the descriptors for the type should have been consistent within the same file at
4593 // compile time
4594 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4595 << " to be of type '" << insn_type
4596 << "' but found type '" << *field_type << "' in Get";
4597 return;
4598 }
4599 } else {
4600 if (!insn_type.IsAssignableFrom(*field_type)) {
4601 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4602 << " to be compatible with type '" << insn_type
4603 << "' but found type '" << *field_type
4604 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004605 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004606 return;
4607 }
4608 }
4609 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004610 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004611 } else {
4612 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004613 }
4614 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004615 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004616 }
4617}
4618
Ian Rogers776ac1f2012-04-13 23:36:36 -07004619bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004620 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004622 return false;
4623 }
4624 return true;
4625}
4626
Stephen Kyle9bc61992014-09-22 13:53:15 +01004627bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4628 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4629 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4631 return false;
4632 }
4633 return true;
4634}
4635
4636bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4637 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4638}
4639
Ian Rogersebbdd872014-07-07 23:53:08 -07004640bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4641 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004642 bool changed = true;
4643 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004644 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004645 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004646 * We haven't processed this instruction before, and we haven't touched the registers here, so
4647 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4648 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004649 */
Andreas Gampea727e372015-08-25 09:22:37 -07004650 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004651 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004652 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004653 merge_line->VerifyMonitorStackEmpty(this);
4654
Ian Rogersb8c78592013-07-25 23:52:52 +00004655 // For returns we only care about the operand to the return, all other registers are dead.
4656 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4657 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004658 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004659 }
jeffhaobdb76512011-09-07 11:43:16 -07004660 } else {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004661 ArenaUniquePtr<RegisterLine> copy;
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004662 if (gDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004663 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004664 copy->CopyFromLine(target_line);
4665 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004666 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004667 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004668 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004669 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004670 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004671 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004672 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004673 << copy->Dump(this) << " MERGE\n"
4674 << merge_line->Dump(this) << " ==\n"
4675 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004676 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004677 if (update_merge_line && changed) {
4678 merge_line->CopyFromLine(target_line);
4679 }
jeffhaobdb76512011-09-07 11:43:16 -07004680 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004681 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004682 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004683 }
4684 return true;
4685}
4686
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004687InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004688 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004689}
4690
Ian Rogersd8f69b02014-09-10 21:43:52 +00004691const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004692 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004693 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004694 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4695 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4696 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004697 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004698 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4699 return_type_class,
4700 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004701 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004702 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4703 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004704 }
4705 }
4706 if (return_type_ == nullptr) {
4707 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4708 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4709 uint16_t return_type_idx = proto_id.return_type_idx_;
4710 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004711 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004712 }
4713 }
4714 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004715}
4716
Ian Rogersd8f69b02014-09-10 21:43:52 +00004717const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004718 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004719 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004720 const char* descriptor
4721 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004722 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004723 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004724 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004725 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004726 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004727 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004728 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004729 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004730}
4731
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004732std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4733 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004734 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004735 std::vector<int32_t> result;
4736 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004737 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004738 if (type.IsConstant()) {
4739 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004740 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4741 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004742 } else if (type.IsConstantLo()) {
4743 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004744 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4745 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004746 } else if (type.IsConstantHi()) {
4747 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004748 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4749 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004750 } else if (type.IsIntegralTypes()) {
4751 result.push_back(kIntVReg);
4752 result.push_back(0);
4753 } else if (type.IsFloat()) {
4754 result.push_back(kFloatVReg);
4755 result.push_back(0);
4756 } else if (type.IsLong()) {
4757 result.push_back(kLongLoVReg);
4758 result.push_back(0);
4759 result.push_back(kLongHiVReg);
4760 result.push_back(0);
4761 ++i;
4762 } else if (type.IsDouble()) {
4763 result.push_back(kDoubleLoVReg);
4764 result.push_back(0);
4765 result.push_back(kDoubleHiVReg);
4766 result.push_back(0);
4767 ++i;
4768 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4769 result.push_back(kUndefined);
4770 result.push_back(0);
4771 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004772 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004773 result.push_back(kReferenceVReg);
4774 result.push_back(0);
4775 }
4776 }
4777 return result;
4778}
4779
Ian Rogersd8f69b02014-09-10 21:43:52 +00004780const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004781 if (precise) {
4782 // Precise constant type.
4783 return reg_types_.FromCat1Const(value, true);
4784 } else {
4785 // Imprecise constant type.
4786 if (value < -32768) {
4787 return reg_types_.IntConstant();
4788 } else if (value < -128) {
4789 return reg_types_.ShortConstant();
4790 } else if (value < 0) {
4791 return reg_types_.ByteConstant();
4792 } else if (value == 0) {
4793 return reg_types_.Zero();
4794 } else if (value == 1) {
4795 return reg_types_.One();
4796 } else if (value < 128) {
4797 return reg_types_.PosByteConstant();
4798 } else if (value < 32768) {
4799 return reg_types_.PosShortConstant();
4800 } else if (value < 65536) {
4801 return reg_types_.CharConstant();
4802 } else {
4803 return reg_types_.IntConstant();
4804 }
4805 }
4806}
4807
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004808void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004809 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004810}
4811
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004812void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004813 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004814}
jeffhaod1224c72012-02-29 13:43:08 -08004815
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004816void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4817 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004818}
4819
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004820void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4821 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004822}
4823
Andreas Gampef23f33d2015-06-23 14:18:17 -07004824const RegType& MethodVerifier::FromClass(const char* descriptor,
4825 mirror::Class* klass,
4826 bool precise) {
4827 DCHECK(klass != nullptr);
4828 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4829 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4830 << "non-instantiable klass " << descriptor;
4831 precise = false;
4832 }
4833 return reg_types_.FromClass(descriptor, klass, precise);
4834}
4835
Ian Rogersd81871c2011-10-03 13:57:23 -07004836} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004837} // namespace art